Skip to content

Console output should prefix messages with "Suite" instead of "Test" for suites #184

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

Merged
merged 2 commits into from
Jan 16, 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
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@ extension Event.HumanReadableOutputRecorder {
}
}

/// Generate a title for the specified test (either "Test" or "Suite"),
/// capitalized and suitable for use as the leading word of a human-readable
/// message string.
///
/// - Parameters:
/// - test: The test to generate a description for, if any.
///
/// - Returns: A human-readable title for the specified test. Defaults to "Test"
/// if `test` is `nil`.
private func _capitalizedTitle(for test: Test?) -> String {
test?.isSuite == true ? "Suite" : "Test"
}

extension Test.Case {
/// The arguments of this test case, formatted for presentation, prefixed by
/// their corresponding parameter label when available.
Expand Down Expand Up @@ -224,7 +237,7 @@ extension Event.HumanReadableOutputRecorder {
return [
Message(
symbol: .default,
stringValue: "Test \(testName) started."
stringValue: "\(_capitalizedTitle(for: test)) \(testName) started."
)
]

Expand All @@ -239,14 +252,14 @@ extension Event.HumanReadableOutputRecorder {
CollectionOfOne(
Message(
symbol: .fail,
stringValue: "Test \(testName) failed after \(duration)\(issues.description)."
stringValue: "\(_capitalizedTitle(for: test)) \(testName) failed after \(duration)\(issues.description)."
)
) + _formattedComments(for: test)
} else {
[
Message(
symbol: .pass(knownIssueCount: issues.knownIssueCount),
stringValue: "Test \(testName) passed after \(duration)\(issues.description)."
stringValue: "\(_capitalizedTitle(for: test)) \(testName) passed after \(duration)\(issues.description)."
)
]
}
Expand All @@ -262,11 +275,11 @@ extension Event.HumanReadableOutputRecorder {
}
return if let comment = skipInfo.comment {
[
Message(symbol: .skip, stringValue: "Test \(testName) skipped: \"\(comment.rawValue)\"")
Message(symbol: .skip, stringValue: "\(_capitalizedTitle(for: test)) \(testName) skipped: \"\(comment.rawValue)\"")
]
} else {
[
Message(symbol: .skip, stringValue: "Test \(testName) skipped.")
Message(symbol: .skip, stringValue: "\(_capitalizedTitle(for: test)) \(testName) skipped.")
]
}

Expand Down Expand Up @@ -318,12 +331,12 @@ extension Event.HumanReadableOutputRecorder {
let primaryMessage: Message = if parameterCount == 0 {
Message(
symbol: symbol,
stringValue: "Test \(testName) recorded a\(known) issue\(atSourceLocation): \(issue.kind)"
stringValue: "\(_capitalizedTitle(for: test)) \(testName) recorded a\(known) issue\(atSourceLocation): \(issue.kind)"
)
} else {
Message(
symbol: symbol,
stringValue: "Test \(testName) recorded a\(known) issue with \(parameterCount.counting("argument")) \(labeledArguments)\(atSourceLocation): \(issue.kind)"
stringValue: "\(_capitalizedTitle(for: test)) \(testName) recorded a\(known) issue with \(parameterCount.counting("argument")) \(labeledArguments)\(atSourceLocation): \(issue.kind)"
)
}
return CollectionOfOne(primaryMessage) + additionalMessages
Expand Down
47 changes: 42 additions & 5 deletions Tests/TestingTests/EventRecorderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,53 @@ struct EventRecorderTests {
}

#if !os(Windows)
@available(_regexAPI, *)
@Test(
"Titles of messages ('Test' vs. 'Suite') are determined correctly",
arguments: [
("f()", false),
("g()", false),
("PredictablyFailingTests", true),
]
)
func messageTitles(testName: String, isSuite: Bool) async throws {
let stream = Stream()

var configuration = Configuration()
let eventRecorder = Event.ConsoleOutputRecorder(writingUsing: stream.write)
configuration.eventHandler = { event, context in
eventRecorder.record(event, in: context)
}

await runTest(for: PredictablyFailingTests.self, configuration: configuration)

let buffer = stream.buffer.wrappedValue
if testsWithSignificantIOAreEnabled {
print(buffer, terminator: "")
}

let testFailureRegex = Regex {
One(.anyGraphemeCluster)
" \(isSuite ? "Suite" : "Test") \(testName) started."
}
#expect(
try buffer
.split(whereSeparator: \.isNewline)
.compactMap(testFailureRegex.wholeMatch(in:))
.first != nil
)
}

@available(_regexAPI, *)
@Test(
"Issue counts are summed correctly on test end",
arguments: [
"f()": (5, 3),
"g()": (2, 1),
"PredictablyFailingTests": (7, 4)
("f()", false, (total: 5, expected: 3)),
("g()", false, (total: 2, expected: 1)),
("PredictablyFailingTests", true, (total: 7, expected: 4)),
]
)
func issueCountSummingAtTestEnd(testName: String, issueCount: (total: Int, expected: Int)) async throws {
func issueCountSummingAtTestEnd(testName: String, isSuite: Bool, issueCount: (total: Int, expected: Int)) async throws {
let stream = Stream()

var configuration = Configuration()
Expand All @@ -117,7 +154,7 @@ struct EventRecorderTests {

let testFailureRegex = Regex {
One(.anyGraphemeCluster)
" Test \(testName) failed"
" \(isSuite ? "Suite" : "Test") \(testName) failed "
ZeroOrMore(.any)
" with "
Capture { OneOrMore(.digit) } transform: { Int($0) }
Expand Down