Skip to content

Commit d8c86b9

Browse files
committed
Add test to make 100% coverage
1 parent bef1003 commit d8c86b9

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

Swift/Sources/StateMachine/StateMachine.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ open class StateMachine<State: StateMachineHashable, Event: StateMachineHashable
1313
public struct Valid: CustomDebugStringConvertible {
1414

1515
public var debugDescription: String {
16-
return "fromState: \(fromState), event: \(event), toState: \(toState), sideEffect: \(sideEffects)"
16+
return "fromState: \(fromState), event: \(event), toState: \(toState), sideEffects: \(sideEffects)"
1717
}
1818

1919
public let fromState: State

Swift/Tests/StateMachineTests/StateMachineTests.swift

+19-7
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ final class StateMachineTests: XCTestCase, StateMachineBuilder {
1111

1212
enum State: StateMachineHashable {
1313

14-
case stateOne, stateTwo
14+
case stateOne, stateTwo, stateThree
1515
}
1616

1717
enum Event: StateMachineHashable {
1818

19-
case eventOne, eventTwo
19+
case eventOne, eventTwo, eventThree
2020
}
2121

22-
enum SideEffect {
22+
enum SideEffect: Equatable {
2323

24-
case commandOne, commandTwo, commandThree
24+
case commandOne, commandTwo, commandThree, commandFour(Int)
2525
}
2626

2727
typealias TestStateMachine = StateMachine<State, Event, SideEffect>
@@ -33,7 +33,7 @@ final class StateMachineTests: XCTestCase, StateMachineBuilder {
3333
initialState(_state)
3434
state(.stateOne) {
3535
on(.eventOne) {
36-
dontTransition(emit: .commandOne)
36+
dontTransition(emit: .commandOne, .commandTwo)
3737
}
3838
on(.eventTwo) {
3939
transition(to: .stateTwo, emit: .commandTwo)
@@ -43,7 +43,11 @@ final class StateMachineTests: XCTestCase, StateMachineBuilder {
4343
on(.eventTwo) {
4444
dontTransition(emit: .commandThree)
4545
}
46+
on(.eventThree) { _, event in
47+
transition(to: .stateThree, emit: .commandFour(try event.string()))
48+
}
4649
}
50+
state(.stateThree)
4751
}
4852
}
4953

@@ -66,7 +70,7 @@ final class StateMachineTests: XCTestCase, StateMachineBuilder {
6670
expect(transition).to(equal(ValidTransition(fromState: .stateOne,
6771
event: .eventOne,
6872
toState: .stateOne,
69-
sideEffects: [.commandOne])))
73+
sideEffects: [.commandOne, .commandTwo])))
7074
}
7175

7276
func testTransition() throws {
@@ -131,7 +135,7 @@ final class StateMachineTests: XCTestCase, StateMachineBuilder {
131135
.success(ValidTransition(fromState: .stateOne,
132136
event: .eventOne,
133137
toState: .stateOne,
134-
sideEffects: [.commandOne])),
138+
sideEffects: [.commandOne, .commandTwo])),
135139
.success(ValidTransition(fromState: .stateOne,
136140
event: .eventTwo,
137141
toState: .stateTwo,
@@ -191,6 +195,14 @@ final class StateMachineTests: XCTestCase, StateMachineBuilder {
191195
// Then
192196
expect(error).to(equal(.recursionDetected))
193197
}
198+
199+
func testGettingNonExistingValue() throws {
200+
// Given
201+
let stateMachine: TestStateMachine = givenState(is: .stateTwo)
202+
203+
// Then
204+
XCTAssertThrowsError(try stateMachine.transition(.eventThree))
205+
}
194206
}
195207

196208
final class Logger {

0 commit comments

Comments
 (0)