rhai/tests/arrays.rs

272 lines
6.3 KiB
Rust
Raw Normal View History

#![cfg(not(feature = "no_index"))]
use rhai::{Array, Engine, EvalAltResult, INT};
2017-11-03 17:58:51 +01:00
2021-04-19 11:02:17 +02:00
fn convert_to_vec<T: Clone + 'static>(array: Array) -> Vec<T> {
array.into_iter().map(|v| v.clone_cast::<T>()).collect()
}
2017-11-03 17:58:51 +01:00
#[test]
fn test_arrays() -> Result<(), Box<EvalAltResult>> {
2021-04-19 11:02:17 +02:00
let mut a = Array::new();
a.push((42 as INT).into());
assert_eq!(a[0].clone_cast::<INT>(), 42);
let engine = Engine::new();
2017-11-03 17:58:51 +01:00
assert_eq!(engine.eval::<INT>("let x = [1, 2, 3]; x[1]")?, 2);
2020-06-16 16:14:46 +02:00
assert_eq!(engine.eval::<INT>("let x = [1, 2, 3,]; x[1]")?, 2);
assert_eq!(engine.eval::<INT>("let y = [1, 2, 3]; y[1] = 5; y[1]")?, 5);
assert_eq!(
engine.eval::<char>(r#"let y = [1, [ 42, 88, "93" ], 3]; y[1][2][1]"#)?,
'3'
);
assert_eq!(engine.eval::<INT>("let y = [1, 2, 3]; y[0]")?, 1);
assert_eq!(engine.eval::<INT>("let y = [1, 2, 3]; y[-1]")?, 3);
2021-04-19 11:02:17 +02:00
assert_eq!(engine.eval::<INT>("let y = [1, 2, 3]; y[-3]")?, 1);
2020-04-06 11:47:34 +02:00
assert!(engine.eval::<bool>("let y = [1, 2, 3]; 2 in y")?);
2020-09-21 10:15:52 +02:00
assert_eq!(engine.eval::<INT>("let y = [1, 2, 3]; y += 4; y[3]")?, 4);
2021-04-19 11:02:17 +02:00
assert_eq!(
convert_to_vec::<INT>(engine.eval("let y = [1, 2, 3]; y[1] += 4; y")?),
[1, 6, 3]
);
2020-09-21 10:15:52 +02:00
#[cfg(not(feature = "no_object"))]
2021-04-19 11:02:17 +02:00
{
assert_eq!(
convert_to_vec::<INT>(engine.eval("let y = [1, 2, 3]; y.push(4); y")?),
[1, 2, 3, 4]
);
assert_eq!(
convert_to_vec::<INT>(engine.eval("let y = [1, 2, 3]; y.insert(0, 4); y")?),
[4, 1, 2, 3]
);
assert_eq!(
convert_to_vec::<INT>(engine.eval("let y = [1, 2, 3]; y.insert(999, 4); y")?),
[1, 2, 3, 4]
);
assert_eq!(
convert_to_vec::<INT>(engine.eval("let y = [1, 2, 3]; y.insert(-2, 4); y")?),
[1, 4, 2, 3]
);
assert_eq!(
convert_to_vec::<INT>(engine.eval("let y = [1, 2, 3]; y.insert(-999, 4); y")?),
[4, 1, 2, 3]
);
assert_eq!(
convert_to_vec::<INT>(engine.eval(
2021-04-20 06:01:35 +02:00
"
2021-04-19 11:02:17 +02:00
let x = [2, 9];
x.insert(-1, 1);
x.insert(999, 3);
x.insert(-9, 99);
2020-03-02 15:11:56 +01:00
2021-04-19 11:02:17 +02:00
let r = x.remove(2);
2020-04-10 15:59:29 +02:00
2021-04-19 11:02:17 +02:00
let y = [4, 5];
x.append(y);
2020-04-10 15:59:29 +02:00
2021-04-19 11:02:17 +02:00
x
"
)?),
[99, 2, 9, 3, 4, 5]
);
}
2020-04-10 15:59:29 +02:00
assert_eq!(
2021-04-19 11:02:17 +02:00
convert_to_vec::<INT>(engine.eval(
2021-04-20 06:01:35 +02:00
"
2020-04-10 15:59:29 +02:00
let x = [1, 2, 3];
x += [4, 5];
2021-04-19 11:02:17 +02:00
x
2020-12-27 09:50:48 +01:00
"
2021-04-19 11:02:17 +02:00
)?),
[1, 2, 3, 4, 5]
);
assert_eq!(
2021-04-19 11:02:17 +02:00
convert_to_vec::<INT>(engine.eval(
2021-04-20 06:01:35 +02:00
"
2021-04-19 11:02:17 +02:00
let x = [1, 2, 3];
let y = [4, 5];
x + y
"
)?),
[1, 2, 3, 4, 5]
);
2020-03-02 15:11:56 +01:00
Ok(())
2017-11-03 17:58:51 +01:00
}
#[test]
#[cfg(not(feature = "no_object"))]
fn test_array_with_structs() -> Result<(), Box<EvalAltResult>> {
2017-11-03 17:58:51 +01:00
#[derive(Clone)]
struct TestStruct {
x: INT,
2017-11-03 17:58:51 +01:00
}
impl TestStruct {
fn update(&mut self) {
self.x += 1000;
}
fn get_x(&mut self) -> INT {
2017-11-03 17:58:51 +01:00
self.x
}
fn set_x(&mut self, new_x: INT) {
2017-11-03 17:58:51 +01:00
self.x = new_x;
}
2020-03-19 06:52:10 +01:00
fn new() -> Self {
2020-10-19 08:26:15 +02:00
Self { x: 1 }
2017-11-03 17:58:51 +01:00
}
}
let mut engine = Engine::new();
engine.register_type::<TestStruct>();
engine.register_get_set("x", TestStruct::get_x, TestStruct::set_x);
engine.register_fn("update", TestStruct::update);
engine.register_fn("new_ts", TestStruct::new);
assert_eq!(engine.eval::<INT>("let a = [new_ts()]; a[0].x")?, 1);
assert_eq!(
engine.eval::<INT>(
2021-04-20 06:01:35 +02:00
"
let a = [new_ts()];
a[0].x = 100;
a[0].update();
a[0].x
"
2020-03-02 15:11:56 +01:00
)?,
1100
);
2020-03-02 15:11:56 +01:00
Ok(())
}
2020-10-12 16:49:51 +02:00
#[cfg(not(feature = "no_object"))]
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "no_closure"))]
#[test]
fn test_arrays_map_reduce() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert_eq!(
2021-04-19 12:08:29 +02:00
convert_to_vec::<INT>(engine.eval(
2021-04-20 06:01:35 +02:00
"
2020-10-12 16:49:51 +02:00
let x = [1, 2, 3];
2021-04-19 12:08:29 +02:00
x.filter(|v| v > 2)
2020-10-12 16:49:51 +02:00
"
2021-04-19 12:08:29 +02:00
)?),
[3]
2020-10-12 16:49:51 +02:00
);
assert_eq!(
2021-04-19 12:08:29 +02:00
convert_to_vec::<INT>(engine.eval(
2021-04-20 06:01:35 +02:00
"
2020-10-12 16:49:51 +02:00
let x = [1, 2, 3];
2021-04-19 12:08:29 +02:00
x.filter(|v, i| v > i)
2020-10-12 16:49:51 +02:00
"
2021-04-19 12:08:29 +02:00
)?),
[1, 2, 3]
2020-10-12 16:49:51 +02:00
);
assert_eq!(
2021-04-19 12:08:29 +02:00
convert_to_vec::<INT>(engine.eval(
2021-04-20 06:01:35 +02:00
"
2020-10-12 16:49:51 +02:00
let x = [1, 2, 3];
2021-04-19 12:08:29 +02:00
x.map(|v| v * 2)
2020-10-12 16:49:51 +02:00
"
2021-04-19 12:08:29 +02:00
)?),
[2, 4, 6]
2020-10-12 16:49:51 +02:00
);
assert_eq!(
2021-04-19 12:08:29 +02:00
convert_to_vec::<INT>(engine.eval(
2021-04-20 06:01:35 +02:00
"
2020-10-12 16:49:51 +02:00
let x = [1, 2, 3];
2021-04-19 12:08:29 +02:00
x.map(|v, i| v * i)
2020-10-12 16:49:51 +02:00
"
2021-04-19 12:08:29 +02:00
)?),
[0, 2, 6]
2020-10-12 16:49:51 +02:00
);
assert_eq!(
engine.eval::<INT>(
r#"
let x = [1, 2, 3];
2020-10-13 04:57:29 +02:00
x.reduce(|sum, v| if sum.type_of() == "()" { v * v } else { sum + v * v })
2020-10-12 16:49:51 +02:00
"#
)?,
14
);
assert_eq!(
engine.eval::<INT>(
2021-06-05 09:26:43 +02:00
"
2020-10-12 16:49:51 +02:00
let x = [1, 2, 3];
2020-12-08 15:47:38 +01:00
x.reduce(|sum, v, i| {
if i == 0 { sum = 10 }
sum + v * v
})
2021-06-05 09:26:43 +02:00
"
2020-10-12 16:49:51 +02:00
)?,
24
);
2020-10-13 04:57:29 +02:00
assert_eq!(
engine.eval::<INT>(
r#"
let x = [1, 2, 3];
x.reduce_rev(|sum, v| if sum.type_of() == "()" { v * v } else { sum + v * v })
"#
)?,
14
);
assert_eq!(
engine.eval::<INT>(
2021-06-05 09:26:43 +02:00
"
2020-10-13 04:57:29 +02:00
let x = [1, 2, 3];
x.reduce_rev(|sum, v, i| { if i == 2 { sum = 10 } sum + v * v })
2021-06-05 09:26:43 +02:00
"
2020-10-13 04:57:29 +02:00
)?,
24
);
assert!(engine.eval::<bool>(
2021-06-05 09:26:43 +02:00
"
2020-10-13 04:57:29 +02:00
let x = [1, 2, 3];
x.some(|v| v > 1)
2021-06-05 09:26:43 +02:00
"
2020-10-13 04:57:29 +02:00
)?);
assert!(engine.eval::<bool>(
2021-06-05 09:26:43 +02:00
"
2020-10-13 04:57:29 +02:00
let x = [1, 2, 3];
x.some(|v, i| v * i == 0)
2021-06-05 09:26:43 +02:00
"
2020-10-13 04:57:29 +02:00
)?);
assert!(!engine.eval::<bool>(
2021-06-05 09:26:43 +02:00
"
2020-10-13 04:57:29 +02:00
let x = [1, 2, 3];
x.all(|v| v > 1)
2021-06-05 09:26:43 +02:00
"
2020-10-13 04:57:29 +02:00
)?);
assert!(engine.eval::<bool>(
2021-06-05 09:26:43 +02:00
"
2020-10-13 04:57:29 +02:00
let x = [1, 2, 3];
x.all(|v, i| v > i)
2021-06-05 09:26:43 +02:00
"
2020-10-13 04:57:29 +02:00
)?);
2020-10-12 16:49:51 +02:00
Ok(())
}