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

View file

@ -37,8 +37,19 @@ measure(1 / 10)
-- Wait should work in other threads, too
local flag: boolean = false
coroutine.wrap(function()
task.spawn(function()
task.wait(0.1)
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)()
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)")