diff --git a/src/engine.rs b/src/engine.rs index f1e6b8a3..e65d6979 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -15,11 +15,9 @@ use crate::parser::{lex, parse, Expr, FnDef, Stmt}; pub enum EvalAltResult { ErrorFunctionNotFound(String), ErrorFunctionArgMismatch, - ErrorFunctionCallNotSupported, ErrorIndexMismatch, ErrorIfGuardMismatch, ErrorVariableNotFound(String), - ErrorFunctionArityNotSupported, ErrorAssignmentToUnknownLHS, ErrorMismatchOutputType(String), ErrorCantOpenScriptFile, @@ -46,11 +44,9 @@ impl PartialEq for EvalAltResult { match (self, other) { (&ErrorFunctionNotFound(ref a), &ErrorFunctionNotFound(ref b)) => a == b, (&ErrorFunctionArgMismatch, &ErrorFunctionArgMismatch) => true, - (&ErrorFunctionCallNotSupported, &ErrorFunctionCallNotSupported) => true, (&ErrorIndexMismatch, &ErrorIndexMismatch) => true, (&ErrorIfGuardMismatch, &ErrorIfGuardMismatch) => true, (&ErrorVariableNotFound(ref a), &ErrorVariableNotFound(ref b)) => a == b, - (&ErrorFunctionArityNotSupported, &ErrorFunctionArityNotSupported) => true, (&ErrorAssignmentToUnknownLHS, &ErrorAssignmentToUnknownLHS) => true, (&ErrorMismatchOutputType(ref a), &ErrorMismatchOutputType(ref b)) => a == b, (&ErrorCantOpenScriptFile, &ErrorCantOpenScriptFile) => true, @@ -66,15 +62,9 @@ impl Error for EvalAltResult { match *self { EvalAltResult::ErrorFunctionNotFound(_) => "Function not found", EvalAltResult::ErrorFunctionArgMismatch => "Function argument types do not match", - EvalAltResult::ErrorFunctionCallNotSupported => { - "Function call with > 2 argument not supported" - } EvalAltResult::ErrorIndexMismatch => "Index does not match array", EvalAltResult::ErrorIfGuardMismatch => "If guards expect boolean expression", EvalAltResult::ErrorVariableNotFound(_) => "Variable not found", - EvalAltResult::ErrorFunctionArityNotSupported => { - "Functions of more than 3 parameters are not yet supported" - } EvalAltResult::ErrorAssignmentToUnknownLHS => { "Assignment to an unsupported left-hand side" } diff --git a/tests/arrays.rs b/tests/arrays.rs index 2845678a..8b3d5884 100644 --- a/tests/arrays.rs +++ b/tests/arrays.rs @@ -5,17 +5,11 @@ use rhai::RegisterFn; fn test_arrays() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("let x = [1, 2, 3]; x[1]") { - assert_eq!(result, 2); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("let y = [1, 2, 3]; y[1] = 5; y[1]") { - assert_eq!(result, 5); - } else { - assert!(false); - } + 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) + ); } #[test] @@ -51,18 +45,15 @@ fn test_array_with_structs() { engine.register_fn("update", TestStruct::update); engine.register_fn("new_ts", TestStruct::new); - if let Ok(result) = engine.eval::("let a = [new_ts()]; a[0].x") { - assert_eq!(result, 1); - } else { - assert!(false); - } + assert_eq!(engine.eval::("let a = [new_ts()]; a[0].x"), Ok(1)); - if let Ok(result) = engine.eval::( - "let a = [new_ts()]; a[0].x = 100; a[0].update(); \ - a[0].x", - ) { - assert_eq!(result, 1100); - } else { - assert!(false); - } + assert_eq!( + engine.eval::( + "let a = [new_ts()]; \ + a[0].x = 100; \ + a[0].update(); \ + a[0].x", + ), + Ok(1100) + ); } diff --git a/tests/binary_ops.rs b/tests/binary_ops.rs index 611f12d2..4978b34d 100644 --- a/tests/binary_ops.rs +++ b/tests/binary_ops.rs @@ -4,39 +4,10 @@ use rhai::Engine; fn test_binary_ops() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("10 % 4") { - assert_eq!(result, 2); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("10 << 4") { - assert_eq!(result, 160); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("10 >> 4") { - assert_eq!(result, 0); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("10 & 4") { - assert_eq!(result, 0); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("10 | 4") { - assert_eq!(result, 14); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("10 ^ 4") { - assert_eq!(result, 14); - } else { - assert!(false); - } + 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)); } diff --git a/tests/bit_shift.rs b/tests/bit_shift.rs index c0043082..184f5262 100644 --- a/tests/bit_shift.rs +++ b/tests/bit_shift.rs @@ -4,20 +4,12 @@ use rhai::Engine; fn test_left_shift() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("4 << 2") { - assert_eq!(result, 16); - } else { - assert!(false); - } + assert_eq!(engine.eval::("4 << 2"), Ok(16)); } #[test] fn test_right_shift() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("9 >> 1") { - assert_eq!(result, 4); - } else { - assert!(false); - } + assert_eq!(engine.eval::("9 >> 1"), Ok(4)); } diff --git a/tests/bool_op.rs b/tests/bool_op.rs index 4803cf18..9565e8ee 100644 --- a/tests/bool_op.rs +++ b/tests/bool_op.rs @@ -4,20 +4,12 @@ use rhai::Engine; fn test_bool_op1() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("true && (false || true)") { - assert_eq!(result, true); - } else { - assert!(false); - } + assert_eq!(engine.eval::("true && (false || true)"), Ok(true)); } #[test] fn test_bool_op2() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("false && (false || true)") { - assert_eq!(result, false); - } else { - assert!(false); - } + assert_eq!(engine.eval::("false && (false || true)"), Ok(false)); } diff --git a/tests/chars.rs b/tests/chars.rs index 7352b203..3c5a920b 100644 --- a/tests/chars.rs +++ b/tests/chars.rs @@ -4,17 +4,8 @@ use rhai::Engine; fn test_chars() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("'y'") { - assert_eq!(result, 'y'); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("'\\u2764'") { - assert_eq!(result, '❤'); - } else { - assert!(false); - } + assert_eq!(engine.eval::("'y'"), Ok('y')); + assert_eq!(engine.eval::("'\\u2764'"), Ok('❤')); match engine.eval::("''") { Err(_) => (), diff --git a/tests/compound_equality.rs b/tests/compound_equality.rs index 096756f0..7b51692a 100644 --- a/tests/compound_equality.rs +++ b/tests/compound_equality.rs @@ -4,116 +4,65 @@ use rhai::Engine; fn test_or_equals() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("let x = 16; x |= 74; x") { - assert_eq!(result, 90); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("let x = true; x |= false; x") { - assert!(result); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("let x = false; x |= true; x") { - assert!(result); - } else { - assert!(false); - } + 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)); } #[test] fn test_and_equals() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("let x = 16; x &= 31; x") { - assert_eq!(result, 16); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("let x = true; x &= false; x") { - assert_eq!(result, false); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("let x = false; x &= true; x") { - assert_eq!(result, false); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("let x = true; x &= true; x") { - assert!(result); - } else { - assert!(false); - } + 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)); } #[test] fn test_xor_equals() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("let x = 90; x ^= 12; x") { - assert_eq!(result, 86); - } else { - assert!(false); - } + assert_eq!(engine.eval::("let x = 90; x ^= 12; x"), Ok(86)); } #[test] fn test_multiply_equals() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("let x = 2; x *= 3; x") { - assert_eq!(result, 6); - } else { - assert!(false); - } + assert_eq!(engine.eval::("let x = 2; x *= 3; x"), Ok(6)); } #[test] fn test_divide_equals() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("let x = 6; x /= 2; x") { - assert_eq!(result, 3); - } else { - assert!(false); - } + assert_eq!(engine.eval::("let x = 6; x /= 2; x"), Ok(3)); } #[test] fn test_left_shift_equals() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("let x = 9; x >>=1; x") { - assert_eq!(result, 4); - } else { - assert!(false); - } + assert_eq!(engine.eval::("let x = 9; x >>=1; x"), Ok(4)); } #[test] fn test_right_shift_equals() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("let x = 4; x<<= 2; x") { - assert_eq!(result, 16); - } else { - assert!(false); - } + assert_eq!(engine.eval::("let x = 4; x<<= 2; x"), Ok(16)); } #[test] fn test_modulo_equals() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("let x = 10; x %= 4; x") { - assert_eq!(result, 2); - } else { - assert!(false); - } + assert_eq!(engine.eval::("let x = 10; x %= 4; x"), Ok(2)); } diff --git a/tests/decrement.rs b/tests/decrement.rs index 874e2675..2cd8b10c 100644 --- a/tests/decrement.rs +++ b/tests/decrement.rs @@ -1,18 +1,16 @@ use rhai::Engine; +use rhai::EvalAltResult; #[test] fn test_decrement() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("let x = 10; x -= 7; x") { - assert_eq!(result, 3); - } else { - assert!(false); - } + assert_eq!(engine.eval::("let x = 10; x -= 7; x"), Ok(3)); - if let Ok(_) = engine.eval::("let s = \"test\"; s -= \"ing\"; s") { - assert!(false); - } else { - assert!(true); - } + assert_eq!( + engine.eval::("let s = \"test\"; s -= \"ing\"; s"), + Err(EvalAltResult::ErrorFunctionNotFound( + "- (alloc::string::String,alloc::string::String)".to_string() + )) + ); } diff --git a/tests/float.rs b/tests/float.rs index 87fd19f1..516980d8 100644 --- a/tests/float.rs +++ b/tests/float.rs @@ -5,23 +5,15 @@ use rhai::RegisterFn; fn test_float() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("let x = 0.0; let y = 1.0; x < y") { - assert!(result); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("let x = 0.0; let y = 1.0; x > y") { - assert!(!result); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("let x = 9.9999; x") { - assert_eq!(result, 9.9999); - } else { - assert!(false); - } + assert_eq!( + engine.eval::("let x = 0.0; let y = 1.0; x < y"), + Ok(true) + ); + assert_eq!( + engine.eval::("let x = 0.0; let y = 1.0; x > y"), + Ok(false) + ); + assert_eq!(engine.eval::("let x = 9.9999; x"), Ok(9.9999)); } #[test] @@ -57,15 +49,12 @@ fn struct_with_float() { engine.register_fn("update", TestStruct::update); engine.register_fn("new_ts", TestStruct::new); - if let Ok(result) = engine.eval::("let ts = new_ts(); ts.update(); ts.x") { - assert_eq!(result, 6.789); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("let ts = new_ts(); ts.x = 10.1001; ts.x") { - assert_eq!(result, 10.1001); - } else { - assert!(false); - } + assert_eq!( + engine.eval::("let ts = new_ts(); ts.update(); ts.x"), + Ok(6.789) + ); + assert_eq!( + engine.eval::("let ts = new_ts(); ts.x = 10.1001; ts.x"), + Ok(10.1001) + ); } diff --git a/tests/get_set.rs b/tests/get_set.rs index 8e76bc27..e3311278 100644 --- a/tests/get_set.rs +++ b/tests/get_set.rs @@ -29,11 +29,10 @@ fn test_get_set() { engine.register_get_set("x", TestStruct::get_x, TestStruct::set_x); engine.register_fn("new_ts", TestStruct::new); - if let Ok(result) = engine.eval::("let a = new_ts(); a.x = 500; a.x") { - assert_eq!(result, 500); - } else { - assert!(false); - } + assert_eq!( + engine.eval::("let a = new_ts(); a.x = 500; a.x"), + Ok(500) + ); } #[test] diff --git a/tests/if_block.rs b/tests/if_block.rs index c6de6c8d..94775618 100644 --- a/tests/if_block.rs +++ b/tests/if_block.rs @@ -4,21 +4,7 @@ use rhai::Engine; fn test_if() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("if true { 55 }") { - assert_eq!(result, 55); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("if false { 55 } else { 44 }") { - assert_eq!(result, 44); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("if true { 55 } else { 44 }") { - assert_eq!(result, 55); - } else { - assert!(false); - } + 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)); } diff --git a/tests/increment.rs b/tests/increment.rs index 3c541802..661ae38e 100644 --- a/tests/increment.rs +++ b/tests/increment.rs @@ -4,15 +4,9 @@ use rhai::Engine; fn test_increment() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("let x = 1; x += 2; x") { - assert_eq!(result, 3); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("let s = \"test\"; s += \"ing\"; s") { - assert_eq!(result, "testing".to_owned()); - } else { - assert!(false); - } + assert_eq!(engine.eval::("let x = 1; x += 2; x"), Ok(3)); + assert_eq!( + engine.eval::("let s = \"test\"; s += \"ing\"; s"), + Ok("testing".to_string()) + ); } diff --git a/tests/internal_fn.rs b/tests/internal_fn.rs index 48409635..f04a46ca 100644 --- a/tests/internal_fn.rs +++ b/tests/internal_fn.rs @@ -4,29 +4,22 @@ use rhai::Engine; fn test_internal_fn() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("fn addme(a, b) { a+b } addme(3, 4)") { - assert_eq!(result, 7); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("fn bob() { return 4; 5 } bob()") { - assert_eq!(result, 4); - } else { - assert!(false); - } + 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)); } #[test] fn test_big_internal_fn() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::( - "fn mathme(a, b, c, d, e, f) { a - b * c + d * e - f \ - } mathme(100, 5, 2, 9, 6, 32)", - ) { - assert_eq!(result, 112); - } else { - assert!(false); - } + 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) + ); } diff --git a/tests/not.rs b/tests/not.rs index 9a7cafba..0e6252a3 100644 --- a/tests/not.rs +++ b/tests/not.rs @@ -5,17 +5,12 @@ fn test_not() { let mut engine = Engine::new(); assert_eq!( - engine - .eval::("let not_true = !true; not_true") - .unwrap(), - false + engine.eval::("let not_true = !true; not_true"), + Ok(false) ); - assert_eq!( - engine.eval::("fn not(x) { !x } not(false)").unwrap(), - true - ); + assert_eq!(engine.eval::("fn not(x) { !x } not(false)"), Ok(true)); // TODO - do we allow stacking unary operators directly? e.g '!!!!!!!true' - assert_eq!(engine.eval::("!(!(!(!(true))))").unwrap(), true) + assert_eq!(engine.eval::("!(!(!(!(true))))"), Ok(true)); } diff --git a/tests/number_literals.rs b/tests/number_literals.rs index 4e5e3d09..05185cb9 100644 --- a/tests/number_literals.rs +++ b/tests/number_literals.rs @@ -4,60 +4,32 @@ use rhai::Engine; fn test_number_literal() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("65") { - assert_eq!(result, 65); - } else { - assert!(false); - } + assert_eq!(engine.eval::("65"), Ok(65)); } #[test] fn test_hex_literal() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("let x = 0xf; x") { - assert_eq!(result, 15); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("let x = 0xff; x") { - assert_eq!(result, 255); - } else { - assert!(false); - } + assert_eq!(engine.eval::("let x = 0xf; x"), Ok(15)); + assert_eq!(engine.eval::("let x = 0xff; x"), Ok(255)); } #[test] fn test_octal_literal() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("let x = 0o77; x") { - assert_eq!(result, 63); - } else { - assert!(false) - } - - if let Ok(result) = engine.eval::("let x = 0o1234; x") { - assert_eq!(result, 668); - } else { - assert!(false); - } + assert_eq!(engine.eval::("let x = 0o77; x"), Ok(63)); + assert_eq!(engine.eval::("let x = 0o1234; x"), Ok(668)); } #[test] fn test_binary_literal() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("let x = 0b1111; x") { - assert_eq!(result, 15); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("let x = 0b0011_1100_1010_0101; x") { - assert_eq!(result, 15525); - } else { - assert!(false); - } + assert_eq!(engine.eval::("let x = 0b1111; x"), Ok(15)); + assert_eq!( + engine.eval::("let x = 0b0011_1100_1010_0101; x"), + Ok(15525) + ); } diff --git a/tests/ops.rs b/tests/ops.rs index ceb5e6fe..c2f2d241 100644 --- a/tests/ops.rs +++ b/tests/ops.rs @@ -4,26 +4,16 @@ use rhai::Engine; fn test_ops() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("60 + 5") { - assert_eq!(result, 65); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("(1 + 2) * (6 - 4) / 2") { - assert_eq!(result, 3); - } else { - assert!(false); - } + assert_eq!(engine.eval::("60 + 5"), Ok(65)); + assert_eq!(engine.eval::("(1 + 2) * (6 - 4) / 2"), Ok(3)); } #[test] fn test_op_prec() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("let x = 0; if x == 10 || true { x = 1} x") { - assert_eq!(result, 1); - } else { - assert!(false); - } + assert_eq!( + engine.eval::("let x = 0; if x == 10 || true { x = 1} x"), + Ok(1) + ); } diff --git a/tests/power_of.rs b/tests/power_of.rs index bcac45df..45211fb6 100644 --- a/tests/power_of.rs +++ b/tests/power_of.rs @@ -4,39 +4,33 @@ use rhai::Engine; fn test_power_of() { let mut engine = Engine::new(); - assert_eq!(engine.eval::("2 ~ 3").unwrap(), 8); - assert_eq!(engine.eval::("(-2 ~ 3)").unwrap(), -8); - assert_eq!( - engine.eval::("2.2 ~ 3.3").unwrap(), - 13.489468760533386_f64 - ); - assert_eq!(engine.eval::("2.0~-2.0").unwrap(), 0.25_f64); - assert_eq!(engine.eval::("(-2.0~-2.0)").unwrap(), 0.25_f64); - assert_eq!(engine.eval::("(-2.0~-2)").unwrap(), 0.25_f64); - assert_eq!(engine.eval::("4~3").unwrap(), 64); + 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)); } #[test] fn test_power_of_equals() { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = 2; x ~= 3; x").unwrap(), 8); - assert_eq!(engine.eval::("let x = -2; x ~= 3; x").unwrap(), -8); + 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.2; x ~= 3.3; x").unwrap(), - 13.489468760533386_f64 + engine.eval::("let x = 2.2; x ~= 3.3; x"), + Ok(13.489468760533386_f64) ); assert_eq!( - engine.eval::("let x = 2.0; x ~= -2.0; x").unwrap(), - 0.25_f64 + 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").unwrap(), - 0.25_f64 + 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").unwrap(), - 0.25_f64 - ); - assert_eq!(engine.eval::("let x =4; x ~= 3; x").unwrap(), 64); + 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)); } diff --git a/tests/string.rs b/tests/string.rs index 26f97441..5b819e94 100644 --- a/tests/string.rs +++ b/tests/string.rs @@ -4,15 +4,12 @@ use rhai::Engine; fn test_string() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("\"Test string: \\u2764\"") { - assert_eq!(result, "Test string: ❤"); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("\"foo\" + \"bar\"") { - assert_eq!(result, "foobar"); - } else { - assert!(false); - } + assert_eq!( + engine.eval::("\"Test string: \\u2764\""), + Ok("Test string: ❤".to_string()) + ); + assert_eq!( + engine.eval::("\"foo\" + \"bar\""), + Ok("foobar".to_string()) + ); } diff --git a/tests/unary_after_binary.rs b/tests/unary_after_binary.rs index 0a1cc169..00778e7b 100644 --- a/tests/unary_after_binary.rs +++ b/tests/unary_after_binary.rs @@ -6,39 +6,10 @@ use rhai::Engine; fn test_unary_after_binary() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("10 % +4") { - assert_eq!(result, 2); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("10 << +4") { - assert_eq!(result, 160); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("10 >> +4") { - assert_eq!(result, 0); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("10 & +4") { - assert_eq!(result, 0); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("10 | +4") { - assert_eq!(result, 14); - } else { - assert!(false); - } - - if let Ok(result) = engine.eval::("10 ^ +4") { - assert_eq!(result, 14); - } else { - assert!(false); - } + 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)); } diff --git a/tests/unary_minus.rs b/tests/unary_minus.rs index 21f49ad4..49701bcb 100644 --- a/tests/unary_minus.rs +++ b/tests/unary_minus.rs @@ -4,9 +4,7 @@ use rhai::Engine; fn test_unary_minus() { let mut engine = Engine::new(); - assert_eq!(engine.eval::("let x = -5; x").unwrap(), -5); - - assert_eq!(engine.eval::("fn n(x) { -x } n(5)").unwrap(), -5); - - assert_eq!(engine.eval::("5 - -(-5)").unwrap(), 0); + 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)); } diff --git a/tests/unit.rs b/tests/unit.rs index 0f8fda65..7283b70b 100644 --- a/tests/unit.rs +++ b/tests/unit.rs @@ -4,31 +4,22 @@ use rhai::Engine; fn test_unit() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::<()>("let x = (); x") { - assert_eq!(result, ()); - } else { - assert!(false); - } + assert_eq!(engine.eval::<()>("let x = (); x"), Ok(())); } #[test] fn test_unit_eq() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::("let x = (); let y = (); x == y") { - assert!(result); - } else { - assert!(false); - } + assert_eq!( + engine.eval::("let x = (); let y = (); x == y"), + Ok(true) + ); } #[test] fn test_unit_with_spaces() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::<()>("let x = ( ); x") { - assert_eq!(result, ()); - } else { - assert!(false); - } + assert_eq!(engine.eval::<()>("let x = ( ); x"), Ok(())); } diff --git a/tests/var_scope.rs b/tests/var_scope.rs index c32cd07d..e802caa3 100644 --- a/tests/var_scope.rs +++ b/tests/var_scope.rs @@ -5,36 +5,24 @@ fn test_var_scope() { let mut engine = Engine::new(); let mut scope = Scope::new(); - if let Ok(_) = engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5") { - } else { - assert!(false); - } + assert_eq!( + engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5"), + Ok(()) + ); - if let Ok(result) = engine.eval_with_scope::(&mut scope, "x") { - assert_eq!(result, 9); - } else { - assert!(false); - } + assert_eq!(engine.eval_with_scope::(&mut scope, "x"), Ok(9)); - if let Ok(_) = engine.eval_with_scope::<()>(&mut scope, "x = x + 1; x = x + 2;") { - } else { - assert!(false); - } + assert_eq!( + engine.eval_with_scope::<()>(&mut scope, "x = x + 1; x = x + 2;"), + Ok(()) + ); - if let Ok(result) = engine.eval_with_scope::(&mut scope, "x") { - assert_eq!(result, 12); - } else { - assert!(false); - } + assert_eq!(engine.eval_with_scope::(&mut scope, "x"), Ok(12)); - if let Ok(_) = engine.eval_with_scope::<()>(&mut scope, "{let x = 3}") { - } else { - assert!(false); - } + assert_eq!( + engine.eval_with_scope::<()>(&mut scope, "{let x = 3}"), + Ok(()) + ); - if let Ok(result) = engine.eval_with_scope::(&mut scope, "x") { - assert_eq!(result, 12); - } else { - assert!(false); - } + assert_eq!(engine.eval_with_scope::(&mut scope, "x"), Ok(12)); } diff --git a/tests/while_loop.rs b/tests/while_loop.rs index 464ff982..3cbba708 100644 --- a/tests/while_loop.rs +++ b/tests/while_loop.rs @@ -4,12 +4,11 @@ use rhai::Engine; fn test_while() { let mut engine = Engine::new(); - if let Ok(result) = engine.eval::( - "let x = 0; while x < 10 { x = x + 1; if x > 5 { \ - break } } x", - ) { - assert_eq!(result, 6); - } else { - assert!(false); - } + assert_eq!( + engine.eval::( + "let x = 0; while x < 10 { x = x + 1; if x > 5 { \ + break } } x", + ), + Ok(6) + ); }