mirror of
https://github.com/roc-lang/roc.git
synced 2024-09-17 13:47:26 +03:00
cleanup test-gen-dev macro code
This commit is contained in:
parent
c8bbb55111
commit
47d4335cda
@ -316,12 +316,6 @@ fn get_test_main_fn<T>(
|
|||||||
get_raw_fn("test_main", lib)
|
get_raw_fn("test_main", lib)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn run_function<T>(fn_name: &str, lib: &libloading::Library) -> T {
|
|
||||||
let main = get_raw_fn::<T>(fn_name, lib);
|
|
||||||
|
|
||||||
unsafe { main() }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn run_test_main<T>(lib: &libloading::Library) -> Result<T, (String, CrashTag)> {
|
pub(crate) fn run_test_main<T>(lib: &libloading::Library) -> Result<T, (String, CrashTag)> {
|
||||||
let main = get_test_main_fn::<T>(lib);
|
let main = get_test_main_fn::<T>(lib);
|
||||||
|
|
||||||
@ -336,10 +330,58 @@ impl<T: Sized> From<RocCallResult<T>> for Result<T, (String, CrashTag)> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// only used in tests
|
||||||
|
pub(crate) fn asm_evals_to<T, U, F>(
|
||||||
|
src: &str,
|
||||||
|
expected: U,
|
||||||
|
transform: F,
|
||||||
|
leak: bool,
|
||||||
|
lazy_literals: bool,
|
||||||
|
) where
|
||||||
|
U: PartialEq + std::fmt::Debug,
|
||||||
|
F: FnOnce(T) -> U,
|
||||||
|
{
|
||||||
|
use bumpalo::Bump;
|
||||||
|
|
||||||
|
let arena = Bump::new();
|
||||||
|
let (_main_fn_name, errors, lib) =
|
||||||
|
crate::helpers::dev::helper(&arena, src, leak, lazy_literals);
|
||||||
|
|
||||||
|
let result = crate::helpers::dev::run_test_main::<T>(&lib);
|
||||||
|
|
||||||
|
if !errors.is_empty() {
|
||||||
|
dbg!(&errors);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
errors,
|
||||||
|
std::vec::Vec::new(),
|
||||||
|
"Encountered errors: {:?}",
|
||||||
|
errors
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(value) => {
|
||||||
|
let expected = expected;
|
||||||
|
#[allow(clippy::redundant_closure_call)]
|
||||||
|
let given = transform(value);
|
||||||
|
assert_eq!(&given, &expected, "output is different");
|
||||||
|
}
|
||||||
|
Err((msg, tag)) => match tag {
|
||||||
|
CrashTag::Roc => panic!(r#"Roc failed with message: "{msg}""#),
|
||||||
|
CrashTag::User => panic!(r#"User crash with message: "{msg}""#),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn identity<T>(x: T) -> T {
|
||||||
|
x
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(unused_macros)]
|
#[allow(unused_macros)]
|
||||||
macro_rules! assert_evals_to {
|
macro_rules! assert_evals_to {
|
||||||
($src:expr, $expected:expr, $ty:ty) => {{
|
($src:expr, $expected:expr, $ty:ty) => {{
|
||||||
assert_evals_to!($src, $expected, $ty, (|val| val));
|
assert_evals_to!($src, $expected, $ty, $crate::helpers::dev::identity);
|
||||||
}};
|
}};
|
||||||
($src:expr, $expected:expr, $ty:ty, $transform:expr) => {
|
($src:expr, $expected:expr, $ty:ty, $transform:expr) => {
|
||||||
// Same as above, except with an additional transformation argument.
|
// Same as above, except with an additional transformation argument.
|
||||||
@ -357,41 +399,13 @@ macro_rules! assert_evals_to {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
($src:expr, $expected:expr, $ty:ty, $transform:expr, $leak:expr, $lazy_literals:expr) => {
|
($src:expr, $expected:expr, $ty:ty, $transform:expr, $leak:expr, $lazy_literals:expr) => {
|
||||||
use bumpalo::Bump;
|
$crate::helpers::dev::asm_evals_to::<$ty, _, _>(
|
||||||
|
$src,
|
||||||
let arena = Bump::new();
|
$expected,
|
||||||
let (_main_fn_name, errors, lib) =
|
$transform,
|
||||||
$crate::helpers::dev::helper(&arena, $src, $leak, $lazy_literals);
|
$leak,
|
||||||
|
$lazy_literals,
|
||||||
let result = $crate::helpers::dev::run_test_main::<$ty>(&lib);
|
);
|
||||||
|
|
||||||
if !errors.is_empty() {
|
|
||||||
dbg!(&errors);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
errors,
|
|
||||||
std::vec::Vec::new(),
|
|
||||||
"Encountered errors: {:?}",
|
|
||||||
errors
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
match result {
|
|
||||||
Ok(value) => {
|
|
||||||
let expected = $expected;
|
|
||||||
#[allow(clippy::redundant_closure_call)]
|
|
||||||
let given = $transform(value);
|
|
||||||
assert_eq!(&given, &expected, "output is different");
|
|
||||||
}
|
|
||||||
Err((msg, tag)) => {
|
|
||||||
use roc_mono::ir::CrashTag;
|
|
||||||
|
|
||||||
match tag {
|
|
||||||
CrashTag::Roc => panic!(r#"Roc failed with message: "{msg}""#),
|
|
||||||
CrashTag::User => panic!(r#"User crash with message: "{msg}""#),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user