Skip to content

Commit

Permalink
Merge pull request #391 from moonbitlang/native_test_with_args
Browse files Browse the repository at this point in the history
internal: support test with args for native backend
  • Loading branch information
Young-Flash authored Oct 15, 2024
2 parents d2f5429 + f6b6f6e commit d15c5dd
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 5 deletions.
17 changes: 13 additions & 4 deletions crates/moon/src/cli/generate_test_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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", "")
Expand Down
56 changes: 56 additions & 0 deletions crates/moon/tests/test_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub fn moonbit_test_driver_internal_execute(file_name: MoonbitTestDriverInternal
fn() {
let it : @moonbitlang/core/test.T = {
name,
buffer: Buffer::new(),
buffer: @moonbitlang/core/builtin.StringBuilder::new(),
}
f!(it)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit d15c5dd

Please sign in to comment.