Refactor tests.

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

View File

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

View File

@ -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::<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)
);
assert_eq!(engine.eval::<i64>("let x = [1, 2, 3]; x[1]")?, 2);
assert_eq!(engine.eval::<i64>("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::<i64>("let a = [new_ts()]; a[0].x"), Ok(1));
assert_eq!(engine.eval::<i64>("let a = [new_ts()]; a[0].x")?, 1);
assert_eq!(
engine.eval::<i64>(
@ -53,7 +51,9 @@ fn test_array_with_structs() {
a[0].x = 100; \
a[0].update(); \
a[0].x",
),
Ok(1100)
)?,
1100
);
Ok(())
}

View File

@ -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::<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));
assert_eq!(engine.eval::<i64>("10 % 4")?, 2);
assert_eq!(engine.eval::<i64>("10 << 4")?, 160);
assert_eq!(engine.eval::<i64>("10 >> 4")?, 0);
assert_eq!(engine.eval::<i64>("10 & 4")?, 0);
assert_eq!(engine.eval::<i64>("10 | 4")?, 14);
assert_eq!(engine.eval::<i64>("10 ^ 4")?, 14);
assert_eq!(engine.eval::<bool>("42 == 42"), Ok(true));
assert_eq!(engine.eval::<bool>("42 > 42"), Ok(false));
assert_eq!(engine.eval::<bool>("42 == 42")?, true);
assert_eq!(engine.eval::<bool>("42 > 42")?, false);
// Incompatible types compare to false
assert_eq!(engine.eval::<bool>("true == 42"), Ok(false));
assert_eq!(engine.eval::<bool>(r#""42" == 42"#), Ok(false));
assert_eq!(engine.eval::<bool>("true == 42")?, false);
assert_eq!(engine.eval::<bool>(r#""42" == 42"#)?, false);
Ok(())
}

View File

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

View File

@ -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::<bool>("true && (false || true)"), Ok(true));
assert_eq!(engine.eval::<bool>("true & (false | true)"), Ok(true));
assert_eq!(engine.eval::<bool>("true && (false || true)")?, true);
assert_eq!(engine.eval::<bool>("true & (false | true)")?, true);
Ok(())
}
#[test]
fn test_bool_op2() {
fn test_bool_op2() -> Result<(), EvalAltResult> {
let mut engine = Engine::new();
assert_eq!(engine.eval::<bool>("false && (false || true)"), Ok(false));
assert_eq!(engine.eval::<bool>("false & (false | true)"), Ok(false));
assert_eq!(engine.eval::<bool>("false && (false || true)")?, false);
assert_eq!(engine.eval::<bool>("false & (false | true)")?, false);
Ok(())
}
#[test]
fn test_bool_op3() {
fn test_bool_op3() -> Result<(), EvalAltResult> {
let mut engine = Engine::new();
assert_eq!(
engine.eval::<bool>("true && (false || 123)"),
Err(EvalAltResult::ErrorBooleanArgMismatch("OR".into()))
);
assert!(engine.eval::<bool>("true && (false || 123)").is_err());
assert_eq!(engine.eval::<bool>("true && (true || 123)")?, true);
assert!(engine.eval::<bool>("123 && (false || true)").is_err());
assert_eq!(engine.eval::<bool>("false && (true || 123)")?, false);
assert_eq!(engine.eval::<bool>("true && (true || 123)"), Ok(true));
assert_eq!(
engine.eval::<bool>("123 && (false || true)"),
Err(EvalAltResult::ErrorBooleanArgMismatch("AND".into()))
);
assert_eq!(engine.eval::<bool>("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::<bool>(
engine
.eval::<bool>(
r"
fn this() { false }
fn that() { 9/0 }
this() | that();
"
),
Ok(false)
)
.unwrap(),
false
);
}
@ -88,14 +89,16 @@ fn test_bool_op_no_short_circuit2() {
let mut engine = Engine::new();
assert_eq!(
engine.eval::<bool>(
engine
.eval::<bool>(
r"
fn this() { false }
fn that() { 9/0 }
this() & that();
"
),
Ok(false)
)
.unwrap(),
false
);
}

View File

@ -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::<char>("'y'"), Ok('y'));
assert_eq!(engine.eval::<char>("'\\u2764'"), Ok('❤'));
assert_eq!(engine.eval::<char>(r#"let x="hello"; x[2]"#), Ok('l'));
assert_eq!(engine.eval::<char>("'y'")?, 'y');
assert_eq!(engine.eval::<char>("'\\u2764'")?, '❤');
assert_eq!(engine.eval::<char>(r#"let x="hello"; x[2]"#)?, 'l');
assert_eq!(
engine.eval::<String>(r#"let x="hello"; x[2]='$'; x"#),
Ok("he$lo".into())
engine.eval::<String>(r#"let x="hello"; x[2]='$'; x"#)?,
"he$lo".to_string()
);
match engine.eval::<char>("'\\uhello'") {
Err(_) => (),
_ => assert!(false),
}
assert!(engine.eval::<char>("'\\uhello'").is_err());
assert!(engine.eval::<char>("''").is_err());
match engine.eval::<char>("''") {
Err(_) => (),
_ => assert!(false),
}
Ok(())
}

View File

@ -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::<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));
assert_eq!(engine.eval::<i64>("let x = 16; x |= 74; x")?, 90);
assert_eq!(engine.eval::<bool>("let x = true; x |= false; x")?, true);
assert_eq!(engine.eval::<bool>("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::<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));
assert_eq!(engine.eval::<i64>("let x = 16; x &= 31; x")?, 16);
assert_eq!(engine.eval::<bool>("let x = true; x &= false; x")?, false);
assert_eq!(engine.eval::<bool>("let x = false; x &= true; x")?, false);
assert_eq!(engine.eval::<bool>("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::<i64>("let x = 90; x ^= 12; x"), Ok(86));
assert_eq!(engine.eval::<i64>("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::<i64>("let x = 2; x *= 3; x"), Ok(6));
assert_eq!(engine.eval::<i64>("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::<i64>("let x = 6; x /= 2; x"), Ok(3));
assert_eq!(engine.eval::<i64>("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::<i64>("let x = 9; x >>=1; x"), Ok(4));
assert_eq!(engine.eval::<i64>("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::<i64>("let x = 4; x<<= 2; x"), Ok(16));
assert_eq!(engine.eval::<i64>("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::<i64>("let x = 10; x %= 4; x"), Ok(2));
assert_eq!(engine.eval::<i64>("let x = 10; x %= 4; x")?, 2);
Ok(())
}

View File

@ -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::<i64>("let x = 10; x -= 7; x"), Ok(3));
assert_eq!(engine.eval::<i64>("let x = 10; x -= 7; x")?, 3);
assert_eq!(
engine.eval::<String>("let s = \"test\"; s -= \"ing\"; s"),
Err(EvalAltResult::ErrorFunctionNotFound(
"- (alloc::string::String, alloc::string::String)".to_string()
))
);
let r = engine.eval::<String>("let s = \"test\"; s -= \"ing\"; s");
match r {
Err(EvalAltResult::ErrorFunctionNotFound(err, _)) if err.starts_with("- ") => (),
_ => panic!(),
}
Ok(())
}

View File

@ -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::<bool>("let x = 0.0; let y = 1.0; x < y"),
Ok(true)
engine.eval::<bool>("let x = 0.0; let y = 1.0; x < y")?,
true
);
assert_eq!(
engine.eval::<bool>("let x = 0.0; let y = 1.0; x > y"),
Ok(false)
engine.eval::<bool>("let x = 0.0; let y = 1.0; x > y")?,
false
);
assert_eq!(engine.eval::<f64>("let x = 9.9999; x"), Ok(9.9999));
assert_eq!(engine.eval::<f64>("let x = 9.9999; x")?, 9.9999);
Ok(())
}
#[test]
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::<f64>("let ts = new_ts(); ts.update(); ts.x"),
Ok(6.789)
engine.eval::<f64>("let ts = new_ts(); ts.update(); ts.x")?,
6.789
);
assert_eq!(
engine.eval::<f64>("let ts = new_ts(); ts.x = 10.1001; ts.x"),
Ok(10.1001)
engine.eval::<f64>("let ts = new_ts(); ts.x = 10.1001; ts.x")?,
10.1001
);
Ok(())
}

View File

@ -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::<i64>(script).unwrap(), 30);
assert_eq!(engine.eval::<i64>(script)?, 30);
Ok(())
}

View File

@ -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::<i64>("let a = new_ts(); a.x = 500; a.x"),
Ok(500)
);
assert_eq!(engine.eval::<i64>("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::<i64>("let a = new_tp(); a.child.x = 500; a.child.x"),
Ok(500)
engine.eval::<i64>("let a = new_tp(); a.child.x = 500; a.child.x")?,
500
);
Ok(())
}

View File

@ -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::<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));
assert_eq!(engine.eval::<i64>("if true { 55 }")?, 55);
assert_eq!(engine.eval::<i64>("if false { 55 } else { 44 }")?, 44);
assert_eq!(engine.eval::<i64>("if true { 55 } else { 44 }")?, 55);
assert_eq!(
engine.eval::<i64>("if false { 55 } else if true { 33 } else { 44 }"),
Ok(33)
engine.eval::<i64>("if false { 55 } else if true { 33 } else { 44 }")?,
33
);
assert_eq!(
engine.eval::<i64>(
@ -21,7 +21,9 @@ fn test_if() {
else if false { 88 }
else { 44 }
"
),
Ok(44)
)?,
44
);
Ok(())
}

View File

@ -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::<i64>("let x = 1; x += 2; x"), Ok(3));
assert_eq!(engine.eval::<i64>("let x = 1; x += 2; x")?, 3);
assert_eq!(
engine.eval::<String>("let s = \"test\"; s += \"ing\"; s"),
Ok("testing".to_string())
engine.eval::<String>("let s = \"test\"; s += \"ing\"; s")?,
"testing".to_string()
);
Ok(())
}

View File

@ -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::<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));
assert_eq!(engine.eval::<i64>("fn addme(a, b) { a+b } addme(3, 4)")?, 7);
assert_eq!(engine.eval::<i64>("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::<i64>(
"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(())
}

View File

@ -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::<bool>(
"
assert!(engine.eval::<bool>(
r"
let x = 0;
let i = 0;
@ -22,6 +21,7 @@ fn test_loop() {
x == 45
"
)
.unwrap())
)?);
Ok(())
}

View File

@ -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::<TestStruct>("let x = new_ts(); x.update(); x") {
assert_eq!(result.x, 1001);
} else {
assert!(false);
}
let ts = engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")?;
assert_eq!(ts.x, 1001);
Ok(())
}

View File

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

View File

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

View File

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

View File

@ -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::<i64>("60 + 5"), Ok(65));
assert_eq!(engine.eval::<i64>("(1 + 2) * (6 - 4) / 2"), Ok(3));
assert_eq!(engine.eval::<i64>("60 + 5")?, 65);
assert_eq!(engine.eval::<i64>("(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::<i64>("let x = 0; if x == 10 || true { x = 1} x"),
Ok(1)
engine.eval::<i64>("let x = 0; if x == 10 || true { x = 1} x")?,
1
);
Ok(())
}

View File

@ -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::<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));
assert_eq!(engine.eval::<i64>("2 ~ 3")?, 8);
assert_eq!(engine.eval::<i64>("(-2 ~ 3)")?, -8);
assert_eq!(engine.eval::<f64>("2.2 ~ 3.3")?, 13.489468760533386_f64);
assert_eq!(engine.eval::<f64>("2.0~-2.0")?, 0.25_f64);
assert_eq!(engine.eval::<f64>("(-2.0~-2.0)")?, 0.25_f64);
assert_eq!(engine.eval::<f64>("(-2.0~-2)")?, 0.25_f64);
assert_eq!(engine.eval::<i64>("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::<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::<i64>("let x = 2; x ~= 3; x")?, 8);
assert_eq!(engine.eval::<i64>("let x = -2; x ~= 3; x")?, -8);
assert_eq!(
engine.eval::<f64>("let x = 2.2; x ~= 3.3; x"),
Ok(13.489468760533386_f64)
engine.eval::<f64>("let x = 2.2; x ~= 3.3; x")?,
13.489468760533386_f64
);
assert_eq!(
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"),
Ok(0.25_f64)
);
assert_eq!(engine.eval::<f64>("let x = -2.0; x ~= -2; x"), Ok(0.25_f64));
assert_eq!(engine.eval::<i64>("let x =4; x ~= 3; x"), Ok(64));
assert_eq!(engine.eval::<f64>("let x = 2.0; x ~= -2.0; x")?, 0.25_f64);
assert_eq!(engine.eval::<f64>("let x = -2.0; x ~= -2.0; x")?, 0.25_f64);
assert_eq!(engine.eval::<f64>("let x = -2.0; x ~= -2; x")?, 0.25_f64);
assert_eq!(engine.eval::<i64>("let x =4; x ~= 3; x")?, 64);
Ok(())
}

View File

@ -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::<String>("\"Test string: \\u2764\""),
Ok("Test string: ❤".to_string())
engine.eval::<String>("\"Test string: \\u2764\"")?,
"Test string: ❤".to_string()
);
assert_eq!(
engine.eval::<String>("\"foo\" + \"bar\""),
Ok("foobar".to_string())
engine.eval::<String>("\"foo\" + \"bar\"")?,
"foobar".to_string()
);
Ok(())
}

