Skip to content

Commit 61227db

Browse files
authored
Console output should prefix messages with "Suite" instead of "Test" for suites (#184)
* Console output should prefix messages with "Suite" instead of "Test" for suites Resolves rdar://120962053 * Change to a free function
1 parent e981e3e commit 61227db

File tree

2 files changed

+63
-13
lines changed

2 files changed

+63
-13
lines changed

Sources/Testing/Events/Recorder/Event.HumanReadableOutputRecorder.swift

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,19 @@ extension Event.HumanReadableOutputRecorder {
142142
}
143143
}
144144

145+
/// Generate a title for the specified test (either "Test" or "Suite"),
146+
/// capitalized and suitable for use as the leading word of a human-readable
147+
/// message string.
148+
///
149+
/// - Parameters:
150+
/// - test: The test to generate a description for, if any.
151+
///
152+
/// - Returns: A human-readable title for the specified test. Defaults to "Test"
153+
/// if `test` is `nil`.
154+
private func _capitalizedTitle(for test: Test?) -> String {
155+
test?.isSuite == true ? "Suite" : "Test"
156+
}
157+
145158
extension Test.Case {
146159
/// The arguments of this test case, formatted for presentation, prefixed by
147160
/// their corresponding parameter label when available.
@@ -224,7 +237,7 @@ extension Event.HumanReadableOutputRecorder {
224237
return [
225238
Message(
226239
symbol: .default,
227-
stringValue: "Test \(testName) started."
240+
stringValue: "\(_capitalizedTitle(for: test)) \(testName) started."
228241
)
229242
]
230243

@@ -239,14 +252,14 @@ extension Event.HumanReadableOutputRecorder {
239252
CollectionOfOne(
240253
Message(
241254
symbol: .fail,
242-
stringValue: "Test \(testName) failed after \(duration)\(issues.description)."
255+
stringValue: "\(_capitalizedTitle(for: test)) \(testName) failed after \(duration)\(issues.description)."
243256
)
244257
) + _formattedComments(for: test)
245258
} else {
246259
[
247260
Message(
248261
symbol: .pass(knownIssueCount: issues.knownIssueCount),
249-
stringValue: "Test \(testName) passed after \(duration)\(issues.description)."
262+
stringValue: "\(_capitalizedTitle(for: test)) \(testName) passed after \(duration)\(issues.description)."
250263
)
251264
]
252265
}
@@ -262,11 +275,11 @@ extension Event.HumanReadableOutputRecorder {
262275
}
263276
return if let comment = skipInfo.comment {
264277
[
265-
Message(symbol: .skip, stringValue: "Test \(testName) skipped: \"\(comment.rawValue)\"")
278+
Message(symbol: .skip, stringValue: "\(_capitalizedTitle(for: test)) \(testName) skipped: \"\(comment.rawValue)\"")
266279
]
267280
} else {
268281
[
269-
Message(symbol: .skip, stringValue: "Test \(testName) skipped.")
282+
Message(symbol: .skip, stringValue: "\(_capitalizedTitle(for: test)) \(testName) skipped.")
270283
]
271284
}
272285

@@ -318,12 +331,12 @@ extension Event.HumanReadableOutputRecorder {
318331
let primaryMessage: Message = if parameterCount == 0 {
319332
Message(
320333
symbol: symbol,
321-
stringValue: "Test \(testName) recorded a\(known) issue\(atSourceLocation): \(issue.kind)"
334+
stringValue: "\(_capitalizedTitle(for: test)) \(testName) recorded a\(known) issue\(atSourceLocation): \(issue.kind)"
322335
)
323336
} else {
324337
Message(
325338
symbol: symbol,
326-
stringValue: "Test \(testName) recorded a\(known) issue with \(parameterCount.counting("argument")) \(labeledArguments)\(atSourceLocation): \(issue.kind)"
339+
stringValue: "\(_capitalizedTitle(for: test)) \(testName) recorded a\(known) issue with \(parameterCount.counting("argument")) \(labeledArguments)\(atSourceLocation): \(issue.kind)"
327340
)
328341
}
329342
return CollectionOfOne(primaryMessage) + additionalMessages

Tests/TestingTests/EventRecorderTests.swift

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ struct EventRecorderTests {
9292
#if !os(Windows)
9393
@available(_regexAPI, *)
9494
@Test(
95-
"Issue counts are summed correctly on test end",
95+
"Titles of messages ('Test' vs. 'Suite') are determined correctly",
9696
arguments: [
97-
"f()": (5, 3),
98-
"g()": (2, 1),
99-
"PredictablyFailingTests": (7, 4)
97+
("f()", false),
98+
("g()", false),
99+
("PredictablyFailingTests", true),
100100
]
101101
)
102-
func issueCountSummingAtTestEnd(testName: String, issueCount: (total: Int, expected: Int)) async throws {
102+
func messageTitles(testName: String, isSuite: Bool) async throws {
103103
let stream = Stream()
104104

105105
var configuration = Configuration()
@@ -117,7 +117,44 @@ struct EventRecorderTests {
117117

118118
let testFailureRegex = Regex {
119119
One(.anyGraphemeCluster)
120-
" Test \(testName) failed"
120+
" \(isSuite ? "Suite" : "Test") \(testName) started."
121+
}
122+
#expect(
123+
try buffer
124+
.split(whereSeparator: \.isNewline)
125+
.compactMap(testFailureRegex.wholeMatch(in:))
126+
.first != nil
127+
)
128+
}
129+
130+
@available(_regexAPI, *)
131+
@Test(
132+
"Issue counts are summed correctly on test end",
133+
arguments: [
134+
("f()", false, (total: 5, expected: 3)),
135+
("g()", false, (total: 2, expected: 1)),
136+
("PredictablyFailingTests", true, (total: 7, expected: 4)),
137+
]
138+
)
139+
func issueCountSummingAtTestEnd(testName: String, isSuite: Bool, issueCount: (total: Int, expected: Int)) async throws {
140+
let stream = Stream()
141+
142+
var configuration = Configuration()
143+
let eventRecorder = Event.ConsoleOutputRecorder(writingUsing: stream.write)
144+
configuration.eventHandler = { event, context in
145+
eventRecorder.record(event, in: context)
146+
}
147+
148+
await runTest(for: PredictablyFailingTests.self, configuration: configuration)
149+
150+
let buffer = stream.buffer.wrappedValue
151+
if testsWithSignificantIOAreEnabled {
152+
print(buffer, terminator: "")
153+
}
154+
155+
let testFailureRegex = Regex {
156+
One(.anyGraphemeCluster)
157+
" \(isSuite ? "Suite" : "Test") \(testName) failed "
121158
ZeroOrMore(.any)
122159
" with "
123160
Capture { OneOrMore(.digit) } transform: { Int($0) }

0 commit comments

Comments
 (0)