Skip to content

Commit

Permalink
Access update name and user_info properties for a State
Browse files Browse the repository at this point in the history
Also updated formatter configuration and include formatting changes due to the configuration change.
  • Loading branch information
insha committed May 31, 2023
1 parent a940f02 commit 8887262
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 49 deletions.
3 changes: 2 additions & 1 deletion .swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
--disable redundantType

--indent 4
--allman true
--allman true
--closingparen same-line
--elseposition next-line
--indentcase true
--patternlet inline
Expand Down
9 changes: 3 additions & 6 deletions Examples/KryptonExample/KryptonExample/AlarmService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,11 @@ extension AlarmService
will_enter: { context, _ in print("[Will Enter] \(context)") },
did_enter: { context, _ in print("[Did Enter] \(context)") },
will_exit: { context, _ in print("[Will Exit] \(context)") },
did_exit: { context, _ in print("[Did Exit] \(context)") }
)
did_exit: { context, _ in print("[Did Exit] \(context)") })

let event_transitions = Event.TransitionContext(
will_fire: { context, _ in print("[Will Fire] \(context)") },
did_fire: { context, _ in print("[Did Fire] \(context)") }
)
did_fire: { context, _ in print("[Did Fire] \(context)") })

let state_armed = try State(name: "Armed", transition_context: state_transitions)
let state_disarmed = try State(name: "Disarmed", transition_context: state_transitions)
Expand All @@ -171,7 +169,6 @@ extension AlarmService
return (
states: [state_armed, state_disarmed, state_alarm],
events: [event_arm, event_disarm, event_reset, event_breach, event_panic],
initial: state_disarmed
)
initial: state_disarmed)
}
}
3 changes: 1 addition & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ let package = Package(
dependencies: [],
targets: [
.target(name: "Krypton", dependencies: []),
.testTarget(name: "KryptonTests",
dependencies: ["Krypton"])
.testTarget(name: "KryptonTests", dependencies: ["Krypton"])
])
4 changes: 1 addition & 3 deletions Sources/Krypton/Builders/EventBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ public final class EventBuilder
transition_context: StateMachine.Event.TransitionContext(
should_fire: context_action_should_fire,
will_fire: context_action_will_fire,
did_fire: context_action_did_fire
)
)
did_fire: context_action_did_fire))
}
}
4 changes: 1 addition & 3 deletions Sources/Krypton/Builders/StateBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ public final class StateBuilder
will_enter: context_action_will_enter,
did_enter: context_action_did_enter,
will_exit: context_action_will_exit,
did_exit: context_action_did_exit
)
)
did_exit: context_action_did_exit))
}
}
6 changes: 2 additions & 4 deletions Sources/Krypton/Event.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public extension StateMachine
public init(
should_fire: TransitionTriggerValidation? = nil,
will_fire: TransitionContextAction<Event>? = nil,
did_fire: TransitionContextAction<Event>? = nil
)
did_fire: TransitionContextAction<Event>? = nil)
{
self.should_fire = should_fire
self.will_fire = will_fire
Expand All @@ -41,8 +40,7 @@ public extension StateMachine
name: String,
sources: Set<State>,
destination: State,
transition_context: TransitionContext = TransitionContext()
) throws
transition_context: TransitionContext = TransitionContext()) throws
{
guard !name.isEmpty
else
Expand Down
8 changes: 4 additions & 4 deletions Sources/Krypton/State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public extension StateMachine
will_enter: TransitionContextAction<State>? = nil,
did_enter: TransitionContextAction<State>? = nil,
will_exit: TransitionContextAction<State>? = nil,
did_exit: TransitionContextAction<State>? = nil
)
did_exit: TransitionContextAction<State>? = nil)
{
self.will_enter = will_enter
self.did_enter = did_enter
Expand All @@ -32,8 +31,9 @@ public extension StateMachine
}
}

let name: String
let user_info: Payload
public let name: String
public let user_info: Payload

let transition_context: Context?

public init(name: String, user_info: Payload = [:], transition_context: Context? = nil) throws
Expand Down
24 changes: 8 additions & 16 deletions Tests/KryptonTests/KryptonEventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ class KryptonEventTests: XCTestCase

