Skip to content

Commit

Permalink
execution-plan: Use snapshot tests for program memory
Browse files Browse the repository at this point in the history
This saves a "snapshot" of program memory (visualized as a table) into the repo. When you rerun the "draw cube via API" unit test, it'll compare the snapshot to what's on disk and fail if there's a change (like expectorate or twenty-twenty).

Use `cargo insta review` for reviewing changes and accepting/rejecting them.
  • Loading branch information
adamchalmers committed Dec 18, 2023
1 parent 9835418 commit 92b1474
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 4 deletions.
104 changes: 104 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions execution-plan/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ description = "A DSL for composing KittyCAD API queries"

[dependencies]
bytes = "1.5"
insta = "1.34.0"
kittycad = { version = "0.2.44", features = ["requests"] }
kittycad-modeling-cmds = { path = "../modeling-cmds" }
kittycad-modeling-session = { path = "../modeling-session" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
source: execution-plan/src/tests.rs
expression: debug_dump_memory(&mem)
---
┌───────┬──────────┬──────────────────────────────────────┐
indexval_typevalue
├───────┼──────────┼──────────────────────────────────────┤
0Uuid │ 4cd175a3-e313-4c91-b624-368bea3c0483
2Float40
3Booltrue
4Stringpng
6Float-20
7Float-20
8Float-20
9Stringline
10Float20
11Float-20
12Float-20
13Boolfalse
14Stringline
15Float20
16Float20
17Float-20
18Boolfalse
19Stringline
20Float-20
21Float20
22Float-20
23Boolfalse
24Stringline
25Float-20
26Float-20
27Float-20
28Boolfalse
99StringTAKE_SNAPSHOT
100Byteslength 129068
└───────┴──────────┴──────────────────────────────────────┘
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
source: execution-plan/src/tests.rs
expression: debug_dump_memory(&mem)
---
┌───────┬──────────┬──────────────────────────────────────┐
indexval_typevalue
├───────┼──────────┼──────────────────────────────────────┤
0Uuid │ 4cd175a3-e313-4c91-b624-368bea3c0483
2Float40
3Booltrue
4Stringpng
6Float-20
7Float-20
8Float-20
9Stringline
10Float20
11Float-20
12Float-20
13Boolfalse
14Stringline
15Float20
16Float20
17Float-20
18Boolfalse
19Stringline
20Float-20
21Float20
22Float-20
23Boolfalse
24Stringline
25Float-20
26Float-20
27Float-20
28Boolfalse
└───────┴──────────┴──────────────────────────────────────┘
11 changes: 7 additions & 4 deletions execution-plan/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::env;

use insta::assert_snapshot;
use kittycad_modeling_cmds::shared::{PathSegment, Point3d};
use kittycad_modeling_session::{Session, SessionBuilder};
use tabled::{settings::Style, Table};
Expand Down Expand Up @@ -133,7 +134,7 @@ async fn api_call_draw_cube() {

// Define primitives, load them into memory.
let path_id_addr = Address(0);
let path = new_id();
let path = ModelingCmdId(Uuid::parse_str("4cd175a3-e313-4c91-b624-368bea3c0483").unwrap());
let cube_height_addr = Address(2);
let cube_height = Primitive::from(CUBE_WIDTH * 2.0);
let cap_addr = Address(3);
Expand Down Expand Up @@ -192,6 +193,7 @@ async fn api_call_draw_cube() {
let size = mem.set_composite(*addr, segment);
segment_addrs.push(Address(addr.0 + size));
}
assert_snapshot!("cube_memory_before", debug_dump_memory(&mem));

// Run the plan!
execute(
Expand Down Expand Up @@ -260,7 +262,7 @@ async fn api_call_draw_cube() {
.unwrap();

// Program executed successfully!
debug_dump_memory(&mem);
assert_snapshot!("cube_memory_after", debug_dump_memory(&mem));

// The image output was set to addr 99.
// Outputs are two addresses long, addr 99 will store the data format (TAKE_SNAPSHOT)
Expand All @@ -279,7 +281,8 @@ async fn api_call_draw_cube() {
twenty_twenty::assert_image("tests/outputs/cube.png", &img, 0.9999);
}

fn debug_dump_memory(mem: &Memory) {
/// Return a nicely-formatted table of memory.
fn debug_dump_memory(mem: &Memory) -> String {
impl Primitive {
fn pretty_print(&self) -> (&'static str, String) {
match self {
Expand Down Expand Up @@ -316,7 +319,7 @@ fn debug_dump_memory(mem: &Memory) {
}
})
.collect();
eprintln!("{}", Table::new(table_data).with(Style::sharp()));
Table::new(table_data).with(Style::sharp()).to_string()
}

fn new_id() -> ModelingCmdId {
Expand Down

0 comments on commit 92b1474

Please sign in to comment.