Use a macro to make KCL snapshot tests easier (#3381)

Now the process for adding a new KCL test is:

 - `cd src/wasm-lib`
 - Write a new .kcl file in the `tests/inputs` directory, e.g. `tests/inputs/foo.kcl`
 - Run `just new-test foo`
 - That created `tests/outputs/foo.png`, so open it and check it looks correct
 - Commit changes and open a PR
This commit is contained in:
Adam Chalmers
2024-08-12 18:18:44 -05:00
committed by GitHub
parent 997f60e1e5
commit 6a09bbc0ef
4 changed files with 53 additions and 119 deletions

4
src/wasm-lib/justfile Normal file
View File

@ -0,0 +1,4 @@
# Create a new KCL snapshot test from `tests/inputs/my-test.kcl`.
new-test name:
echo "kcl_test!(\"{{name}}\", {{name}});" >> tests/executor/visuals.rs
TWENTY_TWENTY=overwrite cargo nextest run --test executor -E 'test(=visuals::{{name}})'

View File

@ -5,6 +5,7 @@ use kcl_lib::{settings::types::UnitLength, test_server::execute_and_snapshot};
const MIN_DIFF: f64 = 0.99;
mod no_visuals;
mod visuals;
macro_rules! kcl_input {
($file:literal) => {
@ -16,87 +17,6 @@ fn assert_out(test_name: &str, result: &image::DynamicImage) {
twenty_twenty::assert_image(format!("tests/executor/outputs/{test_name}.png"), result, MIN_DIFF);
}
#[tokio::test(flavor = "multi_thread")]
async fn kcl_test_sketch_on_face() {
let code = kcl_input!("sketch_on_face");
let result = execute_and_snapshot(code, UnitLength::Mm).await.unwrap();
assert_out("sketch_on_face", &result);
}
#[tokio::test(flavor = "multi_thread")]
async fn kcl_test_riddle_small() {
let code = kcl_input!("riddle_small");
let result = execute_and_snapshot(code, UnitLength::Mm).await.unwrap();
assert_out("riddle_small", &result);
}
#[tokio::test(flavor = "multi_thread")]
async fn kcl_test_tan_arc_x_line() {
let code = kcl_input!("tan_arc_x_line");
let result = execute_and_snapshot(code, UnitLength::Mm).await.unwrap();
assert_out("tan_arc_x_line", &result);
}
#[tokio::test(flavor = "multi_thread")]
async fn kcl_test_lego() {
let code = kcl_input!("lego");
let result = execute_and_snapshot(code, UnitLength::Mm).await.unwrap();
assert_out("lego", &result);
}
#[tokio::test(flavor = "multi_thread")]
async fn kcl_test_computed_var() {
let code = kcl_input!("computed_var");
let result = execute_and_snapshot(code, UnitLength::Mm).await.unwrap();
assert_out("computed_var", &result);
}
#[tokio::test(flavor = "multi_thread")]
async fn kcl_test_pipe_as_arg() {
let code = kcl_input!("pipe_as_arg");
let result = execute_and_snapshot(code, UnitLength::Mm).await.unwrap();
assert_out("pipe_as_arg", &result);
}
#[tokio::test(flavor = "multi_thread")]
async fn kcl_test_pentagon_fillet_sugar() {
let code = kcl_input!("pentagon_fillet_sugar");
let result = execute_and_snapshot(code, UnitLength::Cm).await.unwrap();
assert_out("pentagon_fillet_sugar", &result);
}
#[tokio::test(flavor = "multi_thread")]
async fn kcl_test_sketch_on_face_start() {
let code = kcl_input!("sketch_on_face_start");
let result = execute_and_snapshot(code, UnitLength::Mm).await.unwrap();
assert_out("sketch_on_face_start", &result);
}
#[tokio::test(flavor = "multi_thread")]
async fn kcl_test_mike_stress_lines() {
let code = kcl_input!("mike_stress_test");
let result = execute_and_snapshot(code, UnitLength::Mm).await.unwrap();
assert_out("mike_stress_test", &result);
}
#[tokio::test(flavor = "multi_thread")]
async fn kcl_test_sketch_on_face_end() {
let code = kcl_input!("sketch_on_face_end");
let result = execute_and_snapshot(code, UnitLength::Mm).await.unwrap();
assert_out("sketch_on_face_end", &result);
}
#[tokio::test(flavor = "multi_thread")]
async fn kcl_test_sketch_on_face_end_negative_extrude() {
let code = kcl_input!("sketch_on_face_end_negative_extrude");
let result = execute_and_snapshot(code, UnitLength::Mm).await.unwrap();
assert_out("sketch_on_face_end_negative_extrude", &result);
}
#[tokio::test(flavor = "multi_thread")]
async fn kcl_test_fillet_duplicate_tags() {
let code = kcl_input!("fillet_duplicate_tags");
@ -109,44 +29,6 @@ async fn kcl_test_fillet_duplicate_tags() {
);
}
#[tokio::test(flavor = "multi_thread")]
async fn kcl_test_basic_fillet_cube_start() {
let code = kcl_input!("basic_fillet_cube_start");
let result = execute_and_snapshot(code, UnitLength::Mm).await.unwrap();
assert_out("basic_fillet_cube_start", &result);
}
#[tokio::test(flavor = "multi_thread")]
async fn kcl_test_basic_fillet_cube_end() {
let code = kcl_input!("basic_fillet_cube_end");
let result = execute_and_snapshot(code, UnitLength::Mm).await.unwrap();
assert_out("basic_fillet_cube_end", &result);
}
#[tokio::test(flavor = "multi_thread")]
async fn kcl_test_basic_fillet_cube_close_opposite() {
let code = kcl_input!("basic_fillet_cube_close_opposite");
let result = execute_and_snapshot(code, UnitLength::Mm).await.unwrap();
assert_out("basic_fillet_cube_close_opposite", &result);
}
#[tokio::test(flavor = "multi_thread")]
async fn kcl_test_basic_fillet_cube_next_adjacent() {
let code = kcl_input!("basic_fillet_cube_next_adjacent");
let result = execute_and_snapshot(code, UnitLength::Mm).await.unwrap();
assert_out("basic_fillet_cube_next_adjacent", &result);
}
#[tokio::test(flavor = "multi_thread")]
async fn kcl_test_basic_fillet_cube_previous_adjacent() {
let code = kcl_input!("basic_fillet_cube_previous_adjacent");
let result = execute_and_snapshot(code, UnitLength::Mm).await.unwrap();
assert_out("basic_fillet_cube_previous_adjacent", &result);
}
#[tokio::test(flavor = "multi_thread")]
async fn kcl_test_execute_with_function_sketch() {
let code = kcl_input!("function_sketch");

Binary file not shown.

Before

Width:  |  Height:  |  Size: 94 KiB

After

Width:  |  Height:  |  Size: 131 KiB

View File

@ -0,0 +1,48 @@
macro_rules! kcl_input {
($file:literal) => {
include_str!(concat!("inputs/", $file, ".kcl"))
};
}
macro_rules! kcl_test {
($file:literal, $test_name:ident) => {
#[tokio::test(flavor = "multi_thread")]
async fn $test_name() {
let code = kcl_input!($file);
let result = super::execute_and_snapshot(code, kcl_lib::settings::types::UnitLength::Mm)
.await
.unwrap();
super::assert_out($file, &result);
}
};
}
kcl_test!("sketch_on_face", kcl_test_sketch_on_face);
kcl_test!("basic_fillet_cube_start", kcl_test_basic_fillet_cube_start);
kcl_test!(
"basic_fillet_cube_next_adjacent",
kcl_test_basic_fillet_cube_next_adjacent
);
kcl_test!(
"basic_fillet_cube_previous_adjacent",
kcl_test_basic_fillet_cube_previous_adjacent
);
kcl_test!("basic_fillet_cube_end", kcl_test_basic_fillet_cube_end);
kcl_test!(
"basic_fillet_cube_close_opposite",
kcl_test_basic_fillet_cube_close_opposite
);
kcl_test!("sketch_on_face_end", kcl_test_sketch_on_face_end);
kcl_test!("sketch_on_face_start", kcl_test_sketch_on_face_start);
kcl_test!(
"sketch_on_face_end_negative_extrude",
kcl_test_sketch_on_face_end_negative_extrude
);
kcl_test!("mike_stress_test", kcl_test_mike_stress_test);
kcl_test!("pentagon_fillet_sugar", kcl_test_pentagon_fillet_sugar);
kcl_test!("pipe_as_arg", kcl_test_pipe_as_arg);
kcl_test!("computed_var", kcl_test_computed_var);
kcl_test!("lego", kcl_test_lego);
kcl_test!("riddle_small", kcl_test_riddle_small);
kcl_test!("tan_arc_x_line", kcl_test_tan_arc_x_line);