Skip to content

Commit e408bf9

Browse files
authored
Fix false positive when asserting old image with new empty image (#453)
1 parent 347fb40 commit e408bf9

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

Sources/SnapshotTesting/AssertSnapshot.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,10 @@ public func verifySnapshot<Value, Format>(
265265
let reference = snapshotting.diffing.fromData(data)
266266

267267
#if os(iOS) || os(tvOS)
268-
// If the image generation fails for the diffable part use the reference
269-
if let localDiff = diffable as? UIImage, localDiff.size == .zero {
268+
// If the image generation fails for the diffable part and the reference was empty, use the reference
269+
if let localDiff = diffable as? UIImage,
270+
let refImage = reference as? UIImage,
271+
localDiff.size == .zero && refImage.size == .zero {
270272
diffable = reference
271273
}
272274
#endif

Sources/SnapshotTesting/Snapshotting/UIImage.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ extension Diffing where Value == UIImage {
2929
let difference = SnapshotTesting.diff(old, new)
3030
let oldAttachment = XCTAttachment(image: old)
3131
oldAttachment.name = "reference"
32-
let newAttachment = XCTAttachment(image: new)
32+
let isEmptyImage = new.size == .zero
33+
let newAttachment = XCTAttachment(image: isEmptyImage ? emptyImage() : new)
3334
newAttachment.name = "failure"
3435
let differenceAttachment = XCTAttachment(image: difference)
3536
differenceAttachment.name = "difference"

Tests/SnapshotTestingTests/SnapshotTestingTests.swift

+11
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,17 @@ final class SnapshotTestingTests: XCTestCase {
10641064
#endif
10651065
}
10661066

1067+
func testViewAgainstEmptyImage() {
1068+
#if os(iOS) || os(tvOS)
1069+
let rect = CGRect(x: 0, y: 0, width: 0, height: 0)
1070+
let view = UIView(frame: rect)
1071+
view.backgroundColor = .blue
1072+
1073+
let failure = verifySnapshot(matching: view, as: .image, named: "notEmptyImage")
1074+
XCTAssertNotNil(failure)
1075+
#endif
1076+
}
1077+
10671078
func testEmbeddedWebView() throws {
10681079
#if os(iOS)
10691080
let label = UILabel()

0 commit comments

Comments
 (0)