Skip to content

Commit 9ea175a

Browse files
committed
Add default configuration options
1 parent a02ef9a commit 9ea175a

15 files changed

+44
-27
lines changed

Sources/SnapshotTesting/AssertSnapshot.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public func assertSnapshots<Value, Format>(
155155
/// - name: An optional description of the snapshot.
156156
/// - recording: Whether or not to record a new reference.
157157
/// - snapshotDirectory: Optional directory to save snapshots. By default snapshots will be saved in a directory with the same name as the test file, and that directory will sit inside a directory `__Snapshots__` that sits next to your test file.
158+
/// - snapshotSubdirectory: The default subdirectory for snapshots in the same directory as the test file. Defaults to `__Snapshots__` that sits next to your test file. Only used when `snapshotDirectory` is nil.
158159
/// - timeout: The amount of time a snapshot must be generated in.
159160
/// - file: The file in which failure occurred. Defaults to the file name of the test case in which this function was called.
160161
/// - testName: The name of the test in which failure occurred. Defaults to the function name of the test case in which this function was called.
@@ -165,8 +166,9 @@ public func verifySnapshot<Value, Format>(
165166
as snapshotting: Snapshotting<Value, Format>,
166167
named name: String? = nil,
167168
record recording: Bool = false,
168-
snapshotDirectory: String? = nil,
169-
timeout: TimeInterval = 5,
169+
snapshotDirectory: String? = SnapshottingDefaults.snapshotDirectory,
170+
snapshotSubdirectory: String = SnapshottingDefaults.snapshotSubdirectory,
171+
timeout: TimeInterval = SnapshottingDefaults.timeout,
170172
file: StaticString = #file,
171173
testName: String = #function,
172174
line: UInt = #line
@@ -183,7 +185,7 @@ public func verifySnapshot<Value, Format>(
183185
let snapshotDirectoryUrl = snapshotDirectory.map { URL(fileURLWithPath: $0, isDirectory: true) }
184186
?? fileUrl
185187
.deletingLastPathComponent()
186-
.appendingPathComponent("__Snapshots__")
188+
.appendingPathComponent(snapshotSubdirectory)
187189
.appendingPathComponent(fileName)
188190

189191
let identifier: String

Sources/SnapshotTesting/Snapshotting/CALayer.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension Snapshotting where Value == CALayer, Format == NSImage {
1212
/// - Parameters:
1313
/// - precision: The percentage of pixels that must match.
1414
/// - perceptualPrecision: The percentage a pixel must match the source pixel to be considered a match. [98-99% mimics the precision of the human eye.](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e)
15-
public static func image(precision: Float, perceptualPrecision: Float = 1) -> Snapshotting {
15+
public static func image(precision: Float, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision) -> Snapshotting {
1616
return SimplySnapshotting.image(precision: precision, perceptualPrecision: perceptualPrecision).pullback { layer in
1717
let image = NSImage(size: layer.bounds.size)
1818
image.lockFocus()
@@ -40,7 +40,7 @@ extension Snapshotting where Value == CALayer, Format == UIImage {
4040
/// - precision: The percentage of pixels that must match.
4141
/// - perceptualPrecision: The percentage a pixel must match the source pixel to be considered a match. [98-99% mimics the precision of the human eye.](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e)
4242
/// - traits: A trait collection override.
43-
public static func image(precision: Float = 1, perceptualPrecision: Float = 1, traits: UITraitCollection = .init())
43+
public static func image(precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, traits: UITraitCollection = .init())
4444
-> Snapshotting {
4545
return SimplySnapshotting.image(precision: precision, perceptualPrecision: perceptualPrecision, scale: traits.displayScale).pullback { layer in
4646
renderer(bounds: layer.bounds, for: traits).image { ctx in

Sources/SnapshotTesting/Snapshotting/CGPath.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension Snapshotting where Value == CGPath, Format == NSImage {
1212
/// - Parameters:
1313
/// - precision: The percentage of pixels that must match.
1414
/// - perceptualPrecision: The percentage a pixel must match the source pixel to be considered a match. [98-99% mimics the precision of the human eye.](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e)
15-
public static func image(precision: Float = 1, perceptualPrecision: Float = 1, drawingMode: CGPathDrawingMode = .eoFill) -> Snapshotting {
15+
public static func image(precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, drawingMode: CGPathDrawingMode = .eoFill) -> Snapshotting {
1616
return SimplySnapshotting.image(precision: precision, perceptualPrecision: perceptualPrecision).pullback { path in
1717
let bounds = path.boundingBoxOfPath
1818
var transform = CGAffineTransform(translationX: -bounds.origin.x, y: -bounds.origin.y)
@@ -43,7 +43,7 @@ extension Snapshotting where Value == CGPath, Format == UIImage {
4343
/// - Parameters:
4444
/// - precision: The percentage of pixels that must match.
4545
/// - perceptualPrecision: The percentage a pixel must match the source pixel to be considered a match. [98-99% mimics the precision of the human eye.](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e)
46-
public static func image(precision: Float = 1, perceptualPrecision: Float = 1, scale: CGFloat = 1, drawingMode: CGPathDrawingMode = .eoFill) -> Snapshotting {
46+
public static func image(precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, scale: CGFloat = 1, drawingMode: CGPathDrawingMode = .eoFill) -> Snapshotting {
4747
return SimplySnapshotting.image(precision: precision, perceptualPrecision: perceptualPrecision, scale: scale).pullback { path in
4848
let bounds = path.boundingBoxOfPath
4949
let format: UIGraphicsImageRendererFormat

Sources/SnapshotTesting/Snapshotting/NSBezierPath.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension Snapshotting where Value == NSBezierPath, Format == NSImage {
1212
/// - Parameters:
1313
/// - precision: The percentage of pixels that must match.
1414
/// - perceptualPrecision: The percentage a pixel must match the source pixel to be considered a match. [98-99% mimics the precision of the human eye.](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e)
15-
public static func image(precision: Float = 1, perceptualPrecision: Float = 1) -> Snapshotting {
15+
public static func image(precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision) -> Snapshotting {
1616
return SimplySnapshotting.image(precision: precision, perceptualPrecision: perceptualPrecision).pullback { path in
1717
// Move path info frame:
1818
let bounds = path.bounds

Sources/SnapshotTesting/Snapshotting/NSImage.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension Diffing where Value == NSImage {
1212
/// - precision: The percentage of pixels that must match.
1313
/// - perceptualPrecision: The percentage a pixel must match the source pixel to be considered a match. [98-99% mimics the precision of the human eye.](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e)
1414
/// - Returns: A new diffing strategy.
15-
public static func image(precision: Float = 1, perceptualPrecision: Float = 1) -> Diffing {
15+
public static func image(precision: Float = 1, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision) -> Diffing {
1616
return .init(
1717
toData: { NSImagePNGRepresentation($0)! },
1818
fromData: { NSImage(data: $0)! }
@@ -44,7 +44,7 @@ extension Snapshotting where Value == NSImage, Format == NSImage {
4444
/// - Parameters:
4545
/// - precision: The percentage of pixels that must match.
4646
/// - perceptualPrecision: The percentage a pixel must match the source pixel to be considered a match. [98-99% mimics the precision of the human eye.](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e)
47-
public static func image(precision: Float = 1, perceptualPrecision: Float = 1) -> Snapshotting {
47+
public static func image(precision: Float = 1, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision) -> Snapshotting {
4848
return .init(
4949
pathExtension: "png",
5050
diffing: .image(precision: precision, perceptualPrecision: perceptualPrecision)

Sources/SnapshotTesting/Snapshotting/NSView.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extension Snapshotting where Value == NSView, Format == NSImage {
1313
/// - precision: The percentage of pixels that must match.
1414
/// - perceptualPrecision: The percentage a pixel must match the source pixel to be considered a match. [98-99% mimics the precision of the human eye.](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e)
1515
/// - size: A view size override.
16-
public static func image(precision: Float = 1, perceptualPrecision: Float = 1, size: CGSize? = nil) -> Snapshotting {
16+
public static func image(precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, size: CGSize? = nil) -> Snapshotting {
1717
return SimplySnapshotting.image(precision: precision, perceptualPrecision: perceptualPrecision).asyncPullback { view in
1818
let initialSize = view.frame.size
1919
if let size = size { view.frame.size = size }

Sources/SnapshotTesting/Snapshotting/NSViewController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extension Snapshotting where Value == NSViewController, Format == NSImage {
1313
/// - precision: The percentage of pixels that must match.
1414
/// - perceptualPrecision: The percentage a pixel must match the source pixel to be considered a match. [98-99% mimics the precision of the human eye.](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e)
1515
/// - size: A view size override.
16-
public static func image(precision: Float = 1, perceptualPrecision: Float = 1, size: CGSize? = nil) -> Snapshotting {
16+
public static func image(precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, size: CGSize? = nil) -> Snapshotting {
1717
return Snapshotting<NSView, NSImage>.image(precision: precision, perceptualPrecision: perceptualPrecision, size: size).pullback { $0.view }
1818
}
1919
}

Sources/SnapshotTesting/Snapshotting/SceneKit.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extension Snapshotting where Value == SCNScene, Format == NSImage {
1414
/// - precision: The percentage of pixels that must match.
1515
/// - perceptualPrecision: The percentage a pixel must match the source pixel to be considered a match. [98-99% mimics the precision of the human eye.](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e)
1616
/// - size: The size of the scene.
17-
public static func image(precision: Float = 1, perceptualPrecision: Float = 1, size: CGSize) -> Snapshotting {
17+
public static func image(precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, size: CGSize) -> Snapshotting {
1818
return .scnScene(precision: precision, perceptualPrecision: perceptualPrecision, size: size)
1919
}
2020
}
@@ -26,7 +26,7 @@ extension Snapshotting where Value == SCNScene, Format == UIImage {
2626
/// - precision: The percentage of pixels that must match.
2727
/// - perceptualPrecision: The percentage a pixel must match the source pixel to be considered a match. [98-99% mimics the precision of the human eye.](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e)
2828
/// - size: The size of the scene.
29-
public static func image(precision: Float = 1, perceptualPrecision: Float = 1, size: CGSize) -> Snapshotting {
29+
public static func image(precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, size: CGSize) -> Snapshotting {
3030
return .scnScene(precision: precision, perceptualPrecision: perceptualPrecision, size: size)
3131
}
3232
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import Foundation
2+
3+
/// The global configuration options
4+
public enum SnapshottingDefaults {
5+
/// The default subdirectory for snapshots in the same directory as the test file. Defaults to `__Snapshots__` that sits next to your test file. Only used when `snapshotDirectory` is nil.
6+
public static var snapshotSubdirectory = "__Snapshots__"
7+
/// Optional directory to save snapshots. By default snapshots will be saved in a directory with the same name as the test file, and that directory will sit inside a directory `__Snapshots__` that sits next to your test file.
8+
public static var snapshotDirectory: String? = nil
9+
/// The amount of time a snapshot must be generated in.
10+
public static var timeout: TimeInterval = 5
11+
/// The percentage of pixels that must match. Value between 0-1
12+
public static var precision: Float = 1
13+
/// The percentage a pixel must match the source pixel to be considered a match. [98-99% mimics the precision of the human
14+
public static var perceptualPrecision: Float = 1
15+
}

Sources/SnapshotTesting/Snapshotting/SpriteKit.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extension Snapshotting where Value == SKScene, Format == NSImage {
1414
/// - precision: The percentage of pixels that must match.
1515
/// - perceptualPrecision: The percentage a pixel must match the source pixel to be considered a match. [98-99% mimics the precision of the human eye.](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e)
1616
/// - size: The size of the scene.
17-
public static func image(precision: Float = 1, perceptualPrecision: Float = 1, size: CGSize) -> Snapshotting {
17+
public static func image(precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, size: CGSize) -> Snapshotting {
1818
return .skScene(precision: precision, perceptualPrecision: perceptualPrecision, size: size)
1919
}
2020
}
@@ -26,7 +26,7 @@ extension Snapshotting where Value == SKScene, Format == UIImage {
2626
/// - precision: The percentage of pixels that must match.
2727
/// - perceptualPrecision: The percentage a pixel must match the source pixel to be considered a match. [98-99% mimics the precision of the human eye.](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e)
2828
/// - size: The size of the scene.
29-
public static func image(precision: Float = 1, perceptualPrecision: Float = 1, size: CGSize) -> Snapshotting {
29+
public static func image(precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, size: CGSize) -> Snapshotting {
3030
return .skScene(precision: precision, perceptualPrecision: perceptualPrecision, size: size)
3131
}
3232
}

Sources/SnapshotTesting/Snapshotting/SwiftUIView.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ extension Snapshotting where Value: SwiftUI.View, Format == UIImage {
3333
/// - traits: A trait collection override.
3434
public static func image(
3535
drawHierarchyInKeyWindow: Bool = false,
36-
precision: Float = 1,
37-
perceptualPrecision: Float = 1,
36+
precision: Float = SnapshottingDefaults.precision,
37+
perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision,
3838
layout: SwiftUISnapshotLayout = .sizeThatFits,
3939
traits: UITraitCollection = .init()
4040
)

Sources/SnapshotTesting/Snapshotting/UIBezierPath.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extension Snapshotting where Value == UIBezierPath, Format == UIImage {
1313
/// - precision: The percentage of pixels that must match.
1414
/// - perceptualPrecision: The percentage a pixel must match the source pixel to be considered a match. [98-99% mimics the precision of the human eye.](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e)
1515
/// - scale: The scale to use when loading the reference image from disk.
16-
public static func image(precision: Float = 1, perceptualPrecision: Float = 1, scale: CGFloat = 1) -> Snapshotting {
16+
public static func image(precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, scale: CGFloat = 1) -> Snapshotting {
1717
return SimplySnapshotting.image(precision: precision, perceptualPrecision: perceptualPrecision, scale: scale).pullback { path in
1818
let bounds = path.bounds
1919
let format: UIGraphicsImageRendererFormat

Sources/SnapshotTesting/Snapshotting/UIImage.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extension Diffing where Value == UIImage {
1313
/// - perceptualPrecision: The percentage a pixel must match the source pixel to be considered a match. [98-99% mimics the precision of the human eye.](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e)
1414
/// - scale: Scale to use when loading the reference image from disk. If `nil` or the `UITraitCollection`s default value of `0.0`, the screens scale is used.
1515
/// - Returns: A new diffing strategy.
16-
public static func image(precision: Float = 1, perceptualPrecision: Float = 1, scale: CGFloat? = nil) -> Diffing {
16+
public static func image(precision: Float = 1, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, scale: CGFloat? = nil) -> Diffing {
1717
let imageScale: CGFloat
1818
if let scale = scale, scale != 0.0 {
1919
imageScale = scale
@@ -65,7 +65,7 @@ extension Snapshotting where Value == UIImage, Format == UIImage {
6565
/// - precision: The percentage of pixels that must match.
6666
/// - perceptualPrecision: The percentage a pixel must match the source pixel to be considered a match. [98-99% mimics the precision of the human eye.](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e)
6767
/// - scale: The scale of the reference image stored on disk.
68-
public static func image(precision: Float = 1, perceptualPrecision: Float = 1, scale: CGFloat? = nil) -> Snapshotting {
68+
public static func image(precision: Float = 1, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, scale: CGFloat? = nil) -> Snapshotting {
6969
return .init(
7070
pathExtension: "png",
7171
diffing: .image(precision: precision, perceptualPrecision: perceptualPrecision, scale: scale)

Sources/SnapshotTesting/Snapshotting/UIView.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ extension Snapshotting where Value == UIView, Format == UIImage {
1717
/// - traits: A trait collection override.
1818
public static func image(
1919
drawHierarchyInKeyWindow: Bool = false,
20-
precision: Float = 1,
21-
perceptualPrecision: Float = 1,
20+
precision: Float = SnapshottingDefaults.precision,
21+
perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision,
2222
size: CGSize? = nil,
2323
traits: UITraitCollection = .init()
2424
)

Sources/SnapshotTesting/Snapshotting/UIViewController.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ extension Snapshotting where Value == UIViewController, Format == UIImage {
1717
/// - traits: A trait collection override.
1818
public static func image(
1919
on config: ViewImageConfig,
20-
precision: Float = 1,
21-
perceptualPrecision: Float = 1,
20+
precision: Float = SnapshottingDefaults.precision,
21+
perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision,
2222
size: CGSize? = nil,
2323
traits: UITraitCollection = .init()
2424
)
@@ -45,8 +45,8 @@ extension Snapshotting where Value == UIViewController, Format == UIImage {
4545
/// - traits: A trait collection override.
4646
public static func image(
4747
drawHierarchyInKeyWindow: Bool = false,
48-
precision: Float = 1,
49-
perceptualPrecision: Float = 1,
48+
precision: Float = SnapshottingDefaults.precision,
49+
perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision,
5050
size: CGSize? = nil,
5151
traits: UITraitCollection = .init()
5252
)

0 commit comments

Comments
 (0)