#![cfg(not(feature = "no_optimize"))] use rhai::{Engine, EvalAltResult, OptimizationLevel, RegisterFn, INT}; #[test] fn test_optimizer_run() -> Result<(), Box> { fn run_test(engine: &mut Engine) -> Result<(), Box> { assert_eq!(engine.eval::(r"if true { 42 } else { 123 }")?, 42); assert_eq!( engine.eval::(r"if 1 == 1 || 2 > 3 { 42 } else { 123 }")?, 42 ); assert_eq!( engine.eval::(r#"const abc = "hello"; if abc < "foo" { 42 } else { 123 }"#)?, 123 ); Ok(()) } let mut engine = Engine::new(); engine.set_optimization_level(OptimizationLevel::None); run_test(&mut engine)?; engine.set_optimization_level(OptimizationLevel::Simple); run_test(&mut engine)?; engine.set_optimization_level(OptimizationLevel::Full); run_test(&mut engine)?; // Override == operator engine.register_fn("==", |_x: INT, _y: INT| false); engine.set_optimization_level(OptimizationLevel::Simple); assert_eq!( engine.eval::(r"if 1 == 1 || 2 > 3 { 42 } else { 123 }")?, 123 ); engine.set_optimization_level(OptimizationLevel::Full); assert_eq!( engine.eval::(r"if 1 == 1 || 2 > 3 { 42 } else { 123 }")?, 123 ); Ok(()) } #[test] fn test_optimizer_parse() -> Result<(), Box> { let mut engine = Engine::new(); engine.set_optimization_level(OptimizationLevel::Simple); let ast = engine.compile("{ const DECISION = false; if DECISION { 42 } else { 123 } }")?; assert!(format!("{:?}", ast).starts_with(r#"AST { source: None, body: [Block([Const(BoolConstant(false, 1:20), Ident("DECISION" @ 1:9), false, 1:3), Expr(IntegerConstant(123, 1:53))], 1:1)], functions: Module("#)); let ast = engine.compile("if 1 == 2 { 42 }")?; assert!(format!("{:?}", ast).starts_with("AST { source: None, body: [], functions: Module(")); engine.set_optimization_level(OptimizationLevel::Full); let ast = engine.compile("abs(-42)")?; assert!(format!("{:?}", ast) .starts_with(r"AST { source: None, body: [Expr(IntegerConstant(42, 1:1))]")); Ok(()) }