testing: Update CliTest Support.

And add tests for Cli preprocessing.

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-21 22:40:58 -04:00
parent b3ca30541a
commit d60465e669
6 changed files with 58 additions and 17 deletions
+17 -14
View File
@@ -301,43 +301,47 @@ public struct CliTestDeclarationMacro: PeerMacro, Sendable {
in context: some MacroExpansionContext in context: some MacroExpansionContext
) throws -> [DeclSyntax] { ) throws -> [DeclSyntax] {
let test_name = declaration.cast(FunctionDeclSyntax.self).name
let cli_test_expected_output = node.leadingTrivia.filter({ $0.isComment }).map({ let cli_test_expected_output = node.leadingTrivia.filter({ $0.isComment }).map({
doc_shrink("\($0)") doc_shrink("\($0)")
}).joined(separator: "\\n") }).joined(separator: "\\n")
let cli_test_driver_thunk_name = context.makeUniqueName( let cli_test_driver_thunk_name = context.makeUniqueName(
declaration.cast(FunctionDeclSyntax.self).name.text + "_thunk_") declaration.cast(FunctionDeclSyntax.self).name.text + "_thunk_")
let (expected_decl, expected_label) = if cli_test_expected_output.isEmpty {
("let expected = \"\(test_name)\"", "withExpectedPath:")
} else {
("let expected = \"\(cli_test_expected_output)\"", "withExpected:")
}
let cli_test_driver_thunk: DeclSyntax = """ let cli_test_driver_thunk: DeclSyntax = """
@Sendable private func \(cli_test_driver_thunk_name)() async throws { @Sendable private func \(cli_test_driver_thunk_name)() async throws {
let expected = "\(raw: cli_test_expected_output)" \(raw: expected_decl)
_ = unsafe try await Testing.__requiringUnsafe( _ = unsafe try await Testing.__requiringUnsafe(
Testing.__requiringTry( Testing.__requiringTry(
Testing.__requiringAwait(swiftCliTestRunner(\(declaration.cast(FunctionDeclSyntax.self).name), expected)))) Testing.__requiringAwait(swiftCliTestRunner(\(test_name), \(raw: expected_label) expected))))
} }
""" """
let cli_test_driver_generator_name = context.makeUniqueName( let cli_test_driver_generator_name = context.makeUniqueName(test_name.text + "_generator_")
declaration.cast(FunctionDeclSyntax.self).name.text + "_generator_")
let cli_test_driver_generator: DeclSyntax = """ let cli_test_driver_generator: DeclSyntax = """
@Sendable private func \(cli_test_driver_generator_name)() async -> Testing.Test { @Sendable private func \(cli_test_driver_generator_name)() async -> Testing.Test {
return .__function( return .__function(
named: "xxxxxxxx()", named: "\(test_name)",
in: nil, in: nil,
xcTestCompatibleSelector: Testing.__xcTestCompatibleSelector("xxxxxx:"), xcTestCompatibleSelector: Testing.__xcTestCompatibleSelector("\(test_name)"),
traits: [], traits: [],
sourceLocation: Testing.SourceLocation( sourceLocation: Testing.SourceLocation.__here(),
fileID: "Tests/CliTests/Cli.swift",
filePath: "/Users/hawkinsw/code/p4ce/Tests/p4rseTests/CliTests/Cli.swift", line: 359,
column: 2),
parameters: [], parameters: [],
testFunction: \(cli_test_driver_thunk_name) testFunction: \(cli_test_driver_thunk_name)
) )
} }
""" """
let cli_test_driver_accessor_name = context.makeUniqueName( let cli_test_driver_accessor_name = context.makeUniqueName(test_name.text + "_accessor_")
declaration.cast(FunctionDeclSyntax.self).name.text + "_accessor_")
let cli_test_driver_accessor: DeclSyntax = """ let cli_test_driver_accessor: DeclSyntax = """
private nonisolated let \(cli_test_driver_accessor_name): Accessor = { outValue, type, _, _ in private nonisolated let \(cli_test_driver_accessor_name): Accessor = { outValue, type, _, _ in
@@ -357,8 +361,7 @@ public struct CliTestDeclarationMacro: PeerMacro, Sendable {
) )
""" """
let cli_test_driver_content_container_name = context.makeUniqueName( let cli_test_driver_content_container_name = context.makeUniqueName(test_name.text + "__🟡$_container_")
declaration.cast(FunctionDeclSyntax.self).name.text + "__🟡$_container_")
let cli_test_driver_container: DeclSyntax = """ let cli_test_driver_container: DeclSyntax = """
struct \(cli_test_driver_content_container_name): Testing.__TestContentRecordContainer { struct \(cli_test_driver_content_container_name): Testing.__TestContentRecordContainer {
nonisolated static var __testContentRecord: TestContentRecord { nonisolated static var __testContentRecord: TestContentRecord {
@@ -0,0 +1,8 @@
parser main_parser() {
state start {
transition select (false) {
true: reject;
false: reject;
};
}
};
@@ -0,0 +1 @@
Preprocessor Error: Could not open simple.p for preprocessing
+1
View File
@@ -0,0 +1 @@
Success
+15 -2
View File
@@ -16,8 +16,12 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
import Foundation import Foundation
import SystemPackage
import Testing import Testing
private let TestGoldenPath: FilePath.Component = FilePath.Component("TestData")
private let TestGoldenExtension: String = ".golden"
typealias Accessor = typealias Accessor =
@convention(c) ( @convention(c) (
_ outValue: UnsafeMutableRawPointer, _ outValue: UnsafeMutableRawPointer,
@@ -68,7 +72,6 @@ func swiftRun(withArgs args: [String]) throws -> String? {
let path = swiftPath()! let path = swiftPath()!
let child = Process() let child = Process()
let so = Pipe() let so = Pipe()
let se = Pipe()
child.standardOutput = so child.standardOutput = so
child.executableURL = path child.executableURL = path
@@ -90,7 +93,7 @@ func swiftRun(withArgs args: [String]) throws -> String? {
return output return output
} }
func swiftCliTestRunner(_ arg_gen: () -> [String], _ expected: String) async throws { func swiftCliTestRunner(_ arg_gen: () -> [String], withExpected expected: String) async throws {
let args = arg_gen() let args = arg_gen()
let run_output = try! swiftRun(withArgs: args) let run_output = try! swiftRun(withArgs: args)
@@ -98,7 +101,17 @@ func swiftCliTestRunner(_ arg_gen: () -> [String], _ expected: String) async thr
#expect(run_output == expected) #expect(run_output == expected)
} }
func swiftCliTestRunner(_ arg_gen: () -> [String], withExpectedPath expected_path: String) async throws {
let args = arg_gen()
let run_output = try! swiftRun(withArgs: args)
let expected_source_absolute = FilePath(FileManager().currentDirectoryPath).appending(TestGoldenPath).appending(expected_path + TestGoldenExtension)
let expected_output = try! String(contentsOfFile: expected_source_absolute.string)
#expect(run_output == expected_output)
}
@attached(peer) public macro CliTest() = @attached(peer) public macro CliTest() =
#externalMacro(module: "Macros", type: "CliTestDeclarationMacro") #externalMacro(module: "Macros", type: "CliTestDeclarationMacro")
+15
View File
@@ -24,3 +24,18 @@ import Testing
func simple_cli_test() -> [String] { func simple_cli_test() -> [String] {
return ["p4ce", "--plain", "compile", "simple.p4", "-I", "TestData/Sources/"] return ["p4ce", "--plain", "compile", "simple.p4", "-I", "TestData/Sources/"]
} }
@CliTest()
func simple_cli_testa() -> [String] {
return ["p4ce", "--plain", "compile", "simple.p4", "-I", "TestData/Sources/"]
}
@CliTest()
func simple_cli_preprocessor_test() -> [String] {
return ["p4ce", "--plain", "preprocess", "simple.p4", "-I", "TestData/Sources/"]
}
@CliTest()
func simple_cli_preprocessor_test_file_not_found() -> [String] {
return ["p4ce", "--plain", "preprocess", "simple.p", "-I", "TestData/Sources/"]
}