Add constants.

This commit is contained in:
Stephen Chung
2020-03-13 18:12:41 +08:00
parent 9bd66c7db3
commit 9844ae8665
9 changed files with 426 additions and 155 deletions

View File

@@ -11,12 +11,12 @@ fn test_chars() -> Result<(), EvalAltResult> {
{
assert_eq!(engine.eval::<char>(r#"let x="hello"; x[2]"#)?, 'l');
assert_eq!(
engine.eval::<String>(r#"let x="hello"; x[2]='$'; x"#)?,
engine.eval::<String>(r#"let y="hello"; y[2]='$'; y"#)?,
"he$lo".to_string()
);
}
assert!(engine.eval::<char>("'\\uhello'").is_err());
assert!(engine.eval::<char>(r"'\uhello'").is_err());
assert!(engine.eval::<char>("''").is_err());
Ok(())

16
tests/constants.rs Normal file
View File

@@ -0,0 +1,16 @@
use rhai::{Engine, EvalAltResult};
#[test]
fn test_constant() -> Result<(), EvalAltResult> {
let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("const x = 123; x")?, 123);
match engine.eval::<i64>("const x = 123; x = 42; x") {
Err(EvalAltResult::ErrorAssignmentToConstant(var, _)) if var == "x" => (),
Err(err) => return Err(err),
Ok(_) => panic!("expecting compilation error"),
}
Ok(())
}