rhai/tests/tokens.rs

113 lines
3.0 KiB
Rust
Raw Permalink Normal View History

use rhai::{Engine, EvalAltResult, ParseErrorType, INT};
2020-07-05 09:23:51 +02:00
#[test]
fn test_tokens_disabled() {
let mut engine = Engine::new();
engine.disable_symbol("if"); // disable the 'if' keyword
assert!(matches!(
engine
.compile("let x = if true { 42 } else { 0 };")
2023-04-10 17:23:59 +02:00
.unwrap_err()
.err_type(),
ParseErrorType::Reserved(err) if err == "if"
2020-07-05 09:23:51 +02:00
));
engine.disable_symbol("+="); // disable the '+=' operator
2020-11-02 05:50:27 +01:00
assert_eq!(
*engine
.compile("let x = 40 + 2; x += 1;")
2023-04-10 17:23:59 +02:00
.unwrap_err()
.err_type(),
2020-12-26 16:21:09 +01:00
ParseErrorType::UnknownOperator("+=".to_string())
2020-11-02 05:50:27 +01:00
);
2020-12-26 16:21:09 +01:00
assert!(matches!(
2023-04-10 17:23:59 +02:00
engine.compile("let x = += 0;").unwrap_err().err_type(),
ParseErrorType::Reserved(err) if err == "+="
2020-12-26 16:21:09 +01:00
));
2020-07-05 09:23:51 +02:00
}
2020-07-05 11:41:45 +02:00
2022-07-05 16:59:03 +02:00
#[cfg(not(feature = "no_custom_syntax"))]
2020-07-05 11:41:45 +02:00
#[test]
2020-12-26 16:21:09 +01:00
fn test_tokens_custom_operator_identifiers() -> Result<(), Box<EvalAltResult>> {
2020-07-05 11:41:45 +02:00
let mut engine = Engine::new();
// Register a custom operator called `foo` and give it
2020-07-06 07:01:57 +02:00
// a precedence of 160 (i.e. between +|- and *|/).
engine.register_custom_operator("foo", 160).unwrap();
2020-07-05 11:41:45 +02:00
// Register a binary function named `foo`
engine.register_fn("foo", |x: INT, y: INT| (x * y) - (x + y));
assert_eq!(
engine.eval_expression::<INT>("1 + 2 * 3 foo 4 - 5 / 6")?,
15
);
2020-07-13 13:38:50 +02:00
#[cfg(not(feature = "no_function"))]
2020-07-05 11:41:45 +02:00
assert_eq!(
engine.eval::<INT>(
2021-04-20 06:01:35 +02:00
"
2020-07-05 11:41:45 +02:00
fn foo(x, y) { y - x }
1 + 2 * 3 foo 4 - 5 / 6
"
)?,
-1
);
Ok(())
}
2020-07-29 01:25:37 +02:00
2022-07-05 16:59:03 +02:00
#[cfg(not(feature = "no_custom_syntax"))]
2020-12-26 16:21:09 +01:00
#[test]
fn test_tokens_custom_operator_symbol() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
// Register a custom operator `#` and give it
// a precedence of 160 (i.e. between +|- and *|/).
engine.register_custom_operator("#", 160).unwrap();
// Register a binary function named `#`
engine.register_fn("#", |x: INT, y: INT| (x * y) - (x + y));
assert_eq!(engine.eval_expression::<INT>("1 + 2 * 3 # 4 - 5 / 6")?, 15);
// Register a custom operator named `=>`
assert!(engine.register_custom_operator("=>", 160).is_err());
engine.disable_symbol("=>");
engine.register_custom_operator("=>", 160).unwrap();
engine.register_fn("=>", |x: INT, y: INT| (x * y) - (x + y));
assert_eq!(engine.eval_expression::<INT>("1 + 2 * 3 => 4 - 5 / 6")?, 15);
Ok(())
}
2020-07-29 01:25:37 +02:00
#[test]
fn test_tokens_unicode_xid_ident() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
let result = engine.eval::<INT>(
2021-04-20 06:01:35 +02:00
"
fn () { 42 }
()
",
2020-07-29 01:25:37 +02:00
);
#[cfg(feature = "unicode-xid-ident")]
assert_eq!(result?, 42);
#[cfg(not(feature = "unicode-xid-ident"))]
assert!(result.is_err());
2020-07-29 03:41:44 +02:00
let result = engine.eval::<INT>(
2021-04-20 06:01:35 +02:00
"
fn _1() { 1 }
_1()
",
2020-07-29 03:41:44 +02:00
);
assert!(result.is_err());
2020-07-29 01:25:37 +02:00
Ok(())
}