File tree 3 files changed +36
-12
lines changed
Tests/SnapshotTestingTests
__Snapshots__/AssertSnapshotSwiftTests
3 files changed +36
-12
lines changed Original file line number Diff line number Diff line change @@ -523,27 +523,33 @@ func sanitizePathComponent(_ string: String) -> String {
523
523
}
524
524
#endif
525
525
526
+ extension DispatchQueue {
527
+ private static let key = DispatchSpecificKey < UInt8 > ( )
528
+ private static let value : UInt8 = 0
529
+
530
+ fileprivate static func mainSync< R> ( execute block: ( ) -> R ) -> R {
531
+ Self . main. setSpecific ( key: key, value: value)
532
+ if getSpecific ( key: key) == value {
533
+ return block ( )
534
+ } else {
535
+ return main. sync ( execute: block)
536
+ }
537
+ }
538
+ }
539
+
526
540
// We need to clean counter between tests executions in order to support test-iterations.
527
541
private class CleanCounterBetweenTestCases : NSObject , XCTestObservation {
528
542
private static var registered = false
529
543
530
544
static func registerIfNeeded( ) {
531
- if Thread . isMainThread {
532
- doRegisterIfNeeded ( )
533
- } else {
534
- DispatchQueue . main. sync {
535
- doRegisterIfNeeded ( )
545
+ DispatchQueue . mainSync {
546
+ if !registered {
547
+ registered = true
548
+ XCTestObservationCenter . shared. addTestObserver ( CleanCounterBetweenTestCases ( ) )
536
549
}
537
550
}
538
551
}
539
552
540
- private static func doRegisterIfNeeded( ) {
541
- if !registered {
542
- registered = true
543
- XCTestObservationCenter . shared. addTestObserver ( CleanCounterBetweenTestCases ( ) )
544
- }
545
- }
546
-
547
553
func testCaseDidFinish( _ testCase: XCTestCase ) {
548
554
counterQueue. sync {
549
555
counterMap = [ : ]
Original file line number Diff line number Diff line change 15
15
assertSnapshot ( of: user, as: . dump)
16
16
}
17
17
}
18
+
19
+ @MainActor
20
+ @Suite (
21
+ . snapshots(
22
+ record: . missing
23
+ )
24
+ )
25
+ struct MainActorTests {
26
+ @Test func dump( ) {
27
+ struct User { let id : Int , name : String , bio : String }
28
+ let user = User ( id: 1 , name: " Blobby " , bio: " Blobbed around the world. " )
29
+ assertSnapshot ( of: user, as: . dump)
30
+ }
31
+ }
18
32
#endif
Original file line number Diff line number Diff line change
1
+ ▿ User
2
+ - bio: "Blobbed around the world."
3
+ - id: 1
4
+ - name: "Blobby"
You can’t perform that action at this time.
0 commit comments