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

Add default configuration options #643

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions Sources/SnapshotTesting/AssertSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ public func assertSnapshots<Value, Format>(
/// - 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.
/// - 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.
/// - timeout: The amount of time a snapshot must be generated in.
/// - file: The file in which failure occurred. Defaults to the file name of the test case in
/// which this function was called.
Expand All @@ -186,8 +188,9 @@ public func verifySnapshot<Value, Format>(
as snapshotting: Snapshotting<Value, Format>,
named name: String? = nil,
record recording: Bool = false,
snapshotDirectory: String? = nil,
timeout: TimeInterval = 5,
snapshotDirectory: String? = SnapshottingDefaults.snapshotDirectory,
snapshotSubdirectory: String = SnapshottingDefaults.snapshotSubdirectory,
timeout: TimeInterval = SnapshottingDefaults.timeout,
file: StaticString = #file,
testName: String = #function,
line: UInt = #line
Expand All @@ -204,7 +207,7 @@ public func verifySnapshot<Value, Format>(
snapshotDirectory.map { URL(fileURLWithPath: $0, isDirectory: true) }
?? fileUrl
.deletingLastPathComponent()
.appendingPathComponent("__Snapshots__")
.appendingPathComponent(snapshotSubdirectory)
.appendingPathComponent(fileName)

let identifier: String
Expand Down
6 changes: 3 additions & 3 deletions Sources/SnapshotTesting/Snapshotting/CALayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/// assertSnapshot(of: layer, as: .image(precision: 0.99))
/// ```
public static var image: Snapshotting {
return .image(precision: 1)
return .image(precision: SnapshottingDefaults.precision)
}

/// A snapshot strategy for comparing layers based on pixel equality.
Expand All @@ -25,7 +25,7 @@
/// match. 98-99% mimics
/// [the precision](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e) of the
/// human eye.
public static func image(precision: Float, perceptualPrecision: Float = 1) -> Snapshotting {
public static func image(precision: Float, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision) -> Snapshotting {
return SimplySnapshotting.image(
precision: precision, perceptualPrecision: perceptualPrecision
).pullback { layer in
Expand Down Expand Up @@ -59,7 +59,7 @@
/// human eye.
/// - traits: A trait collection override.
public static func image(
precision: Float = 1, perceptualPrecision: Float = 1, traits: UITraitCollection = .init()
precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, traits: UITraitCollection = .init()
)
-> Snapshotting
{
Expand Down
8 changes: 6 additions & 2 deletions Sources/SnapshotTesting/Snapshotting/CGPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
/// [the precision](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e) of the
/// human eye.
public static func image(
precision: Float = 1, perceptualPrecision: Float = 1, drawingMode: CGPathDrawingMode = .eoFill
precision: Float = SnapshottingDefaults.precision,
perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision,
drawingMode: CGPathDrawingMode = .eoFill
) -> Snapshotting {
return SimplySnapshotting.image(
precision: precision, perceptualPrecision: perceptualPrecision
Expand Down Expand Up @@ -64,7 +66,9 @@
/// [the precision](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e) of the
/// human eye.
public static func image(
precision: Float = 1, perceptualPrecision: Float = 1, scale: CGFloat = 1,
precision: Float = SnapshottingDefaults.precision,
perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision,
scale: CGFloat = 1,
drawingMode: CGPathDrawingMode = .eoFill
) -> Snapshotting {
return SimplySnapshotting.image(
Expand Down
2 changes: 1 addition & 1 deletion Sources/SnapshotTesting/Snapshotting/NSBezierPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/// match. 98-99% mimics
/// [the precision](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e) of the
/// human eye.
public static func image(precision: Float = 1, perceptualPrecision: Float = 1) -> Snapshotting {
public static func image(precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision) -> Snapshotting {
return SimplySnapshotting.image(
precision: precision, perceptualPrecision: perceptualPrecision
).pullback { path in
Expand Down
2 changes: 1 addition & 1 deletion Sources/SnapshotTesting/Snapshotting/NSImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/// [the precision](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e) of the
/// human eye.
/// - Returns: A new diffing strategy.
public static func image(precision: Float = 1, perceptualPrecision: Float = 1) -> Diffing {
public static func image(precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision) -> Diffing {
return .init(
toData: { NSImagePNGRepresentation($0)! },
fromData: { NSImage(data: $0)! }
Expand Down
2 changes: 1 addition & 1 deletion Sources/SnapshotTesting/Snapshotting/NSView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/// human eye.
/// - size: A view size override.
public static func image(
precision: Float = 1, perceptualPrecision: Float = 1, size: CGSize? = nil
precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, size: CGSize? = nil
) -> Snapshotting {
return SimplySnapshotting.image(
precision: precision, perceptualPrecision: perceptualPrecision
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/// human eye.
/// - size: A view size override.
public static func image(
precision: Float = 1, perceptualPrecision: Float = 1, size: CGSize? = nil
precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, size: CGSize? = nil
) -> Snapshotting {
return Snapshotting<NSView, NSImage>.image(
precision: precision, perceptualPrecision: perceptualPrecision, size: size
Expand Down
4 changes: 2 additions & 2 deletions Sources/SnapshotTesting/Snapshotting/SceneKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/// [the precision](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e) of the
/// human eye.
/// - size: The size of the scene.
public static func image(precision: Float = 1, perceptualPrecision: Float = 1, size: CGSize)
public static func image(precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, size: CGSize)
-> Snapshotting
{
return .scnScene(precision: precision, perceptualPrecision: perceptualPrecision, size: size)
Expand All @@ -34,7 +34,7 @@
/// [the precision](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e) of the
/// human eye.
/// - size: The size of the scene.
public static func image(precision: Float = 1, perceptualPrecision: Float = 1, size: CGSize)
public static func image(precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, size: CGSize)
-> Snapshotting
{
return .scnScene(precision: precision, perceptualPrecision: perceptualPrecision, size: size)
Expand Down
15 changes: 15 additions & 0 deletions Sources/SnapshotTesting/Snapshotting/SnapshottingDefaults.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Foundation

/// The global configuration options
public enum SnapshottingDefaults {
/// 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.
public static var snapshotSubdirectory = "__Snapshots__"
/// 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.
public static var snapshotDirectory: String? = nil
/// The amount of time a snapshot must be generated in.
public static var timeout: TimeInterval = 5
/// The percentage of pixels that must match. Value between 0-1
public static var precision: Float = 1
/// The percentage a pixel must match the source pixel to be considered a match. [98-99% mimics the precision of the human
public static var perceptualPrecision: Float = 1
}
4 changes: 2 additions & 2 deletions Sources/SnapshotTesting/Snapshotting/SpriteKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/// [the precision](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e) of the
/// human eye.
/// - size: The size of the scene.
public static func image(precision: Float = 1, perceptualPrecision: Float = 1, size: CGSize)
public static func image(precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, size: CGSize)
-> Snapshotting
{
return .skScene(precision: precision, perceptualPrecision: perceptualPrecision, size: size)
Expand All @@ -34,7 +34,7 @@
/// [the precision](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e) of the
/// human eye.
/// - size: The size of the scene.
public static func image(precision: Float = 1, perceptualPrecision: Float = 1, size: CGSize)
public static func image(precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, size: CGSize)
-> Snapshotting
{
return .skScene(precision: precision, perceptualPrecision: perceptualPrecision, size: size)
Expand Down
4 changes: 2 additions & 2 deletions Sources/SnapshotTesting/Snapshotting/SwiftUIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
/// - traits: A trait collection override.
public static func image(
drawHierarchyInKeyWindow: Bool = false,
precision: Float = 1,
perceptualPrecision: Float = 1,
precision: Float = SnapshottingDefaults.precision,
perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision,
layout: SwiftUISnapshotLayout = .sizeThatFits,
traits: UITraitCollection = .init()
)
Expand Down
2 changes: 1 addition & 1 deletion Sources/SnapshotTesting/Snapshotting/UIBezierPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/// human eye.
/// - scale: The scale to use when loading the reference image from disk.
public static func image(
precision: Float = 1, perceptualPrecision: Float = 1, scale: CGFloat = 1
precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, scale: CGFloat = 1
) -> Snapshotting {
return SimplySnapshotting.image(
precision: precision, perceptualPrecision: perceptualPrecision, scale: scale
Expand Down
4 changes: 2 additions & 2 deletions Sources/SnapshotTesting/Snapshotting/UIImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/// `UITraitCollection`s default value of `0.0`, the screens scale is used.
/// - Returns: A new diffing strategy.
public static func image(
precision: Float = 1, perceptualPrecision: Float = 1, scale: CGFloat? = nil
precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, scale: CGFloat? = nil
) -> Diffing {
let imageScale: CGFloat
if let scale = scale, scale != 0.0 {
Expand Down Expand Up @@ -78,7 +78,7 @@
/// human eye.
/// - scale: The scale of the reference image stored on disk.
public static func image(
precision: Float = 1, perceptualPrecision: Float = 1, scale: CGFloat? = nil
precision: Float = SnapshottingDefaults.precision, perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision, scale: CGFloat? = nil
) -> Snapshotting {
return .init(
pathExtension: "png",
Expand Down
4 changes: 2 additions & 2 deletions Sources/SnapshotTesting/Snapshotting/UIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
/// - traits: A trait collection override.
public static func image(
drawHierarchyInKeyWindow: Bool = false,
precision: Float = 1,
perceptualPrecision: Float = 1,
precision: Float = SnapshottingDefaults.precision,
perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision,
size: CGSize? = nil,
traits: UITraitCollection = .init()
)
Expand Down
8 changes: 4 additions & 4 deletions Sources/SnapshotTesting/Snapshotting/UIViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
/// - traits: A trait collection override.
public static func image(
on config: ViewImageConfig,
precision: Float = 1,
perceptualPrecision: Float = 1,
precision: Float = SnapshottingDefaults.precision,
perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision,
size: CGSize? = nil,
traits: UITraitCollection = .init()
)
Expand Down Expand Up @@ -57,8 +57,8 @@
/// - traits: A trait collection override.
public static func image(
drawHierarchyInKeyWindow: Bool = false,
precision: Float = 1,
perceptualPrecision: Float = 1,
precision: Float = SnapshottingDefaults.precision,
perceptualPrecision: Float = SnapshottingDefaults.perceptualPrecision,
size: CGSize? = nil,
traits: UITraitCollection = .init()
)
Expand Down