Skip to content

Commit 609166c

Browse files
committed
Updated unit tests
1 parent 5aa9b13 commit 609166c

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

‎Tests/SwiftExtensionsTests/Abstractions/StateMachine/AsyncStateMachine Tests.swift‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,26 @@ struct AsyncStateMachine_Tests {
4646
#expect(await receiver.count == 1)
4747
}
4848

49+
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
50+
@Test
51+
func start_NoContext() async {
52+
let lifecycle = AsyncStateMachine<StartableStateMachineState>(initialState: .stopped)
53+
54+
actor Receiver {
55+
var count: Int = 0
56+
func inc() { count += 1 }
57+
init() { }
58+
}
59+
60+
let receiver = Receiver()
61+
62+
await lifecycle.withTransition(to: .started) {
63+
_ = await Task { await receiver.inc() }.value
64+
}
65+
#expect(lifecycle.state == .started)
66+
#expect(await receiver.count == 1)
67+
}
68+
4969
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
5070
@Test
5171
func stop() async {
@@ -209,6 +229,30 @@ struct AsyncStateMachine_Tests {
209229
#expect(lifecycle.state == .stopped) // no change
210230
}
211231

232+
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
233+
@Test
234+
func withState_NoContext() async {
235+
let lifecycle = AsyncStateMachine<StartableStateMachineState>(initialState: .stopped)
236+
237+
await confirmation(expectedCount: 0b1) { confirmation in
238+
await lifecycle.withState(expecting: [.stopped, .stoppedPermanently]) {
239+
confirmation(count: 0b1)
240+
} wrongState: {
241+
confirmation(count: 0b10)
242+
}
243+
}
244+
#expect(lifecycle.state == .stopped)
245+
246+
await confirmation(expectedCount: 0b10) { confirmation in
247+
await lifecycle.withState(expecting: .started) {
248+
confirmation(count: 0b1)
249+
} wrongState: {
250+
confirmation(count: 0b10)
251+
}
252+
}
253+
#expect(lifecycle.state == .stopped) // no change
254+
}
255+
212256
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
213257
@Test
214258
func dummyClass() async {

‎Tests/SwiftExtensionsTests/Abstractions/StateMachine/StateMachine Tests.swift‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,28 @@ struct StateMachine_Tests {
184184
}
185185
#expect(lifecycle.state == .stopped) // no change
186186
}
187+
188+
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
189+
@Test
190+
func withState_NoContext() async {
191+
let lifecycle = StateMachine<StartableStateMachineState>(initialState: .stopped)
192+
193+
await confirmation(expectedCount: 0b1) { confirmation in
194+
lifecycle.withState(expecting: [.stopped, .stoppedPermanently]) {
195+
confirmation(count: 0b1)
196+
} wrongState: {
197+
confirmation(count: 0b10)
198+
}
199+
}
200+
#expect(lifecycle.state == .stopped)
201+
202+
await confirmation(expectedCount: 0b10) { confirmation in
203+
lifecycle.withState(expecting: .started) {
204+
confirmation(count: 0b1)
205+
} wrongState: {
206+
confirmation(count: 0b10)
207+
}
208+
}
209+
#expect(lifecycle.state == .stopped) // no change
210+
}
187211
}

0 commit comments

Comments
 (0)