generated from vextorspace/KotlinMultiplatformTesting
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added guarding test to make sure no triversing back up tree in visitor
- Loading branch information
1 parent
d158343
commit 1d11412
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package model | ||
|
||
class ItemVisitor(val action: ItemVisitorAction) { | ||
fun visit(item: Item) { | ||
action.accept(item) | ||
item.subItems.forEach { visit(it) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package model | ||
|
||
interface ItemVisitorAction { | ||
fun accept(item: Item) | ||
} |
10 changes: 10 additions & 0 deletions
10
composeApp/src/commonTest/kotlin/model/ItemVisitorCollector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package model | ||
|
||
class ItemVisitorCollector : ItemVisitorAction { | ||
val items = mutableListOf<Item>() | ||
|
||
override fun accept(item: Item) { | ||
items.add(item) | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
composeApp/src/commonTest/kotlin/model/ItemVisitorVisitsAllSubItemsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package model | ||
|
||
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder | ||
import kotlin.test.Test | ||
|
||
class ItemVisitorVisitsAllSubItemsTest { | ||
|
||
@Test | ||
fun `ItemVisitor visits all sub items`() { | ||
// Given | ||
val item = Item("parent") | ||
val child = Item("child") | ||
val sibling = Item("sibling") | ||
val grandChild = Item("grandChild") | ||
child.add(grandChild) | ||
item.add(child) | ||
item.add(sibling) | ||
|
||
val itemCollector = ItemVisitorCollector() | ||
val visitor = ItemVisitor(itemCollector) | ||
|
||
// When | ||
visitor.visit(item) | ||
|
||
// Then | ||
itemCollector.items shouldContainExactlyInAnyOrder listOf(item, child, sibling, grandChild) | ||
} | ||
|
||
@Test | ||
fun `ItemVisitor starting at child does not get other branches`() { | ||
// Given | ||
val item = Item("parent") | ||
val child = Item("child") | ||
val sibling = Item("sibling") | ||
val grandChild = Item("grandChild") | ||
child.add(grandChild) | ||
item.add(child) | ||
item.add(sibling) | ||
|
||
val itemCollector = ItemVisitorCollector() | ||
val visitor = ItemVisitor(itemCollector) | ||
|
||
// When | ||
visitor.visit(child) | ||
|
||
// Then | ||
itemCollector.items shouldContainExactlyInAnyOrder listOf(child, grandChild) | ||
} | ||
} |