Skip to content

Commit f73aa80

Browse files
mackojstephencelis
authored andcommitted
Add support for asserting on multiple snapshotting strategy (#150)
1 parent 184019f commit f73aa80

File tree

6 files changed

+89
-0
lines changed

6 files changed

+89
-0
lines changed

‎Sources/SnapshotTesting/AssertSnapshot.swift‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,73 @@ public func assertSnapshot<Value, Format>(
4545
XCTFail(message, file: file, line: line)
4646
}
4747

48+
/// Asserts that a given value matches a reference on disk.
49+
///
50+
/// - Parameters:
51+
/// - value: A value to compare against a reference.
52+
/// - snapshotting: An dictionnay of names and strategies for serializing, deserializing, and comparing values.
53+
/// - recording: Whether or not to record a new reference.
54+
/// - timeout: The amount of time a snapshot must be generated in.
55+
/// - file: The file in which failure occurred. Defaults to the file name of the test case in which this function was called.
56+
/// - 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.
57+
/// - line: The line number on which failure occurred. Defaults to the line number on which this function was called.
58+
public func assertSnapshots<Value, Format>(
59+
matching value: @autoclosure () throws -> Value,
60+
as strategies: [String: Snapshotting<Value, Format>],
61+
record recording: Bool = false,
62+
timeout: TimeInterval = 5,
63+
file: StaticString = #file,
64+
testName: String = #function,
65+
line: UInt = #line
66+
) {
67+
68+
strategies.forEach { name, strategy in
69+
assertSnapshot(
70+
matching: value,
71+
as: strategy,
72+
named: name,
73+
record: recording,
74+
timeout: timeout,
75+
file: file,
76+
testName: testName,
77+
line: line
78+
)
79+
}
80+
}
81+
82+
/// Asserts that a given value matches a reference on disk.
83+
///
84+
/// - Parameters:
85+
/// - value: A value to compare against a reference.
86+
/// - snapshotting: An array of strategies for serializing, deserializing, and comparing values.
87+
/// - recording: Whether or not to record a new reference.
88+
/// - timeout: The amount of time a snapshot must be generated in.
89+
/// - file: The file in which failure occurred. Defaults to the file name of the test case in which this function was called.
90+
/// - 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.
91+
/// - line: The line number on which failure occurred. Defaults to the line number on which this function was called.
92+
public func assertSnapshots<Value, Format>(
93+
matching value: @autoclosure () throws -> Value,
94+
as strategies: [Snapshotting<Value, Format>],
95+
record recording: Bool = false,
96+
timeout: TimeInterval = 5,
97+
file: StaticString = #file,
98+
testName: String = #function,
99+
line: UInt = #line
100+
) {
101+
102+
strategies.forEach { strategy in
103+
assertSnapshot(
104+
matching: value,
105+
as: strategy,
106+
record: recording,
107+
timeout: timeout,
108+
file: file,
109+
testName: testName,
110+
line: line
111+
)
112+
}
113+
}
114+
48115
/// Verifies that a given value matches a reference on disk.
49116
///
50117
/// - Parameters:

‎Tests/SnapshotTestingTests/SnapshotTestingTests.swift‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,28 @@ class SnapshotTestingTests: TestCase {
250250
#endif
251251
}
252252

253+
func testAssertMultipleSnapshot() {
254+
#if os(iOS)
255+
class TableViewController: UITableViewController {
256+
override func viewDidLoad() {
257+
super.viewDidLoad()
258+
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
259+
}
260+
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
261+
return 10
262+
}
263+
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
264+
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
265+
cell.textLabel?.text = "\(indexPath.row)"
266+
return cell
267+
}
268+
}
269+
let tableViewController = TableViewController()
270+
assertSnapshots(matching: tableViewController, as: ["iPhoneSE-image" : .image(on: .iPhoneSe), "iPad-image" : .image(on: .iPadMini)])
271+
assertSnapshots(matching: tableViewController, as: [.image(on: .iPhoneX), .image(on: .iPhoneXsMax)])
272+
#endif
273+
}
274+
253275
func testTraits() {
254276
#if os(iOS) || os(tvOS)
255277
if #available(iOS 11.0, tvOS 11.0, *) {
33.3 KB
Loading
38.3 KB
Loading
66.1 KB
Loading
23.3 KB
Loading

0 commit comments

Comments
 (0)