Skip to content

Commit

Permalink
can add item to item only if not already there
Browse files Browse the repository at this point in the history
  • Loading branch information
vextorspace committed Jul 12, 2024
1 parent 0349c2c commit 1fdc06e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions composeApp/src/commonMain/kotlin/model/Item.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ data class Item(var content: String = "New Item", val subItems: MutableList<Item
constructor(content: String, subItems: Collection<Item>) : this(content, subItems.toMutableList())

fun add(subItem: Item) {
if(subItems.contains(subItem))
return
subItems.add(subItem)
}

Expand Down
10 changes: 10 additions & 0 deletions composeApp/src/commonTest/kotlin/model/SubItemOrderMattersTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package model

import io.kotest.matchers.collections.shouldContainExactly
import io.kotest.matchers.collections.shouldContainInOrder
import kotlin.test.Test

Expand Down Expand Up @@ -27,4 +28,13 @@ class SubItemOrderMattersTest {
item.remove(subItem1)
item.subItems.shouldContainInOrder(subItem3, subItem2)
}

@Test
fun `same item cannot be added twice to an item`() {
val item = Item("item")
val subItem = Item("subItem")
item.add(subItem)
item.add(subItem)
item.subItems.shouldContainExactly(subItem)
}
}

0 comments on commit 1fdc06e

Please sign in to comment.