Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add removesErrorSuffix to make it easier to use with a test that throws #175

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 17 additions & 2 deletions src/iOSSnapshotTestCaseCore/FBSnapshotTestCase.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ - (void)setUp
{
[super setUp];
_snapshotController = [[FBSnapshotTestController alloc] initWithTestClass:[self class]];
_removesErrorSuffix = YES;
}

- (void)tearDown
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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
5 changes: 5 additions & 0 deletions src/iOSSnapshotTestCaseCore/Public/FBSnapshotTestCase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down