Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Compare and record new at the same time #144

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions FBSnapshotTestCase/FBSnapshotTestCase.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
*/
@interface FBSnapshotTestCase : XCTestCase

/**
Test mode. Can be record, compare or both combined.
*/
@property (readwrite, nonatomic, assign) FBSnapshotTestMode mode;

/**
When YES, the test macros will save reference images, rather than performing an actual test.
*/
Expand Down
12 changes: 10 additions & 2 deletions FBSnapshotTestCase/FBSnapshotTestCase.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@ - (void)tearDown

- (BOOL)recordMode
{
return _snapshotController.recordMode;
return _snapshotController.mode & FBSnapshotModeRecord;
}

- (void)setRecordMode:(BOOL)recordMode
{
_snapshotController.mode = recordMode ? FBSnapshotModeRecord : FBSnapshotModeCompare;
}

- (FBSnapshotTestMode)mode{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new line before {

return _snapshotController.mode;
}

- (void)setMode:(FBSnapshotTestMode)mode{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new line before {

NSAssert1(_snapshotController, @"%s cannot be called before [super setUp]", __FUNCTION__);
_snapshotController.recordMode = recordMode;
_snapshotController.mode = mode;
}

- (BOOL)isDeviceAgnostic
Expand Down
11 changes: 9 additions & 2 deletions FBSnapshotTestCase/FBSnapshotTestController.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ typedef NS_ENUM(NSInteger, FBSnapshotTestControllerErrorCode) {
FBSnapshotTestControllerErrorCodeImagesDifferentSizes,
FBSnapshotTestControllerErrorCodeImagesDifferent,
};

typedef NS_OPTIONS(NSUInteger, FBSnapshotTestMode){
FBSnapshotModeNone = 0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need FBSnapshotModeNone ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. Can be removed

FBSnapshotModeCompare = 1 << 0,
FBSnapshotModeRecord = 1 << 1
};

/**
Errors returned by the methods of FBSnapshotTestController use this domain.
*/
Expand All @@ -36,9 +43,9 @@ extern NSString *const FBReferenceImageFilePathKey;
@interface FBSnapshotTestController : NSObject

/**
Record snapshots.
Record, Compare or both
*/
@property (readwrite, nonatomic, assign) BOOL recordMode;
@property (readwrite, nonatomic, assign) FBSnapshotTestMode mode;

/**
When @c YES appends the name of the device model and OS to the snapshot file name.
Expand Down
12 changes: 8 additions & 4 deletions FBSnapshotTestCase/FBSnapshotTestController.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ - (instancetype)initWithTestName:(NSString *)testName
if (self = [super init]) {
_testName = [testName copy];
_deviceAgnostic = NO;
_mode = FBSnapshotModeCompare;

_fileManager = [[NSFileManager alloc] init];
}
Expand Down Expand Up @@ -89,11 +90,14 @@ - (BOOL)compareSnapshotOfViewOrLayer:(id)viewOrLayer
tolerance:(CGFloat)tolerance
error:(NSError **)errorPtr
{
if (self.recordMode) {
return [self _recordSnapshotOfViewOrLayer:viewOrLayer selector:selector identifier:identifier error:errorPtr];
} else {
return [self _performPixelComparisonWithViewOrLayer:viewOrLayer selector:selector identifier:identifier tolerance:tolerance error:errorPtr];
BOOL result = NO;
if (self.mode & FBSnapshotModeCompare) {
result = [self _performPixelComparisonWithViewOrLayer:viewOrLayer selector:selector identifier:identifier tolerance:tolerance error:errorPtr];
}
if (self.mode & FBSnapshotModeRecord) {
result = [self _recordSnapshotOfViewOrLayer:viewOrLayer selector:selector identifier:identifier error:errorPtr];
}
return result;
}

- (UIImage *)referenceImageForSelector:(SEL)selector
Expand Down