Skip to content

Commit

Permalink
Added groupByIdAndRemoveRedundantHandlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
clnhlzmn committed Mar 23, 2021
1 parent 3761beb commit cfe187e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ data class StateConfiguration(val states: Set<State>) {
}

//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<Pair<State, Handler.Event>> {
return orderedStates.flatMap { state -> state.handlers.filterIsInstance<Handler.Event>().map { Pair(state, it) } }
}
Expand All @@ -32,4 +32,13 @@ data class StateConfiguration(val states: Set<State>) {
fun getLeafState(): State {
return orderedStates.first()
}

companion object {
fun List<Pair<State, Handler.Event>>.groupByIdAndRemoveRedundantHandlers(): Map<String, List<Pair<State, Handler.Event>>> {
return groupBy { it.second.id }.mapValues { entry ->
val guards = HashSet<String?>()
entry.value.filter { guards.add(it.second.guard) }
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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())
}
}

0 comments on commit cfe187e

Please sign in to comment.