Skip to content

Commit

Permalink
Add test to make 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Gray-Wind committed Mar 8, 2022
1 parent bef1003 commit d8c86b9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Swift/Sources/StateMachine/StateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ open class StateMachine<State: StateMachineHashable, Event: StateMachineHashable
public struct Valid: CustomDebugStringConvertible {

public var debugDescription: String {
return "fromState: \(fromState), event: \(event), toState: \(toState), sideEffect: \(sideEffects)"
return "fromState: \(fromState), event: \(event), toState: \(toState), sideEffects: \(sideEffects)"
}

public let fromState: State
Expand Down
26 changes: 19 additions & 7 deletions Swift/Tests/StateMachineTests/StateMachineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ final class StateMachineTests: XCTestCase, StateMachineBuilder {

enum State: StateMachineHashable {

case stateOne, stateTwo
case stateOne, stateTwo, stateThree
}

enum Event: StateMachineHashable {

case eventOne, eventTwo
case eventOne, eventTwo, eventThree
}

enum SideEffect {
enum SideEffect: Equatable {

case commandOne, commandTwo, commandThree
case commandOne, commandTwo, commandThree, commandFour(Int)
}

typealias TestStateMachine = StateMachine<State, Event, SideEffect>
Expand All @@ -33,7 +33,7 @@ final class StateMachineTests: XCTestCase, StateMachineBuilder {
initialState(_state)
state(.stateOne) {
on(.eventOne) {
dontTransition(emit: .commandOne)
dontTransition(emit: .commandOne, .commandTwo)
}
on(.eventTwo) {
transition(to: .stateTwo, emit: .commandTwo)
Expand All @@ -43,7 +43,11 @@ final class StateMachineTests: XCTestCase, StateMachineBuilder {
on(.eventTwo) {
dontTransition(emit: .commandThree)
}
on(.eventThree) { _, event in
transition(to: .stateThree, emit: .commandFour(try event.string()))
}
}
state(.stateThree)
}
}

Expand All @@ -66,7 +70,7 @@ final class StateMachineTests: XCTestCase, StateMachineBuilder {
expect(transition).to(equal(ValidTransition(fromState: .stateOne,
event: .eventOne,
toState: .stateOne,
sideEffects: [.commandOne])))
sideEffects: [.commandOne, .commandTwo])))
}

func testTransition() throws {
Expand Down Expand Up @@ -131,7 +135,7 @@ final class StateMachineTests: XCTestCase, StateMachineBuilder {
.success(ValidTransition(fromState: .stateOne,
event: .eventOne,
toState: .stateOne,
sideEffects: [.commandOne])),
sideEffects: [.commandOne, .commandTwo])),
.success(ValidTransition(fromState: .stateOne,
event: .eventTwo,
toState: .stateTwo,
Expand Down Expand Up @@ -191,6 +195,14 @@ final class StateMachineTests: XCTestCase, StateMachineBuilder {
// Then
expect(error).to(equal(.recursionDetected))
}

func testGettingNonExistingValue() throws {
// Given
let stateMachine: TestStateMachine = givenState(is: .stateTwo)

// Then
XCTAssertThrowsError(try stateMachine.transition(.eventThree))
}
}

final class Logger {
Expand Down

0 comments on commit d8c86b9

Please sign in to comment.