From 9068c89c00fdd20d3e0c6e3231f3149298449e8b Mon Sep 17 00:00:00 2001 From: KtorZ Date: Tue, 20 Dec 2022 15:16:05 +0100 Subject: [PATCH] Show trace logs as part of the test output when any. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` ┍━ test ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ │ PASS [mem: 6370, cpu: 2591822] trace_1 │ ↳ is negative │ ↳ is non-negative ┕━━━━━━━━━ 1 tests | 1 passed | 0 failed ``` --- crates/cli/src/lib.rs | 36 ++++++++++++++++++----- crates/project/src/lib.rs | 6 ++-- crates/project/src/script.rs | 1 + examples/acceptance_tests/032/lib/test.ak | 13 ++++++-- 4 files changed, 45 insertions(+), 11 deletions(-) diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs index 93e6285b..c8be9676 100644 --- a/crates/cli/src/lib.rs +++ b/crates/cli/src/lib.rs @@ -171,6 +171,7 @@ fn fmt_test(eval_info: &EvalInfo, max_mem: usize, max_cpu: usize, styled: bool) success, script, spent_budget, + logs, .. } = eval_info; @@ -178,17 +179,38 @@ fn fmt_test(eval_info: &EvalInfo, max_mem: usize, max_cpu: usize, styled: bool) let mem_pad = pretty::pad_left(mem.to_string(), max_mem, " "); let cpu_pad = pretty::pad_left(cpu.to_string(), max_cpu, " "); - format!( - "{} [mem: {}, cpu: {}] {}", - if *success { + let test = format!( + "{status} [mem: {mem_unit}, cpu: {cpu_unit}] {module}", + status = if *success { pretty::style_if(styled, "PASS".to_string(), |s| s.bold().green().to_string()) } else { pretty::style_if(styled, "FAIL".to_string(), |s| s.bold().red().to_string()) }, - pretty::style_if(styled, mem_pad, |s| s.bright_white().to_string()), - pretty::style_if(styled, cpu_pad, |s| s.bright_white().to_string()), - pretty::style_if(styled, script.name.clone(), |s| s.bright_blue().to_string()), - ) + mem_unit = pretty::style_if(styled, mem_pad, |s| s.bright_white().to_string()), + cpu_unit = pretty::style_if(styled, cpu_pad, |s| s.bright_white().to_string()), + module = pretty::style_if(styled, script.name.clone(), |s| s.bright_blue().to_string()), + ); + + let logs = if logs.is_empty() { + String::new() + } else { + logs.iter() + .map(|line| { + format!( + "{arrow} {styled_line}", + arrow = "↳".bright_yellow(), + styled_line = line.bright_black() + ) + }) + .collect::>() + .join("\n") + }; + + if logs.is_empty() { + test + } else { + [test, logs].join("\n") + } } fn fmt_test_summary(tests: &Vec<&EvalInfo>, styled: bool) -> String { diff --git a/crates/project/src/lib.rs b/crates/project/src/lib.rs index 160bc9bf..ec9c3e3e 100644 --- a/crates/project/src/lib.rs +++ b/crates/project/src/lib.rs @@ -714,23 +714,25 @@ where } match script.program.eval(initial_budget) { - (Ok(result), remaining_budget, _) => { + (Ok(result), remaining_budget, logs) => { let eval_info = EvalInfo { success: result != Term::Error && result != Term::Constant(Constant::Bool(false)), script, spent_budget: initial_budget - remaining_budget, output: Some(result), + logs, }; results.push(eval_info); } - (Err(..), remaining_budget, _) => { + (Err(..), remaining_budget, logs) => { let eval_info = EvalInfo { success: false, script, spent_budget: initial_budget - remaining_budget, output: None, + logs, }; results.push(eval_info); diff --git a/crates/project/src/script.rs b/crates/project/src/script.rs index ed34c9c0..c4c2a8cb 100644 --- a/crates/project/src/script.rs +++ b/crates/project/src/script.rs @@ -43,4 +43,5 @@ pub struct EvalInfo { pub script: Script, pub spent_budget: ExBudget, pub output: Option>, + pub logs: Vec, } diff --git a/examples/acceptance_tests/032/lib/test.ak b/examples/acceptance_tests/032/lib/test.ak index e8b8c6e8..0300d140 100644 --- a/examples/acceptance_tests/032/lib/test.ak +++ b/examples/acceptance_tests/032/lib/test.ak @@ -1,5 +1,14 @@ use aiken/builtin -test trace_1() { - builtin.trace("foo", True) +fn is_negative(i : Int) -> Bool { + if i < 0 { + builtin.trace("is negative", True) + } else { + builtin.trace("is non-negative", False) + } +} + + +test trace_1() { + is_negative(-14) && !is_negative(42) }