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

Fix false positive when asserting reference image with empty image #453

Merged
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
6 changes: 4 additions & 2 deletions Sources/SnapshotTesting/AssertSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,10 @@ public func verifySnapshot<Value, Format>(
let reference = snapshotting.diffing.fromData(data)

#if os(iOS) || os(tvOS)
// If the image generation fails for the diffable part use the reference
if let localDiff = diffable as? UIImage, localDiff.size == .zero {
// If the image generation fails for the diffable part and the reference was empty, use the reference
if let localDiff = diffable as? UIImage,
let refImage = reference as? UIImage,
localDiff.size == .zero && refImage.size == .zero {
diffable = reference
}
#endif
Expand Down
3 changes: 2 additions & 1 deletion Sources/SnapshotTesting/Snapshotting/UIImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ extension Diffing where Value == UIImage {
: "Newly-taken snapshot@\(new.size) does not match reference@\(old.size)."
let oldAttachment = XCTAttachment(image: old)
oldAttachment.name = "reference"
let newAttachment = XCTAttachment(image: new)
let isEmptyImage = new.size == .zero
let newAttachment = XCTAttachment(image: isEmptyImage ? emptyImage() : new)
newAttachment.name = "failure"
let differenceAttachment = XCTAttachment(image: difference)
differenceAttachment.name = "difference"
Expand Down
11 changes: 11 additions & 0 deletions Tests/SnapshotTestingTests/SnapshotTestingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,17 @@ final class SnapshotTestingTests: XCTestCase {
#endif
}

func testViewAgainstEmptyImage() {
#if os(iOS) || os(tvOS)
let rect = CGRect(x: 0, y: 0, width: 0, height: 0)
let view = UIView(frame: rect)
view.backgroundColor = .blue

let failure = verifySnapshot(matching: view, as: .image, named: "notEmptyImage")
XCTAssertNotNil(failure)
#endif
}

func testEmbeddedWebView() throws {
#if os(iOS)
let label = UILabel()
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.