Remove unused enums and simplify test assertions

This commit is contained in:
timfish 2019-10-09 12:06:32 +01:00
parent 1999120298
commit 4217e96d19
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 {
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"
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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::<i64>("let x = 10; x -= 7; x") {
assert_eq!(result, 3);
} else {
assert!(false);
}
assert_eq!(engine.eval::<i64>("let x = 10; x -= 7; x"), Ok(3));
if let Ok(_) = engine.eval::<String>("let s = \"test\"; s -= \"ing\"; s") {
assert!(false);
} else {
assert!(true);
}
assert_eq!(
engine.eval::<String>("let s = \"test\"; s -= \"ing\"; s"),
Err(EvalAltResult::ErrorFunctionNotFound(
"- (alloc::string::String,alloc::string::String)".to_string()
))
);
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -5,17 +5,12 @@ fn test_not() {
let mut engine = Engine::new();
assert_eq!(
engine
.eval::<bool>("let not_true = !true; not_true")
.unwrap(),
false
engine.eval::<bool>("let not_true = !true; not_true"),
Ok(false)
);
assert_eq!(
engine.eval::<bool>("fn not(x) { !x } not(false)").unwrap(),
true
);
assert_eq!(engine.eval::<bool>("fn not(x) { !x } not(false)"), Ok(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() {
let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>("65") {
assert_eq!(result, 65);
} else {
assert!(false);
}
assert_eq!(engine.eval::<i64>("65"), Ok(65));
}
#[test]
fn test_hex_literal() {
let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>("let x = 0xf; x") {
assert_eq!(result, 15);
} else {
assert!(false);
}
if let Ok(result) = engine.eval::<i64>("let x = 0xff; x") {
assert_eq!(result, 255);
} else {
assert!(false);
}
assert_eq!(engine.eval::<i64>("let x = 0xf; x"), Ok(15));
assert_eq!(engine.eval::<i64>("let x = 0xff; x"), Ok(255));
}
#[test]
fn test_octal_literal() {
let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>("let x = 0o77; x") {
assert_eq!(result, 63);
} else {
assert!(false)
}
if let Ok(result) = engine.eval::<i64>("let x = 0o1234; x") {
assert_eq!(result, 668);
} else {
assert!(false);
}
assert_eq!(engine.eval::<i64>("let x = 0o77; x"), Ok(63));
assert_eq!(engine.eval::<i64>("let x = 0o1234; x"), Ok(668));
}
#[test]
fn test_binary_literal() {
let mut engine = Engine::new();
if let Ok(result) = engine.eval::<i64>("let x = 0b1111; x") {
assert_eq!(result, 15);
} else {
assert!(false);
}
if let Ok(result) = engine.eval::<i64>("let x = 0b0011_1100_1010_0101; x") {
assert_eq!(result, 15525);
} else {
assert!(false);
}
assert_eq!(engine.eval::<i64>("let x = 0b1111; x"), Ok(15));
assert_eq!(
engine.eval::<i64>("let x = 0b0011_1100_1010_0101; x"),
Ok(15525)
);
}

View File

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

View File

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

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

View File

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

View File

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

View File

@ -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::<bool>("let x = (); let y = (); x == y") {
assert!(result);
} else {
assert!(false);
}
assert_eq!(
engine.eval::<bool>("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(()));
}

View File

@ -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::<i64>(&mut scope, "x") {
assert_eq!(result, 9);
} else {
assert!(false);
}
assert_eq!(engine.eval_with_scope::<i64>(&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::<i64>(&mut scope, "x") {
assert_eq!(result, 12);
} else {
assert!(false);
}
assert_eq!(engine.eval_with_scope::<i64>(&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::<i64>(&mut scope, "x") {
assert_eq!(result, 12);
} else {
assert!(false);
}
assert_eq!(engine.eval_with_scope::<i64>(&mut scope, "x"), Ok(12));
}

View File

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