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

Fix recordIssue for Xcode beta 3. #869

Merged
merged 5 commits into from
Jul 9, 2024
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
98 changes: 67 additions & 31 deletions Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ import Foundation
/// - expected: An optional closure that returns a previously generated snapshot. When omitted,
/// the library will automatically write a snapshot into your test file at the call sight of
/// the assertion.
/// - file: The file where the assertion occurs. The default is the filename of the test case
/// where you call this function.
/// - fileID: The file ID in which failure occurred. Defaults to the file ID of the test case in
/// which this function was called.
/// - file: The file in which failure occurred. Defaults to the file path of the test case in
/// which this function was called.
/// - function: The function where the assertion occurs. The default is the name of the test
/// method where you call this function.
/// - line: The line where the assertion occurs. The default is the line number where you call
/// this function.
/// - column: The column where the assertion occurs. The default is the line column you call
/// this function.
/// - line: The line number on which failure occurred. Defaults to the line number on which this
/// function was called.
/// - column: The column on which failure occurred. Defaults to the column on which this
/// function was called.
public func assertInlineSnapshot<Value>(
of value: @autoclosure () throws -> Value?,
as snapshotting: Snapshotting<Value, String>,
Expand All @@ -39,7 +41,8 @@ import Foundation
timeout: TimeInterval = 5,
syntaxDescriptor: InlineSnapshotSyntaxDescriptor = InlineSnapshotSyntaxDescriptor(),
matches expected: (() -> String)? = nil,
file: StaticString = #filePath,
fileID: StaticString = #fileID,
file filePath: StaticString = #filePath,
function: StaticString = #function,
line: UInt = #line,
column: UInt = #column
Expand Down Expand Up @@ -70,15 +73,29 @@ import Foundation
loaded. If a timeout is unavoidable, consider setting the "timeout" parameter of
"assertInlineSnapshot" to a higher value.
""",
file: file,
line: line
fileID: fileID,
filePath: filePath,
line: line,
column: column
)
return
case .incorrectOrder, .interrupted, .invertedFulfillment:
recordIssue("Couldn't snapshot value", file: file, line: line)
recordIssue(
"Couldn't snapshot value",
fileID: fileID,
filePath: filePath,
line: line,
column: column
)
return
@unknown default:
recordIssue("Couldn't snapshot value", file: file, line: line)
recordIssue(
"Couldn't snapshot value",
fileID: fileID,
filePath: filePath,
line: line,
column: column
)
return
}
}
Expand All @@ -88,7 +105,7 @@ import Foundation
record != .missing || expected != nil
else {
// NB: Write snapshot state before calling `XCTFail` in case `continueAfterFailure = false`
inlineSnapshotState[File(path: file), default: []].append(
inlineSnapshotState[File(path: filePath), default: []].append(
InlineSnapshot(
expected: expected,
actual: actual,
Expand Down Expand Up @@ -119,8 +136,10 @@ import Foundation

Re-run "\(function)" to assert against the newly-recorded snapshot.
""",
file: file,
line: line
fileID: fileID,
filePath: filePath,
line: line,
column: column
)
return
}
Expand All @@ -131,8 +150,10 @@ import Foundation
"""
No expected value to assert against.
""",
file: file,
line: line
fileID: fileID,
filePath: filePath,
line: line,
column: column
)
return
}
Expand All @@ -147,25 +168,34 @@ import Foundation

