diff --git a/tests/custom_syntax.rs b/tests/custom_syntax.rs index 7ba2fc59..cb82491b 100644 --- a/tests/custom_syntax.rs +++ b/tests/custom_syntax.rs @@ -21,8 +21,46 @@ fn test_custom_syntax() -> Result<(), Box> { ParseErrorType::Reserved(err) if err == "while" )); + // Implement ternary operator engine.register_custom_syntax( - &[ + ["iff", "$expr$", "?", "$expr$", ":", "$expr$"], + false, + |context, inputs| match context.eval_expression_tree(&inputs[0])?.as_bool() { + Ok(true) => context.eval_expression_tree(&inputs[1]), + Ok(false) => context.eval_expression_tree(&inputs[2]), + Err(typ) => Err(Box::new(EvalAltResult::ErrorMismatchDataType( + "bool".to_string(), + typ.to_string(), + inputs[0].position(), + ))), + }, + )?; + + assert_eq!( + engine.eval::( + " + let x = 42; + let y = iff x > 40 ? 0 : 123; + y + " + )?, + 0 + ); + + assert_eq!( + engine.eval::( + " + let x = 42; + let y = iff x == 0 ? 0 : 123; + y + " + )?, + 123 + ); + + // Custom syntax + engine.register_custom_syntax( + [ "exec", "[", "$ident$", "$symbol$", "$int$", "]", "->", "$block$", "while", "$expr$", ], true,