View File

@ -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::<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));
assert_eq!(engine.eval::<i64>("10 % +4")?, 2);
assert_eq!(engine.eval::<i64>("10 << +4")?, 160);
assert_eq!(engine.eval::<i64>("10 >> +4")?, 0);
assert_eq!(engine.eval::<i64>("10 & +4")?, 0);
assert_eq!(engine.eval::<i64>("10 | +4")?, 14);
assert_eq!(engine.eval::<i64>("10 ^ +4")?, 14);
Ok(())
}

View File

@ -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::<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));
assert_eq!(engine.eval::<i64>("let x = -5; x")?, -5);
assert_eq!(engine.eval::<i64>("fn n(x) { -x } n(5)")?, -5);
assert_eq!(engine.eval::<i64>("5 - -(-5)")?, 0);
Ok(())
}

View File

@ -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::<bool>("let x = (); let y = (); x == y"),
Ok(true)
);
assert_eq!(engine.eval::<bool>("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(())
}

View File

@ -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"),
engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5")?;
assert_eq!(engine.eval_with_scope::<i64>(&mut scope, "x")?, 9);
engine.eval_with_scope::<()>(&mut scope, "x = x + 1; x = x + 2;")?;
assert_eq!(engine.eval_with_scope::<i64>(&mut scope, "x")?, 12);
assert_eq!(engine.eval_with_scope::<()>(&mut scope, "{let x = 3}")?, ());
assert_eq!(engine.eval_with_scope::<i64>(&mut scope, "x")?, 12);
Ok(())
);
assert_eq!(engine.eval_with_scope::<i64>(&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::<i64>(&mut scope, "x"), Ok(12));
assert_eq!(
engine.eval_with_scope::<()>(&mut scope, "{let x = 3}"),
Ok(())
);
assert_eq!(engine.eval_with_scope::<i64>(&mut scope, "x"), Ok(12));
}

View File

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