More array tests.

This commit is contained in:
Stephen Chung 2021-04-19 17:02:17 +08:00
parent e88fb071f7
commit ea866cead5

View File

@ -1,8 +1,17 @@
#![cfg(not(feature = "no_index"))] #![cfg(not(feature = "no_index"))]
use rhai::{Array, Engine, EvalAltResult, INT}; use rhai::{Array, Engine, EvalAltResult, INT};
fn convert_to_vec<T: Clone + 'static>(array: Array) -> Vec<T> {
array.into_iter().map(|v| v.clone_cast::<T>()).collect()
}
#[test] #[test]
fn test_arrays() -> Result<(), Box<EvalAltResult>> { fn test_arrays() -> Result<(), Box<EvalAltResult>> {
let mut a = Array::new();
a.push((42 as INT).into());
assert_eq!(a[0].clone_cast::<INT>(), 42);
let engine = Engine::new(); let engine = Engine::new();
assert_eq!(engine.eval::<INT>("let x = [1, 2, 3]; x[1]")?, 2); assert_eq!(engine.eval::<INT>("let x = [1, 2, 3]; x[1]")?, 2);
@ -14,58 +23,78 @@ fn test_arrays() -> Result<(), Box<EvalAltResult>> {
); );
assert_eq!(engine.eval::<INT>("let y = [1, 2, 3]; y[0]")?, 1); 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); assert_eq!(engine.eval::<INT>("let y = [1, 2, 3]; y[-1]")?, 3);
assert_eq!(engine.eval::<INT>("let y = [1, 2, 3]; y[-3]")?, 1);
assert!(engine.eval::<bool>("let y = [1, 2, 3]; 2 in y")?); assert!(engine.eval::<bool>("let y = [1, 2, 3]; 2 in y")?);
assert_eq!(engine.eval::<INT>("let y = [1, 2, 3]; y += 4; y[3]")?, 4); assert_eq!(engine.eval::<INT>("let y = [1, 2, 3]; y += 4; y[3]")?, 4);
#[cfg(not(feature = "no_object"))]
assert_eq!(engine.eval::<INT>("let y = [1, 2, 3]; y.push(4); y[3]")?, 4);
#[cfg(not(feature = "no_object"))]
assert_eq!( assert_eq!(
engine.eval::<INT>( convert_to_vec::<INT>(engine.eval("let y = [1, 2, 3]; y[1] += 4; y")?),
r" [1, 6, 3]
let x = [2, 9];
x.insert(-1, 1);
x.insert(999, 3);
let r = x.remove(2);
let y = [4, 5];
x.append(y);
x.len + r
"
)?,
14
); );
#[cfg(not(feature = "no_object"))]
{
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(
r"
let x = [2, 9];
x.insert(-1, 1);
x.insert(999, 3);
x.insert(-9, 99);
let r = x.remove(2);
let y = [4, 5];
x.append(y);
x
"
)?),
[99, 2, 9, 3, 4, 5]
);
}
assert_eq!( assert_eq!(
engine.eval::<INT>( convert_to_vec::<INT>(engine.eval(
r" r"
let x = [1, 2, 3]; let x = [1, 2, 3];
x += [4, 5]; x += [4, 5];
len(x) x
" "
)?, )?),
5 [1, 2, 3, 4, 5]
); );
assert_eq!( assert_eq!(
engine convert_to_vec::<INT>(engine.eval(
.eval::<Array>( r"
r" let x = [1, 2, 3];
let x = [1, 2, 3]; let y = [4, 5];
let y = [4, 5]; x + y
x + y "
" )?),
)? [1, 2, 3, 4, 5]
.len(),
5
); );
let mut a = Array::new();
a.push((42 as INT).into());
assert_eq!(a[0].clone_cast::<INT>(), 42);
Ok(()) Ok(())
} }