testing: Implement Macros For Cli Testing
Continuous Integration / Grammar Tests (push) Successful in 4m5s
Continuous Integration / Library Tests (push) Failing after 8m19s
Continuous Integration / Cli Tests (push) Successful in 6m36s
Continuous Integration / Library Format Tests (push) Successful in 5m0s

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-20 17:53:12 -04:00
parent 3cff82fd5c
commit 7a2c55cc51
3 changed files with 215 additions and 1 deletions
+85 -1
View File
@@ -289,11 +289,95 @@ public struct MustOr: CodeItemMacro {
}
}
public struct CliTestDeclarationMacro: PeerMacro, Sendable {
private static func doc_shrink(_ from: String) -> String {
return from.replacing(Regex(#/^.*\/\/\/[\s]+/#), with: "")
}
public static func expansion(
of node: AttributeSyntax,
providingPeersOf declaration: some DeclSyntaxProtocol,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
let cli_test_expected_output = node.leadingTrivia.filter({ $0.isComment }).map({
doc_shrink("\($0)")
}).joined(separator: "\\n")
let cli_test_driver_thunk_name = context.makeUniqueName(
declaration.cast(FunctionDeclSyntax.self).name.text + "_thunk_")
let cli_test_driver_thunk: DeclSyntax = """
@Sendable private func \(cli_test_driver_thunk_name)() async throws {
let expected = "\(raw: cli_test_expected_output)"
_ = unsafe try await Testing.__requiringUnsafe(
Testing.__requiringTry(
Testing.__requiringAwait(swiftCliTestRunner(\(declaration.cast(FunctionDeclSyntax.self).name), expected))))
}
"""
let cli_test_driver_generator_name = context.makeUniqueName(
declaration.cast(FunctionDeclSyntax.self).name.text + "_generator_")
let cli_test_driver_generator: DeclSyntax = """
@Sendable private func \(cli_test_driver_generator_name)() async -> Testing.Test {
return .__function(
named: "xxxxxxxx()",
in: nil,
xcTestCompatibleSelector: Testing.__xcTestCompatibleSelector("xxxxxx:"),
traits: [],
sourceLocation: Testing.SourceLocation(
fileID: "Tests/CliTests/Cli.swift",
filePath: "/Users/hawkinsw/code/p4ce/Tests/p4rseTests/CliTests/Cli.swift", line: 359,
column: 2),
parameters: [],
testFunction: \(cli_test_driver_thunk_name)
)
}
"""
let cli_test_driver_accessor_name = context.makeUniqueName(
declaration.cast(FunctionDeclSyntax.self).name.text + "_accessor_")
let cli_test_driver_accessor: DeclSyntax = """
private nonisolated let \(cli_test_driver_accessor_name): Accessor = { outValue, type, _, _ in
Testing.Test.__store(\(cli_test_driver_generator_name), into: outValue, asTypeAt: type)
}
"""
let cli_test_driver_content_record_name = context.makeUniqueName(
declaration.cast(FunctionDeclSyntax.self).name.text + "_cr_")
let cli_test_driver_cr: DeclSyntax = """
private nonisolated let \(cli_test_driver_content_record_name): TestContentRecord = (
0x7465_7374, /* indicate a test */
0,
unsafe \(cli_test_driver_accessor_name),
0,
0
)
"""
let cli_test_driver_content_container_name = context.makeUniqueName(
declaration.cast(FunctionDeclSyntax.self).name.text + "__🟡$_container_")
let cli_test_driver_container: DeclSyntax = """
struct \(cli_test_driver_content_container_name): Testing.__TestContentRecordContainer {
nonisolated static var __testContentRecord: TestContentRecord {
unsafe \(cli_test_driver_content_record_name)
}
}
"""
return [
cli_test_driver_thunk, cli_test_driver_generator, cli_test_driver_accessor,
cli_test_driver_cr, cli_test_driver_container,
]
}
}
@main
struct P4Macros: CompilerPlugin {
var providingMacros: [Macro.Type] = [
RequireResult.self, RequireErrorResult.self, UseOkResult.self, UseErrorResult.self,
RequireNodeType.self, SkipUnlessNodeType.self, SkipUnlessNodesTypes.self, RequireNodesType.self,
MustOr.self,
MustOr.self, CliTestDeclarationMacro.self,
]
}