rhai/tests/maps.rs

67 lines
1.7 KiB
Rust
Raw Normal View History

2020-03-29 17:53:35 +02:00
#![cfg(not(feature = "no_object"))]
2020-03-30 11:40:26 +02:00
use rhai::{AnyExt, Engine, EvalAltResult, Map, INT};
2020-03-29 17:53:35 +02:00
#[test]
fn test_map_indexing() -> Result<(), EvalAltResult> {
let mut engine = Engine::new();
2020-03-30 11:40:26 +02:00
#[cfg(not(feature = "no_index"))]
2020-03-29 17:53:35 +02:00
assert_eq!(
2020-03-30 11:40:26 +02:00
engine.eval::<INT>(r#"let x = #{a: 1, b: 2, c: 3}; x["b"]"#)?,
2020-03-29 17:53:35 +02:00
2
);
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
#[cfg(not(feature = "no_index"))]
2020-03-29 17:53:35 +02:00
assert_eq!(
engine.eval::<char>(
r#"
2020-03-30 11:40:26 +02:00
let y = #{d: 1, "e": #{a: 42, b: 88, "": "hello"}, " 123 xyz": 9};
y.e[""][4]
2020-03-29 17:53:35 +02:00
"#
)?,
2020-03-30 11:40:26 +02:00
'o'
2020-03-29 17:53:35 +02:00
);
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
Ok(())
}
#[test]
fn test_map_assign() -> Result<(), EvalAltResult> {
let mut engine = Engine::new();
2020-03-30 11:40:26 +02:00
let x = engine.eval::<Map>(r#"let x = #{a: 1, b: true, "c#": "hello"}; x"#)?;
let a = x.get("a").cloned().unwrap();
let b = x.get("b").cloned().unwrap();
let c = x.get("c#").cloned().unwrap();
2020-03-29 17:53:35 +02:00
2020-03-30 11:40:26 +02:00
assert_eq!(*a.downcast::<INT>().unwrap(), 1);
assert_eq!(*b.downcast::<bool>().unwrap(), true);
assert_eq!(*c.downcast::<String>().unwrap(), "hello");
2020-03-29 17:53:35 +02:00
Ok(())
}
#[test]
fn test_map_return() -> Result<(), EvalAltResult> {
let mut engine = Engine::new();
2020-03-30 11:40:26 +02:00
let x = engine.eval::<Map>(r#"#{a: 1, b: true, c: "hello"}"#)?;
let a = x.get("a").cloned().unwrap();
let b = x.get("b").cloned().unwrap();
let c = x.get("c").cloned().unwrap();
2020-03-29 17:53:35 +02:00
2020-03-30 11:40:26 +02:00
assert_eq!(*a.downcast::<INT>().unwrap(), 1);
assert_eq!(*b.downcast::<bool>().unwrap(), true);
assert_eq!(*c.downcast::<String>().unwrap(), "hello");
2020-03-29 17:53:35 +02:00
Ok(())
}