2020-03-10 12:48:47 +01:00
|
|
|
#![cfg(not(feature = "no_index"))]
|
2020-04-01 16:56:54 +02:00
|
|
|
use rhai::{Array, Engine, EvalAltResult, RegisterFn, INT};
|
2017-11-03 17:58:51 +01:00
|
|
|
|
|
|
|
#[test]
|
2020-03-02 15:11:56 +01:00
|
|
|
fn test_arrays() -> Result<(), EvalAltResult> {
|
2017-11-03 17:58:51 +01:00
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
2020-03-10 16:06:20 +01: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);
|
2020-03-14 12:46:44 +01:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<char>(r#"let y = [1, [ 42, 88, "93" ], 3]; y[1][2][1]"#)?,
|
|
|
|
'3'
|
|
|
|
);
|
2020-03-02 15:11:56 +01:00
|
|
|
|
2020-04-01 16:56:54 +02:00
|
|
|
#[cfg(not(feature = "no_stdlib"))]
|
|
|
|
{
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r"
|
|
|
|
let x = [1, 2, 3];
|
|
|
|
let y = [4, 5];
|
|
|
|
x.append(y);
|
|
|
|
x.len()
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
5
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r"
|
|
|
|
let x = [1, 2, 3];
|
|
|
|
x += [4, 5];
|
|
|
|
x.len()
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
5
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine
|
|
|
|
.eval::<Array>(
|
|
|
|
r"
|
|
|
|
let x = [1, 2, 3];
|
|
|
|
let y = [4, 5];
|
|
|
|
x + y
|
|
|
|
"
|
|
|
|
)?
|
|
|
|
.len(),
|
|
|
|
5
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-02 15:11:56 +01:00
|
|
|
Ok(())
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-03-29 11:15:12 +02:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2020-03-02 15:11:56 +01:00
|
|
|
fn test_array_with_structs() -> Result<(), EvalAltResult> {
|
2017-11-03 17:58:51 +01:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct TestStruct {
|
2020-03-10 16:06:20 +01:00
|
|
|
x: INT,
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestStruct {
|
|
|
|
fn update(&mut self) {
|
|
|
|
self.x += 1000;
|
|
|
|
}
|
|
|
|
|
2020-03-10 16:06:20 +01:00
|
|
|
fn get_x(&mut self) -> INT {
|
2017-11-03 17:58:51 +01:00
|
|
|
self.x
|
|
|
|
}
|
|
|
|
|
2020-03-10 16:06:20 +01:00
|
|
|
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 {
|
2017-11-03 17:58:51 +01:00
|
|
|
TestStruct { x: 1 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2020-03-10 16:06:20 +01:00
|
|
|
assert_eq!(engine.eval::<INT>("let a = [new_ts()]; a[0].x")?, 1);
|
2019-10-09 13:06:32 +02:00
|
|
|
|
|
|
|
assert_eq!(
|
2020-03-10 16:06:20 +01:00
|
|
|
engine.eval::<INT>(
|
2020-03-14 12:46:44 +01:00
|
|
|
r"
|
|
|
|
let a = [new_ts()];
|
|
|
|
a[0].x = 100;
|
|
|
|
a[0].update();
|
|
|
|
a[0].x
|
|
|
|
"
|
2020-03-02 15:11:56 +01:00
|
|
|
)?,
|
|
|
|
1100
|
2019-10-09 13:06:32 +02:00
|
|
|
);
|
2020-03-02 15:11:56 +01:00
|
|
|
|
|
|
|
Ok(())
|
2018-06-13 10:56:29 +02:00
|
|
|
}
|