#![cfg(not(feature = "no_object"))] use rhai::{Engine, EvalAltResult, Map, Scope, INT}; #[test] fn test_map_indexing() -> Result<(), EvalAltResult> { let engine = Engine::new(); #[cfg(not(feature = "no_index"))] { assert_eq!( engine.eval::(r#"let x = #{a: 1, b: 2, c: 3}; x["b"]"#)?, 2 ); assert_eq!( engine.eval::( r#" let y = #{d: 1, "e": #{a: 42, b: 88, "": "hello"}, " 123 xyz": 9}; y.e[""][4] "# )?, 'o' ); } assert_eq!( engine.eval::("let y = #{a: 1, b: 2, c: 3}; y.a = 5; y.a")?, 5 ); engine.eval::<()>("let y = #{a: 1, b: 2, c: 3}; y.z")?; assert!(engine.eval::(r#"let y = #{a: 1, b: 2, c: 3}; "c" in y"#)?); assert!(engine.eval::("let y = #{a: 1, b: 2, c: 3}; 'b' in y")?); assert!(!engine.eval::(r#"let y = #{a: 1, b: 2, c: 3}; "z" in y"#)?); assert_eq!( engine.eval::( r#" let x = #{a: 1, b: 2, c: 3}; let c = x.remove("c"); x.len() + c "# )?, 5 ); assert_eq!( engine.eval::( r" let x = #{a: 1, b: 2, c: 3}; let y = #{b: 42, d: 9}; x.mixin(y); x.len() + x.b " )?, 46 ); assert_eq!( engine.eval::( r" let x = #{a: 1, b: 2, c: 3}; x += #{b: 42, d: 9}; x.len() + x.b " )?, 46 ); assert_eq!( engine .eval::( r" let x = #{a: 1, b: 2, c: 3}; let y = #{b: 42, d: 9}; x + y " )? .len(), 4 ); Ok(()) } #[test] fn test_map_assign() -> Result<(), EvalAltResult> { let engine = Engine::new(); let x = engine.eval::(r#"let x = #{a: 1, b: true, "c$": "hello"}; x"#)?; assert_eq!( x.get("a") .cloned() .expect("should have property a") .cast::(), 1 ); assert_eq!( x.get("b") .cloned() .expect("should have property b") .cast::(), true ); assert_eq!( x.get("c$") .cloned() .expect("should have property c$") .cast::(), "hello" ); Ok(()) } #[test] fn test_map_return() -> Result<(), EvalAltResult> { let engine = Engine::new(); let x = engine.eval::(r#"#{a: 1, b: true, "c$": "hello"}"#)?; assert_eq!( x.get("a") .cloned() .expect("should have property a") .cast::(), 1 ); assert_eq!( x.get("b") .cloned() .expect("should have property b") .cast::(), true ); assert_eq!( x.get("c$") .cloned() .expect("should have property c$") .cast::(), "hello" ); Ok(()) } #[test] fn test_map_for() -> Result<(), EvalAltResult> { let engine = Engine::new(); assert_eq!( engine .eval::( r#" let map = #{a: 1, b_x: true, "$c d e!": "hello"}; let s = ""; for key in keys(map) { s += key; } s "# )? .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 json = r#"{"a":1, "b":true, "c":42, "$d e f!":"hello", "z":null}"#; let map = engine.parse_json(json, true)?; assert!(!map.contains_key("x")); assert_eq!( map.get("a") .cloned() .expect("should have property a") .cast::(), 1 ); assert_eq!( map.get("b") .cloned() .expect("should have property b") .cast::(), true ); assert_eq!( map.get("c") .cloned() .expect("should have property a") .cast::(), 42 ); assert_eq!( map.get("$d e f!") .cloned() .expect("should have property $d e f!") .cast::(), "hello" ); assert_eq!( map.get("z") .cloned() .expect("should have property z") .cast::<()>(), () ); #[cfg(not(feature = "no_index"))] { let mut scope = Scope::new(); scope.push_constant("map", map); assert_eq!( engine .eval_with_scope::( &mut scope, r#" let s = ""; for key in keys(map) { s += key; } s "# )? .len(), 11 ); } Ok(()) }