Refactor tests.

This commit is contained in:
Stephen Chung 2020-03-02 22:11:56 +08:00
parent ed8d2ac20f
commit 0707fad1ca
27 changed files with 338 additions and 317 deletions

View File

@ -25,7 +25,7 @@
//! //!
//! let mut engine = Engine::new(); //! let mut engine = Engine::new();
//! engine.register_fn("compute_something", compute_something); //! engine.register_fn("compute_something", compute_something);
//! assert_eq!(engine.eval_file::<bool>("my_script.rhai"), Ok(true)); //! assert_eq!(engine.eval_file::<bool>("my_script.rhai").unwrap(), true);
//! ``` //! ```
//! //!
//! [Check out the README on GitHub for more information!](https://github.com/jonathandturner/rhai) //! [Check out the README on GitHub for more information!](https://github.com/jonathandturner/rhai)

View File

@ -1,19 +1,17 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult, RegisterFn};
use rhai::RegisterFn;
#[test] #[test]
fn test_arrays() { fn test_arrays() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("let x = [1, 2, 3]; x[1]"), Ok(2)); assert_eq!(engine.eval::<i64>("let x = [1, 2, 3]; x[1]")?, 2);
assert_eq!( assert_eq!(engine.eval::<i64>("let y = [1, 2, 3]; y[1] = 5; y[1]")?, 5);
engine.eval::<i64>("let y = [1, 2, 3]; y[1] = 5; y[1]"),
Ok(5) Ok(())
);
} }
#[test] #[test]
fn test_array_with_structs() { fn test_array_with_structs() -> Result<(), EvalAltResult> {
#[derive(Clone)] #[derive(Clone)]
struct TestStruct { struct TestStruct {
x: i64, x: i64,
@ -45,7 +43,7 @@ fn test_array_with_structs() {
engine.register_fn("update", TestStruct::update); engine.register_fn("update", TestStruct::update);
engine.register_fn("new_ts", TestStruct::new); engine.register_fn("new_ts", TestStruct::new);
assert_eq!(engine.eval::<i64>("let a = [new_ts()]; a[0].x"), Ok(1)); assert_eq!(engine.eval::<i64>("let a = [new_ts()]; a[0].x")?, 1);
assert_eq!( assert_eq!(
engine.eval::<i64>( engine.eval::<i64>(
@ -53,7 +51,9 @@ fn test_array_with_structs() {
a[0].x = 100; \ a[0].x = 100; \
a[0].update(); \ a[0].update(); \
a[0].x", a[0].x",
), )?,
Ok(1100) 1100
); );
Ok(())
} }

View File

@ -1,20 +1,22 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult};
#[test] #[test]
fn test_binary_ops() { fn test_binary_ops() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("10 % 4"), Ok(2)); assert_eq!(engine.eval::<i64>("10 % 4")?, 2);
assert_eq!(engine.eval::<i64>("10 << 4"), Ok(160)); assert_eq!(engine.eval::<i64>("10 << 4")?, 160);
assert_eq!(engine.eval::<i64>("10 >> 4"), Ok(0)); assert_eq!(engine.eval::<i64>("10 >> 4")?, 0);
assert_eq!(engine.eval::<i64>("10 & 4"), Ok(0)); assert_eq!(engine.eval::<i64>("10 & 4")?, 0);
assert_eq!(engine.eval::<i64>("10 | 4"), Ok(14)); assert_eq!(engine.eval::<i64>("10 | 4")?, 14);
assert_eq!(engine.eval::<i64>("10 ^ 4"), Ok(14)); assert_eq!(engine.eval::<i64>("10 ^ 4")?, 14);
assert_eq!(engine.eval::<bool>("42 == 42"), Ok(true)); assert_eq!(engine.eval::<bool>("42 == 42")?, true);
assert_eq!(engine.eval::<bool>("42 > 42"), Ok(false)); assert_eq!(engine.eval::<bool>("42 > 42")?, false);
// Incompatible types compare to false // Incompatible types compare to false
assert_eq!(engine.eval::<bool>("true == 42"), Ok(false)); assert_eq!(engine.eval::<bool>("true == 42")?, false);
assert_eq!(engine.eval::<bool>(r#""42" == 42"#), Ok(false)); assert_eq!(engine.eval::<bool>(r#""42" == 42"#)?, false);
Ok(())
} }

View File

@ -1,15 +1,15 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult};
#[test] #[test]
fn test_left_shift() { fn test_left_shift() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("4 << 2")?, 16);
assert_eq!(engine.eval::<i64>("4 << 2"), Ok(16)); Ok(())
} }
#[test] #[test]
fn test_right_shift() { fn test_right_shift() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("9 >> 1")?, 4);
assert_eq!(engine.eval::<i64>("9 >> 1"), Ok(4)); Ok(())
} }

