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
19 changes: 16 additions & 3 deletions Sources/Testing/Parameterization/TypeInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public struct TypeInfo: Sendable {
///
/// - Parameters:
/// - type: The type which this instance should describe.
init(describing type: any ~Copyable.Type) {
init(describing type: (some ~Copyable).Type) {
_kind = .type(type)
}

Expand All @@ -88,8 +88,21 @@ public struct TypeInfo: Sendable {
///
/// - Parameters:
/// - value: The value whose type this instance should describe.
init(describingTypeOf value: Any) {
self.init(describing: Swift.type(of: value))
init(describingTypeOf value: some Any) {
#if !hasFeature(Embedded)
let value = value as Any
#endif
let type = Swift.type(of: value)
self.init(describing: type)
}

/// Initialize an instance of this type describing the type of the specified
/// value.
///
/// - Parameters:
/// - value: The value whose type this instance should describe.
init<T>(describingTypeOf value: borrowing T) where T: ~Copyable {
self.init(describing: T.self)
}
}

Expand Down
36 changes: 18 additions & 18 deletions Sources/Testing/Test+Macro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,16 @@ extension Test {
///
/// - Warning: This function is used to implement the `@Test` macro. Do not
/// call it directly.
public static func __function(
public static func __function<S>(
named testFunctionName: String,
in containingType: (any ~Copyable.Type)?,
in containingType: S.Type?,
Copy link
Contributor

Choose a reason for hiding this comment

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

S here is a shorthand for Suite?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah. As a bad habit from C++, I tend to spell my generic types with one letter unless it's ambiguous.

xcTestCompatibleSelector: __XCTestCompatibleSelector?,
displayName: String? = nil,
traits: [any TestTrait],
sourceLocation: SourceLocation,
parameters: [__Parameter] = [],
testFunction: @escaping @Sendable () async throws -> Void
) -> Self {
) -> Self where S: ~Copyable {
// Don't use Optional.map here due to a miscompile/crash. Expand out to an
// if expression instead. SEE: rdar://134280902
let containingTypeInfo: TypeInfo? = if let containingType {
Expand Down Expand Up @@ -241,17 +241,17 @@ extension Test {
///
/// - Warning: This function is used to implement the `@Test` macro. Do not
/// call it directly.
public static func __function<C>(
public static func __function<S, C>(
named testFunctionName: String,
in containingType: (any ~Copyable.Type)?,
in containingType: S.Type?,
xcTestCompatibleSelector: __XCTestCompatibleSelector?,
displayName: String? = nil,
traits: [any TestTrait],
arguments collection: @escaping @Sendable () async throws -> C,
sourceLocation: SourceLocation,
parameters paramTuples: [__Parameter],
testFunction: @escaping @Sendable (C.Element) async throws -> Void
) -> Self where C: Collection & Sendable, C.Element: Sendable {
) -> Self where S: ~Copyable, C: Collection & Sendable, C.Element: Sendable {
let containingTypeInfo: TypeInfo? = if let containingType {
TypeInfo(describing: containingType)
} else {
Expand Down Expand Up @@ -388,17 +388,17 @@ extension Test {
///
/// - Warning: This function is used to implement the `@Test` macro. Do not
/// call it directly.
public static func __function<C1, C2>(
public static func __function<S, C1, C2>(
named testFunctionName: String,
in containingType: (any ~Copyable.Type)?,
in containingType: S.Type?,
xcTestCompatibleSelector: __XCTestCompatibleSelector?,
displayName: String? = nil,
traits: [any TestTrait],
arguments collection1: @escaping @Sendable () async throws -> C1, _ collection2: @escaping @Sendable () async throws -> C2,
sourceLocation: SourceLocation,
parameters paramTuples: [__Parameter],
testFunction: @escaping @Sendable (C1.Element, C2.Element) async throws -> Void
) -> Self where C1: Collection & Sendable, C1.Element: Sendable, C2: Collection & Sendable, C2.Element: Sendable {
) -> Self where S: ~Copyable, C1: Collection & Sendable, C1.Element: Sendable, C2: Collection & Sendable, C2.Element: Sendable {
let containingTypeInfo: TypeInfo? = if let containingType {
TypeInfo(describing: containingType)
} else {
Expand All @@ -416,17 +416,17 @@ extension Test {
///
/// - Warning: This function is used to implement the `@Test` macro. Do not
/// call it directly.
public static func __function<C, E1, E2>(
public static func __function<S, C, E1, E2>(
named testFunctionName: String,
in containingType: (any ~Copyable.Type)?,
in containingType: S.Type?,
xcTestCompatibleSelector: __XCTestCompatibleSelector?,
displayName: String? = nil,
traits: [any TestTrait],
arguments collection: @escaping @Sendable () async throws -> C,
sourceLocation: SourceLocation,
parameters paramTuples: [__Parameter],
testFunction: @escaping @Sendable ((E1, E2)) async throws -> Void
) -> Self where C: Collection & Sendable, C.Element == (E1, E2), E1: Sendable, E2: Sendable {
) -> Self where S: ~Copyable, C: Collection & Sendable, C.Element == (E1, E2), E1: Sendable, E2: Sendable {
let containingTypeInfo: TypeInfo? = if let containingType {
TypeInfo(describing: containingType)
} else {
Expand All @@ -447,17 +447,17 @@ extension Test {
///
/// - Warning: This function is used to implement the `@Test` macro. Do not
/// call it directly.
public static func __function<Key, Value>(
public static func __function<S, Key, Value>(
named testFunctionName: String,
in containingType: (any ~Copyable.Type)?,
in containingType: S.Type?,
xcTestCompatibleSelector: __XCTestCompatibleSelector?,
displayName: String? = nil,
traits: [any TestTrait],
arguments dictionary: @escaping @Sendable () async throws -> Dictionary<Key, Value>,
sourceLocation: SourceLocation,
parameters paramTuples: [__Parameter],
testFunction: @escaping @Sendable ((Key, Value)) async throws -> Void
) -> Self where Key: Sendable, Value: Sendable {
) -> Self where S: ~Copyable, Key: Sendable, Value: Sendable {
let containingTypeInfo: TypeInfo? = if let containingType {
TypeInfo(describing: containingType)
} else {
Expand All @@ -472,17 +472,17 @@ extension Test {
///
/// - Warning: This function is used to implement the `@Test` macro. Do not
/// call it directly.
public static func __function<C1, C2>(
public static func __function<S, C1, C2>(
named testFunctionName: String,
in containingType: (any ~Copyable.Type)?,
in containingType: S.Type?,
xcTestCompatibleSelector: __XCTestCompatibleSelector?,
displayName: String? = nil,
traits: [any TestTrait],
arguments zippedCollections: @escaping @Sendable () async throws -> Zip2Sequence<C1, C2>,
sourceLocation: SourceLocation,
parameters paramTuples: [__Parameter],
testFunction: @escaping @Sendable (C1.Element, C2.Element) async throws -> Void
) -> Self where C1: Collection & Sendable, C1.Element: Sendable, C2: Collection & Sendable, C2.Element: Sendable {
) -> Self where S: ~Copyable, C1: Collection & Sendable, C1.Element: Sendable, C2: Collection & Sendable, C2.Element: Sendable {
let containingTypeInfo: TypeInfo? = if let containingType {
TypeInfo(describing: containingType)
} else {
Expand Down
2 changes: 1 addition & 1 deletion Sources/TestingMacros/TestDeclarationMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public struct TestDeclarationMacro: PeerMacro, Sendable {

// Get the name of the type containing the function for passing to the test
// factory function later.
let typeNameExpr: ExprSyntax = typeName.map { "\($0).self" } ?? "nil"
let typeNameExpr: ExprSyntax = typeName.map { "\($0).self" } ?? "nil as Swift.Never.Type?"

if typeName != nil, let genericGuardDecl = makeGenericGuardDecl(guardingAgainst: functionDecl, in: context) {
result.append(genericGuardDecl)
Expand Down
2 changes: 1 addition & 1 deletion Tests/TestingTests/MiscellaneousTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ struct MiscellaneousTests {
let line = 12345
let column = 67890
let sourceLocation = SourceLocation(fileID: fileID, filePath: filePath, line: line, column: column)
let testFunction = Test.__function(named: "myTestFunction()", in: nil, xcTestCompatibleSelector: nil, displayName: nil, traits: [], sourceLocation: sourceLocation) {}
let testFunction = Test.__function(named: "myTestFunction()", in: nil as Never.Type?, xcTestCompatibleSelector: nil, displayName: nil, traits: [], sourceLocation: sourceLocation) {}
#expect(String(describing: testFunction.id) == "Module.myTestFunction()/Y.swift:12345:67890")
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/TestingTests/SwiftPMTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ struct SwiftPMTests {
@Test("Unsupported ABI version")
func unsupportedABIVersion() async throws {
let versionNumber = VersionNumber(-100, 0)
let versionTypeInfo = ABI.version(forVersionNumber: versionNumber).map(TypeInfo.init(describing:))
let versionTypeInfo = ABI.version(forVersionNumber: versionNumber).map {TypeInfo(describing: $0) }
#expect(versionTypeInfo == nil)
}

Expand All @@ -294,7 +294,7 @@ struct SwiftPMTests {
#expect(swiftCompilerVersion >= VersionNumber(6, 0))
#expect(swiftCompilerVersion < VersionNumber(8, 0), "Swift 8.0 is here! Please update this test.")
let versionNumber = VersionNumber(8, 0)
let versionTypeInfo = ABI.version(forVersionNumber: versionNumber).map(TypeInfo.init(describing:))
let versionTypeInfo = ABI.version(forVersionNumber: versionNumber).map {TypeInfo(describing: $0) }
#expect(versionTypeInfo == nil)
}

Expand Down
7 changes: 7 additions & 0 deletions Tests/TestingTests/TypeInfoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ struct TypeInfoTests {
#expect(!TypeInfo(describing: String.self).isSwiftEnumeration)
#expect(TypeInfo(describing: SomeEnum.self).isSwiftEnumeration)
}

@Test func typeOfMoveOnlyValueIsInferred() {
let value = MoveOnlyType()
#expect(TypeInfo(describingTypeOf: value).unqualifiedName == "MoveOnlyType")
}
}

// MARK: - Fixtures
Expand All @@ -131,3 +136,5 @@ extension String {
}

private enum SomeEnum {}

private struct MoveOnlyType: ~Copyable {}