diff --git a/src/tests.rs b/src/tests.rs deleted file mode 100644 index aea39b64..00000000 --- a/src/tests.rs +++ /dev/null @@ -1,838 +0,0 @@ -use engine::{Engine, Scope, EvalAltResult}; -use fn_register::FnRegister; - -#[test] -fn test_number_literal() { - let mut engine = Engine::new(); - - if let Ok(result) = engine.eval::("65") { - assert_eq!(result, 65); - } else { - assert!(false); - } -} - -#[test] -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); - } - - match engine.eval::("''") { - Err(_) => (), - _ => assert!(false), - } -} - -#[test] -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); - } -} - -#[test] -fn test_mismatched_op() { - let mut engine = Engine::new(); - - match engine.eval::("60 + \"hello\"") { - Err(EvalAltResult::ErrorFunctionArgMismatch) => (), - _ => assert!(false), - } -} - -#[test] -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); - } -} - -#[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); - } -} - -#[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); - } -} - -#[test] -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); - } -} - -#[test] -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); - } -} - -#[test] -fn test_var_scope() { - let mut engine = Engine::new(); - let mut scope: Scope = Vec::new(); - - if let Ok(_) = engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5") { - } else { - assert!(false); - } - - if let Ok(result) = engine.eval_with_scope::(&mut scope, "x") { - assert_eq!(result, 9); - } else { - assert!(false); - } - - if let Ok(_) = engine.eval_with_scope::<()>(&mut scope, "x = x + 1; x = x + 2;") { - } else { - assert!(false); - } - - if let Ok(result) = engine.eval_with_scope::(&mut scope, "x") { - assert_eq!(result, 12); - } else { - assert!(false); - } - - if let Ok(_) = engine.eval_with_scope::<()>(&mut scope, "{let x = 3}") { - } else { - assert!(false); - } - - if let Ok(result) = engine.eval_with_scope::(&mut scope, "x") { - assert_eq!(result, 12); - } else { - assert!(false); - } -} - -#[test] -fn test_method_call() { - #[derive(Clone)] - struct TestStruct { - x: i64, - } - - impl TestStruct { - fn update(&mut self) { - self.x += 1000; - } - - fn new() -> TestStruct { - TestStruct { x: 1 } - } - } - - let mut engine = Engine::new(); - - engine.register_type::(); - - engine.register_fn("update", TestStruct::update); - engine.register_fn("new_ts", TestStruct::new); - - if let Ok(result) = engine.eval::("let x = new_ts(); x.update(); x") { - assert_eq!(result.x, 1001); - } else { - assert!(false); - } - -} - -#[test] -fn test_get_set() { - #[derive(Clone)] - struct TestStruct { - x: i64, - } - - impl TestStruct { - fn get_x(&mut self) -> i64 { - self.x - } - - fn set_x(&mut self, new_x: i64) { - self.x = new_x; - } - - fn new() -> TestStruct { - TestStruct { x: 1 } - } - } - - let mut engine = Engine::new(); - - engine.register_type::(); - - 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); - } -} - -#[test] -fn test_big_get_set() { - #[derive(Clone)] - struct TestChild { - x: i64, - } - - impl TestChild { - fn get_x(&mut self) -> i64 { - self.x - } - - fn set_x(&mut self, new_x: i64) { - self.x = new_x; - } - - fn new() -> TestChild { - TestChild { x: 1 } - } - } - - #[derive(Clone)] - struct TestParent { - child: TestChild, - } - - impl TestParent { - fn get_child(&mut self) -> TestChild { - self.child.clone() - } - - fn set_child(&mut self, new_child: TestChild) { - self.child = new_child; - } - - fn new() -> TestParent { - TestParent { child: TestChild::new() } - } - } - - let mut engine = Engine::new(); - - engine.register_type::(); - engine.register_type::(); - - engine.register_get_set("x", TestChild::get_x, TestChild::set_x); - engine.register_get_set("child", TestParent::get_child, TestParent::set_child); - - engine.register_fn("new_tp", TestParent::new); - - if let Ok(result) = engine.eval::("let a = new_tp(); a.child.x = 500; a.child.x") { - assert_eq!(result, 500); - } else { - assert!(false); - } -} - -#[test] -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); - } -} - -#[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); - } -} - -#[test] -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); - } -} - -#[test] -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); - } -} - -#[test] -fn test_array_with_structs() { - #[derive(Clone)] - struct TestStruct { - x: i64, - } - - impl TestStruct { - fn update(&mut self) { - self.x += 1000; - } - - fn get_x(&mut self) -> i64 { - self.x - } - - fn set_x(&mut self, new_x: i64) { - self.x = new_x; - } - - fn new() -> TestStruct { - TestStruct { x: 1 } - } - } - - let mut engine = Engine::new(); - - engine.register_type::(); - - engine.register_get_set("x", TestStruct::get_x, TestStruct::set_x); - 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); - } - - 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); - } -} - -#[test] -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); - } -} - -#[test] -fn struct_with_float() { - #[derive(Clone)] - struct TestStruct { - x: f64, - } - - impl TestStruct { - fn update(&mut self) { - self.x += 5.789_f64; - } - - fn get_x(&mut self) -> f64 { - self.x - } - - fn set_x(&mut self, new_x: f64) { - self.x = new_x; - } - - fn new() -> TestStruct { - TestStruct { x: 1.0 } - } - } - - let mut engine = Engine::new(); - - engine.register_type::(); - - engine.register_get_set("x", TestStruct::get_x, TestStruct::set_x); - 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); - } -} - -#[test] -fn test_comments() { - let mut engine = Engine::new(); - - assert!(engine.eval::("let x = 5; x // I am a single line comment, yay!").is_ok()); - - assert!(engine.eval::("let /* I am a multiline comment, yay! */ x = 5; x").is_ok()); -} - -#[test] -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); -} - -#[test] -fn test_not() { - let mut engine = Engine::new(); - - assert_eq!(engine.eval::("let not_true = !true; not_true").unwrap(), false); - - assert_eq!(engine.eval::("fn not(x) { !x } not(false)").unwrap(), true); - - // TODO - do we allow stacking unary operators directly? e.g '!!!!!!!true' - assert_eq!(engine.eval::("!(!(!(!(true))))").unwrap(), true) -} - -#[test] -fn test_loop() { - let mut engine = Engine::new(); - - assert!( - engine.eval::(" - let x = 0; - let i = 0; - - loop { - if i < 10 { - x = x + i; - i = i + 1; - } - else { - break; - } - } - - x == 45 - ").unwrap() - ) -} - -#[test] -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); - } -} - -#[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); - } - - if let Ok(_) = engine.eval::("let s = \"test\"; s -= \"ing\"; s") { - assert!(false); - } else { - assert!(true); - } -} - -#[test] -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); - } -} - -#[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); - } -} - -#[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); - } -} - -#[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); - } -} - -#[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); - } -} - -#[test] -fn test_left_shift() { - let mut engine = Engine::new(); - - if let Ok(result) = engine.eval::("4 << 2") { - assert_eq!(result, 16); - } else { - assert!(false); - } -} - -#[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); - } -} - -#[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); - } -} - -#[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); - } -} - -#[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); - } -} - -#[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); - } -} - -#[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); - } -} - -#[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); - } -} - -#[test] -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); - } -} - -#[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() -{ - 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); - } -} diff --git a/src/tests/arrays.rs b/src/tests/arrays.rs new file mode 100644 index 00000000..4d87728b --- /dev/null +++ b/src/tests/arrays.rs @@ -0,0 +1,66 @@ +use engine::Engine; +use fn_register::FnRegister; + +#[test] +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); + } +} + +#[test] +fn test_array_with_structs() { + #[derive(Clone)] + struct TestStruct { + x: i64, + } + + impl TestStruct { + fn update(&mut self) { + self.x += 1000; + } + + fn get_x(&mut self) -> i64 { + self.x + } + + fn set_x(&mut self, new_x: i64) { + self.x = new_x; + } + + fn new() -> TestStruct { + TestStruct { x: 1 } + } + } + + let mut engine = Engine::new(); + + engine.register_type::(); + + engine.register_get_set("x", TestStruct::get_x, TestStruct::set_x); + 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); + } + + 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); + } +} \ No newline at end of file diff --git a/src/tests/binary_ops.rs b/src/tests/binary_ops.rs new file mode 100644 index 00000000..c927080d --- /dev/null +++ b/src/tests/binary_ops.rs @@ -0,0 +1,42 @@ +use engine::Engine; + +#[test] +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); + } +} diff --git a/src/tests/bit_shift.rs b/src/tests/bit_shift.rs new file mode 100644 index 00000000..09a42aa9 --- /dev/null +++ b/src/tests/bit_shift.rs @@ -0,0 +1,23 @@ +use engine::Engine; + +#[test] +fn test_left_shift() { + let mut engine = Engine::new(); + + if let Ok(result) = engine.eval::("4 << 2") { + assert_eq!(result, 16); + } else { + assert!(false); + } +} + +#[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); + } +} diff --git a/src/tests/bool_op.rs b/src/tests/bool_op.rs new file mode 100644 index 00000000..6b0f6ee8 --- /dev/null +++ b/src/tests/bool_op.rs @@ -0,0 +1,23 @@ +use engine::Engine; + +#[test] +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); + } +} + +#[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); + } +} \ No newline at end of file diff --git a/src/tests/chars.rs b/src/tests/chars.rs new file mode 100644 index 00000000..6ac9c7d7 --- /dev/null +++ b/src/tests/chars.rs @@ -0,0 +1,23 @@ +use engine::Engine; + +#[test] +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); + } + + match engine.eval::("''") { + Err(_) => (), + _ => assert!(false), + } +} \ No newline at end of file diff --git a/src/tests/comments.rs b/src/tests/comments.rs new file mode 100644 index 00000000..a2193083 --- /dev/null +++ b/src/tests/comments.rs @@ -0,0 +1,10 @@ +use engine::Engine; + +#[test] +fn test_comments() { + let mut engine = Engine::new(); + + assert!(engine.eval::("let x = 5; x // I am a single line comment, yay!").is_ok()); + + assert!(engine.eval::("let /* I am a multiline comment, yay! */ x = 5; x").is_ok()); +} diff --git a/src/tests/compound_equality.rs b/src/tests/compound_equality.rs new file mode 100644 index 00000000..5314fd6f --- /dev/null +++ b/src/tests/compound_equality.rs @@ -0,0 +1,119 @@ +use engine::Engine; + +#[test] +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); + } +} + +#[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); + } +} + +#[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); + } +} + +#[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); + } +} + +#[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); + } +} + +#[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); + } +} + +#[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); + } +} + +#[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); + } +} \ No newline at end of file diff --git a/src/tests/decrement.rs b/src/tests/decrement.rs new file mode 100644 index 00000000..d73e7e31 --- /dev/null +++ b/src/tests/decrement.rs @@ -0,0 +1,18 @@ +use engine::Engine; + +#[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); + } + + if let Ok(_) = engine.eval::("let s = \"test\"; s -= \"ing\"; s") { + assert!(false); + } else { + assert!(true); + } +} diff --git a/src/tests/float.rs b/src/tests/float.rs new file mode 100644 index 00000000..6030a5b2 --- /dev/null +++ b/src/tests/float.rs @@ -0,0 +1,71 @@ +use engine::Engine; +use fn_register::FnRegister; + +#[test] +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); + } +} + +#[test] +fn struct_with_float() { + #[derive(Clone)] + struct TestStruct { + x: f64, + } + + impl TestStruct { + fn update(&mut self) { + self.x += 5.789_f64; + } + + fn get_x(&mut self) -> f64 { + self.x + } + + fn set_x(&mut self, new_x: f64) { + self.x = new_x; + } + + fn new() -> TestStruct { + TestStruct { x: 1.0 } + } + } + + let mut engine = Engine::new(); + + engine.register_type::(); + + engine.register_get_set("x", TestStruct::get_x, TestStruct::set_x); + 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); + } +} diff --git a/src/tests/get_set.rs b/src/tests/get_set.rs new file mode 100644 index 00000000..46790e81 --- /dev/null +++ b/src/tests/get_set.rs @@ -0,0 +1,94 @@ +use engine::Engine; +use fn_register::FnRegister; + +#[test] +fn test_get_set() { + #[derive(Clone)] + struct TestStruct { + x: i64, + } + + impl TestStruct { + fn get_x(&mut self) -> i64 { + self.x + } + + fn set_x(&mut self, new_x: i64) { + self.x = new_x; + } + + fn new() -> TestStruct { + TestStruct { x: 1 } + } + } + + let mut engine = Engine::new(); + + engine.register_type::(); + + 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); + } +} + +#[test] +fn test_big_get_set() { + #[derive(Clone)] + struct TestChild { + x: i64, + } + + impl TestChild { + fn get_x(&mut self) -> i64 { + self.x + } + + fn set_x(&mut self, new_x: i64) { + self.x = new_x; + } + + fn new() -> TestChild { + TestChild { x: 1 } + } + } + + #[derive(Clone)] + struct TestParent { + child: TestChild, + } + + impl TestParent { + fn get_child(&mut self) -> TestChild { + self.child.clone() + } + + fn set_child(&mut self, new_child: TestChild) { + self.child = new_child; + } + + fn new() -> TestParent { + TestParent { child: TestChild::new() } + } + } + + let mut engine = Engine::new(); + + engine.register_type::(); + engine.register_type::(); + + engine.register_get_set("x", TestChild::get_x, TestChild::set_x); + engine.register_get_set("child", TestParent::get_child, TestParent::set_child); + + engine.register_fn("new_tp", TestParent::new); + + if let Ok(result) = engine.eval::("let a = new_tp(); a.child.x = 500; a.child.x") { + assert_eq!(result, 500); + } else { + assert!(false); + } +} diff --git a/src/tests/if_block.rs b/src/tests/if_block.rs new file mode 100644 index 00000000..0c766b34 --- /dev/null +++ b/src/tests/if_block.rs @@ -0,0 +1,24 @@ +use engine::Engine; + +#[test] +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); + } +} diff --git a/src/tests/increment.rs b/src/tests/increment.rs new file mode 100644 index 00000000..e48f9ba7 --- /dev/null +++ b/src/tests/increment.rs @@ -0,0 +1,18 @@ +use engine::Engine; + +#[test] +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); + } +} diff --git a/src/tests/internal_fn.rs b/src/tests/internal_fn.rs new file mode 100644 index 00000000..c0632399 --- /dev/null +++ b/src/tests/internal_fn.rs @@ -0,0 +1,30 @@ +use engine::Engine; + +#[test] +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); + } +} + +#[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); + } +} diff --git a/src/tests/looping.rs b/src/tests/looping.rs new file mode 100644 index 00000000..391af706 --- /dev/null +++ b/src/tests/looping.rs @@ -0,0 +1,25 @@ +use engine::Engine; + +#[test] +fn test_loop() { + let mut engine = Engine::new(); + + assert!( + engine.eval::(" + let x = 0; + let i = 0; + + loop { + if i < 10 { + x = x + i; + i = i + 1; + } + else { + break; + } + } + + x == 45 + ").unwrap() + ) +} diff --git a/src/tests/method_call.rs b/src/tests/method_call.rs new file mode 100644 index 00000000..a2c36ed9 --- /dev/null +++ b/src/tests/method_call.rs @@ -0,0 +1,34 @@ +use engine::Engine; +use fn_register::FnRegister; + +#[test] +fn test_method_call() { + #[derive(Clone)] + struct TestStruct { + x: i64, + } + + impl TestStruct { + fn update(&mut self) { + self.x += 1000; + } + + fn new() -> TestStruct { + TestStruct { x: 1 } + } + } + + let mut engine = Engine::new(); + + engine.register_type::(); + + engine.register_fn("update", TestStruct::update); + engine.register_fn("new_ts", TestStruct::new); + + if let Ok(result) = engine.eval::("let x = new_ts(); x.update(); x") { + assert_eq!(result.x, 1001); + } else { + assert!(false); + } + +} diff --git a/src/tests/mismatched_op.rs b/src/tests/mismatched_op.rs new file mode 100644 index 00000000..381e2b39 --- /dev/null +++ b/src/tests/mismatched_op.rs @@ -0,0 +1,11 @@ +use engine::{Engine, EvalAltResult}; + +#[test] +fn test_mismatched_op() { + let mut engine = Engine::new(); + + match engine.eval::("60 + \"hello\"") { + Err(EvalAltResult::ErrorFunctionArgMismatch) => (), + _ => assert!(false), + } +} diff --git a/src/tests/mod.rs b/src/tests/mod.rs new file mode 100644 index 00000000..c48ea44b --- /dev/null +++ b/src/tests/mod.rs @@ -0,0 +1,24 @@ +mod arrays; +mod binary_ops; +mod bit_shift; +mod bool_op; +mod chars; +mod comments; +mod compound_equality; +mod decrement; +mod float; +mod get_set; +mod if_block; +mod increment; +mod internal_fn; +mod looping; +mod method_call; +mod mismatched_op; +mod not; +mod number_literals; +mod ops; +mod string; +mod unary_after_binary; +mod unary_minus; +mod var_scope; +mod while_loop; \ No newline at end of file diff --git a/src/tests/not.rs b/src/tests/not.rs new file mode 100644 index 00000000..a58f274d --- /dev/null +++ b/src/tests/not.rs @@ -0,0 +1,13 @@ +use engine::Engine; + +#[test] +fn test_not() { + let mut engine = Engine::new(); + + assert_eq!(engine.eval::("let not_true = !true; not_true").unwrap(), false); + + assert_eq!(engine.eval::("fn not(x) { !x } not(false)").unwrap(), true); + + // TODO - do we allow stacking unary operators directly? e.g '!!!!!!!true' + assert_eq!(engine.eval::("!(!(!(!(true))))").unwrap(), true) +} diff --git a/src/tests/number_literals.rs b/src/tests/number_literals.rs new file mode 100644 index 00000000..d71520c4 --- /dev/null +++ b/src/tests/number_literals.rs @@ -0,0 +1,63 @@ +use engine::Engine; + +#[test] +fn test_number_literal() { + let mut engine = Engine::new(); + + if let Ok(result) = engine.eval::("65") { + assert_eq!(result, 65); + } else { + assert!(false); + } +} + +#[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); + } +} + +#[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); + } +} + +#[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); + } +} \ No newline at end of file diff --git a/src/tests/ops.rs b/src/tests/ops.rs new file mode 100644 index 00000000..f18e1036 --- /dev/null +++ b/src/tests/ops.rs @@ -0,0 +1,29 @@ +use engine::Engine; + +#[test] +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); + } +} + +#[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); + } +} \ No newline at end of file diff --git a/src/tests/string.rs b/src/tests/string.rs new file mode 100644 index 00000000..d5d45959 --- /dev/null +++ b/src/tests/string.rs @@ -0,0 +1,18 @@ +use engine::Engine; + +#[test] +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); + } +} diff --git a/src/tests/unary_after_binary.rs b/src/tests/unary_after_binary.rs new file mode 100644 index 00000000..8fc6b855 --- /dev/null +++ b/src/tests/unary_after_binary.rs @@ -0,0 +1,45 @@ +use engine::Engine; + +#[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() +{ + 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); + } +} diff --git a/src/tests/unary_minus.rs b/src/tests/unary_minus.rs new file mode 100644 index 00000000..20b05a16 --- /dev/null +++ b/src/tests/unary_minus.rs @@ -0,0 +1,12 @@ +use engine::Engine; + +#[test] +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); +} diff --git a/src/tests/var_scope.rs b/src/tests/var_scope.rs new file mode 100644 index 00000000..89c6f96d --- /dev/null +++ b/src/tests/var_scope.rs @@ -0,0 +1,40 @@ +use engine::{Engine, Scope}; + +#[test] +fn test_var_scope() { + let mut engine = Engine::new(); + let mut scope: Scope = Vec::new(); + + if let Ok(_) = engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5") { + } else { + assert!(false); + } + + if let Ok(result) = engine.eval_with_scope::(&mut scope, "x") { + assert_eq!(result, 9); + } else { + assert!(false); + } + + if let Ok(_) = engine.eval_with_scope::<()>(&mut scope, "x = x + 1; x = x + 2;") { + } else { + assert!(false); + } + + if let Ok(result) = engine.eval_with_scope::(&mut scope, "x") { + assert_eq!(result, 12); + } else { + assert!(false); + } + + if let Ok(_) = engine.eval_with_scope::<()>(&mut scope, "{let x = 3}") { + } else { + assert!(false); + } + + if let Ok(result) = engine.eval_with_scope::(&mut scope, "x") { + assert_eq!(result, 12); + } else { + assert!(false); + } +} diff --git a/src/tests/while_loop.rs b/src/tests/while_loop.rs new file mode 100644 index 00000000..d81b1a64 --- /dev/null +++ b/src/tests/while_loop.rs @@ -0,0 +1,13 @@ +use engine::Engine; + +#[test] +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); + } +}