Skip to content
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
5 changes: 2 additions & 3 deletions Sources/Testing/Running/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,10 @@ public struct Configuration: Sendable {

/// The maximum number of times the test run should iterate.
///
/// - Precondition: The value of this property must be greater than or equal
/// to `1`.
/// - Precondition: The value of this property must be greater than `0`.
public var maximumIterationCount: Int {
willSet {
precondition(newValue >= 1, "Test runs must iterate at least once.")
precondition(newValue > 0, "Test runs must iterate at least once (maximumIterationCount was \(newValue)).")
}
}

Expand Down
44 changes: 37 additions & 7 deletions Sources/Testing/SourceAttribution/SourceLocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@
public struct SourceLocation: Sendable {
/// The file ID of the source file.
///
/// - Precondition: The value of this property must not be empty and must be
/// formatted as described in the documentation for the
/// [`#fileID`](https://developer.apple.com/documentation/swift/fileID()).
/// macro in the Swift standard library.
///
/// ## See Also
///
/// - ``moduleName``
/// - ``fileName``
public var fileID: String {
didSet {
precondition(!fileID.isEmpty)
precondition(fileID.contains("/"))
willSet {
precondition(!newValue.isEmpty, "SourceLocation.fileID must not be empty (was \(newValue))")
precondition(newValue.contains("/"), "SourceLocation.fileID must be a well-formed file ID (was \(newValue))")
}
}

Expand Down Expand Up @@ -74,20 +79,45 @@ public struct SourceLocation: Sendable {
public var _filePath: String

/// The line in the source file.
///
/// - Precondition: The value of this property must be greater than `0`.
public var line: Int {
didSet {
precondition(line > 0)
willSet {
precondition(newValue > 0, "SourceLocation.line must be greater than 0 (was \(newValue))")
}
}

/// The column in the source file.
///
/// - Precondition: The value of this property must be greater than `0`.
public var column: Int {
didSet {
precondition(column > 0)
willSet {
precondition(newValue > 0, "SourceLocation.column must be greater than 0 (was \(newValue))")
}
}

/// Initialize an instance of this type with the specified location details.
///
/// - Parameters:
/// - fileID: The file ID of the source file, using the format described in
/// the documentation for the
/// [`#fileID`](https://developer.apple.com/documentation/swift/fileID())
/// macro in the Swift standard library.
/// - filePath: The path to the source file.
/// - line: The line in the source file. Must be greater than `0`.
/// - column: The column in the source file. Must be greater than `0`.
///
/// - Precondition: `fileID` must not be empty and must be formatted as
/// described in the documentation for
/// [`#fileID`](https://developer.apple.com/documentation/swift/fileID()).
/// - Precondition: `line` must be greater than `0`.
/// - Precondition: `column` must be greater than `0`.
public init(fileID: String, filePath: String, line: Int, column: Int) {
precondition(!fileID.isEmpty, "SourceLocation.fileID must not be empty (was \(fileID))")
precondition(fileID.contains("/"), "SourceLocation.fileID must be a well-formed file ID (was \(fileID))")
precondition(line > 0, "SourceLocation.line must be greater than 0 (was \(line))")
precondition(column > 0, "SourceLocation.column must be greater than 0 (was \(column))")

self.fileID = fileID
self._filePath = filePath
self.line = line
Expand Down
12 changes: 6 additions & 6 deletions Tests/TestingTests/IssueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1489,8 +1489,8 @@ struct IssueCodingTests {

@Test func sourceLocationPropertyGetter() throws {
let sourceLocation = SourceLocation(
fileID: "fileID",
filePath: "filePath",
fileID: "M/file.swift",
filePath: "M/file.swift",
line: 13,
column: 42
)
Expand All @@ -1509,8 +1509,8 @@ struct IssueCodingTests {

@Test func sourceLocationPropertySetter() throws {
let initialSourceLocation = SourceLocation(
fileID: "fileID",
filePath: "filePath",
fileID: "M/file.swift",
filePath: "file.swift",
line: 13,
column: 42
)
Expand All @@ -1523,8 +1523,8 @@ struct IssueCodingTests {
let issue = Issue(kind: .apiMisused, sourceContext: sourceContext)

let updatedSourceLocation = SourceLocation(
fileID: "fileID2",
filePath: "filePath2",
fileID: "M/file2.swift",
filePath: "file2.swift",
line: 14,
column: 43
)
Expand Down
43 changes: 28 additions & 15 deletions Tests/TestingTests/SourceLocationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,6 @@ struct SourceLocationTests {
#expect(sourceLocation.fileName == "D.swift")
}


#if !SWT_NO_EXIT_TESTS
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just moved this down without modification, to consolidate several exit tests together

@Test("SourceLocation.fileID property must be well-formed")
func sourceLocationFileIDWellFormed() async {
await #expect(exitsWith: .failure) {
var sourceLocation = #_sourceLocation
sourceLocation.fileID = ""
}
await #expect(exitsWith: .failure) {
var sourceLocation = #_sourceLocation
sourceLocation.fileID = "ABC"
}
}
#endif

@Test("SourceLocation.line and .column properties")
func sourceLocationLineAndColumn() {
var sourceLocation = #_sourceLocation
Expand All @@ -81,6 +66,34 @@ struct SourceLocationTests {
}

#if !SWT_NO_EXIT_TESTS
@Test("SourceLocation.init requires well-formed arguments")
func sourceLocationInitPreconditions() async {
await #expect(exitsWith: .failure, "Empty fileID") {
_ = SourceLocation(fileID: "", filePath: "", line: 1, column: 1)
}
await #expect(exitsWith: .failure, "Invalid fileID") {
_ = SourceLocation(fileID: "B.swift", filePath: "", line: 1, column: 1)
}
await #expect(exitsWith: .failure, "Zero line") {
_ = SourceLocation(fileID: "A/B.swift", filePath: "", line: 0, column: 1)
}
await #expect(exitsWith: .failure, "Zero column") {
_ = SourceLocation(fileID: "A/B.swift", filePath: "", line: 1, column: 0)
}
}

@Test("SourceLocation.fileID property must be well-formed")
func sourceLocationFileIDWellFormed() async {
await #expect(exitsWith: .failure) {
var sourceLocation = #_sourceLocation
sourceLocation.fileID = ""
}
await #expect(exitsWith: .failure) {
var sourceLocation = #_sourceLocation
sourceLocation.fileID = "ABC"
}
}

@Test("SourceLocation.line and column properties must be positive")
func sourceLocationLineAndColumnPositive() async {
await #expect(exitsWith: .failure) {
Expand Down