Add docs on JSON parsing.
This commit is contained in:
157
tests/maps.rs
157
tests/maps.rs
@@ -1,6 +1,6 @@
|
||||
#![cfg(not(feature = "no_object"))]
|
||||
|
||||
use rhai::{AnyExt, Engine, EvalAltResult, Map, INT};
|
||||
use rhai::{AnyExt, Engine, EvalAltResult, Map, Scope, INT};
|
||||
|
||||
#[test]
|
||||
fn test_map_indexing() -> Result<(), EvalAltResult> {
|
||||
@@ -75,13 +75,28 @@ fn test_map_assign() -> Result<(), EvalAltResult> {
|
||||
let engine = Engine::new();
|
||||
|
||||
let x = engine.eval::<Map>(r#"let x = #{a: 1, b: true, "c$": "hello"}; x"#)?;
|
||||
let a = x.get("a").cloned().expect("should have property a");
|
||||
let b = x.get("b").cloned().expect("should have property b");
|
||||
let c = x.get("c$").cloned().expect("should have property c$");
|
||||
|
||||
assert_eq!(a.cast::<INT>(), 1);
|
||||
assert_eq!(b.cast::<bool>(), true);
|
||||
assert_eq!(c.cast::<String>(), "hello");
|
||||
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"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -91,13 +106,28 @@ fn test_map_return() -> Result<(), EvalAltResult> {
|
||||
let engine = Engine::new();
|
||||
|
||||
let x = engine.eval::<Map>(r#"#{a: 1, b: true, "c$": "hello"}"#)?;
|
||||
let a = x.get("a").cloned().expect("should have property a");
|
||||
let b = x.get("b").cloned().expect("should have property b");
|
||||
let c = x.get("c$").cloned().expect("should have property c$");
|
||||
|
||||
assert_eq!(a.cast::<INT>(), 1);
|
||||
assert_eq!(b.cast::<bool>(), true);
|
||||
assert_eq!(c.cast::<String>(), "hello");
|
||||
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"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -107,20 +137,101 @@ fn test_map_for() -> Result<(), EvalAltResult> {
|
||||
let engine = Engine::new();
|
||||
|
||||
assert_eq!(
|
||||
engine.eval::<INT>(
|
||||
r#"
|
||||
let map = #{a: 1, b: true, c: 123.456};
|
||||
let s = "";
|
||||
engine
|
||||
.eval::<String>(
|
||||
r#"
|
||||
let map = #{a: 1, b_x: true, "$c d e!": "hello"};
|
||||
let s = "";
|
||||
|
||||
for key in keys(map) {
|
||||
s += key;
|
||||
}
|
||||
for key in keys(map) {
|
||||
s += key;
|
||||
}
|
||||
|
||||
s.len()
|
||||
s
|
||||
"#
|
||||
)?,
|
||||
3
|
||||
)?
|
||||
.len(),
|
||||
11
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[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 mut scope = Scope::new();
|
||||
scope.push_constant("null", ());
|
||||
scope.push_constant("undefined", ());
|
||||
|
||||
let json = r#"{"a":1, "b":true, "c":42, "$d e f!":"hello", "z":null}"#;
|
||||
|
||||
let map = engine.eval_expression_with_scope::<Map>(&mut scope, &("#".to_string() + json))?;
|
||||
|
||||
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"))]
|
||||
{
|
||||
scope.clear();
|
||||
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(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user