From 4be93693825c22669068826181a96b770c103ac7 Mon Sep 17 00:00:00 2001 From: thanasak-s Date: Tue, 8 Jun 2021 20:25:05 +0700 Subject: [PATCH 1/3] feat: compare ignore screen scale --- Nimble_Snapshots/HaveValidSnapshot.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Nimble_Snapshots/HaveValidSnapshot.swift b/Nimble_Snapshots/HaveValidSnapshot.swift index b8d94df..f2f9ab4 100644 --- a/Nimble_Snapshots/HaveValidSnapshot.swift +++ b/Nimble_Snapshots/HaveValidSnapshot.swift @@ -45,13 +45,16 @@ public class FBSnapshotTest: NSObject { tolerance: CGFloat, perPixelTolerance: CGFloat, filename: String, - identifier: String? = nil) -> Bool { + identifier: String? = nil, + isIgnoreScale: Bool = false) -> Bool { let testName = parseFilename(filename: filename) let snapshotController: FBSnapshotTestController = FBSnapshotTestController(test: self) snapshotController.folderName = testName if isDeviceAgnostic { snapshotController.fileNameOptions = [.device, .OS, .screenSize, .screenScale] + } else if isIgnoreScale { + snapshotController.fileNameOptions = .none } else { snapshotController.fileNameOptions = .screenScale } From 85624c82390cc47f923851df3df3d080f5605c3c Mon Sep 17 00:00:00 2001 From: thanasak-s Date: Tue, 8 Jun 2021 21:58:59 +0700 Subject: [PATCH 2/3] feat: compare and default isIgnoreScale --- .../DynamicSize/DynamicSizeSnapshot.swift | 18 +++++--- Nimble_Snapshots/HaveValidSnapshot.swift | 41 ++++++++++++------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/Nimble_Snapshots/DynamicSize/DynamicSizeSnapshot.swift b/Nimble_Snapshots/DynamicSize/DynamicSizeSnapshot.swift index 7a4fb9d..afa9c34 100644 --- a/Nimble_Snapshots/DynamicSize/DynamicSizeSnapshot.swift +++ b/Nimble_Snapshots/DynamicSize/DynamicSizeSnapshot.swift @@ -152,7 +152,8 @@ public func haveValidDynamicSizeSnapshot(named name: String? = nil, usesDrawRect: Bool = false, pixelTolerance: CGFloat? = nil, tolerance: CGFloat? = nil, - resizeMode: ResizeMode = .frame) -> Predicate { + resizeMode: ResizeMode = .frame, + isIgnoreScale: Bool = false) -> Predicate { return Predicate { actualExpression in return performDynamicSizeSnapshotTest(name, identifier: identifier, @@ -163,7 +164,8 @@ public func haveValidDynamicSizeSnapshot(named name: String? = nil, tolerance: tolerance, pixelTolerance: pixelTolerance, isRecord: false, - resizeMode: resizeMode) + resizeMode: resizeMode, + isIgnoreScale: isIgnoreScale) } } @@ -176,7 +178,8 @@ func performDynamicSizeSnapshotTest(_ name: String?, tolerance: CGFloat? = nil, pixelTolerance: CGFloat? = nil, isRecord: Bool, - resizeMode: ResizeMode) -> PredicateResult { + resizeMode: ResizeMode, + isIgnoreScale: Bool = false) -> PredicateResult { // swiftlint:disable:next force_try force_unwrapping let instance = try! actualExpression.evaluate()! let testFileLocation = actualExpression.location.file @@ -204,7 +207,8 @@ func performDynamicSizeSnapshotTest(_ name: String?, snapshot: finalSnapshotName, record: isRecord, referenceDirectory: referenceImageDirectory, tolerance: tolerance, perPixelTolerance: pixelTolerance, - filename: actualExpression.location.file, identifier: nil) + filename: actualExpression.location.file, identifier: nil, + isIgnoreScale: isIgnoreScale) } if isRecord { @@ -243,7 +247,8 @@ public func recordDynamicSizeSnapshot(named name: String? = nil, sizes: [String: CGSize], isDeviceAgnostic: Bool = false, usesDrawRect: Bool = false, - resizeMode: ResizeMode = .frame) -> Predicate { + resizeMode: ResizeMode = .frame, + isIgnoreScale: Bool = false) -> Predicate { return Predicate { actualExpression in return performDynamicSizeSnapshotTest(name, identifier: identifier, @@ -252,7 +257,8 @@ public func recordDynamicSizeSnapshot(named name: String? = nil, usesDrawRect: usesDrawRect, actualExpression: actualExpression, isRecord: true, - resizeMode: resizeMode) + resizeMode: resizeMode, + isIgnoreScale: isIgnoreScale) } } diff --git a/Nimble_Snapshots/HaveValidSnapshot.swift b/Nimble_Snapshots/HaveValidSnapshot.swift index f2f9ab4..ba6f96a 100644 --- a/Nimble_Snapshots/HaveValidSnapshot.swift +++ b/Nimble_Snapshots/HaveValidSnapshot.swift @@ -202,7 +202,8 @@ private func performSnapshotTest(_ name: String?, usesDrawRect: Bool = false, actualExpression: Expression, pixelTolerance: CGFloat? = nil, - tolerance: CGFloat?) -> PredicateResult { + tolerance: CGFloat?, + isIgnoreScale: Bool) -> PredicateResult { // swiftlint:disable:next force_try force_unwrapping let instance = try! actualExpression.evaluate()! let testFileLocation = actualExpression.location.file @@ -215,7 +216,7 @@ private func performSnapshotTest(_ name: String?, usesDrawRect: usesDrawRect, snapshot: snapshotName, record: false, referenceDirectory: referenceImageDirectory, tolerance: tolerance, perPixelTolerance: pixelTolerance, - filename: actualExpression.location.file, identifier: identifier) + filename: actualExpression.location.file, identifier: identifier, isIgnoreScale: isIgnoreScale) return PredicateResult(status: PredicateStatus(bool: result), message: .fail("expected a matching snapshot in \(snapshotName)")) @@ -225,7 +226,8 @@ private func recordSnapshot(_ name: String?, identifier: String? = nil, isDeviceAgnostic: Bool = false, usesDrawRect: Bool = false, - actualExpression: Expression) -> PredicateResult { + actualExpression: Expression, + isIgnoreScale: Bool) -> PredicateResult { // swiftlint:disable:next force_try force_unwrapping let instance = try! actualExpression.evaluate()! let testFileLocation = actualExpression.location.file @@ -244,7 +246,8 @@ private func recordSnapshot(_ name: String?, tolerance: tolerance, perPixelTolerance: pixelTolerance, filename: actualExpression.location.file, - identifier: identifier) { + identifier: identifier, + isIgnoreScale: isIgnoreScale) { let name = name ?? snapshotName message = "snapshot \(name) successfully recorded, replace recordSnapshot with a check" } else if let name = name { @@ -267,14 +270,16 @@ public func haveValidSnapshot(named name: String? = nil, identifier: String? = nil, usesDrawRect: Bool = false, pixelTolerance: CGFloat? = nil, - tolerance: CGFloat? = nil) -> Predicate { + tolerance: CGFloat? = nil, + isIgnoreScale: Bool = false) -> Predicate { return Predicate { actualExpression in if switchChecksWithRecords { return recordSnapshot(name, identifier: identifier, usesDrawRect: usesDrawRect, - actualExpression: actualExpression) + actualExpression: actualExpression, + isIgnoreScale: isIgnoreScale) } return performSnapshotTest(name, @@ -282,7 +287,8 @@ public func haveValidSnapshot(named name: String? = nil, usesDrawRect: usesDrawRect, actualExpression: actualExpression, pixelTolerance: pixelTolerance, - tolerance: tolerance) + tolerance: tolerance, + isIgnoreScale: isIgnoreScale) } } @@ -290,7 +296,8 @@ public func haveValidDeviceAgnosticSnapshot(named name: String? = nil, identifier: String? = nil, usesDrawRect: Bool = false, pixelTolerance: CGFloat? = nil, - tolerance: CGFloat? = nil) -> Predicate { + tolerance: CGFloat? = nil, + isIgnoreScale: Bool = false) -> Predicate { return Predicate { actualExpression in if switchChecksWithRecords { @@ -298,7 +305,8 @@ public func haveValidDeviceAgnosticSnapshot(named name: String? = nil, identifier: identifier, isDeviceAgnostic: true, usesDrawRect: usesDrawRect, - actualExpression: actualExpression) + actualExpression: actualExpression, + isIgnoreScale: isIgnoreScale) } return performSnapshotTest(name, @@ -307,26 +315,31 @@ public func haveValidDeviceAgnosticSnapshot(named name: String? = nil, usesDrawRect: usesDrawRect, actualExpression: actualExpression, pixelTolerance: pixelTolerance, - tolerance: tolerance) + tolerance: tolerance, + isIgnoreScale: isIgnoreScale) } } public func recordSnapshot(named name: String? = nil, identifier: String? = nil, - usesDrawRect: Bool = false) -> Predicate { + usesDrawRect: Bool = false, + isIgnoreScale: Bool = false) -> Predicate { return Predicate { actualExpression in return recordSnapshot(name, identifier: identifier, usesDrawRect: usesDrawRect, - actualExpression: actualExpression) + actualExpression: actualExpression, + isIgnoreScale: isIgnoreScale) } } public func recordDeviceAgnosticSnapshot(named name: String? = nil, identifier: String? = nil, - usesDrawRect: Bool = false) -> Predicate { + usesDrawRect: Bool = false, + isIgnoreScale: Bool = false) -> Predicate { return Predicate { actualExpression in return recordSnapshot(name, identifier: identifier, isDeviceAgnostic: true, usesDrawRect: usesDrawRect, - actualExpression: actualExpression) + actualExpression: actualExpression, + isIgnoreScale: isIgnoreScale) } } From 894069fbc23203419da36607195838c1764aa874 Mon Sep 17 00:00:00 2001 From: thanasak-s Date: Wed, 16 Jun 2021 21:50:32 +0700 Subject: [PATCH 3/3] refactor: According to pull request --- .../DynamicSize/DynamicSizeSnapshot.swift | 12 +++---- Nimble_Snapshots/HaveValidSnapshot.swift | 32 +++++++++---------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/Nimble_Snapshots/DynamicSize/DynamicSizeSnapshot.swift b/Nimble_Snapshots/DynamicSize/DynamicSizeSnapshot.swift index afa9c34..8afbe9c 100644 --- a/Nimble_Snapshots/DynamicSize/DynamicSizeSnapshot.swift +++ b/Nimble_Snapshots/DynamicSize/DynamicSizeSnapshot.swift @@ -153,7 +153,7 @@ public func haveValidDynamicSizeSnapshot(named name: String? = nil, pixelTolerance: CGFloat? = nil, tolerance: CGFloat? = nil, resizeMode: ResizeMode = .frame, - isIgnoreScale: Bool = false) -> Predicate { + shouldIgnoreScale: Bool = false) -> Predicate { return Predicate { actualExpression in return performDynamicSizeSnapshotTest(name, identifier: identifier, @@ -165,7 +165,7 @@ public func haveValidDynamicSizeSnapshot(named name: String? = nil, pixelTolerance: pixelTolerance, isRecord: false, resizeMode: resizeMode, - isIgnoreScale: isIgnoreScale) + shouldIgnoreScale: shouldIgnoreScale) } } @@ -179,7 +179,7 @@ func performDynamicSizeSnapshotTest(_ name: String?, pixelTolerance: CGFloat? = nil, isRecord: Bool, resizeMode: ResizeMode, - isIgnoreScale: Bool = false) -> PredicateResult { + shouldIgnoreScale: Bool = false) -> PredicateResult { // swiftlint:disable:next force_try force_unwrapping let instance = try! actualExpression.evaluate()! let testFileLocation = actualExpression.location.file @@ -208,7 +208,7 @@ func performDynamicSizeSnapshotTest(_ name: String?, referenceDirectory: referenceImageDirectory, tolerance: tolerance, perPixelTolerance: pixelTolerance, filename: actualExpression.location.file, identifier: nil, - isIgnoreScale: isIgnoreScale) + shouldIgnoreScale: shouldIgnoreScale) } if isRecord { @@ -248,7 +248,7 @@ public func recordDynamicSizeSnapshot(named name: String? = nil, isDeviceAgnostic: Bool = false, usesDrawRect: Bool = false, resizeMode: ResizeMode = .frame, - isIgnoreScale: Bool = false) -> Predicate { + shouldIgnoreScale: Bool = false) -> Predicate { return Predicate { actualExpression in return performDynamicSizeSnapshotTest(name, identifier: identifier, @@ -258,7 +258,7 @@ public func recordDynamicSizeSnapshot(named name: String? = nil, actualExpression: actualExpression, isRecord: true, resizeMode: resizeMode, - isIgnoreScale: isIgnoreScale) + shouldIgnoreScale: shouldIgnoreScale) } } diff --git a/Nimble_Snapshots/HaveValidSnapshot.swift b/Nimble_Snapshots/HaveValidSnapshot.swift index ba6f96a..0e7d32d 100644 --- a/Nimble_Snapshots/HaveValidSnapshot.swift +++ b/Nimble_Snapshots/HaveValidSnapshot.swift @@ -46,14 +46,14 @@ public class FBSnapshotTest: NSObject { perPixelTolerance: CGFloat, filename: String, identifier: String? = nil, - isIgnoreScale: Bool = false) -> Bool { + shouldIgnoreScale: Bool = false) -> Bool { let testName = parseFilename(filename: filename) let snapshotController: FBSnapshotTestController = FBSnapshotTestController(test: self) snapshotController.folderName = testName if isDeviceAgnostic { snapshotController.fileNameOptions = [.device, .OS, .screenSize, .screenScale] - } else if isIgnoreScale { + } else if shouldIgnoreScale { snapshotController.fileNameOptions = .none } else { snapshotController.fileNameOptions = .screenScale @@ -203,7 +203,7 @@ private func performSnapshotTest(_ name: String?, actualExpression: Expression, pixelTolerance: CGFloat? = nil, tolerance: CGFloat?, - isIgnoreScale: Bool) -> PredicateResult { + shouldIgnoreScale: Bool) -> PredicateResult { // swiftlint:disable:next force_try force_unwrapping let instance = try! actualExpression.evaluate()! let testFileLocation = actualExpression.location.file @@ -216,7 +216,7 @@ private func performSnapshotTest(_ name: String?, usesDrawRect: usesDrawRect, snapshot: snapshotName, record: false, referenceDirectory: referenceImageDirectory, tolerance: tolerance, perPixelTolerance: pixelTolerance, - filename: actualExpression.location.file, identifier: identifier, isIgnoreScale: isIgnoreScale) + filename: actualExpression.location.file, identifier: identifier, shouldIgnoreScale: shouldIgnoreScale) return PredicateResult(status: PredicateStatus(bool: result), message: .fail("expected a matching snapshot in \(snapshotName)")) @@ -227,7 +227,7 @@ private func recordSnapshot(_ name: String?, isDeviceAgnostic: Bool = false, usesDrawRect: Bool = false, actualExpression: Expression, - isIgnoreScale: Bool) -> PredicateResult { + shouldIgnoreScale: Bool) -> PredicateResult { // swiftlint:disable:next force_try force_unwrapping let instance = try! actualExpression.evaluate()! let testFileLocation = actualExpression.location.file @@ -247,7 +247,7 @@ private func recordSnapshot(_ name: String?, perPixelTolerance: pixelTolerance, filename: actualExpression.location.file, identifier: identifier, - isIgnoreScale: isIgnoreScale) { + shouldIgnoreScale: shouldIgnoreScale) { let name = name ?? snapshotName message = "snapshot \(name) successfully recorded, replace recordSnapshot with a check" } else if let name = name { @@ -271,7 +271,7 @@ public func haveValidSnapshot(named name: String? = nil, usesDrawRect: Bool = false, pixelTolerance: CGFloat? = nil, tolerance: CGFloat? = nil, - isIgnoreScale: Bool = false) -> Predicate { + shouldIgnoreScale: Bool = false) -> Predicate { return Predicate { actualExpression in if switchChecksWithRecords { @@ -279,7 +279,7 @@ public func haveValidSnapshot(named name: String? = nil, identifier: identifier, usesDrawRect: usesDrawRect, actualExpression: actualExpression, - isIgnoreScale: isIgnoreScale) + shouldIgnoreScale: shouldIgnoreScale) } return performSnapshotTest(name, @@ -288,7 +288,7 @@ public func haveValidSnapshot(named name: String? = nil, actualExpression: actualExpression, pixelTolerance: pixelTolerance, tolerance: tolerance, - isIgnoreScale: isIgnoreScale) + shouldIgnoreScale: shouldIgnoreScale) } } @@ -297,7 +297,7 @@ public func haveValidDeviceAgnosticSnapshot(named name: String? = nil, usesDrawRect: Bool = false, pixelTolerance: CGFloat? = nil, tolerance: CGFloat? = nil, - isIgnoreScale: Bool = false) -> Predicate { + shouldIgnoreScale: Bool = false) -> Predicate { return Predicate { actualExpression in if switchChecksWithRecords { @@ -306,7 +306,7 @@ public func haveValidDeviceAgnosticSnapshot(named name: String? = nil, isDeviceAgnostic: true, usesDrawRect: usesDrawRect, actualExpression: actualExpression, - isIgnoreScale: isIgnoreScale) + shouldIgnoreScale: shouldIgnoreScale) } return performSnapshotTest(name, @@ -316,30 +316,30 @@ public func haveValidDeviceAgnosticSnapshot(named name: String? = nil, actualExpression: actualExpression, pixelTolerance: pixelTolerance, tolerance: tolerance, - isIgnoreScale: isIgnoreScale) + shouldIgnoreScale: shouldIgnoreScale) } } public func recordSnapshot(named name: String? = nil, identifier: String? = nil, usesDrawRect: Bool = false, - isIgnoreScale: Bool = false) -> Predicate { + shouldIgnoreScale: Bool = false) -> Predicate { return Predicate { actualExpression in return recordSnapshot(name, identifier: identifier, usesDrawRect: usesDrawRect, actualExpression: actualExpression, - isIgnoreScale: isIgnoreScale) + shouldIgnoreScale: shouldIgnoreScale) } } public func recordDeviceAgnosticSnapshot(named name: String? = nil, identifier: String? = nil, usesDrawRect: Bool = false, - isIgnoreScale: Bool = false) -> Predicate { + shouldIgnoreScale: Bool = false) -> Predicate { return Predicate { actualExpression in return recordSnapshot(name, identifier: identifier, isDeviceAgnostic: true, usesDrawRect: usesDrawRect, actualExpression: actualExpression, - isIgnoreScale: isIgnoreScale) + shouldIgnoreScale: shouldIgnoreScale) } }