diff --git a/makina-compiler/src/xyz/colinholzman/makina/StateConfiguration.kt b/makina-compiler/src/xyz/colinholzman/makina/StateConfiguration.kt index c0cbfcb..41478d3 100644 --- a/makina-compiler/src/xyz/colinholzman/makina/StateConfiguration.kt +++ b/makina-compiler/src/xyz/colinholzman/makina/StateConfiguration.kt @@ -18,7 +18,7 @@ data class StateConfiguration(val states: Set) { } //Returns the list of handlers that this state configuration should handle. - //The handlers are deepest deepest state first and in document order for handlers within a single state. + //The handlers are deepest state first and in document order for handlers within a single state. fun getHandlers(): List> { return orderedStates.flatMap { state -> state.handlers.filterIsInstance().map { Pair(state, it) } } } @@ -32,4 +32,13 @@ data class StateConfiguration(val states: Set) { fun getLeafState(): State { return orderedStates.first() } + + companion object { + fun List>.groupByIdAndRemoveRedundantHandlers(): Map>> { + return groupBy { it.second.id }.mapValues { entry -> + val guards = HashSet() + entry.value.filter { guards.add(it.second.guard) } + } + } + } } \ No newline at end of file diff --git a/makina-compiler/test/xyz/colinholzman/makina/StateConfigurationTest.kt b/makina-compiler/test/xyz/colinholzman/makina/StateConfigurationTest.kt index 00ca6bc..d3f9178 100644 --- a/makina-compiler/test/xyz/colinholzman/makina/StateConfigurationTest.kt +++ b/makina-compiler/test/xyz/colinholzman/makina/StateConfigurationTest.kt @@ -4,6 +4,10 @@ import org.junit.jupiter.api.Test import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.assertThrows +import xyz.colinholzman.makina.StateConfiguration.Companion.groupByIdAndRemoveRedundantHandlers +import xyz.colinholzman.makina.TestStates.Companion.s1 +import xyz.colinholzman.makina.TestStates.Companion.s11 +import xyz.colinholzman.makina.TestStates.Companion.s111 internal class StateConfigurationTest { @@ -49,4 +53,46 @@ internal class StateConfigurationTest { val config = StateConfiguration(setOf(child, parent)) assertEquals(child, config.getLeafState()) } + + @Test + fun filterHandlers() { + var handlers = arrayListOf( + Pair(s111, Handler.Event("foo")), + Pair(s11, Handler.Event("foo")), + Pair(s1, Handler.Event("foo")) + ) + var expected = mapOf(Pair("foo", listOf(Pair(s111, Handler.Event("foo"))))) + assertEquals(expected, handlers.groupByIdAndRemoveRedundantHandlers()) + + + handlers = arrayListOf( + Pair(s111, Handler.Event("foo")), + Pair(s11, Handler.Event("foo")), + Pair(s1, Handler.Event("foo", guard = "bar")) + ) + expected = mapOf(Pair("foo", listOf(Pair(s111, Handler.Event("foo")), + Pair(s1, Handler.Event("foo", guard = "bar"))))) + assertEquals(expected, handlers.groupByIdAndRemoveRedundantHandlers()) + + + handlers = arrayListOf( + Pair(s111, Handler.Event("foo")), + Pair(s11, Handler.Event("bar")), + Pair(s1, Handler.Event("foo")) + ) + expected = mapOf(Pair("foo", listOf(Pair(s111, Handler.Event("foo")))), + Pair("bar", listOf(Pair(s11, Handler.Event("bar"))))) + assertEquals(expected, handlers.groupByIdAndRemoveRedundantHandlers()) + + + handlers = arrayListOf( + Pair(s111, Handler.Event("foo", guard = "bar")), + Pair(s11, Handler.Event("foo", guard = "baz")), + Pair(s1, Handler.Event("foo")) + ) + expected = mapOf(Pair("foo", listOf(Pair(s111, Handler.Event("foo", guard = "bar")), + Pair(s11, Handler.Event("foo", guard = "baz")), + Pair(s1, Handler.Event("foo"))))) + assertEquals(expected, handlers.groupByIdAndRemoveRedundantHandlers()) + } } \ No newline at end of file