View File

@ -1,42 +1,39 @@
use rhai::{Engine, EvalAltResult}; use rhai::{Engine, EvalAltResult};
#[test] #[test]
fn test_bool_op1() { fn test_bool_op1() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<bool>("true && (false || true)"), Ok(true)); assert_eq!(engine.eval::<bool>("true && (false || true)")?, true);
assert_eq!(engine.eval::<bool>("true & (false | true)"), Ok(true)); assert_eq!(engine.eval::<bool>("true & (false | true)")?, true);
Ok(())
} }
#[test] #[test]
fn test_bool_op2() { fn test_bool_op2() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<bool>("false && (false || true)"), Ok(false)); assert_eq!(engine.eval::<bool>("false && (false || true)")?, false);
assert_eq!(engine.eval::<bool>("false & (false | true)"), Ok(false)); assert_eq!(engine.eval::<bool>("false & (false | true)")?, false);
Ok(())
} }
#[test] #[test]
fn test_bool_op3() { fn test_bool_op3() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!( assert!(engine.eval::<bool>("true && (false || 123)").is_err());
engine.eval::<bool>("true && (false || 123)"), assert_eq!(engine.eval::<bool>("true && (true || 123)")?, true);
Err(EvalAltResult::ErrorBooleanArgMismatch("OR".into())) assert!(engine.eval::<bool>("123 && (false || true)").is_err());
); assert_eq!(engine.eval::<bool>("false && (true || 123)")?, false);
assert_eq!(engine.eval::<bool>("true && (true || 123)"), Ok(true)); Ok(())
assert_eq!(
engine.eval::<bool>("123 && (false || true)"),
Err(EvalAltResult::ErrorBooleanArgMismatch("AND".into()))
);
assert_eq!(engine.eval::<bool>("false && (true || 123)"), Ok(false));
} }
#[test] #[test]
fn test_bool_op_short_circuit() { fn test_bool_op_short_circuit() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!( assert_eq!(
@ -47,8 +44,8 @@ fn test_bool_op_short_circuit() {
this() || that(); this() || that();
" "
), )?,
Ok(true) true
); );
assert_eq!( assert_eq!(
@ -59,9 +56,11 @@ fn test_bool_op_short_circuit() {
this() && that(); this() && that();
" "
), )?,
Ok(false) false
); );
Ok(())
} }
#[test] #[test]
@ -70,15 +69,17 @@ fn test_bool_op_no_short_circuit1() {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!( assert_eq!(
engine.eval::<bool>( engine
r" .eval::<bool>(
fn this() { false } r"
fn that() { 9/0 } fn this() { false }
fn that() { 9/0 }
this() | that(); this() | that();
" "
), )
Ok(false) .unwrap(),
false
); );
} }
@ -88,14 +89,16 @@ fn test_bool_op_no_short_circuit2() {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!( assert_eq!(
engine.eval::<bool>( engine
r" .eval::<bool>(
fn this() { false } r"
fn that() { 9/0 } fn this() { false }
fn that() { 9/0 }
this() & that(); this() & that();
" "
), )
Ok(false) .unwrap(),
false
); );
} }

View File

@ -1,24 +1,19 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult};
#[test] #[test]
fn test_chars() { fn test_chars() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<char>("'y'"), Ok('y')); assert_eq!(engine.eval::<char>("'y'")?, 'y');
assert_eq!(engine.eval::<char>("'\\u2764'"), Ok('❤')); assert_eq!(engine.eval::<char>("'\\u2764'")?, '❤');
assert_eq!(engine.eval::<char>(r#"let x="hello"; x[2]"#), Ok('l')); assert_eq!(engine.eval::<char>(r#"let x="hello"; x[2]"#)?, 'l');
assert_eq!( assert_eq!(
engine.eval::<String>(r#"let x="hello"; x[2]='$'; x"#), engine.eval::<String>(r#"let x="hello"; x[2]='$'; x"#)?,
Ok("he$lo".into()) "he$lo".to_string()
); );
match engine.eval::<char>("'\\uhello'") { assert!(engine.eval::<char>("'\\uhello'").is_err());
Err(_) => (), assert!(engine.eval::<char>("''").is_err());
_ => assert!(false),
}
match engine.eval::<char>("''") { Ok(())
Err(_) => (),
_ => assert!(false),
}
} }

View File

@ -1,68 +1,66 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult};
#[test] #[test]
fn test_or_equals() { fn test_or_equals() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("let x = 16; x |= 74; x"), Ok(90)); assert_eq!(engine.eval::<i64>("let x = 16; x |= 74; x")?, 90);
assert_eq!(engine.eval::<bool>("let x = true; x |= false; x"), Ok(true)); assert_eq!(engine.eval::<bool>("let x = true; x |= false; x")?, true);
assert_eq!(engine.eval::<bool>("let x = false; x |= true; x"), Ok(true)); assert_eq!(engine.eval::<bool>("let x = false; x |= true; x")?, true);
Ok(())
} }
#[test] #[test]
fn test_and_equals() { fn test_and_equals() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("let x = 16; x &= 31; x"), Ok(16)); assert_eq!(engine.eval::<i64>("let x = 16; x &= 31; x")?, 16);
assert_eq!( assert_eq!(engine.eval::<bool>("let x = true; x &= false; x")?, false);
engine.eval::<bool>("let x = true; x &= false; x"), assert_eq!(engine.eval::<bool>("let x = false; x &= true; x")?, false);
Ok(false) assert_eq!(engine.eval::<bool>("let x = true; x &= true; x")?, true);
);
assert_eq!( Ok(())
engine.eval::<bool>("let x = false; x &= true; x"),
Ok(false)
);
assert_eq!(engine.eval::<bool>("let x = true; x &= true; x"), Ok(true));
} }
#[test] #[test]
fn test_xor_equals() { fn test_xor_equals() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("let x = 90; x ^= 12; x")?, 86);
assert_eq!(engine.eval::<i64>("let x = 90; x ^= 12; x"), Ok(86)); Ok(())
} }
#[test] #[test]
fn test_multiply_equals() { fn test_multiply_equals() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("let x = 2; x *= 3; x")?, 6);
assert_eq!(engine.eval::<i64>("let x = 2; x *= 3; x"), Ok(6)); Ok(())
} }
#[test] #[test]
fn test_divide_equals() { fn test_divide_equals() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("let x = 6; x /= 2; x")?, 3);
assert_eq!(engine.eval::<i64>("let x = 6; x /= 2; x"), Ok(3)); Ok(())
} }
#[test] #[test]
fn test_left_shift_equals() { fn test_left_shift_equals() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("let x = 9; x >>=1; x")?, 4);
assert_eq!(engine.eval::<i64>("let x = 9; x >>=1; x"), Ok(4)); Ok(())
} }
#[test] #[test]
fn test_right_shift_equals() { fn test_right_shift_equals() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("let x = 4; x<<= 2; x")?, 16);
assert_eq!(engine.eval::<i64>("let x = 4; x<<= 2; x"), Ok(16)); Ok(())
} }
#[test] #[test]
fn test_modulo_equals() { fn test_modulo_equals() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("let x = 10; x %= 4; x")?, 2);
assert_eq!(engine.eval::<i64>("let x = 10; x %= 4; x"), Ok(2)); Ok(())
} }

View File

@ -1,16 +1,17 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult};
use rhai::EvalAltResult;
#[test] #[test]
fn test_decrement() { fn test_decrement() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("let x = 10; x -= 7; x"), Ok(3)); assert_eq!(engine.eval::<i64>("let x = 10; x -= 7; x")?, 3);
assert_eq!( let r = engine.eval::<String>("let s = \"test\"; s -= \"ing\"; s");
engine.eval::<String>("let s = \"test\"; s -= \"ing\"; s"),
Err(EvalAltResult::ErrorFunctionNotFound( match r {
"- (alloc::string::String, alloc::string::String)".to_string() Err(EvalAltResult::ErrorFunctionNotFound(err, _)) if err.starts_with("- ") => (),
)) _ => panic!(),
); }
Ok(())
} }

View File

@ -1,23 +1,24 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult, RegisterFn};
use rhai::RegisterFn;
#[test] #[test]
fn test_float() { fn test_float() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!( assert_eq!(
engine.eval::<bool>("let x = 0.0; let y = 1.0; x < y"), engine.eval::<bool>("let x = 0.0; let y = 1.0; x < y")?,
Ok(true) true
); );
assert_eq!( assert_eq!(
engine.eval::<bool>("let x = 0.0; let y = 1.0; x > y"), engine.eval::<bool>("let x = 0.0; let y = 1.0; x > y")?,
Ok(false) false
); );
assert_eq!(engine.eval::<f64>("let x = 9.9999; x"), Ok(9.9999)); assert_eq!(engine.eval::<f64>("let x = 9.9999; x")?, 9.9999);
Ok(())
} }
#[test] #[test]
fn struct_with_float() { fn struct_with_float() -> Result<(), EvalAltResult> {
#[derive(Clone)] #[derive(Clone)]
struct TestStruct { struct TestStruct {
x: f64, x: f64,
@ -50,11 +51,13 @@ fn struct_with_float() {
engine.register_fn("new_ts", TestStruct::new); engine.register_fn("new_ts", TestStruct::new);
assert_eq!( assert_eq!(
engine.eval::<f64>("let ts = new_ts(); ts.update(); ts.x"), engine.eval::<f64>("let ts = new_ts(); ts.update(); ts.x")?,
Ok(6.789) 6.789
); );
assert_eq!( assert_eq!(
engine.eval::<f64>("let ts = new_ts(); ts.x = 10.1001; ts.x"), engine.eval::<f64>("let ts = new_ts(); ts.x = 10.1001; ts.x")?,
Ok(10.1001) 10.1001
); );
Ok(())
} }

View File

@ -1,7 +1,7 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult};
#[test] #[test]
fn test_for() { fn test_for() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
let script = r" let script = r"
@ -20,5 +20,7 @@ fn test_for() {
sum1 + sum2 sum1 + sum2
"; ";
assert_eq!(engine.eval::<i64>(script).unwrap(), 30); assert_eq!(engine.eval::<i64>(script)?, 30);
Ok(())
} }

View File

@ -1,8 +1,7 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult, RegisterFn};
use rhai::RegisterFn;
#[test] #[test]
fn test_get_set() { fn test_get_set() -> Result<(), EvalAltResult> {
#[derive(Clone)] #[derive(Clone)]
struct TestStruct { struct TestStruct {
x: i64, x: i64,
@ -29,14 +28,13 @@ fn test_get_set() {
engine.register_get_set("x", TestStruct::get_x, TestStruct::set_x); engine.register_get_set("x", TestStruct::get_x, TestStruct::set_x);
engine.register_fn("new_ts", TestStruct::new); engine.register_fn("new_ts", TestStruct::new);
assert_eq!( assert_eq!(engine.eval::<i64>("let a = new_ts(); a.x = 500; a.x")?, 500);
engine.eval::<i64>("let a = new_ts(); a.x = 500; a.x"),
Ok(500) Ok(())
);
} }
#[test] #[test]
fn test_big_get_set() { fn test_big_get_set() -> Result<(), EvalAltResult> {
#[derive(Clone)] #[derive(Clone)]
struct TestChild { struct TestChild {
x: i64, x: i64,
@ -88,7 +86,9 @@ fn test_big_get_set() {
engine.register_fn("new_tp", TestParent::new); engine.register_fn("new_tp", TestParent::new);
assert_eq!( assert_eq!(
engine.eval::<i64>("let a = new_tp(); a.child.x = 500; a.child.x"), engine.eval::<i64>("let a = new_tp(); a.child.x = 500; a.child.x")?,
Ok(500) 500
); );
Ok(())
} }

View File

@ -1,15 +1,15 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult};
#[test] #[test]
fn test_if() { fn test_if() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("if true { 55 }"), Ok(55)); assert_eq!(engine.eval::<i64>("if true { 55 }")?, 55);
assert_eq!(engine.eval::<i64>("if false { 55 } else { 44 }"), Ok(44)); assert_eq!(engine.eval::<i64>("if false { 55 } else { 44 }")?, 44);
assert_eq!(engine.eval::<i64>("if true { 55 } else { 44 }"), Ok(55)); assert_eq!(engine.eval::<i64>("if true { 55 } else { 44 }")?, 55);
assert_eq!( assert_eq!(
engine.eval::<i64>("if false { 55 } else if true { 33 } else { 44 }"), engine.eval::<i64>("if false { 55 } else if true { 33 } else { 44 }")?,
Ok(33) 33
); );
assert_eq!( assert_eq!(
engine.eval::<i64>( engine.eval::<i64>(
@ -21,7 +21,9 @@ fn test_if() {
else if false { 88 } else if false { 88 }
else { 44 } else { 44 }
" "
), )?,
Ok(44) 44
); );
Ok(())
} }

View File

@ -1,12 +1,14 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult};
#[test] #[test]
fn test_increment() { fn test_increment() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("let x = 1; x += 2; x"), Ok(3)); assert_eq!(engine.eval::<i64>("let x = 1; x += 2; x")?, 3);
assert_eq!( assert_eq!(
engine.eval::<String>("let s = \"test\"; s += \"ing\"; s"), engine.eval::<String>("let s = \"test\"; s += \"ing\"; s")?,
Ok("testing".to_string()) "testing".to_string()
); );
Ok(())
} }

View File

@ -1,25 +1,30 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult};
#[test] #[test]
fn test_internal_fn() { fn test_internal_fn() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!( assert_eq!(engine.eval::<i64>("fn addme(a, b) { a+b } addme(3, 4)")?, 7);
engine.eval::<i64>("fn addme(a, b) { a+b } addme(3, 4)"), assert_eq!(engine.eval::<i64>("fn bob() { return 4; 5 } bob()")?, 4);
Ok(7)
); Ok(())
assert_eq!(engine.eval::<i64>("fn bob() { return 4; 5 } bob()"), Ok(4));
} }
#[test] #[test]
fn test_big_internal_fn() { fn test_big_internal_fn() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!( assert_eq!(
engine.eval::<i64>( engine.eval::<i64>(
"fn mathme(a, b, c, d, e, f) { a - b * c + d * e - f \ r"
} mathme(100, 5, 2, 9, 6, 32)", fn mathme(a, b, c, d, e, f) {
), a - b * c + d * e - f
Ok(112) }
mathme(100, 5, 2, 9, 6, 32)
",
)?,
112
); );
Ok(())
} }

View File

@ -1,12 +1,11 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult};
#[test] #[test]
fn test_loop() { fn test_loop() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert!(engine assert!(engine.eval::<bool>(
.eval::<bool>( r"
"
let x = 0; let x = 0;
let i = 0; let i = 0;
@ -22,6 +21,7 @@ fn test_loop() {
x == 45 x == 45
" "
) )?);
.unwrap())
Ok(())
} }

View File

@ -1,8 +1,7 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult, RegisterFn};
use rhai::RegisterFn;
#[test] #[test]
fn test_method_call() { fn test_method_call() -> Result<(), EvalAltResult> {
#[derive(Clone)] #[derive(Clone)]
struct TestStruct { struct TestStruct {
x: i64, x: i64,
@ -25,9 +24,9 @@ fn test_method_call() {
engine.register_fn("update", TestStruct::update); engine.register_fn("update", TestStruct::update);
engine.register_fn("new_ts", TestStruct::new); engine.register_fn("new_ts", TestStruct::new);
if let Ok(result) = engine.eval::<TestStruct>("let x = new_ts(); x.update(); x") { let ts = engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")?;
assert_eq!(result.x, 1001);
} else { assert_eq!(ts.x, 1001);
assert!(false);
} Ok(())
} }

View File

@ -4,12 +4,12 @@ use rhai::{Engine, EvalAltResult, RegisterFn};
fn test_mismatched_op() { fn test_mismatched_op() {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!( let r = engine.eval::<i64>("60 + \"hello\"");
engine.eval::<i64>("60 + \"hello\""),
Err(EvalAltResult::ErrorMismatchOutputType( match r {
"alloc::string::String".into() Err(EvalAltResult::ErrorMismatchOutputType(err, _)) if err == "alloc::string::String" => (),
)) _ => panic!(),
); }
} }
#[test] #[test]
@ -29,10 +29,14 @@ fn test_mismatched_op_custom_type() {
engine.register_type::<TestStruct>(); engine.register_type::<TestStruct>();
engine.register_fn("new_ts", TestStruct::new); engine.register_fn("new_ts", TestStruct::new);
assert_eq!( let r = engine.eval::<i64>("60 + new_ts()");
engine.eval::<i64>("60 + new_ts()"),
Err(EvalAltResult::ErrorFunctionNotFound( match r {
"+ (i64, mismatched_op::test_mismatched_op_custom_type::TestStruct)".into() Err(EvalAltResult::ErrorFunctionNotFound(err, _))
)) if err == "+ (i64, mismatched_op::test_mismatched_op_custom_type::TestStruct)" =>
); {
()
}
_ => panic!(),
}
} }

View File

@ -1,16 +1,18 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult};
#[test] #[test]
fn test_not() { fn test_not() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!( assert_eq!(
engine.eval::<bool>("let not_true = !true; not_true"), engine.eval::<bool>("let not_true = !true; not_true")?,
Ok(false) false
); );
assert_eq!(engine.eval::<bool>("fn not(x) { !x } not(false)"), Ok(true)); assert_eq!(engine.eval::<bool>("fn not(x) { !x } not(false)")?, true);
// TODO - do we allow stacking unary operators directly? e.g '!!!!!!!true' // TODO - do we allow stacking unary operators directly? e.g '!!!!!!!true'
assert_eq!(engine.eval::<bool>("!(!(!(!(true))))"), Ok(true)); assert_eq!(engine.eval::<bool>("!(!(!(!(true))))")?, true);
Ok(())
} }

