Skip to content

Commit

Permalink
Merge pull request ashfurrow#225 from thanasak-s/feature/ignore_scree…
Browse files Browse the repository at this point in the history
…n_scale

Compare snap shot ignore screen scale
  • Loading branch information
ashfurrow authored Jun 21, 2021
2 parents 4f2aad7 + 894069f commit e2103f3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
18 changes: 12 additions & 6 deletions Nimble_Snapshots/DynamicSize/DynamicSizeSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ public func haveValidDynamicSizeSnapshot(named name: String? = nil,
usesDrawRect: Bool = false,
pixelTolerance: CGFloat? = nil,
tolerance: CGFloat? = nil,
resizeMode: ResizeMode = .frame) -> Predicate<Snapshotable> {
resizeMode: ResizeMode = .frame,
shouldIgnoreScale: Bool = false) -> Predicate<Snapshotable> {
return Predicate { actualExpression in
return performDynamicSizeSnapshotTest(name,
identifier: identifier,
Expand All @@ -163,7 +164,8 @@ public func haveValidDynamicSizeSnapshot(named name: String? = nil,
tolerance: tolerance,
pixelTolerance: pixelTolerance,
isRecord: false,
resizeMode: resizeMode)
resizeMode: resizeMode,
shouldIgnoreScale: shouldIgnoreScale)
}
}

Expand All @@ -176,7 +178,8 @@ func performDynamicSizeSnapshotTest(_ name: String?,
tolerance: CGFloat? = nil,
pixelTolerance: CGFloat? = nil,
isRecord: Bool,
resizeMode: ResizeMode) -> PredicateResult {
resizeMode: ResizeMode,
shouldIgnoreScale: Bool = false) -> PredicateResult {
// swiftlint:disable:next force_try force_unwrapping
let instance = try! actualExpression.evaluate()!
let testFileLocation = actualExpression.location.file
Expand Down Expand Up @@ -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,
shouldIgnoreScale: shouldIgnoreScale)
}

