Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kdoc to transitioner and $value to invalid state transition error… #52

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package app.cash.kfsm
class InvalidStateTransition(transition: Transition<*, *>, value: Value<*, *>) : Exception(
"Value cannot transition ${
transition.from.set.toList().sortedBy { it.toString() }.joinToString(", ", prefix = "{", postfix = "}")
} to ${transition.to}, because it is currently ${value.state}"
} to ${transition.to}, because it is currently ${value.state}. [value=$value]"
)
10 changes: 10 additions & 0 deletions lib/src/main/kotlin/app/cash/kfsm/Transitioner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@ package app.cash.kfsm

abstract class Transitioner<T : Transition<V, S>, V : Value<V, S>, S : State<S>> {

/** Will be executed prior to the transition effect. Failure here will terminate the transition */
open fun preHook(value: V, via: T): Result<Unit> = Result.success(Unit)

/** Will be executed after the transition effect. Use this to persist the value. */
open fun persist(value: V, via: T): Result<V> = Result.success(value)

/** Will be executed after the transition effect & value persistence. Use this to perform side effects such as notifications. */
open fun postHook(from: S, value: V, via: T): Result<Unit> = Result.success(Unit)

/**
* Execute the given transition on the given value.
*
* If the target state is already present, then this is a no-op.
* If the provided transition cannot apply to the value's state, then this is a failure.
* Otherwise, the transition is applied and the state is updated in the returned value.
*/
fun transition(
value: V,
transition: T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import io.kotest.matchers.shouldBe
class InvalidStateTransitionTest : StringSpec({
"with single from-state has correct message" {
InvalidStateTransition(LetterTransition(A, B), Letter(E)).message shouldBe
"Value cannot transition {A} to B, because it is currently E"
"Value cannot transition {A} to B, because it is currently E. [value=Letter(state=E)]"
}

"with many from-states has correct message" {
InvalidStateTransition(LetterTransition(States(C, B), D), Letter(E)).message shouldBe
"Value cannot transition {B, C} to D, because it is currently E"
"Value cannot transition {B, C} to D, because it is currently E. [value=Letter(state=E)]"
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class TransitionerAsyncTest : StringSpec({
val transitioner = transitioner()

transitioner.transition(Letter(C), transition).shouldBeFailure()
.shouldHaveMessage("Value cannot transition {A} to B, because it is currently C")
.shouldHaveMessage("Value cannot transition {A} to B, because it is currently C. [value=Letter(state=C)]")

transitioner.preHookExecuted shouldBe 0
transition.effected shouldBe 0
Expand Down
2 changes: 1 addition & 1 deletion lib/src/test/kotlin/app/cash/kfsm/TransitionerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class TransitionerTest : StringSpec({
val transitioner = transitioner()

transitioner.transition(Letter(C), transition).shouldBeFailure()
.shouldHaveMessage("Value cannot transition {A} to B, because it is currently C")
.shouldHaveMessage("Value cannot transition {A} to B, because it is currently C. [value=Letter(state=C)]")

transitioner.preHookExecuted shouldBe 0
transition.effected shouldBe 0
Expand Down
Loading