-
Notifications
You must be signed in to change notification settings - Fork 220
Port Xcode 9 XCTAttachment API integration from FB repo #19
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
Changes from 2 commits
4ea21c4
25feb78
951bb0d
d45a6ff
b85e09e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
|
|
||
| #import <FBSnapshotTestCase/FBSnapshotTestCase.h> | ||
| #import <FBSnapshotTestCase/FBSnapshotTestController.h> | ||
| #import <Availability.h> | ||
|
|
||
| @implementation FBSnapshotTestCase | ||
| { | ||
|
|
@@ -122,17 +123,52 @@ - (NSString *)snapshotVerifyViewOrLayer:(id)viewOrLayer | |
| } | ||
| } | ||
| } | ||
|
|
||
| if (!testSuccess) { | ||
| return [NSString stringWithFormat:@"Snapshot comparison failed: %@", errors.firstObject]; | ||
| } | ||
|
|
||
| [self addAttachmentsWithErrors:errors identifier:identifier]; | ||
|
|
||
| if (self.recordMode) { | ||
| return @"Test ran in record mode. Reference image is now saved. Disable record mode to perform an actual snapshot comparison!"; | ||
| if (errors.count > 0) { | ||
| return [NSString stringWithFormat:@"Snapshot comparison failed: %@", errors.firstObject]; | ||
| } else { | ||
| return @"Test ran in record mode. Reference image is now saved. Disable record mode to perform an actual snapshot comparison!"; | ||
| } | ||
| } else if (!testSuccess) { | ||
| return [NSString stringWithFormat:@"Snapshot comparison failed: %@", errors.firstObject]; | ||
| } | ||
|
|
||
| return nil; | ||
| } | ||
|
|
||
| - (void) addAttachmentsWithErrors:(NSArray<NSError *> *)errors identifier:(NSString *)identifier { | ||
|
||
| #if defined(__IPHONE_11_0) || defined(__TVOS_11_0) | ||
| if (self.recordMode) { | ||
|
||
| UIImage* image = [_snapshotController referenceImageForSelector:self.invocation.selector identifier:identifier error:nil]; | ||
|
||
| if (image) { | ||
| XCTAttachment *attachement = [XCTAttachment attachmentWithImage:image]; | ||
|
||
| attachement.name = @"Reference Image"; | ||
| [self addAttachment:attachement]; | ||
| } | ||
| } else if (errors.firstObject != nil) { | ||
| NSError *error = errors.firstObject; | ||
| if (error.userInfo[FBReferenceImageKey] != nil) { | ||
| XCTAttachment *attachement = [XCTAttachment attachmentWithImage:error.userInfo[FBReferenceImageKey]]; | ||
|
||
| attachement.name = @"Reference Image"; | ||
| [self addAttachment:attachement]; | ||
| } | ||
| if (error.userInfo[FBCapturedImageKey] != nil) { | ||
| XCTAttachment *attachement = [XCTAttachment attachmentWithImage:error.userInfo[FBCapturedImageKey]]; | ||
|
||
| attachement.name = @"Captured Image"; | ||
| [self addAttachment:attachement]; | ||
| } | ||
| if (error.userInfo[FBDiffedImageKey] != nil) { | ||
| XCTAttachment *attachement = [XCTAttachment attachmentWithImage:error.userInfo[FBDiffedImageKey]]; | ||
|
||
| attachement.name = @"Diffed Image"; | ||
| [self addAttachment:attachement]; | ||
| } | ||
| } | ||
| #endif | ||
| } | ||
|
|
||
| - (BOOL)compareSnapshotOfLayer:(CALayer *)layer | ||
| referenceImagesDirectory:(NSString *)referenceImagesDirectory | ||
| identifier:(NSString *)identifier | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,58 +11,15 @@ | |
| #if swift(>=3) | ||
| public extension FBSnapshotTestCase { | ||
| public func FBSnapshotVerifyView(_ view: UIView, identifier: String = "", suffixes: NSOrderedSet = FBSnapshotTestCaseDefaultSuffixes(), tolerance: CGFloat = 0, file: StaticString = #file, line: UInt = #line) { | ||
| FBSnapshotVerifyViewOrLayer(view, identifier: identifier, suffixes: suffixes, tolerance: tolerance, file: file, line: line) | ||
| if let errorDescription = snapshotVerifyViewOrLayer(view, identifier: identifier, suffixes: suffixes, tolerance: tolerance, defaultReferenceDirectory: FB_REFERENCE_IMAGE_DIR) { | ||
| XCTFail(errorDescription, file: file, line: line) | ||
| } | ||
| } | ||
|
|
||
| public func FBSnapshotVerifyLayer(_ layer: CALayer, identifier: String = "", suffixes: NSOrderedSet = FBSnapshotTestCaseDefaultSuffixes(), tolerance: CGFloat = 0, file: StaticString = #file, line: UInt = #line) { | ||
| FBSnapshotVerifyViewOrLayer(layer, identifier: identifier, suffixes: suffixes, tolerance: tolerance, file: file, line: line) | ||
| } | ||
|
|
||
| private func FBSnapshotVerifyViewOrLayer(_ viewOrLayer: AnyObject, identifier: String = "", suffixes: NSOrderedSet = FBSnapshotTestCaseDefaultSuffixes(), tolerance: CGFloat = 0, file: StaticString = #file, line: UInt = #line) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe there's a good reason why |
||
| let envReferenceImageDirectory = self.getReferenceImageDirectory(withDefault: FB_REFERENCE_IMAGE_DIR) | ||
| var error: NSError? | ||
| var comparisonSuccess = false | ||
|
|
||
| if let envReferenceImageDirectory = envReferenceImageDirectory { | ||
| for suffix in suffixes { | ||
| let referenceImagesDirectory = "\(envReferenceImageDirectory)\(suffix)" | ||
| if viewOrLayer.isKind(of: UIView.self) { | ||
| do { | ||
| try compareSnapshot(of: viewOrLayer as! UIView, referenceImagesDirectory: referenceImagesDirectory, identifier: identifier, tolerance: tolerance) | ||
| comparisonSuccess = true | ||
| } catch let error1 as NSError { | ||
| error = error1 | ||
| comparisonSuccess = false | ||
| } | ||
| } else if viewOrLayer.isKind(of: CALayer.self) { | ||
| do { | ||
| try compareSnapshot(of: viewOrLayer as! CALayer, referenceImagesDirectory: referenceImagesDirectory, identifier: identifier, tolerance: tolerance) | ||
| comparisonSuccess = true | ||
| } catch let error1 as NSError { | ||
| error = error1 | ||
| comparisonSuccess = false | ||
| } | ||
| } else { | ||
| assertionFailure("Only UIView and CALayer classes can be snapshotted") | ||
| } | ||
|
|
||
| assert(recordMode == false, message: "Test ran in record mode. Reference image is now saved. Disable record mode to perform an actual snapshot comparison!", file: file, line: line) | ||
|
|
||
| if comparisonSuccess || recordMode { | ||
| break | ||
| } | ||
|
|
||
| assert(comparisonSuccess, message: "Snapshot comparison failed: \(String(describing: error))", file: file, line: line) | ||
| if let errorDescription = snapshotVerifyViewOrLayer(layer, identifier: identifier, suffixes: suffixes, tolerance: tolerance, defaultReferenceDirectory: FB_REFERENCE_IMAGE_DIR) { | ||
| XCTFail(errorDescription, file: file, line: line) | ||
| } | ||
| } else { | ||
| XCTFail("Missing value for referenceImagesDirectory - Set FB_REFERENCE_IMAGE_DIR as Environment variable in your scheme.") | ||
| } | ||
| } | ||
|
|
||
| func assert(_ assertion: Bool, message: String, file: StaticString, line: UInt) { | ||
| if !assertion { | ||
| XCTFail(message, file: file, line: line) | ||
| } | ||
| } | ||
| } | ||
| #else | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this possible? Looking at L102-L105, it is doing this:
When
self.recordModeis on,compareSnapshotOfViewOrLayer:...the BOOL from the function relates to whether or not the reference image was saved.