if isRecord {
Expand Down Expand Up @@ -243,7 +247,8 @@ public func recordDynamicSizeSnapshot(named name: String? = nil,
sizes: [String: CGSize],
isDeviceAgnostic: Bool = false,
usesDrawRect: Bool = false,
resizeMode: ResizeMode = .frame) -> Predicate<Snapshotable> {
resizeMode: ResizeMode = .frame,
shouldIgnoreScale: Bool = false) -> Predicate<Snapshotable> {
return Predicate { actualExpression in
return performDynamicSizeSnapshotTest(name,
identifier: identifier,
Expand All @@ -252,7 +257,8 @@ public func recordDynamicSizeSnapshot(named name: String? = nil,
usesDrawRect: usesDrawRect,
actualExpression: actualExpression,
isRecord: true,
resizeMode: resizeMode)
resizeMode: resizeMode,
shouldIgnoreScale: shouldIgnoreScale)
}
}

Expand Down
46 changes: 31 additions & 15 deletions Nimble_Snapshots/HaveValidSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ public class FBSnapshotTest: NSObject {
tolerance: CGFloat,
perPixelTolerance: CGFloat,
filename: String,
identifier: String? = nil) -> Bool {
identifier: String? = nil,
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 shouldIgnoreScale {
snapshotController.fileNameOptions = .none
} else {
snapshotController.fileNameOptions = .screenScale
}
Expand Down Expand Up @@ -199,7 +202,8 @@ private func performSnapshotTest(_ name: String?,
usesDrawRect: Bool = false,
actualExpression: Expression<Snapshotable>,
pixelTolerance: CGFloat? = nil,
tolerance: CGFloat?) -> PredicateResult {
tolerance: CGFloat?,
shouldIgnoreScale: Bool) -> PredicateResult {
// swiftlint:disable:next force_try force_unwrapping
let instance = try! actualExpression.evaluate()!
let testFileLocation = actualExpression.location.file
Expand All @@ -212,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, shouldIgnoreScale: shouldIgnoreScale)

return PredicateResult(status: PredicateStatus(bool: result),
message: .fail("expected a matching snapshot in \(snapshotName)"))
Expand All @@ -222,7 +226,8 @@ private func recordSnapshot(_ name: String?,
identifier: String? = nil,
isDeviceAgnostic: Bool = false,
usesDrawRect: Bool = false,
actualExpression: Expression<Snapshotable>) -> PredicateResult {
actualExpression: Expression<Snapshotable>,
shouldIgnoreScale: Bool) -> PredicateResult {
// swiftlint:disable:next force_try force_unwrapping
let instance = try! actualExpression.evaluate()!
let testFileLocation = actualExpression.location.file
Expand All @@ -241,7 +246,8 @@ private func recordSnapshot(_ name: String?,
tolerance: tolerance,
perPixelTolerance: pixelTolerance,
filename: actualExpression.location.file,
identifier: identifier) {
identifier: identifier,
shouldIgnoreScale: shouldIgnoreScale) {
let name = name ?? snapshotName
message = "snapshot \(name) successfully recorded, replace recordSnapshot with a check"
} else if let name = name {
Expand All @@ -264,38 +270,43 @@ public func haveValidSnapshot(named name: String? = nil,
identifier: String? = nil,
usesDrawRect: Bool = false,
pixelTolerance: CGFloat? = nil,
tolerance: CGFloat? = nil) -> Predicate<Snapshotable> {
tolerance: CGFloat? = nil,
shouldIgnoreScale: Bool = false) -> Predicate<Snapshotable> {

return Predicate { actualExpression in
if switchChecksWithRecords {
return recordSnapshot(name,
identifier: identifier,
usesDrawRect: usesDrawRect,
actualExpression: actualExpression)
actualExpression: actualExpression,
shouldIgnoreScale: shouldIgnoreScale)
}

return performSnapshotTest(name,
identifier: identifier,
usesDrawRect: usesDrawRect,
actualExpression: actualExpression,
pixelTolerance: pixelTolerance,
tolerance: tolerance)
tolerance: tolerance,
shouldIgnoreScale: shouldIgnoreScale)
}
}

public func haveValidDeviceAgnosticSnapshot(named name: String? = nil,
identifier: String? = nil,
usesDrawRect: Bool = false,
pixelTolerance: CGFloat? = nil,
tolerance: CGFloat? = nil) -> Predicate<Snapshotable> {
tolerance: CGFloat? = nil,
shouldIgnoreScale: Bool = false) -> Predicate<Snapshotable> {

return Predicate { actualExpression in
if switchChecksWithRecords {
return recordSnapshot(name,
identifier: identifier,
isDeviceAgnostic: true,
usesDrawRect: usesDrawRect,
actualExpression: actualExpression)
actualExpression: actualExpression,
shouldIgnoreScale: shouldIgnoreScale)
}

return performSnapshotTest(name,
Expand All @@ -304,26 +315,31 @@ public func haveValidDeviceAgnosticSnapshot(named name: String? = nil,
usesDrawRect: usesDrawRect,
actualExpression: actualExpression,
pixelTolerance: pixelTolerance,
tolerance: tolerance)
tolerance: tolerance,
shouldIgnoreScale: shouldIgnoreScale)
}
}

public func recordSnapshot(named name: String? = nil,
identifier: String? = nil,
usesDrawRect: Bool = false) -> Predicate<Snapshotable> {
usesDrawRect: Bool = false,
shouldIgnoreScale: Bool = false) -> Predicate<Snapshotable> {

return Predicate { actualExpression in
return recordSnapshot(name, identifier: identifier, usesDrawRect: usesDrawRect,
actualExpression: actualExpression)
actualExpression: actualExpression,
shouldIgnoreScale: shouldIgnoreScale)
}
}

public func recordDeviceAgnosticSnapshot(named name: String? = nil,
identifier: String? = nil,
usesDrawRect: Bool = false) -> Predicate<Snapshotable> {
usesDrawRect: Bool = false,
shouldIgnoreScale: Bool = false) -> Predicate<Snapshotable> {

return Predicate { actualExpression in
return recordSnapshot(name, identifier: identifier, isDeviceAgnostic: true, usesDrawRect: usesDrawRect,
actualExpression: actualExpression)
actualExpression: actualExpression,
shouldIgnoreScale: shouldIgnoreScale)
}
}

0 comments on commit e2103f3

Please sign in to comment.