Skip to content

Commit 526ef76

Browse files
authored
Miscellaneous bookkeeping/cleanup of _TestDiscovery. (#1055)
This PR has a few minor changes: - The default implementation of `DiscoverableAsTestContent._testContentTypeNameHint` is not present if `SWT_NO_LEGACY_TEST_DISCOVERY` is defined; - Fixed the precondition in `TestContentKind.init(stringLiteral:)` so it doesn't call `utf8CodeUnitCount` on single-character static strings; - The implementation of `TestContentKind._fourCCValue` uses `isprint()` to check if the kind value looks like a FourCC rather than calling ICU-based functions; - Fixed a type in a comment on `SectionBounds.Kind.segmentAndSectionName`; and - Simplified the implementation of `TestContentKind.commentRepresentation` (in the macro target). ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 1eba9c0 commit 526ef76

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

Sources/TestingMacros/Support/TestContentGeneration.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@ enum TestContentKind: UInt32 {
3030
/// This kind value as a comment (`/* 'abcd' */`) if it looks like it might be
3131
/// a [FourCC](https://en.wikipedia.org/wiki/FourCC) value, or `nil` if not.
3232
var commentRepresentation: Trivia {
33-
switch self {
34-
case .testDeclaration:
35-
.blockComment("/* 'test' */")
36-
case .exitTest:
37-
.blockComment("/* 'exit' */")
33+
let stringValue = withUnsafeBytes(of: self.rawValue.bigEndian) { bytes in
34+
String(decoding: bytes, as: Unicode.ASCII.self)
3835
}
36+
return .blockComment("/* '\(stringValue)' */")
3937
}
4038
}
4139

Sources/_TestDiscovery/DiscoverableAsTestContent.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ public protocol DiscoverableAsTestContent: Sendable, ~Copyable {
4848
#endif
4949
}
5050

51+
#if !SWT_NO_LEGACY_TEST_DISCOVERY
5152
extension DiscoverableAsTestContent where Self: ~Copyable {
5253
public static var _testContentTypeNameHint: String {
5354
"__🟡$"
5455
}
5556
}
57+
#endif

Sources/_TestDiscovery/SectionBounds.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ extension SectionBounds.Kind {
5252
/// The Mach-O segment and section name for this instance as a pair of
5353
/// null-terminated UTF-8 C strings and pass them to a function.
5454
///
55-
/// The values of this property within this function are instances of
56-
/// `StaticString` rather than `String` because the latter's inner storage is
57-
/// sometimes Objective-C-backed and touching it here can cause a recursive
58-
/// access to an internal libobjc lock, whereas `StaticString`'s internal
59-
/// storage is immediately available.
55+
/// The values of this property are instances of `StaticString` rather than
56+
/// `String` because the latter's inner storage is sometimes backed by
57+
/// Objective-C and touching it here can cause a recursive access to an
58+
/// internal libobjc lock, whereas `StaticString`'s internal storage is
59+
/// immediately available.
6060
fileprivate var segmentAndSectionName: (segmentName: StaticString, sectionName: StaticString) {
6161
switch self {
6262
case .testContent:

Sources/_TestDiscovery/TestContentKind.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ extension TestContentKind: Codable {}
6060

6161
extension TestContentKind: ExpressibleByStringLiteral, ExpressibleByIntegerLiteral {
6262
@inlinable public init(stringLiteral stringValue: StaticString) {
63-
precondition(stringValue.utf8CodeUnitCount == MemoryLayout<UInt32>.stride, #""\#(stringValue)".utf8CodeUnitCount = \#(stringValue.utf8CodeUnitCount), expected \#(MemoryLayout<UInt32>.stride)"#)
6463
let rawValue = stringValue.withUTF8Buffer { stringValue in
64+
precondition(stringValue.count == MemoryLayout<UInt32>.stride, #""\#(stringValue)".utf8CodeUnitCount = \#(stringValue.count), expected \#(MemoryLayout<UInt32>.stride)"#)
6565
let bigEndian = UnsafeRawBufferPointer(stringValue).loadUnaligned(as: UInt32.self)
6666
return UInt32(bigEndian: bigEndian)
6767
}
@@ -81,12 +81,11 @@ extension TestContentKind: CustomStringConvertible {
8181
/// value, or `nil` if not.
8282
private var _fourCCValue: String? {
8383
withUnsafeBytes(of: rawValue.bigEndian) { bytes in
84-
if bytes.allSatisfy(Unicode.ASCII.isASCII) {
85-
let characters = String(decoding: bytes, as: Unicode.ASCII.self)
86-
let allAlphanumeric = characters.allSatisfy { $0.isLetter || $0.isWholeNumber }
87-
if allAlphanumeric {
88-
return characters
89-
}
84+
let allPrintableASCII = bytes.allSatisfy { byte in
85+
Unicode.ASCII.isASCII(byte) && 0 != isprint(CInt(byte))
86+
}
87+
if allPrintableASCII {
88+
return String(decoding: bytes, as: Unicode.ASCII.self)
9089
}
9190
return nil
9291
}

Sources/_TestingInternals/include/Includes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
///
2727
/// - Note: Avoid including headers that aren't actually used.
2828

29+
#include <ctype.h>
2930
#include <errno.h>
3031
#include <inttypes.h>
3132
/// Guard against including `signal.h` on WASI. The `signal.h` header file

0 commit comments

Comments
 (0)