-
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.
Introduce States argument to Transition (#37)
* Introduce States argument to Transition * Add test for States * Update readme and changelog
- Loading branch information
Showing
11 changed files
with
97 additions
and
21 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
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
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
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
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,15 @@ | ||
package app.cash.kfsm | ||
|
||
/** A collection of states that is guaranteed to be non-empty. */ | ||
data class States<S : State>(val a: S, val other: Set<S>) { | ||
constructor(first: S, vararg others: S) : this(first, others.toSet()) | ||
|
||
val set: Set<S> = other + a | ||
|
||
companion object { | ||
fun <S: State> Set<S>.toStates(): States<S> = when { | ||
isEmpty() -> throw IllegalArgumentException("Cannot create States from empty set") | ||
else -> toList().let { States(it.first(), it.drop(1).toSet()) } | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,15 +1,14 @@ | ||
package app.cash.kfsm | ||
|
||
open class Transition<V: Value<V, S>, S : State>(val from: Set<S>, val to: S) { | ||
open class Transition<V: Value<V, S>, S : State>(val from: States<S>, val to: S) { | ||
|
||
init { | ||
require(from.isNotEmpty()) { "At least one from state must be defined" } | ||
from.filterNot { it.canDirectlyTransitionTo(to) }.let { | ||
from.set.filterNot { it.canDirectlyTransitionTo(to) }.let { | ||
require(it.isEmpty()) { "invalid transition(s): ${it.map { from -> "$from->$to" }}" } | ||
} | ||
} | ||
|
||
constructor(from: S, to: S) : this(setOf(from), to) | ||
constructor(from: S, to: S) : this(States(from), to) | ||
|
||
open suspend fun effect(value: V): Result<V> = Result.success(value) | ||
} |
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
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
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,30 @@ | ||
package app.cash.kfsm | ||
|
||
import app.cash.kfsm.States.Companion.toStates | ||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.core.spec.style.StringSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.property.Arb | ||
import io.kotest.property.arbitrary.element | ||
import io.kotest.property.arbitrary.set | ||
import io.kotest.property.checkAll | ||
|
||
class StatesTest : StringSpec({ | ||
|
||
"vararg constructor" { | ||
checkAll(Arb.set(arbChar, range = 1 .. 5)) { set -> | ||
States(set.first(), set).set shouldBe set | ||
set.toStates().set shouldBe set | ||
} | ||
} | ||
|
||
"fails to create from empty set" { | ||
shouldThrow<IllegalArgumentException> { | ||
emptySet<Char>().toStates() | ||
} | ||
} | ||
}) { | ||
companion object { | ||
val arbChar: Arb<Char> = Arb.element(A, B, C, D, E) | ||
} | ||
} |
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
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