2020-11-16 07:07:48 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, Module, INT};
|
2020-02-23 15:48:46 +01:00
|
|
|
|
2021-04-24 09:53:02 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
use rhai::FLOAT;
|
|
|
|
|
|
|
|
#[cfg(feature = "decimal")]
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
use rust_decimal::Decimal;
|
|
|
|
|
2020-02-23 15:48:46 +01:00
|
|
|
#[test]
|
2021-04-25 09:27:43 +02:00
|
|
|
fn test_for_loop() -> Result<(), Box<EvalAltResult>> {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2020-02-23 15:48:46 +01:00
|
|
|
|
2021-04-24 09:53:02 +02:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let sum1 = 0;
|
|
|
|
let sum2 = 0;
|
|
|
|
let inputs = [1, 2, 3, 4, 5];
|
2020-02-23 15:48:46 +01:00
|
|
|
|
2021-04-24 09:53:02 +02:00
|
|
|
for x in inputs {
|
|
|
|
sum1 += x;
|
|
|
|
}
|
2020-02-23 15:48:46 +01:00
|
|
|
|
2021-04-24 09:53:02 +02:00
|
|
|
for x in range(1, 6) {
|
|
|
|
sum2 += x;
|
|
|
|
}
|
2020-02-23 15:48:46 +01:00
|
|
|
|
2021-04-24 09:53:02 +02:00
|
|
|
for x in range(1, 6, 3) {
|
|
|
|
sum2 += x;
|
|
|
|
}
|
2020-04-04 06:20:24 +02:00
|
|
|
|
2021-04-24 09:53:02 +02:00
|
|
|
sum1 + sum2
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
35
|
|
|
|
);
|
2020-02-23 15:48:46 +01:00
|
|
|
|
2021-04-24 09:53:02 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let sum = 0;
|
|
|
|
for x in range(1, 10) { sum += x; }
|
|
|
|
sum
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
45
|
|
|
|
);
|
2020-04-04 06:20:24 +02:00
|
|
|
|
2021-03-04 16:47:52 +01:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2021-03-04 16:47:52 +01:00
|
|
|
let sum = 0;
|
|
|
|
for x in range(1, 10, 2) { sum += x; }
|
|
|
|
sum
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
25
|
|
|
|
);
|
|
|
|
|
2021-04-25 09:27:43 +02:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
2021-03-04 16:47:52 +01:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2021-03-04 16:47:52 +01:00
|
|
|
let sum = 0;
|
|
|
|
for x in range(10, 1, 2) { sum += x; }
|
|
|
|
sum
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
2021-04-25 09:27:43 +02:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
2021-03-04 16:47:52 +01:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2021-03-04 16:47:52 +01:00
|
|
|
let sum = 0;
|
|
|
|
for x in range(1, 10, -2) { sum += x; }
|
|
|
|
sum
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2021-03-04 16:47:52 +01:00
|
|
|
let sum = 0;
|
|
|
|
for x in range(10, 1, -2) { sum += x; }
|
|
|
|
sum
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
30
|
|
|
|
);
|
|
|
|
|
2021-04-24 09:53:02 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
{
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<FLOAT>(
|
|
|
|
"
|
|
|
|
let sum = 0.0;
|
|
|
|
for x in range(1.0, 10.0, 2.0) { sum += x; }
|
|
|
|
sum
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
25.0
|
|
|
|
);
|
|
|
|
|
2021-04-25 09:27:43 +02:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
2021-04-24 09:53:02 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<FLOAT>(
|
|
|
|
"
|
|
|
|
let sum = 0.0;
|
|
|
|
for x in range(10.0, 1.0, 2.0) { sum += x; }
|
|
|
|
sum
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
0.0
|
|
|
|
);
|
|
|
|
|
2021-04-25 09:27:43 +02:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
2021-04-24 09:53:02 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<FLOAT>(
|
|
|
|
"
|
|
|
|
let sum = 0.0;
|
|
|
|
for x in range(1.0, 10.0, -2.0) { sum += x; }
|
|
|
|
sum
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
0.0
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<FLOAT>(
|
|
|
|
"
|
|
|
|
let sum = 0.0;
|
|
|
|
for x in range(10.0, 1.0, -2.0) { sum += x; }
|
|
|
|
sum
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
30.0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
#[cfg(feature = "decimal")]
|
|
|
|
{
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<Decimal>(
|
|
|
|
"
|
|
|
|
let sum = to_decimal(0);
|
|
|
|
for x in range(to_decimal(1), to_decimal(10), to_decimal(2)) { sum += x; }
|
|
|
|
sum
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
Decimal::from(25)
|
|
|
|
);
|
|
|
|
|
2021-04-25 09:27:43 +02:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
2021-04-24 09:53:02 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<Decimal>(
|
|
|
|
"
|
|
|
|
let sum = to_decimal(0);
|
|
|
|
for x in range(to_decimal(10), to_decimal(1), to_decimal(2)) { sum += x; }
|
|
|
|
sum
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
Decimal::from(0)
|
|
|
|
);
|
|
|
|
|
2021-04-25 09:27:43 +02:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
2021-04-24 09:53:02 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<Decimal>(
|
|
|
|
"
|
|
|
|
let sum = to_decimal(0);
|
|
|
|
for x in range(to_decimal(1), to_decimal(10), to_decimal(-2)) { sum += x; }
|
|
|
|
sum
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
Decimal::from(0)
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<Decimal>(
|
|
|
|
"
|
|
|
|
let sum = to_decimal(0);
|
|
|
|
for x in range(to_decimal(10), to_decimal(1), to_decimal(-2)) { sum += x; }
|
|
|
|
sum
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
Decimal::from(30)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-04 16:47:52 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
#[test]
|
|
|
|
fn test_for_overflow() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let engine = Engine::new();
|
|
|
|
|
|
|
|
#[cfg(not(feature = "only_i32"))]
|
2021-04-20 06:01:35 +02:00
|
|
|
let script = "
|
2021-03-04 16:47:52 +01:00
|
|
|
let sum = 0;
|
|
|
|
|
|
|
|
for x in range(9223372036854775807, 0, 9223372036854775807) {
|
|
|
|
sum += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sum
|
|
|
|
";
|
|
|
|
#[cfg(feature = "only_i32")]
|
2021-04-20 06:01:35 +02:00
|
|
|
let script = "
|
2021-03-04 16:47:52 +01:00
|
|
|
let sum = 0;
|
|
|
|
|
|
|
|
for x in range(2147483647 , 0, 2147483647 ) {
|
|
|
|
sum += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sum
|
|
|
|
";
|
|
|
|
|
|
|
|
assert_eq!(engine.eval::<INT>(script)?, 0);
|
|
|
|
|
2020-04-04 06:20:24 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-06-16 03:34:30 +02:00
|
|
|
#[test]
|
|
|
|
fn test_for_string() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let engine = Engine::new();
|
|
|
|
|
|
|
|
let script = r#"
|
|
|
|
let s = "hello";
|
|
|
|
let sum = 0;
|
|
|
|
|
|
|
|
for ch in s {
|
2020-07-01 16:21:43 +02:00
|
|
|
sum += to_int(ch);
|
2020-06-16 03:34:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sum
|
|
|
|
"#;
|
|
|
|
|
|
|
|
assert_eq!(engine.eval::<INT>(script)?, 532);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-04 06:20:24 +02:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2020-04-23 08:00:29 +02:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-04-04 06:20:24 +02:00
|
|
|
#[test]
|
2020-04-21 17:25:12 +02:00
|
|
|
fn test_for_object() -> Result<(), Box<EvalAltResult>> {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2020-04-04 06:20:24 +02:00
|
|
|
|
|
|
|
let script = r#"
|
|
|
|
let sum = 0;
|
|
|
|
let keys = "";
|
|
|
|
let map = #{a: 1, b: 2, c: 3};
|
|
|
|
|
2020-10-15 16:11:40 +02:00
|
|
|
for key in map.keys() {
|
2020-04-04 06:20:24 +02:00
|
|
|
keys += key;
|
|
|
|
}
|
2020-10-15 16:11:40 +02:00
|
|
|
for value in map.values() {
|
2020-04-04 06:20:24 +02:00
|
|
|
sum += value;
|
|
|
|
}
|
|
|
|
|
2020-05-30 04:30:21 +02:00
|
|
|
keys.len + sum
|
2020-04-04 06:20:24 +02:00
|
|
|
"#;
|
|
|
|
|
|
|
|
assert_eq!(engine.eval::<INT>(script)?, 9);
|
2020-03-02 15:11:56 +01:00
|
|
|
|
|
|
|
Ok(())
|
2020-02-23 15:48:46 +01:00
|
|
|
}
|
2020-11-16 07:07:48 +01:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct MyIterableType(String);
|
|
|
|
|
|
|
|
impl IntoIterator for MyIterableType {
|
|
|
|
type Item = char;
|
|
|
|
type IntoIter = std::vec::IntoIter<Self::Item>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.0.chars().collect::<Vec<_>>().into_iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
#[test]
|
|
|
|
fn test_for_module_iterator() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
// Set a type iterator deep inside a nested module chain
|
|
|
|
let mut sub_module = Module::new();
|
|
|
|
sub_module.set_iterable::<MyIterableType>();
|
2021-03-15 05:39:06 +01:00
|
|
|
sub_module.set_native_fn("new_ts", || Ok(MyIterableType("hello".to_string())));
|
2020-11-16 07:07:48 +01:00
|
|
|
|
|
|
|
let mut module = Module::new();
|
|
|
|
module.set_sub_module("inner", sub_module);
|
|
|
|
|
2020-12-23 03:08:43 +01:00
|
|
|
engine.register_static_module("testing", module.into());
|
2020-11-16 07:07:48 +01:00
|
|
|
|
|
|
|
let script = r#"
|
|
|
|
let item = testing::inner::new_ts();
|
|
|
|
let result = "";
|
|
|
|
|
|
|
|
for x in item {
|
|
|
|
result += x;
|
|
|
|
}
|
|
|
|
result
|
|
|
|
"#;
|
|
|
|
|
|
|
|
assert_eq!(engine.eval::<String>(script)?, "hello");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|