Factor out a struct for the result of parse_execute (#5629)

Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
Nick Cameron
2025-03-08 08:00:55 +13:00
committed by GitHub
parent f7c192b64b
commit 25cc5581be
3 changed files with 156 additions and 73 deletions

View File

@ -252,7 +252,7 @@ fn generate_changed_program(old_ast: Node<Program>, mut new_ast: Node<Program>,
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::execution::parse_execute; use crate::execution::{parse_execute, ExecTestResults};
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn test_get_changed_program_same_code() { async fn test_get_changed_program_same_code() {
@ -268,16 +268,16 @@ firstSketch = startSketchOn('XY')
// Remove the end face for the extrusion. // Remove the end face for the extrusion.
shell(firstSketch, faces = ['end'], thickness = 0.25)"#; shell(firstSketch, faces = ['end'], thickness = 0.25)"#;
let (program, _, ctx, _) = parse_execute(new).await.unwrap(); let ExecTestResults { program, exec_ctxt, .. } = parse_execute(new).await.unwrap();
let result = get_changed_program( let result = get_changed_program(
CacheInformation { CacheInformation {
ast: &program.ast, ast: &program.ast,
settings: &ctx.settings, settings: &exec_ctxt.settings,
}, },
CacheInformation { CacheInformation {
ast: &program.ast, ast: &program.ast,
settings: &ctx.settings, settings: &exec_ctxt.settings,
}, },
) )
.await; .await;
@ -311,18 +311,18 @@ firstSketch = startSketchOn('XY')
// Remove the end face for the extrusion. // Remove the end face for the extrusion.
shell(firstSketch, faces = ['end'], thickness = 0.25)"#; shell(firstSketch, faces = ['end'], thickness = 0.25)"#;
let (program_old, _, ctx, _) = parse_execute(old).await.unwrap(); let ExecTestResults { program, exec_ctxt, .. } = parse_execute(old).await.unwrap();
let program_new = crate::Program::parse_no_errs(new).unwrap(); let program_new = crate::Program::parse_no_errs(new).unwrap();
let result = get_changed_program( let result = get_changed_program(
CacheInformation { CacheInformation {
ast: &program_old.ast, ast: &program.ast,
settings: &ctx.settings, settings: &exec_ctxt.settings,
}, },
CacheInformation { CacheInformation {
ast: &program_new.ast, ast: &program_new.ast,
settings: &ctx.settings, settings: &exec_ctxt.settings,
}, },
) )
.await; .await;
@ -356,18 +356,18 @@ firstSketch = startSketchOn('XY')
// Remove the end face for the extrusion. // Remove the end face for the extrusion.
shell(firstSketch, faces = ['end'], thickness = 0.25)"#; shell(firstSketch, faces = ['end'], thickness = 0.25)"#;
let (program, _, ctx, _) = parse_execute(old).await.unwrap(); let ExecTestResults { program, exec_ctxt, .. } = parse_execute(old).await.unwrap();
let program_new = crate::Program::parse_no_errs(new).unwrap(); let program_new = crate::Program::parse_no_errs(new).unwrap();
let result = get_changed_program( let result = get_changed_program(
CacheInformation { CacheInformation {
ast: &program.ast, ast: &program.ast,
settings: &ctx.settings, settings: &exec_ctxt.settings,
}, },
CacheInformation { CacheInformation {
ast: &program_new.ast, ast: &program_new.ast,
settings: &ctx.settings, settings: &exec_ctxt.settings,
}, },
) )
.await; .await;
@ -405,18 +405,18 @@ firstSketch = startSketchOn('XY')
// Remove the end face for the extrusion. // Remove the end face for the extrusion.
shell(firstSketch, faces = ['end'], thickness = 0.25)"#; shell(firstSketch, faces = ['end'], thickness = 0.25)"#;
let (program, _, ctx, _) = parse_execute(old).await.unwrap(); let ExecTestResults { program, exec_ctxt, .. } = parse_execute(old).await.unwrap();
let program_new = crate::Program::parse_no_errs(new).unwrap(); let program_new = crate::Program::parse_no_errs(new).unwrap();
let result = get_changed_program( let result = get_changed_program(
CacheInformation { CacheInformation {
ast: &program.ast, ast: &program.ast,
settings: &ctx.settings, settings: &exec_ctxt.settings,
}, },
CacheInformation { CacheInformation {
ast: &program_new.ast, ast: &program_new.ast,
settings: &ctx.settings, settings: &exec_ctxt.settings,
}, },
) )
.await; .await;
@ -439,10 +439,12 @@ firstSketch = startSketchOn('XY')
// Remove the end face for the extrusion. // Remove the end face for the extrusion.
shell(firstSketch, faces = ['end'], thickness = 0.25)"#; shell(firstSketch, faces = ['end'], thickness = 0.25)"#;
let (program, _, mut ctx, _) = parse_execute(new).await.unwrap(); let ExecTestResults {
program, mut exec_ctxt, ..
} = parse_execute(new).await.unwrap();
// Change the settings to cm. // Change the settings to cm.
ctx.settings.units = crate::UnitLength::Cm; exec_ctxt.settings.units = crate::UnitLength::Cm;
let result = get_changed_program( let result = get_changed_program(
CacheInformation { CacheInformation {
@ -451,7 +453,7 @@ shell(firstSketch, faces = ['end'], thickness = 0.25)"#;
}, },
CacheInformation { CacheInformation {
ast: &program.ast, ast: &program.ast,
settings: &ctx.settings, settings: &exec_ctxt.settings,
}, },
) )
.await; .await;
@ -481,10 +483,12 @@ firstSketch = startSketchOn('XY')
// Remove the end face for the extrusion. // Remove the end face for the extrusion.
shell(firstSketch, faces = ['end'], thickness = 0.25)"#; shell(firstSketch, faces = ['end'], thickness = 0.25)"#;
let (program, _, mut ctx, _) = parse_execute(new).await.unwrap(); let ExecTestResults {
program, mut exec_ctxt, ..
} = parse_execute(new).await.unwrap();
// Change the settings. // Change the settings.
ctx.settings.show_grid = !ctx.settings.show_grid; exec_ctxt.settings.show_grid = !exec_ctxt.settings.show_grid;
let result = get_changed_program( let result = get_changed_program(
CacheInformation { CacheInformation {
@ -493,7 +497,7 @@ shell(firstSketch, faces = ['end'], thickness = 0.25)"#;
}, },
CacheInformation { CacheInformation {
ast: &program.ast, ast: &program.ast,
settings: &ctx.settings, settings: &exec_ctxt.settings,
}, },
) )
.await; .await;
@ -516,10 +520,12 @@ firstSketch = startSketchOn('XY')
// Remove the end face for the extrusion. // Remove the end face for the extrusion.
shell(firstSketch, faces = ['end'], thickness = 0.25)"#; shell(firstSketch, faces = ['end'], thickness = 0.25)"#;
let (program, _, mut ctx, _) = parse_execute(new).await.unwrap(); let ExecTestResults {
program, mut exec_ctxt, ..
} = parse_execute(new).await.unwrap();
// Change the settings. // Change the settings.
ctx.settings.highlight_edges = !ctx.settings.highlight_edges; exec_ctxt.settings.highlight_edges = !exec_ctxt.settings.highlight_edges;
let result = get_changed_program( let result = get_changed_program(
CacheInformation { CacheInformation {
@ -528,7 +534,7 @@ shell(firstSketch, faces = ['end'], thickness = 0.25)"#;
}, },
CacheInformation { CacheInformation {
ast: &program.ast, ast: &program.ast,
settings: &ctx.settings, settings: &exec_ctxt.settings,
}, },
) )
.await; .await;
@ -536,8 +542,8 @@ shell(firstSketch, faces = ['end'], thickness = 0.25)"#;
assert_eq!(result, CacheResult::NoAction(true)); assert_eq!(result, CacheResult::NoAction(true));
// Change the settings back. // Change the settings back.
let old_settings = ctx.settings.clone(); let old_settings = exec_ctxt.settings.clone();
ctx.settings.highlight_edges = !ctx.settings.highlight_edges; exec_ctxt.settings.highlight_edges = !exec_ctxt.settings.highlight_edges;
let result = get_changed_program( let result = get_changed_program(
CacheInformation { CacheInformation {
@ -546,7 +552,7 @@ shell(firstSketch, faces = ['end'], thickness = 0.25)"#;
}, },
CacheInformation { CacheInformation {
ast: &program.ast, ast: &program.ast,
settings: &ctx.settings, settings: &exec_ctxt.settings,
}, },
) )
.await; .await;
@ -554,8 +560,8 @@ shell(firstSketch, faces = ['end'], thickness = 0.25)"#;
assert_eq!(result, CacheResult::NoAction(true)); assert_eq!(result, CacheResult::NoAction(true));
// Change the settings back. // Change the settings back.
let old_settings = ctx.settings.clone(); let old_settings = exec_ctxt.settings.clone();
ctx.settings.highlight_edges = !ctx.settings.highlight_edges; exec_ctxt.settings.highlight_edges = !exec_ctxt.settings.highlight_edges;
let result = get_changed_program( let result = get_changed_program(
CacheInformation { CacheInformation {
@ -564,7 +570,7 @@ shell(firstSketch, faces = ['end'], thickness = 0.25)"#;
}, },
CacheInformation { CacheInformation {
ast: &program.ast, ast: &program.ast,
settings: &ctx.settings, settings: &exec_ctxt.settings,
}, },
) )
.await; .await;
@ -583,7 +589,7 @@ startSketchOn('XY')
startSketchOn('XY') startSketchOn('XY')
"#; "#;
let (program, _, ctx, _) = parse_execute(old_code).await.unwrap(); let ExecTestResults { program, exec_ctxt, .. } = parse_execute(old_code).await.unwrap();
let mut new_program = crate::Program::parse_no_errs(new_code).unwrap(); let mut new_program = crate::Program::parse_no_errs(new_code).unwrap();
new_program.compute_digest(); new_program.compute_digest();
@ -591,11 +597,11 @@ startSketchOn('XY')
let result = get_changed_program( let result = get_changed_program(
CacheInformation { CacheInformation {
ast: &program.ast, ast: &program.ast,
settings: &ctx.settings, settings: &exec_ctxt.settings,
}, },
CacheInformation { CacheInformation {
ast: &new_program.ast, ast: &new_program.ast,
settings: &ctx.settings, settings: &exec_ctxt.settings,
}, },
) )
.await; .await;

View File

@ -2105,9 +2105,11 @@ p = {
"#; "#;
let result = parse_execute(program).await.unwrap(); let result = parse_execute(program).await.unwrap();
let mem = result.3.stack(); let mem = result.exec_state.stack();
assert!(matches!( assert!(matches!(
mem.memory.get_from("p", result.1, SourceRange::default(), 0).unwrap(), mem.memory
.get_from("p", result.mem_env, SourceRange::default(), 0)
.unwrap(),
KclValue::Plane { .. } KclValue::Plane { .. }
)); ));
@ -2143,8 +2145,12 @@ p2 = -p
"#; "#;
let result = parse_execute(program).await.unwrap(); let result = parse_execute(program).await.unwrap();
let mem = result.3.stack(); let mem = result.exec_state.stack();
match mem.memory.get_from("p2", result.1, SourceRange::default(), 0).unwrap() { match mem
.memory
.get_from("p2", result.mem_env, SourceRange::default(), 0)
.unwrap()
{
KclValue::Plane { value } => assert_eq!(value.z_axis.z, -1.0), KclValue::Plane { value } => assert_eq!(value.z_axis.z, -1.0),
_ => unreachable!(), _ => unreachable!(),
} }

View File

@ -888,12 +888,10 @@ impl ExecutorContext {
} }
#[cfg(test)] #[cfg(test)]
pub(crate) async fn parse_execute( pub(crate) async fn parse_execute(code: &str) -> Result<ExecTestResults, KclError> {
code: &str,
) -> Result<(crate::Program, EnvironmentRef, ExecutorContext, ExecState), KclError> {
let program = crate::Program::parse_no_errs(code)?; let program = crate::Program::parse_no_errs(code)?;
let ctx = ExecutorContext { let exec_ctxt = ExecutorContext {
engine: Arc::new(Box::new( engine: Arc::new(Box::new(
crate::engine::conn_mock::EngineConnection::new().await.map_err(|err| { crate::engine::conn_mock::EngineConnection::new().await.map_err(|err| {
KclError::Internal(crate::errors::KclErrorDetails { KclError::Internal(crate::errors::KclErrorDetails {
@ -907,10 +905,24 @@ pub(crate) async fn parse_execute(
settings: Default::default(), settings: Default::default(),
context_type: ContextType::Mock, context_type: ContextType::Mock,
}; };
let mut exec_state = ExecState::new(&ctx.settings); let mut exec_state = ExecState::new(&exec_ctxt.settings);
let result = ctx.run(&program, &mut exec_state).await?; let result = exec_ctxt.run(&program, &mut exec_state).await?;
Ok((program, result.0, ctx, exec_state)) Ok(ExecTestResults {
program,
mem_env: result.0,
exec_ctxt,
exec_state,
})
}
#[cfg(test)]
#[derive(Debug)]
pub(crate) struct ExecTestResults {
program: crate::Program,
mem_env: EnvironmentRef,
exec_ctxt: ExecutorContext,
exec_state: ExecState,
} }
#[cfg(test)] #[cfg(test)]
@ -933,8 +945,8 @@ mod tests {
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn test_execute_warn() { async fn test_execute_warn() {
let text = "@blah"; let text = "@blah";
let (_, _, _, exec_state) = parse_execute(text).await.unwrap(); let result = parse_execute(text).await.unwrap();
let errs = exec_state.errors(); let errs = result.exec_state.errors();
assert_eq!(errs.len(), 1); assert_eq!(errs.len(), 1);
assert_eq!(errs[0].severity, crate::errors::Severity::Warning); assert_eq!(errs[0].severity, crate::errors::Severity::Warning);
assert!( assert!(
@ -947,8 +959,8 @@ mod tests {
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn test_warn_on_deprecated() { async fn test_warn_on_deprecated() {
let text = "p = pi()"; let text = "p = pi()";
let (_, _, _, exec_state) = parse_execute(text).await.unwrap(); let result = parse_execute(text).await.unwrap();
let errs = exec_state.errors(); let errs = result.exec_state.errors();
assert_eq!(errs.len(), 1); assert_eq!(errs.len(), 1);
assert_eq!(errs[0].severity, crate::errors::Severity::Warning); assert_eq!(errs[0].severity, crate::errors::Severity::Warning);
assert!( assert!(
@ -1324,8 +1336,8 @@ const answer = returnX()"#;
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn test_override_prelude() { async fn test_override_prelude() {
let text = "PI = 3.0"; let text = "PI = 3.0";
let (_, _, _, exec_state) = parse_execute(text).await.unwrap(); let result = parse_execute(text).await.unwrap();
let errs = exec_state.errors(); let errs = result.exec_state.errors();
assert!(errs.is_empty()); assert!(errs.is_empty());
} }
@ -1391,50 +1403,79 @@ let shape = layer() |> patternTransform(instances = 10, transform = transform)
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn test_math_execute_with_functions() { async fn test_math_execute_with_functions() {
let ast = r#"const myVar = 2 + min(100, -1 + legLen(5, 3))"#; let ast = r#"const myVar = 2 + min(100, -1 + legLen(5, 3))"#;
let (_, env, _, exec_state) = parse_execute(ast).await.unwrap(); let result = parse_execute(ast).await.unwrap();
assert_eq!(5.0, mem_get_json(exec_state.stack(), env, "myVar").as_f64().unwrap()); assert_eq!(
5.0,
mem_get_json(result.exec_state.stack(), result.mem_env, "myVar")
.as_f64()
.unwrap()
);
} }
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn test_math_execute() { async fn test_math_execute() {
let ast = r#"const myVar = 1 + 2 * (3 - 4) / -5 + 6"#; let ast = r#"const myVar = 1 + 2 * (3 - 4) / -5 + 6"#;
let (_, env, _, exec_state) = parse_execute(ast).await.unwrap(); let result = parse_execute(ast).await.unwrap();
assert_eq!(7.4, mem_get_json(exec_state.stack(), env, "myVar").as_f64().unwrap()); assert_eq!(
7.4,
mem_get_json(result.exec_state.stack(), result.mem_env, "myVar")
.as_f64()
.unwrap()
);
} }
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn test_math_execute_start_negative() { async fn test_math_execute_start_negative() {
let ast = r#"const myVar = -5 + 6"#; let ast = r#"const myVar = -5 + 6"#;
let (_, env, _, exec_state) = parse_execute(ast).await.unwrap(); let result = parse_execute(ast).await.unwrap();
assert_eq!(1.0, mem_get_json(exec_state.stack(), env, "myVar").as_f64().unwrap()); assert_eq!(
1.0,
mem_get_json(result.exec_state.stack(), result.mem_env, "myVar")
.as_f64()
.unwrap()
);
} }
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn test_math_execute_with_pi() { async fn test_math_execute_with_pi() {
let ast = r#"const myVar = PI * 2"#; let ast = r#"const myVar = PI * 2"#;
let (_, env, _, exec_state) = parse_execute(ast).await.unwrap(); let result = parse_execute(ast).await.unwrap();
assert_eq!( assert_eq!(
std::f64::consts::TAU, std::f64::consts::TAU,
mem_get_json(exec_state.stack(), env, "myVar").as_f64().unwrap() mem_get_json(result.exec_state.stack(), result.mem_env, "myVar")
.as_f64()
.unwrap()
); );
} }
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn test_math_define_decimal_without_leading_zero() { async fn test_math_define_decimal_without_leading_zero() {
let ast = r#"let thing = .4 + 7"#; let ast = r#"let thing = .4 + 7"#;
let (_, env, _, exec_state) = parse_execute(ast).await.unwrap(); let result = parse_execute(ast).await.unwrap();
assert_eq!(7.4, mem_get_json(exec_state.stack(), env, "thing").as_f64().unwrap()); assert_eq!(
7.4,
mem_get_json(result.exec_state.stack(), result.mem_env, "thing")
.as_f64()
.unwrap()
);
} }
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn test_unit_default() { async fn test_unit_default() {
let ast = r#"const inMm = 25.4 * mm() let ast = r#"const inMm = 25.4 * mm()
const inInches = 1.0 * inch()"#; const inInches = 1.0 * inch()"#;
let (_, env, _, exec_state) = parse_execute(ast).await.unwrap(); let result = parse_execute(ast).await.unwrap();
assert_eq!(25.4, mem_get_json(exec_state.stack(), env, "inMm").as_f64().unwrap());
assert_eq!( assert_eq!(
25.4, 25.4,
mem_get_json(exec_state.stack(), env, "inInches").as_f64().unwrap() mem_get_json(result.exec_state.stack(), result.mem_env, "inMm")
.as_f64()
.unwrap()
);
assert_eq!(
25.4,
mem_get_json(result.exec_state.stack(), result.mem_env, "inInches")
.as_f64()
.unwrap()
); );
} }
@ -1443,12 +1484,20 @@ const inInches = 1.0 * inch()"#;
let ast = r#"@settings(defaultLengthUnit = inch) let ast = r#"@settings(defaultLengthUnit = inch)
const inMm = 25.4 * mm() const inMm = 25.4 * mm()
const inInches = 1.0 * inch()"#; const inInches = 1.0 * inch()"#;
let (_, env, _, exec_state) = parse_execute(ast).await.unwrap(); let result = parse_execute(ast).await.unwrap();
assert_eq!( assert_eq!(
1.0, 1.0,
mem_get_json(exec_state.stack(), env, "inMm").as_f64().unwrap().round() mem_get_json(result.exec_state.stack(), result.mem_env, "inMm")
.as_f64()
.unwrap()
.round()
);
assert_eq!(
1.0,
mem_get_json(result.exec_state.stack(), result.mem_env, "inInches")
.as_f64()
.unwrap()
); );
assert_eq!(1.0, mem_get_json(exec_state.stack(), env, "inInches").as_f64().unwrap());
} }
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
@ -1456,12 +1505,20 @@ const inInches = 1.0 * inch()"#;
let ast = r#"@settings(defaultLengthUnit = in) let ast = r#"@settings(defaultLengthUnit = in)
const inMm = 25.4 * mm() const inMm = 25.4 * mm()
const inInches = 2.0 * inch()"#; const inInches = 2.0 * inch()"#;
let (_, env, _, exec_state) = parse_execute(ast).await.unwrap(); let result = parse_execute(ast).await.unwrap();
assert_eq!( assert_eq!(
1.0, 1.0,
mem_get_json(exec_state.stack(), env, "inMm").as_f64().unwrap().round() mem_get_json(result.exec_state.stack(), result.mem_env, "inMm")
.as_f64()
.unwrap()
.round()
);
assert_eq!(
2.0,
mem_get_json(result.exec_state.stack(), result.mem_env, "inInches")
.as_f64()
.unwrap()
); );
assert_eq!(2.0, mem_get_json(exec_state.stack(), env, "inInches").as_f64().unwrap());
} }
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
@ -1500,17 +1557,31 @@ fn check = (x) => {
} }
check(false) check(false)
"#; "#;
let (_, env, _, exec_state) = parse_execute(ast).await.unwrap(); let result = parse_execute(ast).await.unwrap();
assert_eq!( assert_eq!(
false, false,
mem_get_json(exec_state.stack(), env, "notTrue").as_bool().unwrap() mem_get_json(result.exec_state.stack(), result.mem_env, "notTrue")
.as_bool()
.unwrap()
); );
assert_eq!( assert_eq!(
true, true,
mem_get_json(exec_state.stack(), env, "notFalse").as_bool().unwrap() mem_get_json(result.exec_state.stack(), result.mem_env, "notFalse")
.as_bool()
.unwrap()
);
assert_eq!(
true,
mem_get_json(result.exec_state.stack(), result.mem_env, "c")
.as_bool()
.unwrap()
);
assert_eq!(
false,
mem_get_json(result.exec_state.stack(), result.mem_env, "d")
.as_bool()
.unwrap()
); );
assert_eq!(true, mem_get_json(exec_state.stack(), env, "c").as_bool().unwrap());
assert_eq!(false, mem_get_json(exec_state.stack(), env, "d").as_bool().unwrap());
} }
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]