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 optional name parameter to assertion functions #2

Merged
merged 3 commits into from
Aug 16, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extension PreviewSnapshots {
///
/// - Parameters:
/// - snapshotting: Snapshotting instance for `AnyView` into a `UIImage`.
/// - name: An optional description of the snapshot to include with the configuration name.
/// - recording: Whether or not to record a new reference.
/// - file: The file in which failure occurred. Defaults to the file name of the test case in
/// which this function was called.
Expand All @@ -30,6 +31,7 @@ extension PreviewSnapshots {
/// this function was called.
public func assertSnapshots(
as snapshotting: Snapshotting<AnyView, UIImage> = .image,
named name: String? = nil,
record recording: Bool = false,
file: StaticString = #file,
testName: String = #function,
Expand All @@ -39,7 +41,7 @@ extension PreviewSnapshots {
assertSnapshot(
matching: configure(configuration.state),
as: snapshotting,
named: configuration.name,
named: configuration.snapshotName(prefix: name),
record: recording,
file: file, testName: testName, line: line
)
Expand All @@ -52,6 +54,7 @@ extension PreviewSnapshots {
/// - Parameters:
/// - strategies: A dictionary of names and strategies for serializing, deserializing, and
/// comparing values.
/// - name: An optional description of the snapshot to include with the configuration name.
/// - recording: Whether or not to record a new reference.
/// - file: The file in which failure occurred. Defaults to the file name of the test case in
/// which this function was called.
Expand All @@ -61,6 +64,7 @@ extension PreviewSnapshots {
/// this function was called.
public func assertSnapshots(
as strategies: [String: Snapshotting<AnyView, UIImage>],
named name: String? = nil,
record recording: Bool = false,
file: StaticString = #file,
testName: String = #function,
Expand All @@ -71,7 +75,7 @@ extension PreviewSnapshots {
assertSnapshot(
matching: configure(configuration.state),
as: strategy,
named: configuration.name + "-\(key)",
named: configuration.snapshotName(prefix: name) + "-\(key)",
record: recording,
file: file, testName: testName, line: line
)
Expand All @@ -84,6 +88,7 @@ extension PreviewSnapshots {
///
/// - Parameters:
/// - strategies: An array of strategies for serializing, deserializing, and comparing values.
/// - name: An optional description of the snapshot to include with the configuration name.
/// - recording: Whether or not to record a new reference.
/// - file: The file in which failure occurred. Defaults to the file name of the test case in
/// which this function was called.
Expand All @@ -93,6 +98,7 @@ extension PreviewSnapshots {
/// this function was called.
public func assertSnapshots(
as strategies: [Snapshotting<AnyView, UIImage>],
named name: String? = nil,
record recording: Bool = false,
file: StaticString = #file,
testName: String = #function,
Expand All @@ -103,7 +109,7 @@ extension PreviewSnapshots {
assertSnapshot(
matching: configure(configuration.state),
as: strategy,
named: configuration.name + "-\(position + 1)",
named: configuration.snapshotName(prefix: name) + "-\(position + 1)",
record: recording,
file: file, testName: testName, line: line
)
Expand Down Expand Up @@ -133,6 +139,7 @@ extension PreviewSnapshots {
///
/// - Parameters:
/// - snapshotting: Snapshotting instance that converts an `AnyView` into a `UIImage`.
/// - name: An optional description of the snapshot to include with the configuration name.
/// - recording: Whether or not to record a new reference.
/// - file: The file in which failure occurred. Defaults to the file name of the test case in
/// which this function was called.
Expand All @@ -143,6 +150,7 @@ extension PreviewSnapshots {
/// - modify: A closure to update the preview content before snapshotting.
public func assertSnapshots<Modified: View>(
as snapshotting: Snapshotting<Modified, UIImage> = .image,
named name: String? = nil,
record recording: Bool = false,
file: StaticString = #file,
testName: String = #function,
Expand All @@ -153,7 +161,7 @@ extension PreviewSnapshots {
assertSnapshot(
matching: modify(configure(configuration.state)),
as: snapshotting,
named: configuration.name,
named: configuration.snapshotName(prefix: name),
record: recording,
file: file, testName: testName, line: line
)
Expand All @@ -179,6 +187,7 @@ extension PreviewSnapshots {
/// - Parameters:
/// - strategies: A dictionary of names and strategies for serializing, deserializing, and
/// comparing values.
/// - name: An optional description of the snapshot to include with the configuration name.
/// - recording: Whether or not to record a new reference.
/// - file: The file in which failure occurred. Defaults to the file name of the test case in
/// which this function was called.
Expand All @@ -189,6 +198,7 @@ extension PreviewSnapshots {
/// - modify: A closure to update the preview content before snapshotting.
public func assertSnapshots<Modified: View>(
as strategies: [String: Snapshotting<AnyView, UIImage>],
named name: String? = nil,
record recording: Bool = false,
file: StaticString = #file,
testName: String = #function,
Expand All @@ -200,7 +210,7 @@ extension PreviewSnapshots {
assertSnapshot(
matching: configure(configuration.state),
as: strategy,
named: configuration.name + "-\(key)",
named: configuration.snapshotName(prefix: name) + "-\(key)",
record: recording,
file: file, testName: testName, line: line
)
Expand All @@ -226,6 +236,7 @@ extension PreviewSnapshots {
///
/// - Parameters:
/// - snapshotting: Snapshotting instance that converts an `AnyView` into a `UIImage`.
/// - name: An optional description of the snapshot to include with the configuration name.
/// - recording: Whether or not to record a new reference.
/// - file: The file in which failure occurred. Defaults to the file name of the test case in
/// which this function was called.
Expand All @@ -236,6 +247,7 @@ extension PreviewSnapshots {
/// - modify: A closure to update the preview content before snapshotting.
public func assertSnapshots<Modified: View>(
as strategies: [Snapshotting<Modified, UIImage>],
named name: String? = nil,
record recording: Bool = false,
file: StaticString = #file,
testName: String = #function,
Expand All @@ -247,11 +259,21 @@ extension PreviewSnapshots {
assertSnapshot(
matching: modify(configure(configuration.state)),
as: strategy,
named: configuration.name + "-\(position)",
named: configuration.snapshotName(prefix: name) + "-\(position)",
record: recording,
file: file, testName: testName, line: line
)
}
}
}
}

// MARK: Configuration name helper

private extension PreviewSnapshots.Configuration {
/// Construct a snapshot name based on the configuration name and an optional prefix.
func snapshotName(prefix: String?) -> String {
guard let prefix else { return name }
return "\(prefix)-\(name)"
}
}
26 changes: 26 additions & 0 deletions Tests/PreviewSnapshotsTests/PreviewSnapshotsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ final class PreviewSnapshotsTests: XCTestCase {
snapshots.assertSnapshots(as: .testStrategy)
}

/// PreviewSnapshots assertion using `named` parameter
func test_namedAssertion() {
struct ContentView: View {
let message: String

var body: some View {
Text(message)
.font(.largeTitle)
.foregroundColor(.blue)
.padding(8)
}
}

let snapshots = PreviewSnapshots<String>(
configurations: [
.init(name: "Short Message", state: "Hello!"),
.init(name: "Long Message", state: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
],
configure: { message in
ContentView(message: message)
}
)

snapshots.assertSnapshots(as: .testStrategy, named: "Named Assertion")
}

/// PreviewSnapshots with a tuple as the state
func test_tupleState() {
struct ContentView: View {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.