From 0707fad1ca40331b676716c8475b413c27d3dfde Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 2 Mar 2020 22:11:56 +0800 Subject: [PATCH] Refactor tests. --- src/lib.rs | 2 +- tests/arrays.rs | 24 +++++------ tests/binary_ops.rs | 26 ++++++------ tests/bit_shift.rs | 14 +++---- tests/bool_op.rs | 83 +++++++++++++++++++------------------ tests/chars.rs | 25 +++++------ tests/compound_equality.rs | 66 ++++++++++++++--------------- tests/decrement.rs | 21 +++++----- tests/float.rs | 29 +++++++------ tests/for.rs | 8 ++-- tests/get_set.rs | 20 ++++----- tests/if_block.rs | 20 +++++---- tests/increment.rs | 12 +++--- tests/internal_fn.rs | 29 +++++++------ tests/looping.rs | 14 +++---- tests/method_call.rs | 15 ++++--- tests/mismatched_op.rs | 28 +++++++------ tests/not.rs | 14 ++++--- tests/number_literals.rs | 34 +++++++++------ tests/ops.rs | 18 ++++---- tests/power_of.rs | 46 ++++++++++---------- tests/string.rs | 14 ++++--- tests/unary_after_binary.rs | 18 ++++---- tests/unary_minus.rs | 12 +++--- tests/unit.rs | 23 +++++----- tests/var_scope.rs | 30 ++++---------- tests/while_loop.rs | 10 +++-- 27 files changed, 338 insertions(+), 317 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f7a3907e..a7ac4d8e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,7 @@ //! //! let mut engine = Engine::new(); //! engine.register_fn("compute_something", compute_something); -//! assert_eq!(engine.eval_file::("my_script.rhai"), Ok(true)); +//! assert_eq!(engine.eval_file::("my_script.rhai").unwrap(), true); //! ``` //! //! [Check out the README on GitHub for more information!](https://github.com/jonathandturner/rhai) diff --git a/tests/arrays.rs b/tests/arrays.rs index 8b3d5884..cfd74e6e 100644 --- a/tests/arrays.rs +++ b/tests/arrays.rs @@ -1,19 +1,17 @@ -use rhai::Engine; -use rhai::RegisterFn; +use rhai::{Engine, EvalAltResult, RegisterFn}; #[test] -fn test_arrays() { +fn test_arrays() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = [1, 2, 3]; x[1]"), Ok(2)); - assert_eq!( - engine.eval::("let y = [1, 2, 3]; y[1] = 5; y[1]"), - Ok(5) - ); + assert_eq!(engine.eval::("let x = [1, 2, 3]; x[1]")?, 2); + assert_eq!(engine.eval::("let y = [1, 2, 3]; y[1] = 5; y[1]")?, 5); + + Ok(()) } #[test] -fn test_array_with_structs() { +fn test_array_with_structs() -> Result<(), EvalAltResult> { #[derive(Clone)] struct TestStruct { x: i64, @@ -45,7 +43,7 @@ fn test_array_with_structs() { engine.register_fn("update", TestStruct::update); engine.register_fn("new_ts", TestStruct::new); - assert_eq!(engine.eval::("let a = [new_ts()]; a[0].x"), Ok(1)); + assert_eq!(engine.eval::("let a = [new_ts()]; a[0].x")?, 1); assert_eq!( engine.eval::( @@ -53,7 +51,9 @@ fn test_array_with_structs() { a[0].x = 100; \ a[0].update(); \ a[0].x", - ), - Ok(1100) + )?, + 1100 ); + + Ok(()) } diff --git a/tests/binary_ops.rs b/tests/binary_ops.rs index a0f7c451..edf560aa 100644 --- a/tests/binary_ops.rs +++ b/tests/binary_ops.rs @@ -1,20 +1,22 @@ -use rhai::Engine; +use rhai::{Engine, EvalAltResult}; #[test] -fn test_binary_ops() { +fn test_binary_ops() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("10 % 4"), Ok(2)); - assert_eq!(engine.eval::("10 << 4"), Ok(160)); - assert_eq!(engine.eval::("10 >> 4"), Ok(0)); - assert_eq!(engine.eval::("10 & 4"), Ok(0)); - assert_eq!(engine.eval::("10 | 4"), Ok(14)); - assert_eq!(engine.eval::("10 ^ 4"), Ok(14)); + assert_eq!(engine.eval::("10 % 4")?, 2); + assert_eq!(engine.eval::("10 << 4")?, 160); + assert_eq!(engine.eval::("10 >> 4")?, 0); + assert_eq!(engine.eval::("10 & 4")?, 0); + assert_eq!(engine.eval::("10 | 4")?, 14); + assert_eq!(engine.eval::("10 ^ 4")?, 14); - assert_eq!(engine.eval::("42 == 42"), Ok(true)); - assert_eq!(engine.eval::("42 > 42"), Ok(false)); + assert_eq!(engine.eval::("42 == 42")?, true); + assert_eq!(engine.eval::("42 > 42")?, false); // Incompatible types compare to false - assert_eq!(engine.eval::("true == 42"), Ok(false)); - assert_eq!(engine.eval::(r#""42" == 42"#), Ok(false)); + assert_eq!(engine.eval::("true == 42")?, false); + assert_eq!(engine.eval::(r#""42" == 42"#)?, false); + + Ok(()) } diff --git a/tests/bit_shift.rs b/tests/bit_shift.rs index 184f5262..7b0a4ab5 100644 --- a/tests/bit_shift.rs +++ b/tests/bit_shift.rs @@ -1,15 +1,15 @@ -use rhai::Engine; +use rhai::{Engine, EvalAltResult}; #[test] -fn test_left_shift() { +fn test_left_shift() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - - assert_eq!(engine.eval::("4 << 2"), Ok(16)); + assert_eq!(engine.eval::("4 << 2")?, 16); + Ok(()) } #[test] -fn test_right_shift() { +fn test_right_shift() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - - assert_eq!(engine.eval::("9 >> 1"), Ok(4)); + assert_eq!(engine.eval::("9 >> 1")?, 4); + Ok(()) } diff --git a/tests/bool_op.rs b/tests/bool_op.rs index ffd90746..7c1654bb 100644 --- a/tests/bool_op.rs +++ b/tests/bool_op.rs @@ -1,42 +1,39 @@ use rhai::{Engine, EvalAltResult}; #[test] -fn test_bool_op1() { +fn test_bool_op1() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("true && (false || true)"), Ok(true)); - assert_eq!(engine.eval::("true & (false | true)"), Ok(true)); + assert_eq!(engine.eval::("true && (false || true)")?, true); + assert_eq!(engine.eval::("true & (false | true)")?, true); + + Ok(()) } #[test] -fn test_bool_op2() { +fn test_bool_op2() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("false && (false || true)"), Ok(false)); - assert_eq!(engine.eval::("false & (false | true)"), Ok(false)); + assert_eq!(engine.eval::("false && (false || true)")?, false); + assert_eq!(engine.eval::("false & (false | true)")?, false); + + Ok(()) } #[test] -fn test_bool_op3() { +fn test_bool_op3() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!( - engine.eval::("true && (false || 123)"), - Err(EvalAltResult::ErrorBooleanArgMismatch("OR".into())) - ); + assert!(engine.eval::("true && (false || 123)").is_err()); + assert_eq!(engine.eval::("true && (true || 123)")?, true); + assert!(engine.eval::("123 && (false || true)").is_err()); + assert_eq!(engine.eval::("false && (true || 123)")?, false); - assert_eq!(engine.eval::("true && (true || 123)"), Ok(true)); - - assert_eq!( - engine.eval::("123 && (false || true)"), - Err(EvalAltResult::ErrorBooleanArgMismatch("AND".into())) - ); - - assert_eq!(engine.eval::("false && (true || 123)"), Ok(false)); + Ok(()) } #[test] -fn test_bool_op_short_circuit() { +fn test_bool_op_short_circuit() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); assert_eq!( @@ -47,8 +44,8 @@ fn test_bool_op_short_circuit() { this() || that(); " - ), - Ok(true) + )?, + true ); assert_eq!( @@ -59,9 +56,11 @@ fn test_bool_op_short_circuit() { this() && that(); " - ), - Ok(false) + )?, + false ); + + Ok(()) } #[test] @@ -70,15 +69,17 @@ fn test_bool_op_no_short_circuit1() { let mut engine = Engine::new(); assert_eq!( - engine.eval::( - r" - fn this() { false } - fn that() { 9/0 } + engine + .eval::( + r" + fn this() { false } + fn that() { 9/0 } - this() | that(); - " - ), - Ok(false) + this() | that(); + " + ) + .unwrap(), + false ); } @@ -88,14 +89,16 @@ fn test_bool_op_no_short_circuit2() { let mut engine = Engine::new(); assert_eq!( - engine.eval::( - r" - fn this() { false } - fn that() { 9/0 } + engine + .eval::( + r" + fn this() { false } + fn that() { 9/0 } - this() & that(); - " - ), - Ok(false) + this() & that(); + " + ) + .unwrap(), + false ); } diff --git a/tests/chars.rs b/tests/chars.rs index 44657000..4a5c2d98 100644 --- a/tests/chars.rs +++ b/tests/chars.rs @@ -1,24 +1,19 @@ -use rhai::Engine; +use rhai::{Engine, EvalAltResult}; #[test] -fn test_chars() { +fn test_chars() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("'y'"), Ok('y')); - assert_eq!(engine.eval::("'\\u2764'"), Ok('❤')); - assert_eq!(engine.eval::(r#"let x="hello"; x[2]"#), Ok('l')); + assert_eq!(engine.eval::("'y'")?, 'y'); + assert_eq!(engine.eval::("'\\u2764'")?, '❤'); + assert_eq!(engine.eval::(r#"let x="hello"; x[2]"#)?, 'l'); assert_eq!( - engine.eval::(r#"let x="hello"; x[2]='$'; x"#), - Ok("he$lo".into()) + engine.eval::(r#"let x="hello"; x[2]='$'; x"#)?, + "he$lo".to_string() ); - match engine.eval::("'\\uhello'") { - Err(_) => (), - _ => assert!(false), - } + assert!(engine.eval::("'\\uhello'").is_err()); + assert!(engine.eval::("''").is_err()); - match engine.eval::("''") { - Err(_) => (), - _ => assert!(false), - } + Ok(()) } diff --git a/tests/compound_equality.rs b/tests/compound_equality.rs index 7b51692a..65b3b3e4 100644 --- a/tests/compound_equality.rs +++ b/tests/compound_equality.rs @@ -1,68 +1,66 @@ -use rhai::Engine; +use rhai::{Engine, EvalAltResult}; #[test] -fn test_or_equals() { +fn test_or_equals() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 16; x |= 74; x"), Ok(90)); - assert_eq!(engine.eval::("let x = true; x |= false; x"), Ok(true)); - assert_eq!(engine.eval::("let x = false; x |= true; x"), Ok(true)); + assert_eq!(engine.eval::("let x = 16; x |= 74; x")?, 90); + assert_eq!(engine.eval::("let x = true; x |= false; x")?, true); + assert_eq!(engine.eval::("let x = false; x |= true; x")?, true); + + Ok(()) } #[test] -fn test_and_equals() { +fn test_and_equals() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 16; x &= 31; x"), Ok(16)); - assert_eq!( - engine.eval::("let x = true; x &= false; x"), - Ok(false) - ); - assert_eq!( - engine.eval::("let x = false; x &= true; x"), - Ok(false) - ); - assert_eq!(engine.eval::("let x = true; x &= true; x"), Ok(true)); + assert_eq!(engine.eval::("let x = 16; x &= 31; x")?, 16); + assert_eq!(engine.eval::("let x = true; x &= false; x")?, false); + assert_eq!(engine.eval::("let x = false; x &= true; x")?, false); + assert_eq!(engine.eval::("let x = true; x &= true; x")?, true); + + Ok(()) } #[test] -fn test_xor_equals() { +fn test_xor_equals() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - - assert_eq!(engine.eval::("let x = 90; x ^= 12; x"), Ok(86)); + assert_eq!(engine.eval::("let x = 90; x ^= 12; x")?, 86); + Ok(()) } #[test] -fn test_multiply_equals() { +fn test_multiply_equals() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - - assert_eq!(engine.eval::("let x = 2; x *= 3; x"), Ok(6)); + assert_eq!(engine.eval::("let x = 2; x *= 3; x")?, 6); + Ok(()) } #[test] -fn test_divide_equals() { +fn test_divide_equals() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - - assert_eq!(engine.eval::("let x = 6; x /= 2; x"), Ok(3)); + assert_eq!(engine.eval::("let x = 6; x /= 2; x")?, 3); + Ok(()) } #[test] -fn test_left_shift_equals() { +fn test_left_shift_equals() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - - assert_eq!(engine.eval::("let x = 9; x >>=1; x"), Ok(4)); + assert_eq!(engine.eval::("let x = 9; x >>=1; x")?, 4); + Ok(()) } #[test] -fn test_right_shift_equals() { +fn test_right_shift_equals() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - - assert_eq!(engine.eval::("let x = 4; x<<= 2; x"), Ok(16)); + assert_eq!(engine.eval::("let x = 4; x<<= 2; x")?, 16); + Ok(()) } #[test] -fn test_modulo_equals() { +fn test_modulo_equals() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - - assert_eq!(engine.eval::("let x = 10; x %= 4; x"), Ok(2)); + assert_eq!(engine.eval::("let x = 10; x %= 4; x")?, 2); + Ok(()) } diff --git a/tests/decrement.rs b/tests/decrement.rs index ab1dc6c8..344c70ac 100644 --- a/tests/decrement.rs +++ b/tests/decrement.rs @@ -1,16 +1,17 @@ -use rhai::Engine; -use rhai::EvalAltResult; +use rhai::{Engine, EvalAltResult}; #[test] -fn test_decrement() { +fn test_decrement() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 10; x -= 7; x"), Ok(3)); + assert_eq!(engine.eval::("let x = 10; x -= 7; x")?, 3); - assert_eq!( - engine.eval::("let s = \"test\"; s -= \"ing\"; s"), - Err(EvalAltResult::ErrorFunctionNotFound( - "- (alloc::string::String, alloc::string::String)".to_string() - )) - ); + let r = engine.eval::("let s = \"test\"; s -= \"ing\"; s"); + + match r { + Err(EvalAltResult::ErrorFunctionNotFound(err, _)) if err.starts_with("- ") => (), + _ => panic!(), + } + + Ok(()) } diff --git a/tests/float.rs b/tests/float.rs index 516980d8..8e291e9e 100644 --- a/tests/float.rs +++ b/tests/float.rs @@ -1,23 +1,24 @@ -use rhai::Engine; -use rhai::RegisterFn; +use rhai::{Engine, EvalAltResult, RegisterFn}; #[test] -fn test_float() { +fn test_float() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); assert_eq!( - engine.eval::("let x = 0.0; let y = 1.0; x < y"), - Ok(true) + engine.eval::("let x = 0.0; let y = 1.0; x < y")?, + true ); assert_eq!( - engine.eval::("let x = 0.0; let y = 1.0; x > y"), - Ok(false) + engine.eval::("let x = 0.0; let y = 1.0; x > y")?, + false ); - assert_eq!(engine.eval::("let x = 9.9999; x"), Ok(9.9999)); + assert_eq!(engine.eval::("let x = 9.9999; x")?, 9.9999); + + Ok(()) } #[test] -fn struct_with_float() { +fn struct_with_float() -> Result<(), EvalAltResult> { #[derive(Clone)] struct TestStruct { x: f64, @@ -50,11 +51,13 @@ fn struct_with_float() { engine.register_fn("new_ts", TestStruct::new); assert_eq!( - engine.eval::("let ts = new_ts(); ts.update(); ts.x"), - Ok(6.789) + engine.eval::("let ts = new_ts(); ts.update(); ts.x")?, + 6.789 ); assert_eq!( - engine.eval::("let ts = new_ts(); ts.x = 10.1001; ts.x"), - Ok(10.1001) + engine.eval::("let ts = new_ts(); ts.x = 10.1001; ts.x")?, + 10.1001 ); + + Ok(()) } diff --git a/tests/for.rs b/tests/for.rs index 687c1b4b..63390b31 100644 --- a/tests/for.rs +++ b/tests/for.rs @@ -1,7 +1,7 @@ -use rhai::Engine; +use rhai::{Engine, EvalAltResult}; #[test] -fn test_for() { +fn test_for() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); let script = r" @@ -20,5 +20,7 @@ fn test_for() { sum1 + sum2 "; - assert_eq!(engine.eval::(script).unwrap(), 30); + assert_eq!(engine.eval::(script)?, 30); + + Ok(()) } diff --git a/tests/get_set.rs b/tests/get_set.rs index e3311278..d2718a50 100644 --- a/tests/get_set.rs +++ b/tests/get_set.rs @@ -1,8 +1,7 @@ -use rhai::Engine; -use rhai::RegisterFn; +use rhai::{Engine, EvalAltResult, RegisterFn}; #[test] -fn test_get_set() { +fn test_get_set() -> Result<(), EvalAltResult> { #[derive(Clone)] struct TestStruct { x: i64, @@ -29,14 +28,13 @@ fn test_get_set() { engine.register_get_set("x", TestStruct::get_x, TestStruct::set_x); engine.register_fn("new_ts", TestStruct::new); - assert_eq!( - engine.eval::("let a = new_ts(); a.x = 500; a.x"), - Ok(500) - ); + assert_eq!(engine.eval::("let a = new_ts(); a.x = 500; a.x")?, 500); + + Ok(()) } #[test] -fn test_big_get_set() { +fn test_big_get_set() -> Result<(), EvalAltResult> { #[derive(Clone)] struct TestChild { x: i64, @@ -88,7 +86,9 @@ fn test_big_get_set() { engine.register_fn("new_tp", TestParent::new); assert_eq!( - engine.eval::("let a = new_tp(); a.child.x = 500; a.child.x"), - Ok(500) + engine.eval::("let a = new_tp(); a.child.x = 500; a.child.x")?, + 500 ); + + Ok(()) } diff --git a/tests/if_block.rs b/tests/if_block.rs index 7440e448..286aa7fd 100644 --- a/tests/if_block.rs +++ b/tests/if_block.rs @@ -1,15 +1,15 @@ -use rhai::Engine; +use rhai::{Engine, EvalAltResult}; #[test] -fn test_if() { +fn test_if() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("if true { 55 }"), Ok(55)); - assert_eq!(engine.eval::("if false { 55 } else { 44 }"), Ok(44)); - assert_eq!(engine.eval::("if true { 55 } else { 44 }"), Ok(55)); + assert_eq!(engine.eval::("if true { 55 }")?, 55); + assert_eq!(engine.eval::("if false { 55 } else { 44 }")?, 44); + assert_eq!(engine.eval::("if true { 55 } else { 44 }")?, 55); assert_eq!( - engine.eval::("if false { 55 } else if true { 33 } else { 44 }"), - Ok(33) + engine.eval::("if false { 55 } else if true { 33 } else { 44 }")?, + 33 ); assert_eq!( engine.eval::( @@ -21,7 +21,9 @@ fn test_if() { else if false { 88 } else { 44 } " - ), - Ok(44) + )?, + 44 ); + + Ok(()) } diff --git a/tests/increment.rs b/tests/increment.rs index 661ae38e..62b1b36c 100644 --- a/tests/increment.rs +++ b/tests/increment.rs @@ -1,12 +1,14 @@ -use rhai::Engine; +use rhai::{Engine, EvalAltResult}; #[test] -fn test_increment() { +fn test_increment() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 1; x += 2; x"), Ok(3)); + assert_eq!(engine.eval::("let x = 1; x += 2; x")?, 3); assert_eq!( - engine.eval::("let s = \"test\"; s += \"ing\"; s"), - Ok("testing".to_string()) + engine.eval::("let s = \"test\"; s += \"ing\"; s")?, + "testing".to_string() ); + + Ok(()) } diff --git a/tests/internal_fn.rs b/tests/internal_fn.rs index f04a46ca..95f584a5 100644 --- a/tests/internal_fn.rs +++ b/tests/internal_fn.rs @@ -1,25 +1,30 @@ -use rhai::Engine; +use rhai::{Engine, EvalAltResult}; #[test] -fn test_internal_fn() { +fn test_internal_fn() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!( - engine.eval::("fn addme(a, b) { a+b } addme(3, 4)"), - Ok(7) - ); - assert_eq!(engine.eval::("fn bob() { return 4; 5 } bob()"), Ok(4)); + assert_eq!(engine.eval::("fn addme(a, b) { a+b } addme(3, 4)")?, 7); + assert_eq!(engine.eval::("fn bob() { return 4; 5 } bob()")?, 4); + + Ok(()) } #[test] -fn test_big_internal_fn() { +fn test_big_internal_fn() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); assert_eq!( engine.eval::( - "fn mathme(a, b, c, d, e, f) { a - b * c + d * e - f \ - } mathme(100, 5, 2, 9, 6, 32)", - ), - Ok(112) + r" + fn mathme(a, b, c, d, e, f) { + a - b * c + d * e - f + } + mathme(100, 5, 2, 9, 6, 32) + ", + )?, + 112 ); + + Ok(()) } diff --git a/tests/looping.rs b/tests/looping.rs index 4359df16..4baa0aab 100644 --- a/tests/looping.rs +++ b/tests/looping.rs @@ -1,12 +1,11 @@ -use rhai::Engine; +use rhai::{Engine, EvalAltResult}; #[test] -fn test_loop() { +fn test_loop() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert!(engine - .eval::( - " + assert!(engine.eval::( + r" let x = 0; let i = 0; @@ -22,6 +21,7 @@ fn test_loop() { x == 45 " - ) - .unwrap()) + )?); + + Ok(()) } diff --git a/tests/method_call.rs b/tests/method_call.rs index ee6b3dc0..f9f09652 100644 --- a/tests/method_call.rs +++ b/tests/method_call.rs @@ -1,8 +1,7 @@ -use rhai::Engine; -use rhai::RegisterFn; +use rhai::{Engine, EvalAltResult, RegisterFn}; #[test] -fn test_method_call() { +fn test_method_call() -> Result<(), EvalAltResult> { #[derive(Clone)] struct TestStruct { x: i64, @@ -25,9 +24,9 @@ fn test_method_call() { engine.register_fn("update", TestStruct::update); engine.register_fn("new_ts", TestStruct::new); - if let Ok(result) = engine.eval::("let x = new_ts(); x.update(); x") { - assert_eq!(result.x, 1001); - } else { - assert!(false); - } + let ts = engine.eval::("let x = new_ts(); x.update(); x")?; + + assert_eq!(ts.x, 1001); + + Ok(()) } diff --git a/tests/mismatched_op.rs b/tests/mismatched_op.rs index 944051d7..314ea378 100644 --- a/tests/mismatched_op.rs +++ b/tests/mismatched_op.rs @@ -4,12 +4,12 @@ use rhai::{Engine, EvalAltResult, RegisterFn}; fn test_mismatched_op() { let mut engine = Engine::new(); - assert_eq!( - engine.eval::("60 + \"hello\""), - Err(EvalAltResult::ErrorMismatchOutputType( - "alloc::string::String".into() - )) - ); + let r = engine.eval::("60 + \"hello\""); + + match r { + Err(EvalAltResult::ErrorMismatchOutputType(err, _)) if err == "alloc::string::String" => (), + _ => panic!(), + } } #[test] @@ -29,10 +29,14 @@ fn test_mismatched_op_custom_type() { engine.register_type::(); engine.register_fn("new_ts", TestStruct::new); - assert_eq!( - engine.eval::("60 + new_ts()"), - Err(EvalAltResult::ErrorFunctionNotFound( - "+ (i64, mismatched_op::test_mismatched_op_custom_type::TestStruct)".into() - )) - ); + let r = engine.eval::("60 + new_ts()"); + + match r { + Err(EvalAltResult::ErrorFunctionNotFound(err, _)) + if err == "+ (i64, mismatched_op::test_mismatched_op_custom_type::TestStruct)" => + { + () + } + _ => panic!(), + } } diff --git a/tests/not.rs b/tests/not.rs index 0e6252a3..8bade80a 100644 --- a/tests/not.rs +++ b/tests/not.rs @@ -1,16 +1,18 @@ -use rhai::Engine; +use rhai::{Engine, EvalAltResult}; #[test] -fn test_not() { +fn test_not() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); assert_eq!( - engine.eval::("let not_true = !true; not_true"), - Ok(false) + engine.eval::("let not_true = !true; not_true")?, + false ); - assert_eq!(engine.eval::("fn not(x) { !x } not(false)"), Ok(true)); + assert_eq!(engine.eval::("fn not(x) { !x } not(false)")?, true); // TODO - do we allow stacking unary operators directly? e.g '!!!!!!!true' - assert_eq!(engine.eval::("!(!(!(!(true))))"), Ok(true)); + assert_eq!(engine.eval::("!(!(!(!(true))))")?, true); + + Ok(()) } diff --git a/tests/number_literals.rs b/tests/number_literals.rs index 05185cb9..16e12719 100644 --- a/tests/number_literals.rs +++ b/tests/number_literals.rs @@ -1,35 +1,43 @@ -use rhai::Engine; +use rhai::{Engine, EvalAltResult}; #[test] -fn test_number_literal() { +fn test_number_literal() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("65"), Ok(65)); + assert_eq!(engine.eval::("65")?, 65); + + Ok(()) } #[test] -fn test_hex_literal() { +fn test_hex_literal() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 0xf; x"), Ok(15)); - assert_eq!(engine.eval::("let x = 0xff; x"), Ok(255)); + assert_eq!(engine.eval::("let x = 0xf; x")?, 15); + assert_eq!(engine.eval::("let x = 0xff; x")?, 255); + + Ok(()) } #[test] -fn test_octal_literal() { +fn test_octal_literal() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 0o77; x"), Ok(63)); - assert_eq!(engine.eval::("let x = 0o1234; x"), Ok(668)); + assert_eq!(engine.eval::("let x = 0o77; x")?, 63); + assert_eq!(engine.eval::("let x = 0o1234; x")?, 668); + + Ok(()) } #[test] -fn test_binary_literal() { +fn test_binary_literal() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 0b1111; x"), Ok(15)); + assert_eq!(engine.eval::("let x = 0b1111; x")?, 15); assert_eq!( - engine.eval::("let x = 0b0011_1100_1010_0101; x"), - Ok(15525) + engine.eval::("let x = 0b0011_1100_1010_0101; x")?, + 15525 ); + + Ok(()) } diff --git a/tests/ops.rs b/tests/ops.rs index c2f2d241..d1464dfb 100644 --- a/tests/ops.rs +++ b/tests/ops.rs @@ -1,19 +1,23 @@ -use rhai::Engine; +use rhai::{Engine, EvalAltResult}; #[test] -fn test_ops() { +fn test_ops() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("60 + 5"), Ok(65)); - assert_eq!(engine.eval::("(1 + 2) * (6 - 4) / 2"), Ok(3)); + assert_eq!(engine.eval::("60 + 5")?, 65); + assert_eq!(engine.eval::("(1 + 2) * (6 - 4) / 2")?, 3); + + Ok(()) } #[test] -fn test_op_prec() { +fn test_op_prec() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); assert_eq!( - engine.eval::("let x = 0; if x == 10 || true { x = 1} x"), - Ok(1) + engine.eval::("let x = 0; if x == 10 || true { x = 1} x")?, + 1 ); + + Ok(()) } diff --git a/tests/power_of.rs b/tests/power_of.rs index 45211fb6..ac41a611 100644 --- a/tests/power_of.rs +++ b/tests/power_of.rs @@ -1,36 +1,34 @@ -use rhai::Engine; +use rhai::{Engine, EvalAltResult}; #[test] -fn test_power_of() { +fn test_power_of() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("2 ~ 3"), Ok(8)); - assert_eq!(engine.eval::("(-2 ~ 3)"), Ok(-8)); - assert_eq!(engine.eval::("2.2 ~ 3.3"), Ok(13.489468760533386_f64)); - assert_eq!(engine.eval::("2.0~-2.0"), Ok(0.25_f64)); - assert_eq!(engine.eval::("(-2.0~-2.0)"), Ok(0.25_f64)); - assert_eq!(engine.eval::("(-2.0~-2)"), Ok(0.25_f64)); - assert_eq!(engine.eval::("4~3"), Ok(64)); + assert_eq!(engine.eval::("2 ~ 3")?, 8); + assert_eq!(engine.eval::("(-2 ~ 3)")?, -8); + assert_eq!(engine.eval::("2.2 ~ 3.3")?, 13.489468760533386_f64); + assert_eq!(engine.eval::("2.0~-2.0")?, 0.25_f64); + assert_eq!(engine.eval::("(-2.0~-2.0)")?, 0.25_f64); + assert_eq!(engine.eval::("(-2.0~-2)")?, 0.25_f64); + assert_eq!(engine.eval::("4~3")?, 64); + + Ok(()) } #[test] -fn test_power_of_equals() { +fn test_power_of_equals() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 2; x ~= 3; x"), Ok(8)); - assert_eq!(engine.eval::("let x = -2; x ~= 3; x"), Ok(-8)); + assert_eq!(engine.eval::("let x = 2; x ~= 3; x")?, 8); + assert_eq!(engine.eval::("let x = -2; x ~= 3; x")?, -8); assert_eq!( - engine.eval::("let x = 2.2; x ~= 3.3; x"), - Ok(13.489468760533386_f64) + engine.eval::("let x = 2.2; x ~= 3.3; x")?, + 13.489468760533386_f64 ); - assert_eq!( - engine.eval::("let x = 2.0; x ~= -2.0; x"), - Ok(0.25_f64) - ); - assert_eq!( - engine.eval::("let x = -2.0; x ~= -2.0; x"), - Ok(0.25_f64) - ); - assert_eq!(engine.eval::("let x = -2.0; x ~= -2; x"), Ok(0.25_f64)); - assert_eq!(engine.eval::("let x =4; x ~= 3; x"), Ok(64)); + assert_eq!(engine.eval::("let x = 2.0; x ~= -2.0; x")?, 0.25_f64); + assert_eq!(engine.eval::("let x = -2.0; x ~= -2.0; x")?, 0.25_f64); + assert_eq!(engine.eval::("let x = -2.0; x ~= -2; x")?, 0.25_f64); + assert_eq!(engine.eval::("let x =4; x ~= 3; x")?, 64); + + Ok(()) } diff --git a/tests/string.rs b/tests/string.rs index 5b819e94..e7df5d05 100644 --- a/tests/string.rs +++ b/tests/string.rs @@ -1,15 +1,17 @@ -use rhai::Engine; +use rhai::{Engine, EvalAltResult}; #[test] -fn test_string() { +fn test_string() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); assert_eq!( - engine.eval::("\"Test string: \\u2764\""), - Ok("Test string: ❤".to_string()) + engine.eval::("\"Test string: \\u2764\"")?, + "Test string: ❤".to_string() ); assert_eq!( - engine.eval::("\"foo\" + \"bar\""), - Ok("foobar".to_string()) + engine.eval::("\"foo\" + \"bar\"")?, + "foobar".to_string() ); + + Ok(()) } diff --git a/tests/unary_after_binary.rs b/tests/unary_after_binary.rs index 00778e7b..f44111d4 100644 --- a/tests/unary_after_binary.rs +++ b/tests/unary_after_binary.rs @@ -1,15 +1,17 @@ -use rhai::Engine; +use rhai::{Engine, EvalAltResult}; #[test] // TODO also add test case for unary after compound // 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(); - assert_eq!(engine.eval::("10 % +4"), Ok(2)); - assert_eq!(engine.eval::("10 << +4"), Ok(160)); - assert_eq!(engine.eval::("10 >> +4"), Ok(0)); - assert_eq!(engine.eval::("10 & +4"), Ok(0)); - assert_eq!(engine.eval::("10 | +4"), Ok(14)); - assert_eq!(engine.eval::("10 ^ +4"), Ok(14)); + assert_eq!(engine.eval::("10 % +4")?, 2); + assert_eq!(engine.eval::("10 << +4")?, 160); + assert_eq!(engine.eval::("10 >> +4")?, 0); + assert_eq!(engine.eval::("10 & +4")?, 0); + assert_eq!(engine.eval::("10 | +4")?, 14); + assert_eq!(engine.eval::("10 ^ +4")?, 14); + + Ok(()) } diff --git a/tests/unary_minus.rs b/tests/unary_minus.rs index 49701bcb..b32e6860 100644 --- a/tests/unary_minus.rs +++ b/tests/unary_minus.rs @@ -1,10 +1,12 @@ -use rhai::Engine; +use rhai::{Engine, EvalAltResult}; #[test] -fn test_unary_minus() { +fn test_unary_minus() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = -5; x"), Ok(-5)); - assert_eq!(engine.eval::("fn n(x) { -x } n(5)"), Ok(-5)); - assert_eq!(engine.eval::("5 - -(-5)"), Ok(0)); + assert_eq!(engine.eval::("let x = -5; x")?, -5); + assert_eq!(engine.eval::("fn n(x) { -x } n(5)")?, -5); + assert_eq!(engine.eval::("5 - -(-5)")?, 0); + + Ok(()) } diff --git a/tests/unit.rs b/tests/unit.rs index 7283b70b..5f54bb15 100644 --- a/tests/unit.rs +++ b/tests/unit.rs @@ -1,25 +1,22 @@ -use rhai::Engine; +use rhai::{Engine, EvalAltResult}; #[test] -fn test_unit() { +fn test_unit() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - - assert_eq!(engine.eval::<()>("let x = (); x"), Ok(())); + assert_eq!(engine.eval::<()>("let x = (); x")?, ()); + Ok(()) } #[test] -fn test_unit_eq() { +fn test_unit_eq() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - - assert_eq!( - engine.eval::("let x = (); let y = (); x == y"), - Ok(true) - ); + assert_eq!(engine.eval::("let x = (); let y = (); x == y")?, true); + Ok(()) } #[test] -fn test_unit_with_spaces() { +fn test_unit_with_spaces() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); - - assert_eq!(engine.eval::<()>("let x = ( ); x"), Ok(())); + assert_eq!(engine.eval::<()>("let x = ( ); x")?, ()); + Ok(()) } diff --git a/tests/var_scope.rs b/tests/var_scope.rs index e802caa3..6b378299 100644 --- a/tests/var_scope.rs +++ b/tests/var_scope.rs @@ -1,28 +1,16 @@ -use rhai::{Engine, Scope}; +use rhai::{Engine, EvalAltResult, Scope}; #[test] -fn test_var_scope() { +fn test_var_scope() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); let mut scope = Scope::new(); - assert_eq!( - engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5"), - Ok(()) - ); + engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5")?; + assert_eq!(engine.eval_with_scope::(&mut scope, "x")?, 9); + engine.eval_with_scope::<()>(&mut scope, "x = x + 1; x = x + 2;")?; + assert_eq!(engine.eval_with_scope::(&mut scope, "x")?, 12); + assert_eq!(engine.eval_with_scope::<()>(&mut scope, "{let x = 3}")?, ()); + assert_eq!(engine.eval_with_scope::(&mut scope, "x")?, 12); - assert_eq!(engine.eval_with_scope::(&mut scope, "x"), Ok(9)); - - assert_eq!( - engine.eval_with_scope::<()>(&mut scope, "x = x + 1; x = x + 2;"), - Ok(()) - ); - - assert_eq!(engine.eval_with_scope::(&mut scope, "x"), Ok(12)); - - assert_eq!( - engine.eval_with_scope::<()>(&mut scope, "{let x = 3}"), - Ok(()) - ); - - assert_eq!(engine.eval_with_scope::(&mut scope, "x"), Ok(12)); + Ok(()) } diff --git a/tests/while_loop.rs b/tests/while_loop.rs index 3cbba708..8dc1ae85 100644 --- a/tests/while_loop.rs +++ b/tests/while_loop.rs @@ -1,14 +1,16 @@ -use rhai::Engine; +use rhai::{Engine, EvalAltResult}; #[test] -fn test_while() { +fn test_while() -> Result<(), EvalAltResult> { let mut engine = Engine::new(); assert_eq!( engine.eval::( "let x = 0; while x < 10 { x = x + 1; if x > 5 { \ break } } x", - ), - Ok(6) + )?, + 6 ); + + Ok(()) }