Skip to content

Convert Macro tests to swift-testing #1352

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 1 commit into from
Jun 13, 2025
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
3 changes: 1 addition & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,7 @@ package.targets.append(contentsOf: [
.testTarget(
name: "FoundationMacrosTests",
dependencies: [
"FoundationMacros",
"TestSupport"
"FoundationMacros"
],
swiftSettings: availabilityMacros + featureSettings + testOnlySwiftSettings
)
Expand Down
9 changes: 5 additions & 4 deletions Tests/FoundationMacrosTests/BundleMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
//
//===----------------------------------------------------------------------===//

import XCTest
import Testing
import FoundationMacros

final class BundleMacroTests: XCTestCase {
@Suite("#bundle Macro")
private struct BundleMacroTests {

func testSimple() {
@Test func testSimple() {
AssertMacroExpansion(
macros: ["bundle": BundleMacro.self],
"""
Expand All @@ -35,7 +36,7 @@ final class BundleMacroTests: XCTestCase {
)
}

func testUsingParenthesis() {
@Test func testUsingParenthesis() {
AssertMacroExpansion(
macros: ["bundle": BundleMacro.self],
"""
Expand Down
47 changes: 29 additions & 18 deletions Tests/FoundationMacrosTests/MacroTestUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
//
//===----------------------------------------------------------------------===//

import XCTest
import Testing
import Foundation
import FoundationMacros
import SwiftSyntax
import SwiftSyntaxMacros
Expand Down Expand Up @@ -46,9 +47,9 @@ struct DiagnosticTest : ExpressibleByStringLiteral, Hashable, CustomStringConver

var mappedToExpression: Self {
DiagnosticTest(
message.replacing("Predicate", with: "Expression").replacing("predicate", with: "expression"),
message._replacing("Predicate", with: "Expression")._replacing("predicate", with: "expression"),
fixIts: fixIts.map {
FixItTest($0.message, result: $0.result.replacing("#Predicate", with: "#Expression"))
FixItTest($0.message, result: $0.result._replacing("#Predicate", with: "#Expression"))
}
)
}
Expand Down Expand Up @@ -99,7 +100,7 @@ extension Diagnostic {
} else {
var result = "Message: \(debugDescription)\nFix-Its:\n"
for fixIt in fixIts {
result += "\t\(fixIt.message.message)\n\t\(fixIt.changes.first!._result.replacingOccurrences(of: "\n", with: "\n\t"))"
result += "\t\(fixIt.message.message)\n\t\(fixIt.changes.first!._result._replacing("\n", with: "\n\t"))"
}
return result
}
Expand All @@ -113,14 +114,14 @@ extension DiagnosticTest {
} else {
var result = "Message: \(message)\nFix-Its:\n"
for fixIt in fixIts {
result += "\t\(fixIt.message)\n\t\(fixIt.result.replacingOccurrences(of: "\n", with: "\n\t"))"
result += "\t\(fixIt.message)\n\t\(fixIt.result._replacing("\n", with: "\n\t"))"
}
return result
}
}
}

func AssertMacroExpansion(macros: [String : Macro.Type], testModuleName: String = "TestModule", testFileName: String = "test.swift", _ source: String, _ result: String = "", diagnostics: Set<DiagnosticTest> = [], file: StaticString = #filePath, line: UInt = #line) {
func AssertMacroExpansion(macros: [String : Macro.Type], testModuleName: String = "TestModule", testFileName: String = "test.swift", _ source: String, _ result: String = "", diagnostics: Set<DiagnosticTest> = [], sourceLocation: Testing.SourceLocation = #_sourceLocation) {
let origSourceFile = Parser.parse(source: source)
let expandedSourceFile: Syntax
let context: BasicMacroExpansionContext
Expand All @@ -131,43 +132,53 @@ func AssertMacroExpansion(macros: [String : Macro.Type], testModuleName: String
BasicMacroExpansionContext(sharingWith: context, lexicalContext: [$0])
}
} catch {
XCTFail("Operator folding on input source failed with error \(error)")
Issue.record("Operator folding on input source failed with error \(error)")
return
}
let expansionResult = expandedSourceFile.description
if !context.diagnostics.contains(where: { $0.diagMessage.severity == .error }) {
XCTAssertEqual(expansionResult, result, file: file, line: line)
#expect(expansionResult == result, sourceLocation: sourceLocation)
}
for diagnostic in context.diagnostics {
if !diagnostics.contains(where: { $0.matches(diagnostic) }) {
XCTFail("Produced extra diagnostic:\n\(diagnostic._assertionDescription)", file: file, line: line)
Issue.record("Produced extra diagnostic:\n\(diagnostic._assertionDescription)", sourceLocation: sourceLocation)
} else {
let location = context.location(of: diagnostic.node, at: .afterLeadingTrivia, filePathMode: .fileID)
XCTAssertNotNil(location, "Produced diagnostic without attached source information:\n\(diagnostic._assertionDescription)", file: file, line: line)
#expect(location != nil, "Produced diagnostic without attached source information:\n\(diagnostic._assertionDescription)", sourceLocation: sourceLocation)
}
}
for diagnostic in diagnostics {
if !context.diagnostics.contains(where: { diagnostic.matches($0) }) {
XCTFail("Failed to produce diagnostic:\n\(diagnostic._assertionDescription)", file: file, line: line)
Issue.record("Failed to produce diagnostic:\n\(diagnostic._assertionDescription)", sourceLocation: sourceLocation)
}
}
}

func AssertPredicateExpansion(_ source: String, _ result: String = "", diagnostics: Set<DiagnosticTest> = [], file: StaticString = #filePath, line: UInt = #line) {
func AssertPredicateExpansion(_ source: String, _ result: String = "", diagnostics: Set<DiagnosticTest> = [], sourceLocation: Testing.SourceLocation = #_sourceLocation) {
AssertMacroExpansion(
macros: ["Predicate": PredicateMacro.self],
source,
result,
diagnostics: diagnostics,
file: file,
line: line
sourceLocation: sourceLocation
)
AssertMacroExpansion(
macros: ["Expression" : FoundationMacros.ExpressionMacro.self],
source.replacing("#Predicate", with: "#Expression"),
result.replacing(".Predicate", with: ".Expression"),
source._replacing("#Predicate", with: "#Expression"),
result._replacing(".Predicate", with: ".Expression"),
diagnostics: Set(diagnostics.map(\.mappedToExpression)),
file: file,
line: line
sourceLocation: sourceLocation
)
}

extension String {
func _replacing(_ text: String, with other: String) -> Self {
if #available(macOS 13.0, *) {
// Use the stdlib API if available
self.replacing(text, with: other)
} else {
// Use the Foundation API on older OSes
self.replacingOccurrences(of: text, with: other, options: [.literal])
}
}
}
21 changes: 11 additions & 10 deletions Tests/FoundationMacrosTests/PredicateMacroBasicTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
//
//===----------------------------------------------------------------------===//

import XCTest
import Testing

final class PredicateMacroBasicTests: XCTestCase {
func testSimple() {
@Suite("#Predicate Macro Basics")
private struct PredicateMacroBasicTests {
@Test func simple() {
AssertPredicateExpansion(
"""
#Predicate<Object> { input in
Expand All @@ -30,7 +31,7 @@ final class PredicateMacroBasicTests: XCTestCase {
)
}

func testImplicitReturn() {
@Test func implicitReturn() {
AssertPredicateExpansion(
"""
#Predicate<Object> { input in
Expand All @@ -47,7 +48,7 @@ final class PredicateMacroBasicTests: XCTestCase {
)
}

func testInferredGenerics() {
@Test func inferredGenerics() {
AssertPredicateExpansion(
"""
#Predicate { input in
Expand All @@ -64,7 +65,7 @@ final class PredicateMacroBasicTests: XCTestCase {
)
}

func testShorthandArgumentNames() {
@Test func shorthandArgumentNames() {
AssertPredicateExpansion(
"""
#Predicate<Object> {
Expand All @@ -81,7 +82,7 @@ final class PredicateMacroBasicTests: XCTestCase {
)
}

func testExplicitClosureArgumentTypes() {
@Test func explicitClosureArgumentTypes() {
AssertPredicateExpansion(
"""
#Predicate<Int, String> { (a: Int, b: String) -> Bool in
Expand All @@ -98,7 +99,7 @@ final class PredicateMacroBasicTests: XCTestCase {
)
}

func testDiagnoseMissingTrailingClosure() {
@Test func diagnoseMissingTrailingClosure() {
AssertPredicateExpansion(
"""
#Predicate
Expand Down Expand Up @@ -141,7 +142,7 @@ final class PredicateMacroBasicTests: XCTestCase {
)
}

func testKeyPath() {
@Test func keyPath() {
AssertPredicateExpansion(
"""
#Predicate<Object> {
Expand Down Expand Up @@ -192,7 +193,7 @@ final class PredicateMacroBasicTests: XCTestCase {
)
}

func testComments() {
@Test func comments() {
AssertPredicateExpansion(
"""
// comment
Expand Down
31 changes: 16 additions & 15 deletions Tests/FoundationMacrosTests/PredicateMacroFunctionCallTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
//
//===----------------------------------------------------------------------===//

import XCTest
import Testing

final class PredicateMacroFunctionCallTests: XCTestCase {
func testSubscript() {
@Suite("#Predicate Macro Function Calls")
private struct PredicateMacroFunctionCallTests {
@Test func `subscript`() {
AssertPredicateExpansion(
"""
#Predicate<Object> { input in
Expand Down Expand Up @@ -96,7 +97,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
)
}

func testContains() {
@Test func contains() {
AssertPredicateExpansion(
"""
#Predicate<Object, Object> { inputA, inputB in
Expand Down Expand Up @@ -130,7 +131,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
)
}

func testContainsWhere() {
@Test func containsWhere() {
AssertPredicateExpansion(
"""
#Predicate<Object> { inputA in
Expand Down Expand Up @@ -175,7 +176,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
)
}

func testAllSatisfy() {
@Test func allSatisfy() {
AssertPredicateExpansion(
"""
#Predicate<Object> { inputA in
Expand Down Expand Up @@ -220,7 +221,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
)
}

func testFilter() {
@Test func filter() {
AssertPredicateExpansion(
"""
#Predicate<Object> { inputA in
Expand Down Expand Up @@ -347,7 +348,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
)
}

func testStartsWith() {
@Test func startsWith() {
AssertPredicateExpansion(
"""
#Predicate<Object> { inputA in
Expand Down Expand Up @@ -386,7 +387,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
)
}

func testMin() {
@Test func min() {
AssertPredicateExpansion(
"""
#Predicate<[Int]> { inputA in
Expand All @@ -406,7 +407,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
)
}

func testMax() {
@Test func max() {
AssertPredicateExpansion(
"""
#Predicate<[Int]> { inputA in
Expand All @@ -426,7 +427,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
)
}

func testLocalizedStandardContains() {
@Test func localizedStandardContains() {
AssertPredicateExpansion(
"""
#Predicate<String> { inputA in
Expand Down Expand Up @@ -462,7 +463,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
)
}

func testLocalizedStandardCompare() {
@Test func localizedStandardCompare() {
AssertPredicateExpansion(
"""
#Predicate<String> { inputA in
Expand Down Expand Up @@ -516,7 +517,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
)
}

func testCaseInsensitiveCompare() {
@Test func caseInsensitiveCompare() {
AssertPredicateExpansion(
"""
#Predicate<String> { inputA in
Expand All @@ -535,7 +536,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
}

#if FOUNDATION_FRAMEWORK
func testEvaluate() {
@Test func evaluate() {
AssertPredicateExpansion(
"""
#Predicate<String> { input in
Expand Down Expand Up @@ -584,7 +585,7 @@ final class PredicateMacroFunctionCallTests: XCTestCase {
}
#endif

func testDiagnoseUnsupportedFunction() {
@Test func diagnoseUnsupportedFunction() {
AssertPredicateExpansion(
"""
#Predicate<Object> { inputA in
Expand Down
Loading
Loading