2020-03-10 12:48:47 +01:00
|
|
|
#![cfg(not(feature = "no_index"))]
|
2022-05-17 09:20:32 +02:00
|
|
|
use rhai::{Array, Dynamic, Engine, EvalAltResult, ParseErrorType, INT};
|
2022-12-30 18:07:39 +01:00
|
|
|
use std::iter::FromIterator;
|
2021-04-19 11:02:17 +02:00
|
|
|
|
2017-11-03 17:58:51 +01:00
|
|
|
#[test]
|
2020-04-21 17:25:12 +02:00
|
|
|
fn test_arrays() -> Result<(), Box<EvalAltResult>> {
|
2022-12-30 18:07:39 +01:00
|
|
|
let a = Array::from_iter([(42 as INT).into()]);
|
2021-04-19 11:02:17 +02:00
|
|
|
|
|
|
|
assert_eq!(a[0].clone_cast::<INT>(), 42);
|
|
|
|
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2017-11-03 17:58:51 +01:00
|
|
|
|
2020-03-10 16:06:20 +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);
|
2020-03-10 16:06:20 +01:00
|
|
|
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'
|
|
|
|
);
|
2021-04-10 09:00:03 +02:00
|
|
|
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")?);
|
2022-11-30 07:11:09 +01:00
|
|
|
assert!(engine.eval::<bool>("let y = [1, 2, 3]; 42 !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);
|
2022-01-13 15:05:07 +01:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>("let y = [1, 2, 3]; pad(y, 5, 42); len(y)")?,
|
|
|
|
5
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>("let y = [1, 2, 3]; pad(y, 5, [42]); len(y)")?,
|
|
|
|
5
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>("let y = [1, 2, 3]; pad(y, 5, [42, 999, 123]); y[4][0]")?,
|
|
|
|
42
|
|
|
|
);
|
2021-04-19 11:02:17 +02:00
|
|
|
assert_eq!(
|
2021-11-23 05:45:59 +01:00
|
|
|
engine
|
|
|
|
.eval::<Dynamic>("let y = [1, 2, 3]; y[1] += 4; y")?
|
|
|
|
.into_typed_array::<INT>()?,
|
2021-04-19 11:02:17 +02:00
|
|
|
[1, 6, 3]
|
|
|
|
);
|
2022-01-13 11:13:27 +01:00
|
|
|
assert_eq!(
|
|
|
|
engine
|
|
|
|
.eval::<Dynamic>("let y = [1, 2, 3]; extract(y, 1, 10)")?
|
|
|
|
.into_typed_array::<INT>()?,
|
|
|
|
vec![2, 3]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine
|
|
|
|
.eval::<Dynamic>("let y = [1, 2, 3]; extract(y, -3, 1)")?
|
|
|
|
.into_typed_array::<INT>()?,
|
|
|
|
vec![1]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine
|
|
|
|
.eval::<Dynamic>("let y = [1, 2, 3]; extract(y, -99, 2)")?
|
|
|
|
.into_typed_array::<INT>()?,
|
|
|
|
vec![1, 2]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine
|
|
|
|
.eval::<Dynamic>("let y = [1, 2, 3]; extract(y, 99, 1)")?
|
|
|
|
.into_typed_array::<INT>()?,
|
|
|
|
vec![] as Vec<INT>
|
|
|
|
);
|
2020-09-21 10:15:52 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2021-04-19 11:02:17 +02:00
|
|
|
{
|
|
|
|
assert_eq!(
|
2021-11-23 05:45:59 +01:00
|
|
|
engine
|
|
|
|
.eval::<Dynamic>("let y = [1, 2, 3]; y.push(4); y")?
|
|
|
|
.into_typed_array::<INT>()?,
|
2021-04-19 11:02:17 +02:00
|
|
|
[1, 2, 3, 4]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-11-23 05:45:59 +01:00
|
|
|
engine
|
|
|
|
.eval::<Dynamic>("let y = [1, 2, 3]; y.insert(0, 4); y")?
|
|
|
|
.into_typed_array::<INT>()?,
|
2021-04-19 11:02:17 +02:00
|
|
|
[4, 1, 2, 3]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-11-23 05:45:59 +01:00
|
|
|
engine
|
|
|
|
.eval::<Dynamic>("let y = [1, 2, 3]; y.insert(999, 4); y")?
|
|
|
|
.into_typed_array::<INT>()?,
|
2021-04-19 11:02:17 +02:00
|
|
|
[1, 2, 3, 4]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-11-23 05:45:59 +01:00
|
|
|
engine
|
|
|
|
.eval::<Dynamic>("let y = [1, 2, 3]; y.insert(-2, 4); y")?
|
|
|
|
.into_typed_array::<INT>()?,
|
2021-04-19 11:02:17 +02:00
|
|
|
[1, 4, 2, 3]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-11-23 05:45:59 +01:00
|
|
|
engine
|
|
|
|
.eval::<Dynamic>("let y = [1, 2, 3]; y.insert(-999, 4); y")?
|
|
|
|
.into_typed_array::<INT>()?,
|
2021-04-19 11:02:17 +02:00
|
|
|
[4, 1, 2, 3]
|
|
|
|
);
|
2021-07-24 06:27:33 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>("let y = [1, 2, 3]; let z = [42]; y[z.len]")?,
|
|
|
|
2
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>("let y = [1, 2, [3, 4, 5, 6]]; let z = [42]; y[2][z.len]")?,
|
|
|
|
4
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>("let y = [1, 2, 3]; let z = [2]; y[z[0]]")?,
|
|
|
|
3
|
|
|
|
);
|
2021-04-19 11:02:17 +02:00
|
|
|
|
|
|
|
assert_eq!(
|
2021-11-23 05:45:59 +01:00
|
|
|
engine
|
|
|
|
.eval::<Dynamic>(
|
|
|
|
"
|
|
|
|
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);
|
|
|
|
|
2022-12-30 18:07:39 +01:00
|
|
|
x
|
2021-11-23 05:45:59 +01:00
|
|
|
"
|
|
|
|
)?
|
|
|
|
.into_typed_array::<INT>()?,
|
2021-04-19 11:02:17 +02:00
|
|
|
[99, 2, 9, 3, 4, 5]
|
|
|
|
);
|
|
|
|
}
|
2020-04-10 15:59:29 +02:00
|
|
|
|
2023-01-25 00:37:44 +01:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let x = #{ foo: 42 };
|
|
|
|
let n = 0;
|
|
|
|
let a = [[x]];
|
|
|
|
let i = [n];
|
|
|
|
a[n][i[n]].foo
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
2020-04-08 04:19:03 +02:00
|
|
|
assert_eq!(
|
2021-11-23 05:45:59 +01:00
|
|
|
engine
|
|
|
|
.eval::<Dynamic>(
|
|
|
|
"
|
|
|
|
let x = [1, 2, 3];
|
|
|
|
x += [4, 5];
|
|
|
|
x
|
|
|
|
"
|
|
|
|
)?
|
|
|
|
.into_typed_array::<INT>()?,
|
2021-04-19 11:02:17 +02:00
|
|
|
[1, 2, 3, 4, 5]
|
2020-04-08 04:19:03 +02:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-11-23 05:45:59 +01:00
|
|
|
engine
|
|
|
|
.eval::<Dynamic>(
|
|
|
|
"
|
|
|
|
let x = [1, 2, 3];
|
|
|
|
let y = [4, 5];
|
|
|
|
x + y
|
|
|
|
"
|
|
|
|
)?
|
|
|
|
.into_typed_array::<INT>()?,
|
2021-04-19 11:02:17 +02:00
|
|
|
[1, 2, 3, 4, 5]
|
2020-04-08 04:19:03 +02:00
|
|
|
);
|
2022-01-09 10:26:46 +01:00
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
assert!(!engine.eval::<bool>(
|
|
|
|
"
|
|
|
|
let x = 42;
|
|
|
|
let y = [];
|
|
|
|
let f = || x;
|
|
|
|
for n in 0..10 {
|
|
|
|
y += x;
|
|
|
|
}
|
|
|
|
some(y, |x| is_shared(x))
|
|
|
|
"
|
|
|
|
)?);
|
2020-04-01 16:56:54 +02:00
|
|
|
|
2022-09-02 17:45:25 +02:00
|
|
|
let value = vec![
|
|
|
|
String::from("hello"),
|
|
|
|
String::from("world"),
|
|
|
|
String::from("foo"),
|
|
|
|
String::from("bar"),
|
|
|
|
];
|
|
|
|
|
|
|
|
let array: Dynamic = value.into();
|
|
|
|
|
|
|
|
assert_eq!(array.type_name(), "array");
|
|
|
|
|
|
|
|
let array = array.cast::<Array>();
|
|
|
|
|
|
|
|
assert_eq!(array[0].type_name(), "string");
|
|
|
|
assert_eq!(array.len(), 4);
|
|
|
|
|
2020-03-02 15:11:56 +01:00
|
|
|
Ok(())
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|
|
|
|
|
2022-05-17 09:20:32 +02:00
|
|
|
#[test]
|
|
|
|
fn test_array_index_types() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let engine = Engine::new();
|
|
|
|
|
|
|
|
engine.compile("[1, 2, 3][0]['x']")?;
|
|
|
|
|
|
|
|
assert!(matches!(
|
2022-07-29 03:42:30 +02:00
|
|
|
engine
|
2022-05-17 09:20:32 +02:00
|
|
|
.compile("[1, 2, 3]['x']")
|
|
|
|
.expect_err("should error")
|
2022-07-29 03:42:30 +02:00
|
|
|
.err_type(),
|
2022-05-17 09:20:32 +02:00
|
|
|
ParseErrorType::MalformedIndexExpr(..)
|
|
|
|
));
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
assert!(matches!(
|
2022-07-29 03:42:30 +02:00
|
|
|
engine
|
2022-05-17 09:20:32 +02:00
|
|
|
.compile("[1, 2, 3][123.456]")
|
|
|
|
.expect_err("should error")
|
2022-07-29 03:42:30 +02:00
|
|
|
.err_type(),
|
2022-05-17 09:20:32 +02:00
|
|
|
ParseErrorType::MalformedIndexExpr(..)
|
|
|
|
));
|
|
|
|
|
|
|
|
assert!(matches!(
|
2022-07-29 03:42:30 +02:00
|
|
|
engine
|
|
|
|
.compile("[1, 2, 3][()]")
|
|
|
|
.expect_err("should error")
|
|
|
|
.err_type(),
|
2022-05-17 09:20:32 +02:00
|
|
|
ParseErrorType::MalformedIndexExpr(..)
|
|
|
|
));
|
|
|
|
|
|
|
|
assert!(matches!(
|
2022-07-29 03:42:30 +02:00
|
|
|
engine
|
2022-05-17 09:20:32 +02:00
|
|
|
.compile(r#"[1, 2, 3]["hello"]"#)
|
|
|
|
.expect_err("should error")
|
2022-07-29 03:42:30 +02:00
|
|
|
.err_type(),
|
2022-05-17 09:20:32 +02:00
|
|
|
ParseErrorType::MalformedIndexExpr(..)
|
|
|
|
));
|
|
|
|
|
|
|
|
assert!(matches!(
|
2022-07-29 03:42:30 +02:00
|
|
|
engine
|
2022-05-17 09:20:32 +02:00
|
|
|
.compile("[1, 2, 3][true && false]")
|
|
|
|
.expect_err("should error")
|
2022-07-29 03:42:30 +02:00
|
|
|
.err_type(),
|
2022-05-17 09:20:32 +02:00
|
|
|
ParseErrorType::MalformedIndexExpr(..)
|
|
|
|
));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-11-03 17:58:51 +01:00
|
|
|
#[test]
|
2020-03-29 11:15:12 +02:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2020-04-21 17:25:12 +02:00
|
|
|
fn test_array_with_structs() -> Result<(), Box<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 {
|
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);
|
|
|
|
|
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>(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2020-03-14 12:46:44 +01:00
|
|
|
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
|
|
|
}
|
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();
|
|
|
|
|
2021-08-17 09:32:48 +02:00
|
|
|
assert_eq!(engine.eval::<INT>("[1].map(|x| x + 41)[0]")?, 42);
|
2022-12-24 12:37:06 +01:00
|
|
|
assert_eq!(engine.eval::<INT>("[1].map(|| this + 41)[0]")?, 42);
|
2022-12-25 05:44:49 +01:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>("let x = [1, 2, 3]; x.for_each(|| this += 41); x[0]")?,
|
|
|
|
42
|
|
|
|
);
|
2022-12-26 15:05:29 +01:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let x = [1, 2, 3];
|
|
|
|
let sum = 0;
|
|
|
|
let factor = 2;
|
|
|
|
x.for_each(|| sum += this * factor);
|
|
|
|
sum
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
12
|
|
|
|
);
|
2021-08-17 09:32:48 +02:00
|
|
|
assert_eq!(engine.eval::<INT>("([1].map(|x| x + 41))[0]")?, 42);
|
2021-11-23 15:56:31 +01:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>("let c = 40; let y = 1; [1].map(|x, i| c + x + y + i)[0]")?,
|
|
|
|
42
|
|
|
|
);
|
2022-12-25 05:44:49 +01:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>("let x = [1, 2, 3]; x.for_each(|i| this += i); x[2]")?,
|
|
|
|
5
|
|
|
|
);
|
2021-08-17 09:32:48 +02:00
|
|
|
|
2020-10-12 16:49:51 +02:00
|
|
|
assert_eq!(
|
2021-11-23 05:45:59 +01:00
|
|
|
engine
|
|
|
|
.eval::<Dynamic>(
|
|
|
|
"
|
|
|
|
let x = [1, 2, 3];
|
|
|
|
x.filter(|v| v > 2)
|
|
|
|
"
|
|
|
|
)?
|
|
|
|
.into_typed_array::<INT>()?,
|
2021-04-19 12:08:29 +02:00
|
|
|
[3]
|
2020-10-12 16:49:51 +02:00
|
|
|
);
|
|
|
|
|
2022-12-24 12:37:06 +01:00
|
|
|
assert_eq!(
|
|
|
|
engine
|
|
|
|
.eval::<Dynamic>(
|
|
|
|
"
|
|
|
|
let x = [1, 2, 3];
|
|
|
|
x.filter(|| this > 2)
|
|
|
|
"
|
|
|
|
)?
|
|
|
|
.into_typed_array::<INT>()?,
|
|
|
|
[3]
|
|
|
|
);
|
|
|
|
|
2020-10-12 16:49:51 +02:00
|
|
|
assert_eq!(
|
2021-11-23 05:45:59 +01:00
|
|
|
engine
|
|
|
|
.eval::<Dynamic>(
|
|
|
|
"
|
|
|
|
let x = [1, 2, 3];
|
|
|
|
x.filter(|v, i| v > i)
|
|
|
|
"
|
|
|
|
)?
|
|
|
|
.into_typed_array::<INT>()?,
|
2021-04-19 12:08:29 +02:00
|
|
|
[1, 2, 3]
|
2020-10-12 16:49:51 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-11-23 05:45:59 +01:00
|
|
|
engine
|
|
|
|
.eval::<Dynamic>(
|
|
|
|
"
|
|
|
|
let x = [1, 2, 3];
|
|
|
|
x.map(|v| v * 2)
|
|
|
|
"
|
|
|
|
)?
|
|
|
|
.into_typed_array::<INT>()?,
|
2021-04-19 12:08:29 +02:00
|
|
|
[2, 4, 6]
|
2020-10-12 16:49:51 +02:00
|
|
|
);
|
|
|
|
|
2022-12-24 12:37:06 +01:00
|
|
|
assert_eq!(
|
|
|
|
engine
|
|
|
|
.eval::<Dynamic>(
|
|
|
|
"
|
|
|
|
let x = [1, 2, 3];
|
|
|
|
x.map(|| this * 2)
|
|
|
|
"
|
|
|
|
)?
|
|
|
|
.into_typed_array::<INT>()?,
|
|
|
|
[2, 4, 6]
|
|
|
|
);
|
|
|
|
|
2020-10-12 16:49:51 +02:00
|
|
|
assert_eq!(
|
2021-11-23 05:45:59 +01:00
|
|
|
engine
|
|
|
|
.eval::<Dynamic>(
|
|
|
|
"
|
|
|
|
let x = [1, 2, 3];
|
|
|
|
x.map(|v, i| v * i)
|
|
|
|
"
|
|
|
|
)?
|
|
|
|
.into_typed_array::<INT>()?,
|
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
|
|
|
|
);
|
|
|
|
|
2022-12-25 05:44:49 +01:00
|
|
|
// assert_eq!(
|
|
|
|
// engine.eval::<INT>(
|
|
|
|
// "
|
|
|
|
// let x = [1, 2, 3];
|
|
|
|
// x.reduce(|sum, v, i| {
|
|
|
|
// if i == 0 { sum = 10 }
|
|
|
|
// sum + v * v
|
|
|
|
// })
|
|
|
|
// "
|
|
|
|
// )?,
|
|
|
|
// 24
|
|
|
|
// );
|
|
|
|
|
|
|
|
// assert_eq!(
|
|
|
|
// engine.eval::<INT>(
|
|
|
|
// "
|
|
|
|
// let x = [1, 2, 3];
|
|
|
|
// x.reduce(|sum, i| {
|
|
|
|
// if i == 0 { sum = 10 }
|
|
|
|
// sum + this * this
|
|
|
|
// })
|
|
|
|
// "
|
|
|
|
// )?,
|
|
|
|
// 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
|
|
|
)?);
|
|
|
|
|
2022-12-20 13:28:24 +01:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let x = [1, 2, 3];
|
|
|
|
x.find(|v| v > 2)
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
3
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let x = [1, 2, 3];
|
|
|
|
x.find(|v, i| v * i == 6)
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
3
|
|
|
|
);
|
|
|
|
|
2022-12-30 18:07:39 +01:00
|
|
|
engine
|
|
|
|
.eval::<()>(
|
2022-12-20 13:28:24 +01:00
|
|
|
"
|
|
|
|
let x = [1, 2, 3, 2, 1];
|
|
|
|
x.find(|v| v > 4)
|
2022-12-30 18:07:39 +01:00
|
|
|
",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2022-12-20 13:28:24 +01:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let x = [#{alice: 1}, #{bob: 2}, #{clara: 3}];
|
|
|
|
x.find_map(|v| v.bob)
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
2
|
|
|
|
);
|
|
|
|
|
2022-12-30 18:07:39 +01:00
|
|
|
engine
|
|
|
|
.eval::<()>(
|
2022-12-20 13:28:24 +01:00
|
|
|
"
|
|
|
|
let x = [#{alice: 1}, #{bob: 2}, #{clara: 3}];
|
|
|
|
x.find_map(|v| v.dave)
|
2022-12-30 18:07:39 +01:00
|
|
|
",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2022-12-20 13:28:24 +01:00
|
|
|
|
2020-10-12 16:49:51 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-06-11 18:32:12 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_arrays_elvis() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let engine = Engine::new();
|
|
|
|
|
2022-12-30 18:07:39 +01:00
|
|
|
engine.eval::<()>("let x = (); x?[2]").unwrap();
|
2022-06-11 18:32:12 +02:00
|
|
|
|
|
|
|
engine.run("let x = (); x?[2] = 42")?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|