rhai/tests/assignments.rs

68 lines
2.2 KiB
Rust
Raw Normal View History

2020-12-29 03:41:20 +01:00
use rhai::{Engine, EvalAltResult, ParseErrorType, INT};
#[test]
fn test_assignments() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert_eq!(engine.eval::<INT>("let x = 42; x = 123; x")?, 123);
assert_eq!(engine.eval::<INT>("let x = 42; x += 123; x")?, 165);
#[cfg(not(feature = "no_index"))]
assert_eq!(engine.eval::<INT>("let x = [42]; x[0] += 123; x[0]")?, 165);
#[cfg(not(feature = "no_object"))]
assert_eq!(engine.eval::<INT>("let x = #{a:42}; x.a += 123; x.a")?, 165);
Ok(())
}
#[test]
fn test_assignments_bad_lhs() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert_eq!(
2023-04-10 17:23:59 +02:00
*engine.compile("(x+y) = 42;").unwrap_err().err_type(),
2022-08-21 11:35:44 +02:00
ParseErrorType::AssignmentToInvalidLHS(String::new())
2020-12-29 03:41:20 +01:00
);
assert_eq!(
2023-04-10 17:23:59 +02:00
*engine.compile("foo(x) = 42;").unwrap_err().err_type(),
2022-08-21 11:35:44 +02:00
ParseErrorType::AssignmentToInvalidLHS(String::new())
2020-12-29 03:41:20 +01:00
);
assert_eq!(
2023-04-10 17:23:59 +02:00
*engine.compile("true = 42;").unwrap_err().err_type(),
2022-08-21 11:35:44 +02:00
ParseErrorType::AssignmentToConstant(String::new())
2020-12-29 03:41:20 +01:00
);
assert_eq!(
2023-04-10 17:23:59 +02:00
*engine.compile("123 = 42;").unwrap_err().err_type(),
2022-08-21 11:35:44 +02:00
ParseErrorType::AssignmentToConstant(String::new())
2020-12-29 03:41:20 +01:00
);
#[cfg(not(feature = "no_object"))]
{
assert_eq!(
2023-04-10 17:23:59 +02:00
*engine.compile("x.foo() = 42;").unwrap_err().err_type(),
2022-08-21 11:35:44 +02:00
ParseErrorType::AssignmentToInvalidLHS(String::new())
2020-12-29 03:41:20 +01:00
);
assert_eq!(
2023-04-10 17:23:59 +02:00
*engine.compile("x.foo().x.y = 42;").unwrap_err().err_type(),
2022-08-21 11:35:44 +02:00
ParseErrorType::AssignmentToInvalidLHS(String::new())
2020-12-29 03:41:20 +01:00
);
assert_eq!(
2023-04-10 17:23:59 +02:00
*engine.compile("x.y.z.foo() = 42;").unwrap_err().err_type(),
2022-08-21 11:35:44 +02:00
ParseErrorType::AssignmentToInvalidLHS(String::new())
2020-12-29 03:41:20 +01:00
);
#[cfg(not(feature = "no_index"))]
assert_eq!(
2023-04-10 17:23:59 +02:00
*engine.compile("x.foo()[0] = 42;").unwrap_err().err_type(),
2022-08-21 11:35:44 +02:00
ParseErrorType::AssignmentToInvalidLHS(String::new())
2020-12-29 03:41:20 +01:00
);
#[cfg(not(feature = "no_index"))]
assert_eq!(
2023-04-10 17:23:59 +02:00
*engine.compile("x[y].z.foo() = 42;").unwrap_err().err_type(),
2022-08-21 11:35:44 +02:00
ParseErrorType::AssignmentToInvalidLHS(String::new())
2020-12-29 03:41:20 +01:00
);
}
Ok(())
}