diff --git a/crates/moon/src/cli/generate_test_driver.rs b/crates/moon/src/cli/generate_test_driver.rs index 7e8bf532..55417e06 100644 --- a/crates/moon/src/cli/generate_test_driver.rs +++ b/crates/moon/src/cli/generate_test_driver.rs @@ -265,6 +265,7 @@ fn generate_driver( TargetBackend::Native => "", }; + #[allow(clippy::collapsible_else_if)] let mut template = if only_no_arg_tests { if target_backend.unwrap_or_default() == TargetBackend::Native { include_str!(concat!( @@ -279,10 +280,18 @@ fn generate_driver( } } else { - include_str!(concat!( - env!("CARGO_MANIFEST_DIR"), - "/../moonbuild/template/test_driver/with_args_driver_template.mbt" - )).to_string() + if target_backend.unwrap_or_default() == TargetBackend::Native { + include_str!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../moonbuild/template/test_driver/with_args_driver_template_native.mbt" + )).to_string() + } + else { + include_str!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../moonbuild/template/test_driver/with_args_driver_template.mbt" + )).to_string() + } } .replace("\r\n", "\n") .replace("fn moonbit_test_driver_internal_get_file_name(file_name : MoonbitTestDriverInternalExternString) -> String { panic() }\n", "") diff --git a/crates/moon/tests/test_cases/mod.rs b/crates/moon/tests/test_cases/mod.rs index e231bc2e..5cd96c62 100644 --- a/crates/moon/tests/test_cases/mod.rs +++ b/crates/moon/tests/test_cases/mod.rs @@ -6292,6 +6292,62 @@ fn test_snapshot_test() { Total tests: 6, passed: 1, failed: 5. "#]], ); + check( + get_err_stdout( + &dir, + [ + "test", + "--sort-input", + "--no-parallelize", + "--target", + "native", + ], + ), + expect![[r#" + test username/hello/lib/hello_test.mbt::snapshot in blackbox test failed + expect test failed at $ROOT/src/lib/hello_test.mbt:9:3 + Diff: + ---- + Hello, world! + ---- + + test username/hello/lib/hello.mbt::test snapshot 1 failed + expect test failed at $ROOT/src/lib/hello.mbt:14:3 + Diff: + ---- + hello + snapshot + testing + + ---- + + test username/hello/lib/hello.mbt::test snapshot 2 failed + expect test failed at $ROOT/src/lib/hello.mbt:26:3 + Diff: + ---- + should + be + work + + ---- + + test username/hello/lib/hello.mbt::test inspect 1 failed + expect test failed at $ROOT/src/lib/hello.mbt:6:3-6:16 + Diff: + ---- + a + ---- + + test username/hello/lib/hello.mbt::test inspect 2 failed + expect test failed at $ROOT/src/lib/hello.mbt:18:3-18:16 + Diff: + ---- + c + ---- + + Total tests: 6, passed: 1, failed: 5. + "#]], + ); check( get_stdout(&dir, ["test", "-u", "--no-parallelize"]), expect![[r#" diff --git a/crates/moonbuild/template/test_driver/with_args_driver_template_native.mbt b/crates/moonbuild/template/test_driver/with_args_driver_template_native.mbt new file mode 100644 index 00000000..e02eabb6 --- /dev/null +++ b/crates/moonbuild/template/test_driver/with_args_driver_template_native.mbt @@ -0,0 +1,117 @@ +// Generated by moon test. + +fn moonbit_test_driver_internal_error_to_string(x : Error) -> String = "%error.to_string" + +typealias Moonbit_Test_Driver_Internal_No_Args_Function = () -> Unit!Error + +typealias Moonbit_Test_Driver_Internal_With_Args_Function = (@moonbitlang/core/test.T) -> Unit!Error + +typealias Moonbit_Test_Driver_Internal_No_Args_Map = @moonbitlang/core/builtin.Map[ + String, + @moonbitlang/core/builtin.Map[ + Int, + (Moonbit_Test_Driver_Internal_No_Args_Function, @moonbitlang/core/builtin.Array[String]), + ], +] + +typealias Moonbit_Test_Driver_Internal_TestDriver_With_Args_Map = @moonbitlang/core/builtin.Map[ + String, + @moonbitlang/core/builtin.Map[ + Int, + (Moonbit_Test_Driver_Internal_With_Args_Function, @moonbitlang/core/builtin.Array[String]), + ], +] + +struct Moonbit_Test_Driver_Internal_Meta { + filename : String + index : Int + attrs : @moonbitlang/core/builtin.Array[String] +} + +enum Moonbit_Test_Driver_Internal__F { + F0(Moonbit_Test_Driver_Internal_No_Args_Function) + F1(Moonbit_Test_Driver_Internal_With_Args_Function) +} + +struct Moonbit_Test_Driver_Internal__TestCase { + f : Moonbit_Test_Driver_Internal__F + meta : Moonbit_Test_Driver_Internal_Meta +} + +let moonbit_test_driver_internal_no_args_tests : Moonbit_Test_Driver_Internal_No_Args_Map = { } // WILL BE REPLACED +let moonbit_test_driver_internal_with_args_tests : Moonbit_Test_Driver_Internal_TestDriver_With_Args_Map = { } // WILL BE REPLACED + +pub fn moonbit_test_driver_internal_execute() -> Unit { + let all_tests: @moonbitlang/core/builtin.Array[Moonbit_Test_Driver_Internal__TestCase] = []; + moonbit_test_driver_internal_with_args_tests.iter().each(fn(tuple_of_filename_and_index_func_map) { + let (file_name, index_func_map) = tuple_of_filename_and_index_func_map + index_func_map.iter().each(fn(tuple_of_index_and_func) { + let (index, (func, attrs)) = tuple_of_index_and_func + all_tests.push({ + f: Moonbit_Test_Driver_Internal__F::F1(func), + meta: { filename: file_name, index, attrs } + }); + }); + }); + moonbit_test_driver_internal_no_args_tests.iter().each(fn(tuple_of_filename_and_index_func_map) { + let (file_name, index_func_map) = tuple_of_filename_and_index_func_map + index_func_map.iter().each(fn(tuple_of_index_and_func) { + let (index, (func, attrs)) = tuple_of_index_and_func + all_tests.push({ + f: Moonbit_Test_Driver_Internal__F::F0(func), + meta: { filename: file_name, index, attrs } + }); + }); + }); + + for item in all_tests { + let mut message = "" + + let attrs = item.meta.attrs + let file_name = item.meta.filename + let index = item.meta.index + let name = if attrs.is_empty() { "" } else { attrs[0] } + let test_name = if name.length() == 0 { + item.meta.index.to_string() + } else { + name + } + + try { + let func = match item.f { + Moonbit_Test_Driver_Internal__F::F0(f) => f + Moonbit_Test_Driver_Internal__F::F1(f) => + fn() { + let it : @moonbitlang/core/test.T = { + name, + buffer: @moonbitlang/core/builtin.StringBuilder::new(), + } + f!(it) + } + } + func!() + } catch { + Failure(e) | InspectError(e) | SnapshotError(e) => { + message = e + } + e => { + message = moonbit_test_driver_internal_error_to_string(e) + } + } + + let file_name = file_name.escape() + let test_name = test_name.escape() + let message = message.escape() + @moonbitlang/core/builtin.println("{BEGIN_MOONTEST}") + @moonbitlang/core/builtin.println( + "{\"package\": \"{PACKAGE}\", \"filename\": \{file_name}, \"index\": \"\{index}\", \"test_name\": \{test_name}, \"message\": \{message}}", + ) + @moonbitlang/core/builtin.println("{END_MOONTEST}") + // {COVERAGE_END} + } + +} + +fn main { + moonbit_test_driver_internal_execute() +}