mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-04 04:08:19 +00:00
stylistic improvements
This commit is contained in:
parent
71b97572ae
commit
76c47c5d6f
6 changed files with 83 additions and 66 deletions
|
@ -437,6 +437,13 @@ pub fn test(_matches: &ArgMatches, _triple: Triple) -> io::Result<i32> {
|
|||
todo!("running tests does not work on windows right now")
|
||||
}
|
||||
|
||||
struct ModuleTestResults {
|
||||
module_id: ModuleId,
|
||||
failed_count: usize,
|
||||
passed_count: usize,
|
||||
tests_duration: Duration,
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
pub fn test(matches: &ArgMatches, triple: Triple) -> io::Result<i32> {
|
||||
use roc_build::program::report_problems_monomorphized;
|
||||
|
@ -516,7 +523,7 @@ pub fn test(matches: &ArgMatches, triple: Triple) -> io::Result<i32> {
|
|||
|
||||
let interns = loaded.interns.clone();
|
||||
|
||||
let (lib, expects_by_module, layout_interner) =
|
||||
let (dyn_lib, expects_by_module, layout_interner) =
|
||||
roc_repl_expect::run::expect_mono_module_to_dylib(
|
||||
arena,
|
||||
target.clone(),
|
||||
|
@ -549,6 +556,7 @@ pub fn test(matches: &ArgMatches, triple: Triple) -> io::Result<i32> {
|
|||
|
||||
let mut results_by_module = Vec::new();
|
||||
let global_layout_interner = layout_interner.into_global();
|
||||
|
||||
for (module_id, expects) in expects_by_module.into_iter() {
|
||||
let test_start_time = Instant::now();
|
||||
let (failed_count, passed_count) = roc_repl_expect::run::run_toplevel_expects(
|
||||
|
@ -557,16 +565,23 @@ pub fn test(matches: &ArgMatches, triple: Triple) -> io::Result<i32> {
|
|||
arena,
|
||||
interns,
|
||||
&global_layout_interner,
|
||||
&lib,
|
||||
&dyn_lib,
|
||||
&mut expectations,
|
||||
expects,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let duration = test_start_time.elapsed();
|
||||
let tests_duration = test_start_time.elapsed();
|
||||
|
||||
results_by_module.push(ModuleTestResults {
|
||||
module_id,
|
||||
failed_count,
|
||||
passed_count,
|
||||
tests_duration,
|
||||
});
|
||||
|
||||
total_failed_count += failed_count;
|
||||
total_passed_count += passed_count;
|
||||
results_by_module.push((module_id, failed_count, passed_count, duration));
|
||||
}
|
||||
|
||||
if total_failed_count == 0 && total_passed_count == 0 {
|
||||
|
@ -581,15 +596,9 @@ pub fn test(matches: &ArgMatches, triple: Triple) -> io::Result<i32> {
|
|||
Ok(2)
|
||||
} else {
|
||||
let show_module_label = results_by_module.len() > 1;
|
||||
for (module_id, failed_count, passed_count, duration) in results_by_module {
|
||||
print_test_results(
|
||||
module_id,
|
||||
show_module_label,
|
||||
failed_count,
|
||||
passed_count,
|
||||
duration,
|
||||
interns,
|
||||
);
|
||||
|
||||
for module_test_results in results_by_module {
|
||||
print_test_results(module_test_results, show_module_label, interns);
|
||||
}
|
||||
|
||||
Ok((total_failed_count > 0) as i32)
|
||||
|
@ -597,13 +606,17 @@ pub fn test(matches: &ArgMatches, triple: Triple) -> io::Result<i32> {
|
|||
}
|
||||
|
||||
fn print_test_results(
|
||||
module_id: ModuleId,
|
||||
module_test_results: ModuleTestResults,
|
||||
show_module_label: bool,
|
||||
failed_count: usize,
|
||||
passed_count: usize,
|
||||
duration: Duration,
|
||||
interns: &Interns,
|
||||
) {
|
||||
let ModuleTestResults {
|
||||
module_id,
|
||||
failed_count,
|
||||
passed_count,
|
||||
tests_duration,
|
||||
} = module_test_results;
|
||||
|
||||
let failed_color = if failed_count == 0 {
|
||||
ANSI_STYLE_CODES.green
|
||||
} else {
|
||||
|
@ -613,17 +626,18 @@ fn print_test_results(
|
|||
let passed_color = ANSI_STYLE_CODES.green;
|
||||
let reset = ANSI_STYLE_CODES.reset;
|
||||
|
||||
let test_summary_str =
|
||||
format!(
|
||||
"{failed_color}{failed_count}{reset} failed and {passed_color}{passed_count}{reset} passed in {} ms.",
|
||||
tests_duration.as_millis()
|
||||
);
|
||||
|
||||
if show_module_label {
|
||||
let module_name = module_id.to_ident_str(interns);
|
||||
println!(
|
||||
"\n{module_name}.roc:\n {failed_color}{failed_count}{reset} failed and {passed_color}{passed_count}{reset} passed in {} ms.\n",
|
||||
duration.as_millis(),
|
||||
);
|
||||
|
||||
println!("\n{module_name}.roc:\n {test_summary_str}",);
|
||||
} else {
|
||||
println!(
|
||||
"\n{failed_color}{failed_count}{reset} failed and {passed_color}{passed_count}{reset} passed in {} ms.\n",
|
||||
duration.as_millis(),
|
||||
);
|
||||
println!("{test_summary_str}");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -200,7 +200,7 @@ mod cli_run {
|
|||
vec.into_iter()
|
||||
};
|
||||
|
||||
let out = match cli_mode {
|
||||
let cmd_output = match cli_mode {
|
||||
CliMode::RocBuild => {
|
||||
run_roc_on_failure_is_panic(
|
||||
file,
|
||||
|
@ -296,34 +296,32 @@ mod cli_run {
|
|||
}
|
||||
};
|
||||
|
||||
let mut actual = strip_colors(&out.stdout);
|
||||
|
||||
actual = ignore_test_timings(&actual);
|
||||
|
||||
let self_path = file.display().to_string();
|
||||
actual = actual.replace(&self_path, "<ignored for tests>");
|
||||
|
||||
if !actual.ends_with(expected_ending) {
|
||||
let actual_cmd_stdout = ignore_test_timings(&strip_colors(&cmd_output.stdout))
|
||||
.replace(&self_path, "<ignored for tests>");
|
||||
|
||||
if !actual_cmd_stdout.ends_with(expected_ending) {
|
||||
panic!(
|
||||
"> expected output to end with:\n{}\n> but instead got:\n{}\n> stderr was:\n{}",
|
||||
expected_ending, actual, out.stderr
|
||||
expected_ending, actual_cmd_stdout, cmd_output.stderr
|
||||
);
|
||||
}
|
||||
|
||||
if !out.status.success() && !matches!(cli_mode, CliMode::RocTest) {
|
||||
if !cmd_output.status.success() && !matches!(cli_mode, CliMode::RocTest) {
|
||||
// We don't need stdout, Cargo prints it for us.
|
||||
panic!(
|
||||
"Example program exited with status {:?}\nstderr was:\n{:#?}",
|
||||
out.status, out.stderr
|
||||
cmd_output.status, cmd_output.stderr
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn ignore_test_timings(output: &str) -> String {
|
||||
fn ignore_test_timings(cmd_output: &str) -> String {
|
||||
let regex = Regex::new(r" passed in (\d+) ms\.").expect("Invalid regex pattern");
|
||||
let replacement = " passed in <ignored for test> ms.";
|
||||
regex.replace_all(output, replacement).to_string()
|
||||
regex.replace_all(cmd_output, replacement).to_string()
|
||||
}
|
||||
|
||||
// when you want to run `roc test` to execute `expect`s, perhaps on a library rather than an application.
|
||||
|
@ -640,8 +638,8 @@ mod cli_run {
|
|||
b = 2
|
||||
|
||||
|
||||
|
||||
1 failed and 0 passed in <ignored for test> ms."#
|
||||
1 failed and 0 passed in <ignored for test> ms.
|
||||
"#
|
||||
),
|
||||
UseValgrind::Yes,
|
||||
TestCliCommands::Test,
|
||||
|
@ -659,10 +657,8 @@ mod cli_run {
|
|||
Direct.roc:
|
||||
0 failed and 2 passed in <ignored for test> ms.
|
||||
|
||||
|
||||
Transitive.roc:
|
||||
0 failed and 1 passed in <ignored for test> ms.
|
||||
|
||||
"#
|
||||
),
|
||||
);
|
||||
|
|
|
@ -55,7 +55,6 @@ use roc_std::RocDec;
|
|||
use roc_target::{PtrWidth, TargetInfo};
|
||||
use std::convert::TryInto;
|
||||
use std::path::Path;
|
||||
use std::result;
|
||||
use target_lexicon::{Aarch64Architecture, Architecture, OperatingSystem, Triple};
|
||||
|
||||
use super::convert::{struct_type_from_union_layout, RocUnion};
|
||||
|
@ -5671,8 +5670,10 @@ pub fn build_procedures_expose_expects<'a>(
|
|||
expects_by_module: MutMap<ModuleId, Vec<'a, Symbol>>,
|
||||
procedures: MutMap<(Symbol, ProcLayout<'a>), roc_mono::ir::Proc<'a>>,
|
||||
) -> MutMap<ModuleId, Vec<'a, &'a str>> {
|
||||
// converts Vec<Vec<Symbol>> into Vec<Symbol>
|
||||
let flattened_symbols: Vec<Symbol> =
|
||||
Vec::from_iter_in(expects_by_module.values().flatten().copied(), env.arena);
|
||||
|
||||
let entry_point = EntryPoint::Expects {
|
||||
symbols: &flattened_symbols,
|
||||
};
|
||||
|
@ -5695,30 +5696,34 @@ pub fn build_procedures_expose_expects<'a>(
|
|||
niche: captures_niche,
|
||||
};
|
||||
|
||||
let mut names_by_module = MutMap::default();
|
||||
let mut expect_names_by_module = MutMap::default();
|
||||
|
||||
for (module_id, expects) in expects_by_module {
|
||||
let mut expect_names = Vec::with_capacity_in(expects.len(), env.arena);
|
||||
|
||||
for symbol in expects.iter().copied() {
|
||||
let it = top_level.arguments.iter().copied();
|
||||
let bytes = roc_alias_analysis::func_name_bytes_help(
|
||||
let args_iter = top_level.arguments.iter().copied();
|
||||
|
||||
let func_name_bytes = roc_alias_analysis::func_name_bytes_help(
|
||||
symbol,
|
||||
it,
|
||||
args_iter,
|
||||
captures_niche,
|
||||
top_level.result,
|
||||
);
|
||||
let func_name = FuncName(&bytes);
|
||||
|
||||
let func_name = FuncName(&func_name_bytes);
|
||||
let func_solutions = mod_solutions.func_solutions(func_name).unwrap();
|
||||
|
||||
let mut it = func_solutions.specs();
|
||||
let func_spec = match it.next() {
|
||||
let mut func_spec_iter = func_solutions.specs();
|
||||
|
||||
let func_spec = match func_spec_iter.next() {
|
||||
Some(spec) => spec,
|
||||
None => panic!("no specialization for expect {symbol}"),
|
||||
None => panic!("No specialization for expect {symbol}."),
|
||||
};
|
||||
|
||||
debug_assert!(
|
||||
it.next().is_none(),
|
||||
"we expect only one specialization of this symbol"
|
||||
func_spec_iter.next().is_none(),
|
||||
"We expect only one specialization of this symbol."
|
||||
);
|
||||
|
||||
// NOTE fake layout; it is only used for debug prints
|
||||
|
@ -5728,8 +5733,8 @@ pub fn build_procedures_expose_expects<'a>(
|
|||
let name = roc_main_fn.get_name().to_str().unwrap();
|
||||
|
||||
let expect_name = &format!("Expect_{name}");
|
||||
let expect_name = env.arena.alloc_str(expect_name);
|
||||
expect_names.push(&*expect_name);
|
||||
let expect_name_str = env.arena.alloc_str(expect_name);
|
||||
expect_names.push(&*expect_name_str);
|
||||
|
||||
// Add main to the module.
|
||||
let _ = expose_function_to_host_help_c_abi(
|
||||
|
@ -5742,9 +5747,10 @@ pub fn build_procedures_expose_expects<'a>(
|
|||
&format!("Expect_{name}"),
|
||||
);
|
||||
}
|
||||
names_by_module.insert(module_id, expect_names);
|
||||
expect_names_by_module.insert(module_id, expect_names);
|
||||
}
|
||||
names_by_module
|
||||
|
||||
expect_names_by_module
|
||||
}
|
||||
|
||||
fn build_procedures_help<'a>(
|
||||
|
|
|
@ -2779,7 +2779,7 @@ fn update<'a>(
|
|||
|
||||
let subs = solved_subs.into_inner();
|
||||
|
||||
if toplevel_expects.pure.len() > 0 || toplevel_expects.fx.len() > 0 {
|
||||
if !toplevel_expects.pure.is_empty() || !toplevel_expects.fx.is_empty() {
|
||||
state.toplevel_expects.insert(module_id, toplevel_expects);
|
||||
}
|
||||
|
||||
|
|
|
@ -150,7 +150,7 @@ mod test {
|
|||
|
||||
let interns = loaded.interns.clone();
|
||||
|
||||
let (lib, expects_by_module, layout_interner) = expect_mono_module_to_dylib(
|
||||
let (dy_lib, expects_by_module, layout_interner) = expect_mono_module_to_dylib(
|
||||
arena,
|
||||
target.clone(),
|
||||
loaded,
|
||||
|
@ -168,23 +168,23 @@ mod test {
|
|||
let mut memory = crate::run::ExpectMemory::from_slice(&mut shared_buffer);
|
||||
|
||||
// communicate the mmapped name to zig/roc
|
||||
let set_shared_buffer = run_roc_dylib!(lib, "set_shared_buffer", (*mut u8, usize), ());
|
||||
let set_shared_buffer = run_roc_dylib!(dy_lib, "set_shared_buffer", (*mut u8, usize), ());
|
||||
let mut result = RocCallResult::default();
|
||||
unsafe { set_shared_buffer((shared_buffer.as_mut_ptr(), BUFFER_SIZE), &mut result) };
|
||||
|
||||
let mut writer = Vec::with_capacity(1024);
|
||||
|
||||
let global_layout_interner = layout_interner.into_global();
|
||||
for (_, expects) in expects_by_module {
|
||||
for (_, expect_funcs) in expects_by_module {
|
||||
let (_failed, _passed) = crate::run::run_expects_with_memory(
|
||||
&mut writer,
|
||||
RenderTarget::ColorTerminal,
|
||||
arena,
|
||||
interns,
|
||||
&global_layout_interner,
|
||||
&lib,
|
||||
&dy_lib,
|
||||
&mut expectations,
|
||||
expects,
|
||||
expect_funcs,
|
||||
&mut memory,
|
||||
)
|
||||
.unwrap();
|
||||
|
|
|
@ -691,7 +691,8 @@ pub fn expect_mono_module_to_dylib<'a>(
|
|||
procedures,
|
||||
);
|
||||
|
||||
let mut result_expects: MutMap<ModuleId, ExpectFunctions> = MutMap::default();
|
||||
let mut modules_expects: MutMap<ModuleId, ExpectFunctions> = MutMap::default();
|
||||
|
||||
for (module_id, expects) in toplevel_expects.into_iter() {
|
||||
let expect_names = expect_names.get(&module_id).unwrap();
|
||||
|
||||
|
@ -720,12 +721,12 @@ pub fn expect_mono_module_to_dylib<'a>(
|
|||
env.arena,
|
||||
);
|
||||
|
||||
let expects = ExpectFunctions {
|
||||
let expect_funs = ExpectFunctions {
|
||||
pure: expects_pure,
|
||||
fx: expects_fx,
|
||||
};
|
||||
|
||||
result_expects.insert(module_id, expects);
|
||||
modules_expects.insert(module_id, expect_funs);
|
||||
}
|
||||
|
||||
env.dibuilder.finalize();
|
||||
|
@ -753,5 +754,5 @@ pub fn expect_mono_module_to_dylib<'a>(
|
|||
}
|
||||
|
||||
llvm_module_to_dylib(env.module, &target, opt_level)
|
||||
.map(|lib| (lib, result_expects, layout_interner))
|
||||
.map(|dy_lib| (dy_lib, modules_expects, layout_interner))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue