From a1e6b32299a81d7bddffa38d309f80df8d526e0a Mon Sep 17 00:00:00 2001 From: Marcelo Fabri Date: Mon, 14 Mar 2022 17:50:30 -0700 Subject: [PATCH] Add removesErrorSuffix to make it easier to use with a test that throws --- .../FBSnapshotTestCaseSwiftTests.swift | 2 +- ...apshotTestCaseCarthageDemoSwiftTests.swift | 2 +- ...napshotTestCaseSwiftPMDemoSwiftTests.swift | 2 +- .../FBSnapshotTestCase.m | 19 +++++++++++++++++-- .../Public/FBSnapshotTestCase.h | 5 +++++ 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/demos/FBSnapshotTestCaseDemo/FBSnapshotTestCaseDemoTests/FBSnapshotTestCaseSwiftTests.swift b/demos/FBSnapshotTestCaseDemo/FBSnapshotTestCaseDemoTests/FBSnapshotTestCaseSwiftTests.swift index fa283f8..fd35dba 100644 --- a/demos/FBSnapshotTestCaseDemo/FBSnapshotTestCaseDemoTests/FBSnapshotTestCaseSwiftTests.swift +++ b/demos/FBSnapshotTestCaseDemo/FBSnapshotTestCaseDemoTests/FBSnapshotTestCaseSwiftTests.swift @@ -15,7 +15,7 @@ class FBSnapshotTestCaseSwiftTest: FBSnapshotTestCase { recordMode = false } - func testExample() { + func testExample() throws { let view = UIView(frame: CGRect(x: 0, y: 0, width: 64, height: 64)) view.backgroundColor = UIColor.blue FBSnapshotVerifyView(view) diff --git a/demos/iOSSnapshotTestCaseCarthageDemo/iOSSnapshotTestCaseCarthageDemoSwiftTests/iOSSnapshotTestCaseCarthageDemoSwiftTests.swift b/demos/iOSSnapshotTestCaseCarthageDemo/iOSSnapshotTestCaseCarthageDemoSwiftTests/iOSSnapshotTestCaseCarthageDemoSwiftTests.swift index 0e84213..8baf1ec 100644 --- a/demos/iOSSnapshotTestCaseCarthageDemo/iOSSnapshotTestCaseCarthageDemoSwiftTests/iOSSnapshotTestCaseCarthageDemoSwiftTests.swift +++ b/demos/iOSSnapshotTestCaseCarthageDemo/iOSSnapshotTestCaseCarthageDemoSwiftTests/iOSSnapshotTestCaseCarthageDemoSwiftTests.swift @@ -16,7 +16,7 @@ class iOSSnapshotTestCaseCarthageDemoSwiftTests: FBSnapshotTestCase { recordMode = false } - func testExample() { + func testExample() throws { let view = UIView(frame: CGRect(x: 0, y: 0, width: 64, height: 64)) view.backgroundColor = UIColor.blue FBSnapshotVerifyView(view) diff --git a/demos/iOSSnapshotTestCaseSwiftPMDemo/Tests iOS/iOSSnapshotTestCaseSwiftPMDemoSwiftTests.swift b/demos/iOSSnapshotTestCaseSwiftPMDemo/Tests iOS/iOSSnapshotTestCaseSwiftPMDemoSwiftTests.swift index d1039a7..febd6f1 100644 --- a/demos/iOSSnapshotTestCaseSwiftPMDemo/Tests iOS/iOSSnapshotTestCaseSwiftPMDemoSwiftTests.swift +++ b/demos/iOSSnapshotTestCaseSwiftPMDemo/Tests iOS/iOSSnapshotTestCaseSwiftPMDemoSwiftTests.swift @@ -16,7 +16,7 @@ class iOSSnapshotTestCaseSwiftPMDemoSwiftTests: FBSnapshotTestCase { recordMode = false } - func testExample() { + func testExample() throws { let view = UIView(frame: CGRect(x: 0, y: 0, width: 64, height: 64)) view.backgroundColor = UIColor.blue FBSnapshotVerifyView(view) diff --git a/src/iOSSnapshotTestCaseCore/FBSnapshotTestCase.m b/src/iOSSnapshotTestCaseCore/FBSnapshotTestCase.m index 6e65372..f9e9dc1 100644 --- a/src/iOSSnapshotTestCaseCore/FBSnapshotTestCase.m +++ b/src/iOSSnapshotTestCaseCore/FBSnapshotTestCase.m @@ -20,6 +20,7 @@ - (void)setUp { [super setUp]; _snapshotController = [[FBSnapshotTestController alloc] initWithTestClass:[self class]]; + _removesErrorSuffix = YES; } - (void)tearDown @@ -236,7 +237,7 @@ - (BOOL)referenceImageRecordedInDirectory:(NSString *)referenceImagesDirectory { NSAssert1(_snapshotController, @"%s cannot be called before [super setUp]", __FUNCTION__); _snapshotController.referenceImagesDirectory = referenceImagesDirectory; - UIImage *referenceImage = [_snapshotController referenceImageForSelector:self.invocation.selector + UIImage *referenceImage = [_snapshotController referenceImageForSelector:[self adjustedCurrentTestSelector] identifier:identifier error:errorPtr]; @@ -286,11 +287,25 @@ - (BOOL)_compareSnapshotOfViewOrLayer:(id)viewOrLayer _snapshotController.referenceImagesDirectory = referenceImagesDirectory; _snapshotController.imageDiffDirectory = imageDiffDirectory; return [_snapshotController compareSnapshotOfViewOrLayer:viewOrLayer - selector:self.invocation.selector + selector:[self adjustedCurrentTestSelector] identifier:identifier perPixelTolerance:perPixelTolerance overallTolerance:overallTolerance error:errorPtr]; } +- (SEL)adjustedCurrentTestSelector { + SEL selector = self.invocation.selector; + if (!self.removesErrorSuffix || self.invocation.methodSignature.numberOfArguments != 3) { + return selector; + } + + NSString *name = NSStringFromSelector(selector); + if ([name hasSuffix:@"AndReturnError:"]) { + return NSSelectorFromString([name stringByReplacingOccurrencesOfString:@"AndReturnError:" withString:@""]); + } + + return selector; + } + @end diff --git a/src/iOSSnapshotTestCaseCore/Public/FBSnapshotTestCase.h b/src/iOSSnapshotTestCaseCore/Public/FBSnapshotTestCase.h index 13c26d2..b894878 100644 --- a/src/iOSSnapshotTestCaseCore/Public/FBSnapshotTestCase.h +++ b/src/iOSSnapshotTestCaseCore/Public/FBSnapshotTestCase.h @@ -174,6 +174,11 @@ NS_ASSUME_NONNULL_BEGIN */ @property (readwrite, nonatomic, assign) BOOL usesDrawViewHierarchyInRect; +/// Whether to remove the "AndReturnError" suffix used when a test `throws` in Swift. +/// This allows to change a test between throwing and not throwing without recording snapshots again. +/// The default is YES. +@property (readwrite, nonatomic, assign) BOOL removesErrorSuffix; + - (void)setUp NS_REQUIRES_SUPER; - (void)tearDown NS_REQUIRES_SUPER;