Skip to content

Commit 8e49f03

Browse files
committed
Test Store Ergonomics (#285)
* Test Store Ergonomics * fix merge * fix
1 parent c0e5c0d commit 8e49f03

File tree

1 file changed

+54
-19
lines changed

1 file changed

+54
-19
lines changed

Sources/ComposableArchitecture/TestSupport/TestStore.swift

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,23 @@
265265
if expectedState != actualState {
266266
let diff =
267267
debugDiff(expectedState, actualState)
268-
.map { ": …\n\n\($0.indent(by: 4))\n\n(Expected: −, Actual: +)" }
269-
?? ""
268+
.map { "\($0.indent(by: 4))\n\n(Expected: −, Actual: +)" }
269+
?? """
270+
Expected:
271+
\(String(describing: expectedState).indent(by: 2))
272+
273+
Actual:
274+
\(String(describing: actualState).indent(by: 2))
275+
"""
276+
270277
_XCTFail(
271278
"""
272-
State change does not match expectation\(diff)
279+
State change does not match expectation: …
280+
281+
\(diff)
273282
""",
274-
file: step.file, line: step.line
283+
file: step.file,
284+
line: step.line
275285
)
276286
}
277287
}
@@ -291,7 +301,11 @@
291301
)
292302
}
293303
viewStore.send(action)
294-
update(&expectedState)
304+
do {
305+
try update(&expectedState)
306+
} catch {
307+
_XCTFail("Threw error: \(error)", file: step.file, line: step.line)
308+
}
295309
expectedStateShouldMatch(actualState: toLocalState(snapshotState))
296310

297311
case let .receive(expectedAction, update):
@@ -308,16 +322,29 @@
308322
if expectedAction != receivedAction {
309323
let diff =
310324
debugDiff(expectedAction, receivedAction)
311-
.map { ": …\n\n\($0.indent(by: 4))\n\n(Expected: −, Actual: +)" }
312-
?? ""
325+
.map { "\($0.indent(by: 4))\n\n(Expected: −, Received: +)" }
326+
?? """
327+
Expected:
328+
\(String(describing: expectedState).indent(by: 2))
329+
330+
Received:
331+
\(String(describing: receivedAction).indent(by: 2))
332+
"""
333+
313334
_XCTFail(
314335
"""
315-
Received unexpected action\(diff)
336+
Received unexpected action: …
337+
338+
\(diff)
316339
""",
317340
file: step.file, line: step.line
318341
)
319342
}
320-
update(&expectedState)
343+
do {
344+
try update(&expectedState)
345+
} catch {
346+
_XCTFail("Threw error: \(error)", file: step.file, line: step.line)
347+
}
321348
expectedStateShouldMatch(actualState: toLocalState(state))
322349
snapshotState = state
323350

@@ -333,7 +360,11 @@
333360
file: step.file, line: step.line
334361
)
335362
}
336-
work(&self.environment)
363+
do {
364+
try work(&self.environment)
365+
} catch {
366+
_XCTFail("Threw error: \(error)", file: step.file, line: step.line)
367+
}
337368

338369
case let .do(work):
339370
if !receivedActions.isEmpty {
@@ -347,7 +378,11 @@
347378
file: step.file, line: step.line
348379
)
349380
}
350-
work()
381+
do {
382+
try work()
383+
} catch {
384+
_XCTFail("Threw error: \(error)", file: step.file, line: step.line)
385+
}
351386
}
352387
}
353388

@@ -456,7 +491,7 @@
456491
_ action: LocalAction,
457492
file: StaticString = #file,
458493
line: UInt = #line,
459-
_ update: @escaping (inout LocalState) -> Void = { _ in }
494+
_ update: @escaping (inout LocalState) throws -> Void = { _ in }
460495
) -> Step {
461496
Step(.send(action, update), file: file, line: line)
462497
}
@@ -473,7 +508,7 @@
473508
_ action: Action,
474509
file: StaticString = #file,
475510
line: UInt = #line,
476-
_ update: @escaping (inout LocalState) -> Void = { _ in }
511+
_ update: @escaping (inout LocalState) throws -> Void = { _ in }
477512
) -> Step {
478513
Step(.receive(action, update), file: file, line: line)
479514
}
@@ -486,7 +521,7 @@
486521
public static func environment(
487522
file: StaticString = #file,
488523
line: UInt = #line,
489-
_ update: @escaping (inout Environment) -> Void
524+
_ update: @escaping (inout Environment) throws -> Void
490525
) -> Step {
491526
Step(.environment(update), file: file, line: line)
492527
}
@@ -498,16 +533,16 @@
498533
public static func `do`(
499534
file: StaticString = #file,
500535
line: UInt = #line,
501-
_ work: @escaping () -> Void
536+
_ work: @escaping () throws -> Void
502537
) -> Step {
503538
Step(.do(work), file: file, line: line)
504539
}
505540

506541
fileprivate enum StepType {
507-
case send(LocalAction, (inout LocalState) -> Void)
508-
case receive(Action, (inout LocalState) -> Void)
509-
case environment((inout Environment) -> Void)
510-
case `do`(() -> Void)
542+
case send(LocalAction, (inout LocalState) throws -> Void)
543+
case receive(Action, (inout LocalState) throws -> Void)
544+
case environment((inout Environment) throws -> Void)
545+
case `do`(() throws -> Void)
511546
}
512547
}
513548

0 commit comments

Comments
 (0)