View File

@ -1,35 +1,43 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult};
#[test] #[test]
fn test_number_literal() { fn test_number_literal() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("65"), Ok(65)); assert_eq!(engine.eval::<i64>("65")?, 65);
Ok(())
} }
#[test] #[test]
fn test_hex_literal() { fn test_hex_literal() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("let x = 0xf; x"), Ok(15)); assert_eq!(engine.eval::<i64>("let x = 0xf; x")?, 15);
assert_eq!(engine.eval::<i64>("let x = 0xff; x"), Ok(255)); assert_eq!(engine.eval::<i64>("let x = 0xff; x")?, 255);
Ok(())
} }
#[test] #[test]
fn test_octal_literal() { fn test_octal_literal() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("let x = 0o77; x"), Ok(63)); assert_eq!(engine.eval::<i64>("let x = 0o77; x")?, 63);
assert_eq!(engine.eval::<i64>("let x = 0o1234; x"), Ok(668)); assert_eq!(engine.eval::<i64>("let x = 0o1234; x")?, 668);
Ok(())
} }
#[test] #[test]
fn test_binary_literal() { fn test_binary_literal() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("let x = 0b1111; x"), Ok(15)); assert_eq!(engine.eval::<i64>("let x = 0b1111; x")?, 15);
assert_eq!( assert_eq!(
engine.eval::<i64>("let x = 0b0011_1100_1010_0101; x"), engine.eval::<i64>("let x = 0b0011_1100_1010_0101; x")?,
Ok(15525) 15525
); );
Ok(())
} }

View File

@ -1,19 +1,23 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult};
#[test] #[test]
fn test_ops() { fn test_ops() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("60 + 5"), Ok(65)); assert_eq!(engine.eval::<i64>("60 + 5")?, 65);
assert_eq!(engine.eval::<i64>("(1 + 2) * (6 - 4) / 2"), Ok(3)); assert_eq!(engine.eval::<i64>("(1 + 2) * (6 - 4) / 2")?, 3);
Ok(())
} }
#[test] #[test]
fn test_op_prec() { fn test_op_prec() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!( assert_eq!(
engine.eval::<i64>("let x = 0; if x == 10 || true { x = 1} x"), engine.eval::<i64>("let x = 0; if x == 10 || true { x = 1} x")?,
Ok(1) 1
); );
Ok(())
} }

