Fix custom syntax test.
This commit is contained in:
parent
8a0d0e3e20
commit
07af296ab0
@ -1,6 +1,4 @@
|
|||||||
use rhai::{
|
use rhai::{Engine, EvalAltResult, EvalContext, Expression, ParseErrorType, Scope, INT};
|
||||||
Engine, EvalAltResult, EvalContext, Expression, ParseError, ParseErrorType, Scope, INT,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
|
fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
|
||||||
@ -19,43 +17,35 @@ fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
|
|||||||
ParseErrorType::Reserved(err) if err == "while"
|
ParseErrorType::Reserved(err) if err == "while"
|
||||||
));
|
));
|
||||||
|
|
||||||
engine
|
engine.register_custom_syntax(
|
||||||
.register_custom_syntax(
|
&[
|
||||||
&[
|
"do", "|", "$ident$", "|", "->", "$block$", "while", "$expr$",
|
||||||
"do", "|", "$ident$", "|", "->", "$block$", "while", "$expr$",
|
],
|
||||||
],
|
1,
|
||||||
1,
|
|engine: &Engine, context: &mut EvalContext, scope: &mut Scope, inputs: &[Expression]| {
|
||||||
|engine: &Engine,
|
let var_name = inputs[0].get_variable_name().unwrap().to_string();
|
||||||
context: &mut EvalContext,
|
let stmt = inputs.get(1).unwrap();
|
||||||
scope: &mut Scope,
|
let expr = inputs.get(2).unwrap();
|
||||||
inputs: &[Expression]| {
|
|
||||||
let var_name = inputs[0].get_variable_name().unwrap().to_string();
|
|
||||||
let stmt = inputs.get(1).unwrap();
|
|
||||||
let expr = inputs.get(2).unwrap();
|
|
||||||
|
|
||||||
scope.push(var_name, 0 as INT);
|
scope.push(var_name, 0 as INT);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
engine.eval_expression_tree(context, scope, stmt)?;
|
engine.eval_expression_tree(context, scope, stmt)?;
|
||||||
|
|
||||||
if !engine
|
if !engine
|
||||||
.eval_expression_tree(context, scope, expr)?
|
.eval_expression_tree(context, scope, expr)?
|
||||||
.as_bool()
|
.as_bool()
|
||||||
.map_err(|_| {
|
.map_err(|_| {
|
||||||
EvalAltResult::ErrorBooleanArgMismatch(
|
EvalAltResult::ErrorBooleanArgMismatch("do-while".into(), expr.position())
|
||||||
"do-while".into(),
|
})?
|
||||||
expr.position(),
|
{
|
||||||
)
|
break;
|
||||||
})?
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(().into())
|
Ok(().into())
|
||||||
},
|
},
|
||||||
)
|
)?;
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// 'while' is now a custom keyword so this it can no longer be a variable
|
// 'while' is now a custom keyword so this it can no longer be a variable
|
||||||
engine.consume("let while = 0").expect_err("should error");
|
engine.consume("let while = 0").expect_err("should error");
|
||||||
@ -71,10 +61,13 @@ fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// The first symbol must be an identifier
|
// The first symbol must be an identifier
|
||||||
assert!(matches!(
|
assert_eq!(
|
||||||
*engine.register_custom_syntax(&["!"], 0, |_, _, _, _| Ok(().into())).expect_err("should error"),
|
*engine
|
||||||
ParseError(err, _) if *err == ParseErrorType::BadInput("Improper symbol for custom syntax: '!'".to_string())
|
.register_custom_syntax(&["!"], 0, |_, _, _, _| Ok(().into()))
|
||||||
));
|
.expect_err("should error")
|
||||||
|
.0,
|
||||||
|
ParseErrorType::BadInput("Improper symbol for custom syntax: '!'".to_string())
|
||||||
|
);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user