Improve test output

This commit is contained in:
Filip Tibell 2023-01-22 19:25:36 -05:00
parent 276200225b
commit 4ed69994a2
No known key found for this signature in database
2 changed files with 34 additions and 18 deletions

View file

@ -85,11 +85,17 @@ impl Lune {
let result = lua.load(&run_chunk).set_name(&run_name)?.exec_async().await; let result = lua.load(&run_chunk).set_name(&run_name)?.exec_async().await;
match result { match result {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(e) => bail!( Err(e) => {
"\n{}\n{}", if cfg!(test) {
format_label("ERROR"), bail!(pretty_format_luau_error(&e))
pretty_format_luau_error(&e) } else {
), bail!(
"\n{}\n{}",
format_label("ERROR"),
pretty_format_luau_error(&e)
)
}
}
} }
}) })
.await .await
@ -102,28 +108,27 @@ impl Lune {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::Lune; use crate::Lune;
use anyhow::Result;
use std::env::current_dir;
use tokio::fs::read_to_string;
const ARGS: &[&str] = &["Foo", "Bar"];
macro_rules! run_tests { macro_rules! run_tests {
($($name:ident: $value:expr,)*) => { ($($name:ident: $value:expr,)*) => {
$( $(
#[tokio::test] #[tokio::test]
async fn $name() { async fn $name() -> Result<()> {
let args = vec![ let path = current_dir()
"Foo".to_owned(),
"Bar".to_owned()
];
let path = std::env::current_dir()
.unwrap() .unwrap()
.join(format!("src/tests/{}.luau", $value)); .join(format!("src/tests/{}.luau", $value));
let script = tokio::fs::read_to_string(&path) let script = read_to_string(&path)
.await .await
.unwrap(); .unwrap();
let lune = Lune::new() let lune = Lune::new()
.with_args(args) .with_args(ARGS.clone().iter().map(ToString::to_string).collect())
.with_all_globals(); .with_all_globals();
if let Err(e) = lune.run($value, &script).await { lune.run($value, &script).await
panic!("\nTest '{}' failed!\n{}\n", $value, e.to_string())
}
} }
)* )*
} }

View file

@ -37,8 +37,19 @@ measure(1 / 10)
-- Wait should work in other threads, too -- Wait should work in other threads, too
local flag: boolean = false local flag: boolean = false
coroutine.wrap(function() task.spawn(function()
task.wait(0.1) task.wait(0.1)
flag = true flag = true
end)
assert(not flag, "Wait failed for a task-spawned thread (1)")
task.wait(0.2)
assert(flag, "Wait failed for a task-spawned thread (2)")
local flag2: boolean = false
coroutine.wrap(function()
task.wait(0.1)
flag2 = true
end)() end)()
assert(flag, "Wait failed while in a coroutine") assert(not flag2, "Wait failed for a coroutine (1)")
task.wait(0.2)
assert(flag2, "Wait failed for a coroutine (2)")