\(difference.indenting(by: 2))
""",
file: file,
fileID: fileID,
file: filePath,
line: line,
column: column
)
} catch {
recordIssue("Threw error: \(error)", file: file, line: line)
recordIssue(
"Threw error: \(error)",
fileID: fileID,
filePath: filePath,
line: line,
column: column
)
}
}
}
#else
@available(*, unavailable, message: "'assertInlineSnapshot' requires 'swift-syntax' >= 509.0.0")
public func assertInlineSnapshot<Value>(
of value: @autoclosure () throws -> Value,
of value: @autoclosure () throws -> Value?,
as snapshotting: Snapshotting<Value, String>,
message: @autoclosure () -> String = "",
record isRecording: Bool? = nil,
timeout: TimeInterval = 5,
syntaxDescriptor: InlineSnapshotSyntaxDescriptor = InlineSnapshotSyntaxDescriptor(),
matches expected: (() -> String)? = nil,
file: StaticString = #filePath,
fileID: StaticString = #fileID,
file filePath: StaticString = #filePath,
function: StaticString = #function,
line: UInt = #line,
column: UInt = #column
Expand Down Expand Up @@ -242,20 +272,23 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
///
/// - Parameters:
/// - message: An optional description of the assertion, for inclusion in test results.
/// - file: The file where the assertion occurs. The default is the filename of the test case
/// where you call `assertInlineSnapshot`.
/// - line: The line where the assertion occurs. The default is the line number where you call
/// `assertInlineSnapshot`.
/// - column: The column where the assertion occurs. The default is the column where you call
/// `assertInlineSnapshot`.
/// - fileID: The file ID in which failure occurred. Defaults to the file ID of the test case
/// in which this function was called.
/// - file: The file in which failure occurred. Defaults to the file path of the test case in
/// which this function was called.
/// - line: The line number on which failure occurred. Defaults to the line number on which
/// this function was called.
/// - column: The column on which failure occurred. Defaults to the column on which this
/// function was called.
public func fail(
_ message: @autoclosure () -> String = "",
file: StaticString,
fileID: StaticString,
file filePath: StaticString,
line: UInt,
column: UInt
) {
var trailingClosureLine: Int?
if let testSource = try? testSource(file: File(path: file)) {
if let testSource = try? testSource(file: File(path: filePath)) {
let visitor = SnapshotVisitor(
functionCallLine: Int(line),
functionCallColumn: Int(column),
Expand All @@ -267,8 +300,10 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
}
recordIssue(
message(),
file: file,
line: trailingClosureLine.map(UInt.init) ?? line
fileID: fileID,
filePath: filePath,
line: trailingClosureLine.map(UInt.init) ?? line,
column: column
)
}

Expand All @@ -279,7 +314,8 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
@available(*, unavailable, message: "'assertInlineSnapshot' requires 'swift-syntax' >= 509.0.0")
public func fail(
_ message: @autoclosure () -> String = "",
file: StaticString,
fileID: StaticString,
file filePath: StaticString,
line: UInt,
column: UInt
) {
Expand Down
70 changes: 51 additions & 19 deletions Sources/SnapshotTesting/AssertSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,48 @@ public var _record: SnapshotTestingConfiguration.Record = {
/// - name: An optional description of the snapshot.
/// - recording: Whether or not to record a new reference.
/// - 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
/// - fileID: The file ID in which failure occurred. Defaults to the file ID of the test case in
/// which this function was called.
/// - file: The file in which failure occurred. Defaults to the file path of the test case in
/// which this function was called.
/// - 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.
/// - line: The line number on which failure occurred. Defaults to the line number on which this
/// function was called.
/// - column: The column on which failure occurred. Defaults to the column on which this function
/// was called.
public func assertSnapshot<Value, Format>(
of value: @autoclosure () throws -> Value,
as snapshotting: Snapshotting<Value, Format>,
named name: String? = nil,
record recording: Bool? = nil,
timeout: TimeInterval = 5,
file: StaticString = #file,
fileID: StaticString = #fileID,
file filePath: StaticString = #filePath,
testName: String = #function,
line: UInt = #line
line: UInt = #line,
column: UInt = #column
) {
let failure = verifySnapshot(
of: try value(),
as: snapshotting,
named: name,
record: recording,
timeout: timeout,
file: file,
fileID: fileID,
file: filePath,
testName: testName,
line: line
line: line,
column: column
)
guard let message = failure else { return }
recordIssue(message, file: file, line: line)
recordIssue(
message,
fileID: fileID,
filePath: filePath,
line: line,
column: column
)
}

/// Asserts that a given value matches references on disk.
Expand All @@ -83,20 +97,26 @@ public func assertSnapshot<Value, Format>(
/// comparing values.
/// - recording: Whether or not to record a new reference.
/// - 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
/// - fileID: The file ID in which failure occurred. Defaults to the file ID of the test case in
/// which this function was called.
/// - file: The file in which failure occurred. Defaults to the file path of the test case in
/// which this function was called.
/// - 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.
/// - line: The line number on which failure occurred. Defaults to the line number on which this
/// function was called.
/// - column: The column on which failure occurred. Defaults to the column on which this function
/// was called.
public func assertSnapshots<Value, Format>(
of value: @autoclosure () throws -> Value,
as strategies: [String: Snapshotting<Value, Format>],
record recording: Bool? = nil,
timeout: TimeInterval = 5,
file: StaticString = #file,
fileID: StaticString = #fileID,
file filePath: StaticString = #filePath,
testName: String = #function,
line: UInt = #line
line: UInt = #line,
column: UInt = #column
) {
try? strategies.forEach { name, strategy in
assertSnapshot(
Expand All @@ -105,9 +125,11 @@ public func assertSnapshots<Value, Format>(
named: name,
record: recording,
timeout: timeout,
file: file,
fileID: fileID,
file: filePath,
testName: testName,
line: line
line: line,
column: column
)
}
}
Expand All @@ -119,30 +141,38 @@ public func assertSnapshots<Value, Format>(
/// - strategies: An array of strategies for serializing, deserializing, and comparing values.
/// - recording: Whether or not to record a new reference.
/// - 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
/// - fileID: The file ID in which failure occurred. Defaults to the file ID of the test case in
/// which this function was called.
/// - file: The file in which failure occurred. Defaults to the file path of the test case in
/// which this function was called.
/// - 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.
/// - line: The line number on which failure occurred. Defaults to the line number on which this
/// function was called.
/// - column: The column on which failure occurred. Defaults to the column on which this function
/// was called.
public func assertSnapshots<Value, Format>(
of value: @autoclosure () throws -> Value,
as strategies: [Snapshotting<Value, Format>],
record recording: Bool? = nil,
timeout: TimeInterval = 5,
file: StaticString = #file,
fileID: StaticString = #fileID,
file filePath: StaticString = #filePath,
testName: String = #function,
line: UInt = #line
line: UInt = #line,
column: UInt = #column
) {
try? strategies.forEach { strategy in
assertSnapshot(
of: try value(),
as: strategy,
record: recording,
timeout: timeout,
file: file,
fileID: fileID,
file: filePath,
testName: testName,
line: line
line: line,
column: column
)
}
}
Expand Down Expand Up @@ -205,9 +235,11 @@ public func verifySnapshot<Value, Format>(
record recording: Bool? = nil,
snapshotDirectory: String? = nil,
timeout: TimeInterval = 5,
file: StaticString = #file,
fileID: StaticString = #fileID,
file filePath: StaticString = #file,
testName: String = #function,
line: UInt = #line
line: UInt = #line,
column: UInt = #column
) -> String? {
CleanCounterBetweenTestCases.registerIfNeeded()

Expand All @@ -217,7 +249,7 @@ public func verifySnapshot<Value, Format>(
?? _record
return withSnapshotTesting(record: record) { () -> String? in
do {
let fileUrl = URL(fileURLWithPath: "\(file)", isDirectory: false)
let fileUrl = URL(fileURLWithPath: "\(filePath)", isDirectory: false)
let fileName = fileUrl.deletingPathExtension().lastPathComponent

let snapshotDirectoryUrl =
Expand Down
Loading
Loading