Skip to content

Commit

Permalink
feat(#12): overload element's updating in NotEmptyMutableCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
LVMVRQUXL committed Aug 11, 2022
1 parent 4da3806 commit ff3219e
Show file tree
Hide file tree
Showing 2 changed files with 248 additions and 1 deletion.
Expand Up @@ -131,5 +131,47 @@ public sealed interface NotEmptyMutableCollection<E> : MutableCollection<E>,
* [index] is out of bounds.
*/
@Throws(IndexOutOfBoundsException::class)
public fun set(index: Int, element: E): E
public operator fun set(index: Int, element: E): E

/**
* Replaces the element at the specified [index] in this list with the
* specified [element], or throws an [IndexOutOfBoundsException] if the
* [index] is out of bounds.
*/
@Throws(IndexOutOfBoundsException::class)
public operator fun set(index: PositiveInt, element: E): E =
set(index.value, element)

/**
* Replaces the element at the specified [index] in this list with the
* specified [element], or throws an [IndexOutOfBoundsException] if the
* [index] is out of bounds.
*/
@Throws(IndexOutOfBoundsException::class)
public operator fun set(index: StrictlyPositiveInt, element: E): E =
set(index.value, element)

/**
* Replaces the element at the specified [index] in this list with the
* specified [element], or returns `null` if the [index] is out of bounds.
*/
public fun setOrNull(index: Int, element: E): E? = try {
set(index, element)
} catch (_: IndexOutOfBoundsException) {
null
}

/**
* Replaces the element at the specified [index] in this list with the
* specified [element], or returns `null` if the [index] is out of bounds.
*/
public fun setOrNull(index: PositiveInt, element: E): E? =
setOrNull(index.value, element)

/**
* Replaces the element at the specified [index] in this list with the
* specified [element], or returns `null` if the [index] is out of bounds.
*/
public fun setOrNull(index: StrictlyPositiveInt, element: E): E? =
setOrNull(index.value, element)
}
Expand Up @@ -759,4 +759,209 @@ class NotEmptyMutableCollectionTest {
}
}
}

@Nested
inner class Set {
// ---------- PositiveInt ----------

@Test
fun `should replace the head with an index as a positive int that equals 0`() {
// GIVEN
val collection: NotEmptyMutableCollection<String> =
NotEmptyMutableList("one")
val index = PositiveInt(0)
val element = "update"
// WHEN
assertDoesNotThrow { collection[index] = element }
// THEN
element.run {
assertEquals(collection[index])
assertEquals(collection.head)
}
}

@Test
fun `should replace a tail's element with an index as a positive int in 1 until the collection's size`() {
// GIVEN
val collection: NotEmptyMutableCollection<String> =
NotEmptyMutableList("one", "two")
val index = PositiveInt(1)
val element = "update"
// WHEN
assertDoesNotThrow { collection[index] = element }
// THEN
collection[index] assertEquals element
}

@Test
fun `should throw an error with an index as a positive int that is out of bounds`() {
// GIVEN
val collection: NotEmptyMutableCollection<String> =
NotEmptyMutableList("one")
val index = PositiveInt(collection.size)
val element = "update"
// WHEN & THEN
assertFailsWith<IndexOutOfBoundsException> {
collection[index] = element
}
}

// ---------- StrictlyPositiveInt ----------

@Test
fun `should replace a tail's element with an index as a strictly positive int in 1 until the collection's size`() {
// GIVEN
val collection: NotEmptyMutableCollection<String> =
NotEmptyMutableList("one", "two")
val index = StrictlyPositiveInt(1)
val element = "update"
// WHEN
assertDoesNotThrow { collection[index] = element }
// THEN
collection[index] assertEquals element
}

@Test
fun `should throw an error with an index as a strictly positive int that is out of bounds`() {
// GIVEN
val collection: NotEmptyMutableCollection<String> =
NotEmptyMutableList("one")
val index = StrictlyPositiveInt(collection.size)
val element = "update"
// WHEN & THEN
assertFailsWith<IndexOutOfBoundsException> {
collection[index] = element
}
}
}

@Nested
inner class SetOrNull {
// ---------- Int ----------

@Test
fun `should replace the head with an index as an int that equals 0`() {
// GIVEN
val collection: NotEmptyMutableCollection<String> =
NotEmptyMutableList("one")
val index = 0
val element = "update"
// WHEN
val result: String? = collection.setOrNull(index, element)
// THEN
element.run {
assertEquals(result.assertNotNull())
assertEquals(collection[index])
assertEquals(collection.head)
}
}

@Test
fun `should replace a tail's element with an index as an int in 1 until the collection's size`() {
// GIVEN
val collection: NotEmptyMutableCollection<String> =
NotEmptyMutableList("one", "two")
val index = 1
val element = "update"
// WHEN
val result: String? = collection.setOrNull(index, element)
// THEN
element.run {
assertEquals(result.assertNotNull())
assertEquals(collection[index])
}
}

@Test
fun `should return null with an index as an int that is out of bounds`() {
// GIVEN
val collection: NotEmptyMutableCollection<String> =
NotEmptyMutableList("one", "two")
val index: Int = collection.size
val element = "update"
// WHEN
val result: String? = collection.setOrNull(index, element)
// THEN
result.assertNull()
}

// ---------- PositiveInt ----------

@Test
fun `should replace the head with an index as a positive int that equals 0`() {
// GIVEN
val collection: NotEmptyMutableCollection<String> =
NotEmptyMutableList("one")
val index = PositiveInt(0)
val element = "update"
// WHEN
val result: String? = collection.setOrNull(index, element)
// THEN
element.run {
assertEquals(result.assertNotNull())
assertEquals(collection[index])
assertEquals(collection.head)
}
}

@Test
fun `should replace a tail's element with an index as a positive int in 1 until the collection's size`() {
// GIVEN
val collection: NotEmptyMutableCollection<String> =
NotEmptyMutableList("one", "two")
val index = PositiveInt(1)
val element = "update"
// WHEN
val result: String? = collection.setOrNull(index, element)
// THEN
element.run {
assertEquals(result.assertNotNull())
assertEquals(collection[index])
}
}

@Test
fun `should return null with an index as a positive int that is out of bounds`() {
// GIVEN
val collection: NotEmptyMutableCollection<String> =
NotEmptyMutableList("one", "two")
val index = PositiveInt(collection.size)
val element = "update"
// WHEN
val result: String? = collection.setOrNull(index, element)
// THEN
result.assertNull()
}

// ---------- StrictlyPositiveInt ----------

@Test
fun `should replace a tail's element with an index as a strictly positive int in 1 until the collection's size`() {
// GIVEN
val collection: NotEmptyMutableCollection<String> =
NotEmptyMutableList("one", "two")
val index = StrictlyPositiveInt(1)
val element = "update"
// WHEN
val result: String? = collection.setOrNull(index, element)
// THEN
element.run {
assertEquals(result.assertNotNull())
assertEquals(collection[index])
}
}

@Test
fun `should return null with an index as a strictly positive int that is out of bounds`() {
// GIVEN
val collection: NotEmptyMutableCollection<String> =
NotEmptyMutableList("one", "two")
val index = StrictlyPositiveInt(collection.size)
val element = "update"
// WHEN
val result: String? = collection.setOrNull(index, element)
// THEN
result.assertNull()
}
}
}

0 comments on commit ff3219e

Please sign in to comment.