@@ -360,11 +360,68 @@ extension XCTest {
360
360
return outputActual
361
361
}
362
362
363
- public func AssertGenerateManual(
363
+ public func AssertJSONEqualFromString< T: Codable & Equatable > ( actual: String , expected: String , for type: T . Type , file: StaticString = #filePath, line: UInt = #line) throws {
364
+ if #available( macOS 10 . 13 , iOS 11 . 0 , tvOS 11 . 0 , watchOS 4 . 0 , * ) {
365
+ AssertEqualStrings (
366
+ actual: actual. trimmingCharacters ( in: . whitespacesAndNewlines) ,
367
+ expected: expected. trimmingCharacters ( in: . whitespacesAndNewlines) ,
368
+ file: file,
369
+ line: line)
370
+ }
371
+
372
+ let actualJSONData = try XCTUnwrap ( actual. data ( using: . utf8) , file: file, line: line)
373
+ let actualDumpJSON = try XCTUnwrap ( JSONDecoder ( ) . decode ( type, from: actualJSONData) , file: file, line: line)
374
+
375
+ let expectedJSONData = try XCTUnwrap ( expected. data ( using: . utf8) , file: file, line: line)
376
+ let expectedDumpJSON = try XCTUnwrap ( JSONDecoder ( ) . decode ( type, from: expectedJSONData) , file: file, line: line)
377
+ XCTAssertEqual ( actualDumpJSON, expectedDumpJSON)
378
+ }
379
+ }
380
+
381
+
382
+ // MARK: - Snapshot testing
383
+ extension XCTest {
384
+ @discardableResult
385
+ public func assertSnapshot(
386
+ actual: String ,
387
+ extension: String ,
388
+ record: Bool = false ,
389
+ test: StaticString = #function,
390
+ file: StaticString = #filePath,
391
+ line: UInt = #line
392
+ ) throws -> String {
393
+ let snapshotDirectoryURL = URL ( fileURLWithPath: " \( file) " )
394
+ . deletingLastPathComponent ( )
395
+ . appendingPathComponent ( " Snapshots " )
396
+ let snapshotFileURL = snapshotDirectoryURL
397
+ . appendingPathComponent ( " \( test) . \( `extension`) " )
398
+
399
+ if record || !FileManager. default. fileExists ( atPath: snapshotFileURL. path) {
400
+ let recordedValue = actual + " \n "
401
+ try FileManager . default. createDirectory (
402
+ at: snapshotDirectoryURL,
403
+ withIntermediateDirectories: true ,
404
+ attributes: nil )
405
+ try recordedValue. write ( to: snapshotFileURL, atomically: true , encoding: . utf8)
406
+ XCTFail ( " Recorded new baseline " , file: file, line: line)
407
+ struct EarlyExit : Error { }
408
+ throw EarlyExit ( )
409
+ } else {
410
+ let expected = try String ( contentsOf: snapshotFileURL, encoding: . utf8)
411
+ AssertEqualStrings (
412
+ actual: actual,
413
+ expected: expected. trimmingCharacters ( in: . newlines) ,
414
+ file: file,
415
+ line: line)
416
+ return expected
417
+ }
418
+ }
419
+
420
+ public func assertGenerateManual(
364
421
multiPage: Bool ,
365
422
command: String ,
366
- expected: URL ,
367
423
record: Bool = false ,
424
+ test: StaticString = #function,
368
425
file: StaticString = #filePath,
369
426
line: UInt = #line
370
427
) throws {
@@ -390,24 +447,19 @@ extension XCTest {
390
447
file: file,
391
448
line: line)
392
449
393
- if record || !FileManager. default. fileExists ( atPath: expected. path) {
394
- let recordedValue = actual + " \n "
395
- try recordedValue. write ( to: expected, atomically: true , encoding: . utf8)
396
- XCTFail ( " Recorded new baseline " , file: file, line: line)
397
- } else {
398
- let expected = try String ( contentsOf: expected, encoding: . utf8)
399
- AssertEqualStrings (
400
- actual: actual,
401
- expected: expected. trimmingCharacters ( in: . whitespacesAndNewlines) ,
402
- file: file,
403
- line: line)
404
- }
450
+ try self . assertSnapshot (
451
+ actual: actual,
452
+ extension: " mdoc " ,
453
+ record: record,
454
+ test: test,
455
+ file: file,
456
+ line: line)
405
457
}
406
458
407
- public func AssertGenerateDoccReference (
459
+ public func assertGenerateDoccReference (
408
460
command: String ,
409
- expected: URL ,
410
461
record: Bool = false ,
462
+ test: StaticString = #function,
411
463
file: StaticString = #filePath,
412
464
line: UInt = #line
413
465
) throws {
@@ -424,58 +476,55 @@ extension XCTest {
424
476
command: command,
425
477
file: file,
426
478
line: line)
427
- if record || !FileManager. default. fileExists ( atPath: expected. path) {
428
- let recordedValue = actual + " \n "
429
- try recordedValue. write ( to: expected, atomically: true , encoding: . utf8)
430
- XCTFail ( " Recorded new baseline " , file: file, line: line)
431
- } else {
432
- let expected = try String ( contentsOf: expected, encoding: . utf8)
433
- AssertEqualStrings (
434
- actual: actual,
435
- expected: expected. trimmingCharacters ( in: . whitespacesAndNewlines) ,
436
- file: file,
437
- line: line)
438
- }
479
+
480
+ try self . assertSnapshot (
481
+ actual: actual,
482
+ extension: " md " ,
483
+ record: record,
484
+ test: test,
485
+ file: file,
486
+ line: line)
439
487
}
440
488
441
- public func AssertDump < T: ParsableArguments > (
489
+ public func assertDumpHelp < T: ParsableArguments > (
442
490
type: T . Type ,
443
- expected: URL ,
444
491
record: Bool = false ,
492
+ test: StaticString = #function,
445
493
file: StaticString = #filePath,
446
494
line: UInt = #line
447
495
) throws {
448
- let cliOutput : String
496
+ let actual : String
449
497
do {
450
498
_ = try T . parse ( [ " --experimental-dump-help " ] )
451
499
XCTFail ( file: file, line: line)
452
500
return
453
501
} catch {
454
- cliOutput = T . fullMessage ( for: error)
502
+ actual = T . fullMessage ( for: error)
455
503
}
456
504
457
505
let apiOutput = T . _dumpHelp ( )
458
- AssertEqualStrings ( actual: cliOutput , expected: apiOutput)
506
+ AssertEqualStrings ( actual: actual , expected: apiOutput)
459
507
460
- if record || !FileManager. default. fileExists ( atPath: expected. path) {
461
- let recordedValue = apiOutput + " \n "
462
- try recordedValue. write ( to: expected, atomically: true , encoding: . utf8)
463
- XCTFail ( " Recorded new baseline " , file: file, line: line)
464
- } else {
465
- let expected = try String ( contentsOf: expected, encoding: . utf8)
466
- try AssertJSONEqualFromString (
467
- actual: apiOutput,
468
- expected: expected,
469
- for: ToolInfoV0 . self,
470
- file: file,
471
- line: line)
472
- }
508
+ let expected = try self . assertSnapshot (
509
+ actual: actual,
510
+ extension: " json " ,
511
+ record: record,
512
+ test: test,
513
+ file: file,
514
+ line: line)
515
+
516
+ try AssertJSONEqualFromString (
517
+ actual: actual,
518
+ expected: expected,
519
+ for: ToolInfoV0 . self,
520
+ file: file,
521
+ line: line)
473
522
}
474
523
475
- public func AssertDump (
524
+ public func assertDumpHelp (
476
525
command: String ,
477
- expected: URL ,
478
526
record: Bool = false ,
527
+ test: StaticString = #function,
479
528
file: StaticString = #filePath,
480
529
line: UInt = #line
481
530
) throws {
@@ -484,35 +533,12 @@ extension XCTest {
484
533
expected: nil ,
485
534
file: file,
486
535
line: line)
487
- if record || !FileManager. default. fileExists ( atPath: expected. path) {
488
- let recordedValue = actual + " \n "
489
- try recordedValue. write ( to: expected, atomically: true , encoding: . utf8)
490
- XCTFail ( " Recorded new baseline " , file: file, line: line)
491
- } else {
492
- let expected = try String ( contentsOf: expected, encoding: . utf8)
493
- try AssertJSONEqualFromString (
494
- actual: actual,
495
- expected: expected,
496
- for: ToolInfoV0 . self,
497
- file: file,
498
- line: line)
499
- }
500
- }
501
-
502
- public func AssertJSONEqualFromString< T: Codable & Equatable > ( actual: String , expected: String , for type: T . Type , file: StaticString = #filePath, line: UInt = #line) throws {
503
- if #available( macOS 10 . 13 , iOS 11 . 0 , tvOS 11 . 0 , watchOS 4 . 0 , * ) {
504
- AssertEqualStrings (
505
- actual: actual. trimmingCharacters ( in: . whitespacesAndNewlines) ,
506
- expected: expected. trimmingCharacters ( in: . whitespacesAndNewlines) ,
507
- file: file,
508
- line: line)
509
- }
510
-
511
- let actualJSONData = try XCTUnwrap ( actual. data ( using: . utf8) , file: file, line: line)
512
- let actualDumpJSON = try XCTUnwrap ( JSONDecoder ( ) . decode ( type, from: actualJSONData) , file: file, line: line)
513
-
514
- let expectedJSONData = try XCTUnwrap ( expected. data ( using: . utf8) , file: file, line: line)
515
- let expectedDumpJSON = try XCTUnwrap ( JSONDecoder ( ) . decode ( type, from: expectedJSONData) , file: file, line: line)
516
- XCTAssertEqual ( actualDumpJSON, expectedDumpJSON)
536
+ try self . assertSnapshot (
537
+ actual: actual,
538
+ extension: " json " ,
539
+ record: record,
540
+ test: test,
541
+ file: file,
542
+ line: line)
517
543
}
518
544
}
0 commit comments