diff --git a/Sources/Macros/Macros.swift b/Sources/Macros/Macros.swift index c85a7d5..ea6b5d0 100644 --- a/Sources/Macros/Macros.swift +++ b/Sources/Macros/Macros.swift @@ -301,43 +301,47 @@ public struct CliTestDeclarationMacro: PeerMacro, Sendable { in context: some MacroExpansionContext ) throws -> [DeclSyntax] { + let test_name = declaration.cast(FunctionDeclSyntax.self).name + 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 (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 = """ @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( 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( - declaration.cast(FunctionDeclSyntax.self).name.text + "_generator_") + let cli_test_driver_generator_name = context.makeUniqueName(test_name.text + "_generator_") let cli_test_driver_generator: DeclSyntax = """ @Sendable private func \(cli_test_driver_generator_name)() async -> Testing.Test { return .__function( - named: "xxxxxxxx()", + named: "\(test_name)", in: nil, - xcTestCompatibleSelector: Testing.__xcTestCompatibleSelector("xxxxxx:"), + xcTestCompatibleSelector: Testing.__xcTestCompatibleSelector("\(test_name)"), traits: [], - sourceLocation: Testing.SourceLocation( - fileID: "Tests/CliTests/Cli.swift", - filePath: "/Users/hawkinsw/code/p4ce/Tests/p4rseTests/CliTests/Cli.swift", line: 359, - column: 2), + sourceLocation: Testing.SourceLocation.__here(), 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_name = context.makeUniqueName(test_name.text + "_accessor_") let cli_test_driver_accessor: DeclSyntax = """ 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( - declaration.cast(FunctionDeclSyntax.self).name.text + "__🟡$_container_") + let cli_test_driver_content_container_name = context.makeUniqueName(test_name.text + "__🟡$_container_") let cli_test_driver_container: DeclSyntax = """ struct \(cli_test_driver_content_container_name): Testing.__TestContentRecordContainer { nonisolated static var __testContentRecord: TestContentRecord { diff --git a/TestData/simple_cli_preprocessor_test.golden b/TestData/simple_cli_preprocessor_test.golden new file mode 100644 index 0000000..b5a1837 --- /dev/null +++ b/TestData/simple_cli_preprocessor_test.golden @@ -0,0 +1,8 @@ + parser main_parser() { + state start { + transition select (false) { + true: reject; + false: reject; + }; + } + }; diff --git a/TestData/simple_cli_preprocessor_test_file_not_found.golden b/TestData/simple_cli_preprocessor_test_file_not_found.golden new file mode 100644 index 0000000..756a52a --- /dev/null +++ b/TestData/simple_cli_preprocessor_test_file_not_found.golden @@ -0,0 +1 @@ +Preprocessor Error: Could not open simple.p for preprocessing diff --git a/TestData/simple_cli_testa.golden b/TestData/simple_cli_testa.golden new file mode 100644 index 0000000..3582111 --- /dev/null +++ b/TestData/simple_cli_testa.golden @@ -0,0 +1 @@ +Success diff --git a/Tests/Support.swift b/Tests/Support.swift index a2f827c..6f88b20 100644 --- a/Tests/Support.swift +++ b/Tests/Support.swift @@ -16,8 +16,12 @@ // along with this program. If not, see . import Foundation +import SystemPackage import Testing +private let TestGoldenPath: FilePath.Component = FilePath.Component("TestData") +private let TestGoldenExtension: String = ".golden" + typealias Accessor = @convention(c) ( _ outValue: UnsafeMutableRawPointer, @@ -68,7 +72,6 @@ func swiftRun(withArgs args: [String]) throws -> String? { let path = swiftPath()! let child = Process() let so = Pipe() - let se = Pipe() child.standardOutput = so child.executableURL = path @@ -90,7 +93,7 @@ func swiftRun(withArgs args: [String]) throws -> String? { 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 run_output = try! swiftRun(withArgs: args) @@ -98,7 +101,17 @@ func swiftCliTestRunner(_ arg_gen: () -> [String], _ expected: String) async thr #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() = - #externalMacro(module: "Macros", type: "CliTestDeclarationMacro") \ No newline at end of file + #externalMacro(module: "Macros", type: "CliTestDeclarationMacro") diff --git a/Tests/p4rseTests/CliTests/Cli.swift b/Tests/p4rseTests/CliTests/Cli.swift index 3ac0b95..1c57fbc 100644 --- a/Tests/p4rseTests/CliTests/Cli.swift +++ b/Tests/p4rseTests/CliTests/Cli.swift @@ -23,4 +23,19 @@ import Testing @CliTest() func simple_cli_test() -> [String] { 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/"] } \ No newline at end of file