View File

@ -1,36 +1,34 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult};
#[test] #[test]
fn test_power_of() { fn test_power_of() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("2 ~ 3"), Ok(8)); assert_eq!(engine.eval::<i64>("2 ~ 3")?, 8);
assert_eq!(engine.eval::<i64>("(-2 ~ 3)"), Ok(-8)); assert_eq!(engine.eval::<i64>("(-2 ~ 3)")?, -8);
assert_eq!(engine.eval::<f64>("2.2 ~ 3.3"), Ok(13.489468760533386_f64)); assert_eq!(engine.eval::<f64>("2.2 ~ 3.3")?, 13.489468760533386_f64);
assert_eq!(engine.eval::<f64>("2.0~-2.0"), Ok(0.25_f64)); assert_eq!(engine.eval::<f64>("2.0~-2.0")?, 0.25_f64);
assert_eq!(engine.eval::<f64>("(-2.0~-2.0)"), Ok(0.25_f64)); assert_eq!(engine.eval::<f64>("(-2.0~-2.0)")?, 0.25_f64);
assert_eq!(engine.eval::<f64>("(-2.0~-2)"), Ok(0.25_f64)); assert_eq!(engine.eval::<f64>("(-2.0~-2)")?, 0.25_f64);
assert_eq!(engine.eval::<i64>("4~3"), Ok(64)); assert_eq!(engine.eval::<i64>("4~3")?, 64);
Ok(())
} }
#[test] #[test]
fn test_power_of_equals() { fn test_power_of_equals() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("let x = 2; x ~= 3; x"), Ok(8)); assert_eq!(engine.eval::<i64>("let x = 2; x ~= 3; x")?, 8);
assert_eq!(engine.eval::<i64>("let x = -2; x ~= 3; x"), Ok(-8)); assert_eq!(engine.eval::<i64>("let x = -2; x ~= 3; x")?, -8);
assert_eq!( assert_eq!(
engine.eval::<f64>("let x = 2.2; x ~= 3.3; x"), engine.eval::<f64>("let x = 2.2; x ~= 3.3; x")?,
Ok(13.489468760533386_f64) 13.489468760533386_f64
); );
assert_eq!( assert_eq!(engine.eval::<f64>("let x = 2.0; x ~= -2.0; x")?, 0.25_f64);
engine.eval::<f64>("let x = 2.0; x ~= -2.0; x"), assert_eq!(engine.eval::<f64>("let x = -2.0; x ~= -2.0; x")?, 0.25_f64);
Ok(0.25_f64) assert_eq!(engine.eval::<f64>("let x = -2.0; x ~= -2; x")?, 0.25_f64);
); assert_eq!(engine.eval::<i64>("let x =4; x ~= 3; x")?, 64);
assert_eq!(
engine.eval::<f64>("let x = -2.0; x ~= -2.0; x"), Ok(())
Ok(0.25_f64)
);
assert_eq!(engine.eval::<f64>("let x = -2.0; x ~= -2; x"), Ok(0.25_f64));
assert_eq!(engine.eval::<i64>("let x =4; x ~= 3; x"), Ok(64));
} }

View File

@ -1,15 +1,17 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult};
#[test] #[test]
fn test_string() { fn test_string() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!( assert_eq!(
engine.eval::<String>("\"Test string: \\u2764\""), engine.eval::<String>("\"Test string: \\u2764\"")?,
Ok("Test string: ❤".to_string()) "Test string: ❤".to_string()
); );
assert_eq!( assert_eq!(
engine.eval::<String>("\"foo\" + \"bar\""), engine.eval::<String>("\"foo\" + \"bar\"")?,
Ok("foobar".to_string()) "foobar".to_string()
); );
Ok(())
} }

View File

@ -1,15 +1,17 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult};
#[test] #[test]
// TODO also add test case for unary after compound // TODO also add test case for unary after compound
// Hah, turns out unary + has a good use after all! // Hah, turns out unary + has a good use after all!
fn test_unary_after_binary() { fn test_unary_after_binary() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("10 % +4"), Ok(2)); assert_eq!(engine.eval::<i64>("10 % +4")?, 2);
assert_eq!(engine.eval::<i64>("10 << +4"), Ok(160)); assert_eq!(engine.eval::<i64>("10 << +4")?, 160);
assert_eq!(engine.eval::<i64>("10 >> +4"), Ok(0)); assert_eq!(engine.eval::<i64>("10 >> +4")?, 0);
assert_eq!(engine.eval::<i64>("10 & +4"), Ok(0)); assert_eq!(engine.eval::<i64>("10 & +4")?, 0);
assert_eq!(engine.eval::<i64>("10 | +4"), Ok(14)); assert_eq!(engine.eval::<i64>("10 | +4")?, 14);
assert_eq!(engine.eval::<i64>("10 ^ +4"), Ok(14)); assert_eq!(engine.eval::<i64>("10 ^ +4")?, 14);
Ok(())
} }

View File

@ -1,10 +1,12 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult};
#[test] #[test]
fn test_unary_minus() { fn test_unary_minus() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("let x = -5; x"), Ok(-5)); assert_eq!(engine.eval::<i64>("let x = -5; x")?, -5);
assert_eq!(engine.eval::<i64>("fn n(x) { -x } n(5)"), Ok(-5)); assert_eq!(engine.eval::<i64>("fn n(x) { -x } n(5)")?, -5);
assert_eq!(engine.eval::<i64>("5 - -(-5)"), Ok(0)); assert_eq!(engine.eval::<i64>("5 - -(-5)")?, 0);
Ok(())
} }

View File

@ -1,25 +1,22 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult};
#[test] #[test]
fn test_unit() { fn test_unit() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<()>("let x = (); x")?, ());
assert_eq!(engine.eval::<()>("let x = (); x"), Ok(())); Ok(())
} }
#[test] #[test]
fn test_unit_eq() { fn test_unit_eq() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<bool>("let x = (); let y = (); x == y")?, true);
assert_eq!( Ok(())
engine.eval::<bool>("let x = (); let y = (); x == y"),
Ok(true)
);
} }
#[test] #[test]
fn test_unit_with_spaces() { fn test_unit_with_spaces() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<()>("let x = ( ); x")?, ());
assert_eq!(engine.eval::<()>("let x = ( ); x"), Ok(())); Ok(())
} }

View File

@ -1,28 +1,16 @@
use rhai::{Engine, Scope}; use rhai::{Engine, EvalAltResult, Scope};
#[test] #[test]
fn test_var_scope() { fn test_var_scope() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
let mut scope = Scope::new(); let mut scope = Scope::new();
assert_eq!( engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5")?;
engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5"), assert_eq!(engine.eval_with_scope::<i64>(&mut scope, "x")?, 9);
Ok(()) engine.eval_with_scope::<()>(&mut scope, "x = x + 1; x = x + 2;")?;
); assert_eq!(engine.eval_with_scope::<i64>(&mut scope, "x")?, 12);
assert_eq!(engine.eval_with_scope::<()>(&mut scope, "{let x = 3}")?, ());
assert_eq!(engine.eval_with_scope::<i64>(&mut scope, "x")?, 12);
assert_eq!(engine.eval_with_scope::<i64>(&mut scope, "x"), Ok(9)); Ok(())
assert_eq!(
engine.eval_with_scope::<()>(&mut scope, "x = x + 1; x = x + 2;"),
Ok(())
);
assert_eq!(engine.eval_with_scope::<i64>(&mut scope, "x"), Ok(12));
assert_eq!(
engine.eval_with_scope::<()>(&mut scope, "{let x = 3}"),
Ok(())
);
assert_eq!(engine.eval_with_scope::<i64>(&mut scope, "x"), Ok(12));
} }

View File

@ -1,14 +1,16 @@
use rhai::Engine; use rhai::{Engine, EvalAltResult};
#[test] #[test]
fn test_while() { fn test_while() -> Result<(), EvalAltResult> {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!( assert_eq!(
engine.eval::<i64>( engine.eval::<i64>(
"let x = 0; while x < 10 { x = x + 1; if x > 5 { \ "let x = 0; while x < 10 { x = x + 1; if x > 5 { \
break } } x", break } } x",
), )?,
Ok(6) 6
); );
Ok(())
} }