XCTAssertTrue(
actualValue == expectedValue,
"The description, `\(actualValue)`, does not match what was expected, `\(expectedValue)`"
)
"The description, `\(actualValue)`, does not match what was expected, `\(expectedValue)`")
}

func testAnEventDescriptionWithMultipleSourceState() throws
Expand All @@ -60,8 +59,7 @@ class KryptonEventTests: XCTestCase

XCTAssertTrue(
actualValue == expectedValue,
"The description, `\(actualValue)`, does not match what was expected, `\(expectedValue)`"
)
"The description, `\(actualValue)`, does not match what was expected, `\(expectedValue)`")
}

func testEventThatIsDeclined() throws
Expand All @@ -72,14 +70,12 @@ class KryptonEventTests: XCTestCase
let event_transition = StateMachine.Event.TransitionContext(
should_fire: disallow_event,
will_fire: nil,
did_fire: nil
)
did_fire: nil)
let eventA = try StateMachine.Event(
name: "Event-A-to-B",
sources: [stateA],
destination: stateB,
transition_context: event_transition
)
transition_context: event_transition)
let system = try StateMachine(initial_state: stateA)

system.add(states: [stateA, stateB])
Expand All @@ -88,8 +84,7 @@ class KryptonEventTests: XCTestCase

XCTAssertThrowsError(
try system.fire(event: eventA),
"We expected the `declined` error; but no errors were thrown."
)
"We expected the `declined` error; but no errors were thrown.")
}

func testEventThatIsExplicitlyAllowedToBeTriggered() throws
Expand All @@ -100,14 +95,12 @@ class KryptonEventTests: XCTestCase
let event_transition = StateMachine.Event.TransitionContext(
should_fire: allow_event,
will_fire: nil,
did_fire: nil
)
did_fire: nil)
let eventA = try StateMachine.Event(
name: "Event-A-to-B",
sources: [stateA],
destination: stateB,
transition_context: event_transition
)
transition_context: event_transition)
let system = try StateMachine(initial_state: stateA)

system.add(states: [stateA, stateB])
Expand All @@ -116,7 +109,6 @@ class KryptonEventTests: XCTestCase

XCTAssertNoThrow(
try system.fire(event: eventA),
"We expected the state machine to allow the event, but it was declined."
)
"We expected the state machine to allow the event, but it was declined.")
}
}
3 changes: 1 addition & 2 deletions Tests/KryptonTests/KryptonStateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ final class KryptonStateTests: XCTestCase
{
XCTAssertNoThrow(
try StateMachine.State(name: "State-A"),
"We expected an state to be created; but it wasn't."
)
"We expected an state to be created; but it wasn't.")
}

func testFailureToCreateAState() throws
Expand Down
12 changes: 4 additions & 8 deletions Tests/KryptonTests/KryptonTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ final class KryptonTests: XCTestCase

XCTAssertTrue(
value,
"We expected the state machine to return a `notActivated` error, but did not received any error."
)
"We expected the state machine to return a `notActivated` error, but did not received any error.")
}

func testStateMachineInCorrectState() throws
Expand All @@ -243,8 +242,7 @@ final class KryptonTests: XCTestCase

XCTAssertTrue(
system.isIn(state: stateB),
"We expected the state machine to be in state `\(stateB.name)`, but it is in `\(system.current_state.name)`."
)
"We expected the state machine to be in state `\(stateB.name)`, but it is in `\(system.current_state.name)`.")
}

func testStateMachineCannotFireEventWhenNotActivated() throws
Expand All @@ -259,8 +257,7 @@ final class KryptonTests: XCTestCase

XCTAssertThrowsError(
try system.fire(event: eventA),
"We expected the `not_activated` error; but no errors were thrown."
)
"We expected the `not_activated` error; but no errors were thrown.")
}

func testTransitionIsNotPermitted() throws
Expand All @@ -278,7 +275,6 @@ final class KryptonTests: XCTestCase

XCTAssertThrowsError(
try system.fire(event: eventA),
"We expected the `cannot_fire` error; but no errors were thrown."
)
"We expected the `cannot_fire` error; but no errors were thrown.")
}
}

0 comments on commit 8887262

Please sign in to comment.