-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #391 from moonbitlang/native_test_with_args
internal: support test with args for native backend
- Loading branch information
Showing
4 changed files
with
187 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
crates/moonbuild/template/test_driver/with_args_driver_template_native.mbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |