Merge pull request #90 from timfish/fixes/minor

Remove unused enums and simplify test assertions
This commit is contained in:
Jonathan Turner 2019-10-10 05:47:16 +13:00 committed by GitHub
commit c4a2e3bd44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 177 additions and 447 deletions

View File

@ -15,11 +15,9 @@ use crate::parser::{lex, parse, Expr, FnDef, Stmt};
pub enum EvalAltResult { pub enum EvalAltResult {
ErrorFunctionNotFound(String), ErrorFunctionNotFound(String),
ErrorFunctionArgMismatch, ErrorFunctionArgMismatch,
ErrorFunctionCallNotSupported,
ErrorIndexMismatch, ErrorIndexMismatch,
ErrorIfGuardMismatch, ErrorIfGuardMismatch,
ErrorVariableNotFound(String), ErrorVariableNotFound(String),
ErrorFunctionArityNotSupported,
ErrorAssignmentToUnknownLHS, ErrorAssignmentToUnknownLHS,
ErrorMismatchOutputType(String), ErrorMismatchOutputType(String),
ErrorCantOpenScriptFile, ErrorCantOpenScriptFile,
@ -46,11 +44,9 @@ impl PartialEq for EvalAltResult {
match (self, other) { match (self, other) {
(&ErrorFunctionNotFound(ref a), &ErrorFunctionNotFound(ref b)) => a == b, (&ErrorFunctionNotFound(ref a), &ErrorFunctionNotFound(ref b)) => a == b,
(&ErrorFunctionArgMismatch, &ErrorFunctionArgMismatch) => true, (&ErrorFunctionArgMismatch, &ErrorFunctionArgMismatch) => true,
(&ErrorFunctionCallNotSupported, &ErrorFunctionCallNotSupported) => true,
(&ErrorIndexMismatch, &ErrorIndexMismatch) => true, (&ErrorIndexMismatch, &ErrorIndexMismatch) => true,
(&ErrorIfGuardMismatch, &ErrorIfGuardMismatch) => true, (&ErrorIfGuardMismatch, &ErrorIfGuardMismatch) => true,
(&ErrorVariableNotFound(ref a), &ErrorVariableNotFound(ref b)) => a == b, (&ErrorVariableNotFound(ref a), &ErrorVariableNotFound(ref b)) => a == b,
(&ErrorFunctionArityNotSupported, &ErrorFunctionArityNotSupported) => true,
(&ErrorAssignmentToUnknownLHS, &ErrorAssignmentToUnknownLHS) => true, (&ErrorAssignmentToUnknownLHS, &ErrorAssignmentToUnknownLHS) => true,
(&ErrorMismatchOutputType(ref a), &ErrorMismatchOutputType(ref b)) => a == b, (&ErrorMismatchOutputType(ref a), &ErrorMismatchOutputType(ref b)) => a == b,
(&ErrorCantOpenScriptFile, &ErrorCantOpenScriptFile) => true, (&ErrorCantOpenScriptFile, &ErrorCantOpenScriptFile) => true,
@ -66,15 +62,9 @@ impl Error for EvalAltResult {
match *self { match *self {
EvalAltResult::ErrorFunctionNotFound(_) => "Function not found", EvalAltResult::ErrorFunctionNotFound(_) => "Function not found",
EvalAltResult::ErrorFunctionArgMismatch => "Function argument types do not match", 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::ErrorIndexMismatch => "Index does not match array",
EvalAltResult::ErrorIfGuardMismatch => "If guards expect boolean expression", EvalAltResult::ErrorIfGuardMismatch => "If guards expect boolean expression",
EvalAltResult::ErrorVariableNotFound(_) => "Variable not found", EvalAltResult::ErrorVariableNotFound(_) => "Variable not found",
EvalAltResult::ErrorFunctionArityNotSupported => {
"Functions of more than 3 parameters are not yet supported"
}
EvalAltResult::ErrorAssignmentToUnknownLHS => { EvalAltResult::ErrorAssignmentToUnknownLHS => {
"Assignment to an unsupported left-hand side" "Assignment to an unsupported left-hand side"
} }

View File

@ -5,17 +5,11 @@ use rhai::RegisterFn;
fn test_arrays() { fn test_arrays() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>("let x = [1, 2, 3]; x[1]") { assert_eq!(engine.eval::<i64>("let x = [1, 2, 3]; x[1]"), Ok(2));
assert_eq!(result, 2); assert_eq!(
} else { engine.eval::<i64>("let y = [1, 2, 3]; y[1] = 5; y[1]"),
assert!(false); Ok(5)
} );
if let Ok(result) = engine.eval::<i64>("let y = [1, 2, 3]; y[1] = 5; y[1]") {
assert_eq!(result, 5);
} else {
assert!(false);
}
} }
#[test] #[test]
@ -51,18 +45,15 @@ 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);
if let Ok(result) = engine.eval::<i64>("let a = [new_ts()]; a[0].x") { assert_eq!(engine.eval::<i64>("let a = [new_ts()]; a[0].x"), Ok(1));
assert_eq!(result, 1);
} else {
assert!(false);
}
if let Ok(result) = engine.eval::<i64>( assert_eq!(
"let a = [new_ts()]; a[0].x = 100; a[0].update(); \ engine.eval::<i64>(
a[0].x", "let a = [new_ts()]; \
) { a[0].x = 100; \
assert_eq!(result, 1100); a[0].update(); \
} else { a[0].x",
assert!(false); ),
} Ok(1100)
);
} }

View File

@ -4,39 +4,10 @@ use rhai::Engine;
fn test_binary_ops() { fn test_binary_ops() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>("10 % 4") { assert_eq!(engine.eval::<i64>("10 % 4"), Ok(2));
assert_eq!(result, 2); assert_eq!(engine.eval::<i64>("10 << 4"), Ok(160));
} else { assert_eq!(engine.eval::<i64>("10 >> 4"), Ok(0));
assert!(false); assert_eq!(engine.eval::<i64>("10 & 4"), Ok(0));
} assert_eq!(engine.eval::<i64>("10 | 4"), Ok(14));
assert_eq!(engine.eval::<i64>("10 ^ 4"), Ok(14));
if let Ok(result) = engine.eval::<i64>("10 << 4") {
assert_eq!(result, 160);
} else {
assert!(false);
}
if let Ok(result) = engine.eval::<i64>("10 >> 4") {
assert_eq!(result, 0);
} else {
assert!(false);
}
if let Ok(result) = engine.eval::<i64>("10 & 4") {
assert_eq!(result, 0);
} else {
assert!(false);
}
if let Ok(result) = engine.eval::<i64>("10 | 4") {
assert_eq!(result, 14);
} else {
assert!(false);
}
if let Ok(result) = engine.eval::<i64>("10 ^ 4") {
assert_eq!(result, 14);
} else {
assert!(false);
}
} }

View File

@ -4,20 +4,12 @@ use rhai::Engine;
fn test_left_shift() { fn test_left_shift() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>("4 << 2") { assert_eq!(engine.eval::<i64>("4 << 2"), Ok(16));
assert_eq!(result, 16);
} else {
assert!(false);
}
} }
#[test] #[test]
fn test_right_shift() { fn test_right_shift() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>("9 >> 1") { assert_eq!(engine.eval::<i64>("9 >> 1"), Ok(4));
assert_eq!(result, 4);
} else {
assert!(false);
}
} }

View File

@ -4,20 +4,12 @@ use rhai::Engine;
fn test_bool_op1() { fn test_bool_op1() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<bool>("true && (false || true)") { assert_eq!(engine.eval::<bool>("true && (false || true)"), Ok(true));
assert_eq!(result, true);
} else {
assert!(false);
}
} }
#[test] #[test]
fn test_bool_op2() { fn test_bool_op2() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<bool>("false && (false || true)") { assert_eq!(engine.eval::<bool>("false && (false || true)"), Ok(false));
assert_eq!(result, false);
} else {
assert!(false);
}
} }

View File

@ -4,17 +4,8 @@ use rhai::Engine;
fn test_chars() { fn test_chars() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<char>("'y'") { assert_eq!(engine.eval::<char>("'y'"), Ok('y'));
assert_eq!(result, 'y'); assert_eq!(engine.eval::<char>("'\\u2764'"), Ok('❤'));
} else {
assert!(false);
}
if let Ok(result) = engine.eval::<char>("'\\u2764'") {
assert_eq!(result, '❤');
} else {
assert!(false);
}
match engine.eval::<char>("''") { match engine.eval::<char>("''") {
Err(_) => (), Err(_) => (),

View File

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

View File

@ -1,18 +1,16 @@
use rhai::Engine; use rhai::Engine;
use rhai::EvalAltResult;
#[test] #[test]
fn test_decrement() { fn test_decrement() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>("let x = 10; x -= 7; x") { assert_eq!(engine.eval::<i64>("let x = 10; x -= 7; x"), Ok(3));
assert_eq!(result, 3);
} else {
assert!(false);
}
if let Ok(_) = engine.eval::<String>("let s = \"test\"; s -= \"ing\"; s") { assert_eq!(
assert!(false); engine.eval::<String>("let s = \"test\"; s -= \"ing\"; s"),
} else { Err(EvalAltResult::ErrorFunctionNotFound(
assert!(true); "- (alloc::string::String,alloc::string::String)".to_string()
} ))
);
} }

View File

@ -5,23 +5,15 @@ use rhai::RegisterFn;
fn test_float() { fn test_float() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<bool>("let x = 0.0; let y = 1.0; x < y") { assert_eq!(
assert!(result); engine.eval::<bool>("let x = 0.0; let y = 1.0; x < y"),
} else { Ok(true)
assert!(false); );
} assert_eq!(
engine.eval::<bool>("let x = 0.0; let y = 1.0; x > y"),
if let Ok(result) = engine.eval::<bool>("let x = 0.0; let y = 1.0; x > y") { Ok(false)
assert!(!result); );
} else { assert_eq!(engine.eval::<f64>("let x = 9.9999; x"), Ok(9.9999));
assert!(false);
}
if let Ok(result) = engine.eval::<f64>("let x = 9.9999; x") {
assert_eq!(result, 9.9999);
} else {
assert!(false);
}
} }
#[test] #[test]
@ -57,15 +49,12 @@ fn struct_with_float() {
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::<f64>("let ts = new_ts(); ts.update(); ts.x") { assert_eq!(
assert_eq!(result, 6.789); engine.eval::<f64>("let ts = new_ts(); ts.update(); ts.x"),
} else { Ok(6.789)
assert!(false); );
} assert_eq!(
engine.eval::<f64>("let ts = new_ts(); ts.x = 10.1001; ts.x"),
if let Ok(result) = engine.eval::<f64>("let ts = new_ts(); ts.x = 10.1001; ts.x") { Ok(10.1001)
assert_eq!(result, 10.1001); );
} else {
assert!(false);
}
} }

View File

@ -29,11 +29,10 @@ 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);
if let Ok(result) = engine.eval::<i64>("let a = new_ts(); a.x = 500; a.x") { assert_eq!(
assert_eq!(result, 500); engine.eval::<i64>("let a = new_ts(); a.x = 500; a.x"),
} else { Ok(500)
assert!(false); );
}
} }
#[test] #[test]

View File

@ -4,21 +4,7 @@ use rhai::Engine;
fn test_if() { fn test_if() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>("if true { 55 }") { assert_eq!(engine.eval::<i64>("if true { 55 }"), Ok(55));
assert_eq!(result, 55); assert_eq!(engine.eval::<i64>("if false { 55 } else { 44 }"), Ok(44));
} else { assert_eq!(engine.eval::<i64>("if true { 55 } else { 44 }"), Ok(55));
assert!(false);
}
if let Ok(result) = engine.eval::<i64>("if false { 55 } else { 44 }") {
assert_eq!(result, 44);
} else {
assert!(false);
}
if let Ok(result) = engine.eval::<i64>("if true { 55 } else { 44 }") {
assert_eq!(result, 55);
} else {
assert!(false);
}
} }

View File

@ -4,15 +4,9 @@ use rhai::Engine;
fn test_increment() { fn test_increment() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>("let x = 1; x += 2; x") { assert_eq!(engine.eval::<i64>("let x = 1; x += 2; x"), Ok(3));
assert_eq!(result, 3); assert_eq!(
} else { engine.eval::<String>("let s = \"test\"; s += \"ing\"; s"),
assert!(false); Ok("testing".to_string())
} );
if let Ok(result) = engine.eval::<String>("let s = \"test\"; s += \"ing\"; s") {
assert_eq!(result, "testing".to_owned());
} else {
assert!(false);
}
} }

View File

@ -4,29 +4,22 @@ use rhai::Engine;
fn test_internal_fn() { fn test_internal_fn() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>("fn addme(a, b) { a+b } addme(3, 4)") { assert_eq!(
assert_eq!(result, 7); engine.eval::<i64>("fn addme(a, b) { a+b } addme(3, 4)"),
} else { Ok(7)
assert!(false); );
} assert_eq!(engine.eval::<i64>("fn bob() { return 4; 5 } bob()"), Ok(4));
if let Ok(result) = engine.eval::<i64>("fn bob() { return 4; 5 } bob()") {
assert_eq!(result, 4);
} else {
assert!(false);
}
} }
#[test] #[test]
fn test_big_internal_fn() { fn test_big_internal_fn() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>( assert_eq!(
"fn mathme(a, b, c, d, e, f) { a - b * c + d * e - f \ engine.eval::<i64>(
} mathme(100, 5, 2, 9, 6, 32)", "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 { Ok(112)
assert!(false); );
}
} }

View File

@ -5,17 +5,12 @@ fn test_not() {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!( assert_eq!(
engine engine.eval::<bool>("let not_true = !true; not_true"),
.eval::<bool>("let not_true = !true; not_true") Ok(false)
.unwrap(),
false
); );
assert_eq!( assert_eq!(engine.eval::<bool>("fn not(x) { !x } not(false)"), Ok(true));
engine.eval::<bool>("fn not(x) { !x } not(false)").unwrap(),
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))))").unwrap(), true) assert_eq!(engine.eval::<bool>("!(!(!(!(true))))"), Ok(true));
} }

View File

@ -4,60 +4,32 @@ use rhai::Engine;
fn test_number_literal() { fn test_number_literal() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>("65") { assert_eq!(engine.eval::<i64>("65"), Ok(65));
assert_eq!(result, 65);
} else {
assert!(false);
}
} }
#[test] #[test]
fn test_hex_literal() { fn test_hex_literal() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>("let x = 0xf; x") { assert_eq!(engine.eval::<i64>("let x = 0xf; x"), Ok(15));
assert_eq!(result, 15); assert_eq!(engine.eval::<i64>("let x = 0xff; x"), Ok(255));
} else {
assert!(false);
}
if let Ok(result) = engine.eval::<i64>("let x = 0xff; x") {
assert_eq!(result, 255);
} else {
assert!(false);
}
} }
#[test] #[test]
fn test_octal_literal() { fn test_octal_literal() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>("let x = 0o77; x") { assert_eq!(engine.eval::<i64>("let x = 0o77; x"), Ok(63));
assert_eq!(result, 63); assert_eq!(engine.eval::<i64>("let x = 0o1234; x"), Ok(668));
} else {
assert!(false)
}
if let Ok(result) = engine.eval::<i64>("let x = 0o1234; x") {
assert_eq!(result, 668);
} else {
assert!(false);
}
} }
#[test] #[test]
fn test_binary_literal() { fn test_binary_literal() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>("let x = 0b1111; x") { assert_eq!(engine.eval::<i64>("let x = 0b1111; x"), Ok(15));
assert_eq!(result, 15); assert_eq!(
} else { engine.eval::<i64>("let x = 0b0011_1100_1010_0101; x"),
assert!(false); Ok(15525)
} );
if let Ok(result) = engine.eval::<i64>("let x = 0b0011_1100_1010_0101; x") {
assert_eq!(result, 15525);
} else {
assert!(false);
}
} }

View File

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

View File

@ -4,39 +4,33 @@ use rhai::Engine;
fn test_power_of() { fn test_power_of() {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("2 ~ 3").unwrap(), 8); assert_eq!(engine.eval::<i64>("2 ~ 3"), Ok(8));
assert_eq!(engine.eval::<i64>("(-2 ~ 3)").unwrap(), -8); assert_eq!(engine.eval::<i64>("(-2 ~ 3)"), Ok(-8));
assert_eq!( assert_eq!(engine.eval::<f64>("2.2 ~ 3.3"), Ok(13.489468760533386_f64));
engine.eval::<f64>("2.2 ~ 3.3").unwrap(), assert_eq!(engine.eval::<f64>("2.0~-2.0"), Ok(0.25_f64));
13.489468760533386_f64 assert_eq!(engine.eval::<f64>("(-2.0~-2.0)"), Ok(0.25_f64));
); assert_eq!(engine.eval::<f64>("(-2.0~-2)"), Ok(0.25_f64));
assert_eq!(engine.eval::<f64>("2.0~-2.0").unwrap(), 0.25_f64); assert_eq!(engine.eval::<i64>("4~3"), Ok(64));
assert_eq!(engine.eval::<f64>("(-2.0~-2.0)").unwrap(), 0.25_f64);
assert_eq!(engine.eval::<f64>("(-2.0~-2)").unwrap(), 0.25_f64);
assert_eq!(engine.eval::<i64>("4~3").unwrap(), 64);
} }
#[test] #[test]
fn test_power_of_equals() { fn test_power_of_equals() {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("let x = 2; x ~= 3; x").unwrap(), 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").unwrap(), -8); assert_eq!(engine.eval::<i64>("let x = -2; x ~= 3; x"), Ok(-8));
assert_eq!( assert_eq!(
engine.eval::<f64>("let x = 2.2; x ~= 3.3; x").unwrap(), engine.eval::<f64>("let x = 2.2; x ~= 3.3; x"),
13.489468760533386_f64 Ok(13.489468760533386_f64)
); );
assert_eq!( assert_eq!(
engine.eval::<f64>("let x = 2.0; x ~= -2.0; x").unwrap(), engine.eval::<f64>("let x = 2.0; x ~= -2.0; x"),
0.25_f64 Ok(0.25_f64)
); );
assert_eq!( assert_eq!(
engine.eval::<f64>("let x = -2.0; x ~= -2.0; x").unwrap(), engine.eval::<f64>("let x = -2.0; x ~= -2.0; x"),
0.25_f64 Ok(0.25_f64)
); );
assert_eq!( assert_eq!(engine.eval::<f64>("let x = -2.0; x ~= -2; x"), Ok(0.25_f64));
engine.eval::<f64>("let x = -2.0; x ~= -2; x").unwrap(), assert_eq!(engine.eval::<i64>("let x =4; x ~= 3; x"), Ok(64));
0.25_f64
);
assert_eq!(engine.eval::<i64>("let x =4; x ~= 3; x").unwrap(), 64);
} }

View File

@ -4,15 +4,12 @@ use rhai::Engine;
fn test_string() { fn test_string() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<String>("\"Test string: \\u2764\"") { assert_eq!(
assert_eq!(result, "Test string: ❤"); engine.eval::<String>("\"Test string: \\u2764\""),
} else { Ok("Test string: ❤".to_string())
assert!(false); );
} assert_eq!(
engine.eval::<String>("\"foo\" + \"bar\""),
if let Ok(result) = engine.eval::<String>("\"foo\" + \"bar\"") { Ok("foobar".to_string())
assert_eq!(result, "foobar"); );
} else {
assert!(false);
}
} }

View File

@ -6,39 +6,10 @@ use rhai::Engine;
fn test_unary_after_binary() { fn test_unary_after_binary() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>("10 % +4") { assert_eq!(engine.eval::<i64>("10 % +4"), Ok(2));
assert_eq!(result, 2); assert_eq!(engine.eval::<i64>("10 << +4"), Ok(160));
} else { assert_eq!(engine.eval::<i64>("10 >> +4"), Ok(0));
assert!(false); assert_eq!(engine.eval::<i64>("10 & +4"), Ok(0));
} assert_eq!(engine.eval::<i64>("10 | +4"), Ok(14));
assert_eq!(engine.eval::<i64>("10 ^ +4"), Ok(14));
if let Ok(result) = engine.eval::<i64>("10 << +4") {
assert_eq!(result, 160);
} else {
assert!(false);
}
if let Ok(result) = engine.eval::<i64>("10 >> +4") {
assert_eq!(result, 0);
} else {
assert!(false);
}
if let Ok(result) = engine.eval::<i64>("10 & +4") {
assert_eq!(result, 0);
} else {
assert!(false);
}
if let Ok(result) = engine.eval::<i64>("10 | +4") {
assert_eq!(result, 14);
} else {
assert!(false);
}
if let Ok(result) = engine.eval::<i64>("10 ^ +4") {
assert_eq!(result, 14);
} else {
assert!(false);
}
} }

View File

@ -4,9 +4,7 @@ use rhai::Engine;
fn test_unary_minus() { fn test_unary_minus() {
let mut engine = Engine::new(); let mut engine = Engine::new();
assert_eq!(engine.eval::<i64>("let x = -5; x").unwrap(), -5); assert_eq!(engine.eval::<i64>("let x = -5; x"), Ok(-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)").unwrap(), -5); assert_eq!(engine.eval::<i64>("5 - -(-5)"), Ok(0));
assert_eq!(engine.eval::<i64>("5 - -(-5)").unwrap(), 0);
} }

View File

@ -4,31 +4,22 @@ use rhai::Engine;
fn test_unit() { fn test_unit() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<()>("let x = (); x") { assert_eq!(engine.eval::<()>("let x = (); x"), Ok(()));
assert_eq!(result, ());
} else {
assert!(false);
}
} }
#[test] #[test]
fn test_unit_eq() { fn test_unit_eq() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<bool>("let x = (); let y = (); x == y") { assert_eq!(
assert!(result); engine.eval::<bool>("let x = (); let y = (); x == y"),
} else { Ok(true)
assert!(false); );
}
} }
#[test] #[test]
fn test_unit_with_spaces() { fn test_unit_with_spaces() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<()>("let x = ( ); x") { assert_eq!(engine.eval::<()>("let x = ( ); x"), Ok(()));
assert_eq!(result, ());
} else {
assert!(false);
}
} }

View File

@ -5,36 +5,24 @@ fn test_var_scope() {
let mut engine = Engine::new(); let mut engine = Engine::new();
let mut scope = Scope::new(); let mut scope = Scope::new();
if let Ok(_) = engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5") { assert_eq!(
} else { engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5"),
assert!(false); Ok(())
} );
if let Ok(result) = engine.eval_with_scope::<i64>(&mut scope, "x") { assert_eq!(engine.eval_with_scope::<i64>(&mut scope, "x"), Ok(9));
assert_eq!(result, 9);
} else {
assert!(false);
}
if let Ok(_) = engine.eval_with_scope::<()>(&mut scope, "x = x + 1; x = x + 2;") { assert_eq!(
} else { engine.eval_with_scope::<()>(&mut scope, "x = x + 1; x = x + 2;"),
assert!(false); Ok(())
} );
if let Ok(result) = engine.eval_with_scope::<i64>(&mut scope, "x") { assert_eq!(engine.eval_with_scope::<i64>(&mut scope, "x"), Ok(12));
assert_eq!(result, 12);
} else {
assert!(false);
}
if let Ok(_) = engine.eval_with_scope::<()>(&mut scope, "{let x = 3}") { assert_eq!(
} else { engine.eval_with_scope::<()>(&mut scope, "{let x = 3}"),
assert!(false); Ok(())
} );
if let Ok(result) = engine.eval_with_scope::<i64>(&mut scope, "x") { assert_eq!(engine.eval_with_scope::<i64>(&mut scope, "x"), Ok(12));
assert_eq!(result, 12);
} else {
assert!(false);
}
} }

View File

@ -4,12 +4,11 @@ use rhai::Engine;
fn test_while() { fn test_while() {
let mut engine = Engine::new(); let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>( assert_eq!(
"let x = 0; while x < 10 { x = x + 1; if x > 5 { \ engine.eval::<i64>(
break } } x", "let x = 0; while x < 10 { x = x + 1; if x > 5 { \
) { break } } x",
assert_eq!(result, 6); ),
} else { Ok(6)
assert!(false); );
}
} }