2020-03-29 17:53:35 +02:00
|
|
|
#![cfg(not(feature = "no_object"))]
|
|
|
|
|
2020-04-10 09:18:26 +02:00
|
|
|
use rhai::{AnyExt, Engine, EvalAltResult, Map, Scope, INT};
|
2020-03-29 17:53:35 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_map_indexing() -> Result<(), EvalAltResult> {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2020-03-29 17:53:35 +02:00
|
|
|
|
2020-03-30 11:40:26 +02:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-04-01 16:56:54 +02:00
|
|
|
{
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(r#"let x = #{a: 1, b: 2, c: 3}; x["b"]"#)?,
|
|
|
|
2
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<char>(
|
|
|
|
r#"
|
|
|
|
let y = #{d: 1, "e": #{a: 42, b: 88, "": "hello"}, " 123 xyz": 9};
|
|
|
|
y.e[""][4]
|
|
|
|
"#
|
|
|
|
)?,
|
|
|
|
'o'
|
|
|
|
);
|
|
|
|
}
|
2020-03-30 11:40:26 +02:00
|
|
|
|
2020-03-29 17:53:35 +02:00
|
|
|
assert_eq!(
|
2020-03-30 11:40:26 +02:00
|
|
|
engine.eval::<INT>("let y = #{a: 1, b: 2, c: 3}; y.a = 5; y.a")?,
|
2020-03-29 17:53:35 +02:00
|
|
|
5
|
|
|
|
);
|
2020-03-30 11:40:26 +02:00
|
|
|
engine.eval::<()>("let y = #{a: 1, b: 2, c: 3}; y.z")?;
|
2020-03-29 17:53:35 +02:00
|
|
|
|
2020-04-06 11:47:34 +02:00
|
|
|
assert!(engine.eval::<bool>(r#"let y = #{a: 1, b: 2, c: 3}; "c" in y"#)?);
|
|
|
|
assert!(engine.eval::<bool>("let y = #{a: 1, b: 2, c: 3}; 'b' in y")?);
|
|
|
|
assert!(!engine.eval::<bool>(r#"let y = #{a: 1, b: 2, c: 3}; "z" in y"#)?);
|
|
|
|
|
2020-04-08 04:19:03 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r"
|
|
|
|
let x = #{a: 1, b: 2, c: 3};
|
|
|
|
let y = #{b: 42, d: 9};
|
|
|
|
x.mixin(y);
|
|
|
|
x.len() + x.b
|
2020-04-01 16:56:54 +02:00
|
|
|
"
|
2020-04-08 04:19:03 +02:00
|
|
|
)?,
|
|
|
|
46
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r"
|
|
|
|
let x = #{a: 1, b: 2, c: 3};
|
|
|
|
x += #{b: 42, d: 9};
|
|
|
|
x.len() + x.b
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
46
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine
|
|
|
|
.eval::<Map>(
|
2020-04-01 16:56:54 +02:00
|
|
|
r"
|
|
|
|
let x = #{a: 1, b: 2, c: 3};
|
2020-04-08 04:19:03 +02:00
|
|
|
let y = #{b: 42, d: 9};
|
|
|
|
x + y
|
2020-04-01 16:56:54 +02:00
|
|
|
"
|
2020-04-08 04:19:03 +02:00
|
|
|
)?
|
|
|
|
.len(),
|
|
|
|
4
|
|
|
|
);
|
2020-04-01 16:56:54 +02:00
|
|
|
|
2020-03-29 17:53:35 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_map_assign() -> Result<(), EvalAltResult> {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2020-03-29 17:53:35 +02:00
|
|
|
|
2020-04-02 06:35:44 +02:00
|
|
|
let x = engine.eval::<Map>(r#"let x = #{a: 1, b: true, "c$": "hello"}; x"#)?;
|
2020-03-29 17:53:35 +02:00
|
|
|
|
2020-04-10 09:18:26 +02:00
|
|
|
assert_eq!(
|
|
|
|
x.get("a")
|
|
|
|
.cloned()
|
|
|
|
.expect("should have property a")
|
|
|
|
.cast::<INT>(),
|
|
|
|
1
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
x.get("b")
|
|
|
|
.cloned()
|
|
|
|
.expect("should have property b")
|
|
|
|
.cast::<bool>(),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
x.get("c$")
|
|
|
|
.cloned()
|
|
|
|
.expect("should have property c$")
|
|
|
|
.cast::<String>(),
|
|
|
|
"hello"
|
|
|
|
);
|
2020-03-29 17:53:35 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_map_return() -> Result<(), EvalAltResult> {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2020-03-29 17:53:35 +02:00
|
|
|
|
2020-04-02 06:35:44 +02:00
|
|
|
let x = engine.eval::<Map>(r#"#{a: 1, b: true, "c$": "hello"}"#)?;
|
2020-03-29 17:53:35 +02:00
|
|
|
|
2020-04-10 09:18:26 +02:00
|
|
|
assert_eq!(
|
|
|
|
x.get("a")
|
|
|
|
.cloned()
|
|
|
|
.expect("should have property a")
|
|
|
|
.cast::<INT>(),
|
|
|
|
1
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
x.get("b")
|
|
|
|
.cloned()
|
|
|
|
.expect("should have property b")
|
|
|
|
.cast::<bool>(),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
x.get("c$")
|
|
|
|
.cloned()
|
|
|
|
.expect("should have property c$")
|
|
|
|
.cast::<String>(),
|
|
|
|
"hello"
|
|
|
|
);
|
2020-03-29 17:53:35 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-04 06:20:24 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_map_for() -> Result<(), EvalAltResult> {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2020-04-04 06:20:24 +02:00
|
|
|
|
|
|
|
assert_eq!(
|
2020-04-10 09:18:26 +02:00
|
|
|
engine
|
|
|
|
.eval::<String>(
|
|
|
|
r#"
|
|
|
|
let map = #{a: 1, b_x: true, "$c d e!": "hello"};
|
|
|
|
let s = "";
|
2020-04-04 06:20:24 +02:00
|
|
|
|
2020-04-10 09:18:26 +02:00
|
|
|
for key in keys(map) {
|
|
|
|
s += key;
|
|
|
|
}
|
2020-04-04 06:20:24 +02:00
|
|
|
|
2020-04-10 09:18:26 +02:00
|
|
|
s
|
2020-04-04 06:20:24 +02:00
|
|
|
"#
|
2020-04-10 09:18:26 +02:00
|
|
|
)?
|
|
|
|
.len(),
|
|
|
|
11
|
2020-04-04 06:20:24 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-10 09:18:26 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
/// Because a Rhai object map literal is almost the same as JSON,
|
|
|
|
/// it is possible to convert from JSON into a Rhai object map.
|
|
|
|
fn test_map_json() -> Result<(), EvalAltResult> {
|
|
|
|
let engine = Engine::new();
|
|
|
|
|
|
|
|
let json = r#"{"a":1, "b":true, "c":42, "$d e f!":"hello", "z":null}"#;
|
|
|
|
|
2020-04-10 11:14:07 +02:00
|
|
|
let map = engine.parse_json(json, true)?;
|
2020-04-10 09:18:26 +02:00
|
|
|
|
|
|
|
assert!(!map.contains_key("x"));
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
map.get("a")
|
|
|
|
.cloned()
|
|
|
|
.expect("should have property a")
|
|
|
|
.cast::<INT>(),
|
|
|
|
1
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map.get("b")
|
|
|
|
.cloned()
|
|
|
|
.expect("should have property b")
|
|
|
|
.cast::<bool>(),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map.get("c")
|
|
|
|
.cloned()
|
|
|
|
.expect("should have property a")
|
|
|
|
.cast::<INT>(),
|
|
|
|
42
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map.get("$d e f!")
|
|
|
|
.cloned()
|
|
|
|
.expect("should have property $d e f!")
|
|
|
|
.cast::<String>(),
|
|
|
|
"hello"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
map.get("z")
|
|
|
|
.cloned()
|
|
|
|
.expect("should have property z")
|
|
|
|
.cast::<()>(),
|
|
|
|
()
|
|
|
|
);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
{
|
2020-04-10 11:14:07 +02:00
|
|
|
let mut scope = Scope::new();
|
2020-04-10 09:18:26 +02:00
|
|
|
scope.push_constant("map", map);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine
|
|
|
|
.eval_with_scope::<String>(
|
|
|
|
&mut scope,
|
|
|
|
r#"
|
|
|
|
let s = "";
|
|
|
|
|
|
|
|
for key in keys(map) {
|
|
|
|
s += key;
|
|
|
|
}
|
|
|
|
|
|
|
|
s
|
|
|
|
"#
|
|
|
|
)?
|
|
|
|
.len(),
|
|
|
|
11
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|