diff --git a/tests/arrays.rs b/tests/arrays.rs index e0dc764d..fb6e4a4d 100644 --- a/tests/arrays.rs +++ b/tests/arrays.rs @@ -1,5 +1,5 @@ #![cfg(not(feature = "no_index"))] -use rhai::{Array, Dynamic, Engine, EvalAltResult, INT}; +use rhai::{Array, Dynamic, Engine, EvalAltResult, ParseErrorType, INT}; #[test] fn test_arrays() -> Result<(), Box> { @@ -172,6 +172,53 @@ fn test_arrays() -> Result<(), Box> { Ok(()) } +#[test] +fn test_array_index_types() -> Result<(), Box> { + let engine = Engine::new(); + + engine.compile("[1, 2, 3][0]['x']")?; + + assert!(matches!( + *engine + .compile("[1, 2, 3]['x']") + .expect_err("should error") + .0, + ParseErrorType::MalformedIndexExpr(..) + )); + + #[cfg(not(feature = "no_float"))] + assert!(matches!( + *engine + .compile("[1, 2, 3][123.456]") + .expect_err("should error") + .0, + ParseErrorType::MalformedIndexExpr(..) + )); + + assert!(matches!( + *engine.compile("[1, 2, 3][()]").expect_err("should error").0, + ParseErrorType::MalformedIndexExpr(..) + )); + + assert!(matches!( + *engine + .compile(r#"[1, 2, 3]["hello"]"#) + .expect_err("should error") + .0, + ParseErrorType::MalformedIndexExpr(..) + )); + + assert!(matches!( + *engine + .compile("[1, 2, 3][true && false]") + .expect_err("should error") + .0, + ParseErrorType::MalformedIndexExpr(..) + )); + + Ok(()) +} + #[test] #[cfg(not(feature = "no_object"))] fn test_array_with_structs() -> Result<(), Box> { diff --git a/tests/maps.rs b/tests/maps.rs index 9a4414f1..daf8b051 100644 --- a/tests/maps.rs +++ b/tests/maps.rs @@ -124,6 +124,56 @@ fn test_map_prop() -> Result<(), Box> { Ok(()) } +#[test] +fn test_map_index_types() -> Result<(), Box> { + let engine = Engine::new(); + + engine.compile(r#"#{a:1, b:2, c:3}["a"]['x']"#)?; + + assert!(matches!( + *engine + .compile("#{a:1, b:2, c:3}['x']") + .expect_err("should error") + .0, + ParseErrorType::MalformedIndexExpr(..) + )); + + assert!(matches!( + *engine + .compile("#{a:1, b:2, c:3}[1]") + .expect_err("should error") + .0, + ParseErrorType::MalformedIndexExpr(..) + )); + + #[cfg(not(feature = "no_float"))] + assert!(matches!( + *engine + .compile("#{a:1, b:2, c:3}[123.456]") + .expect_err("should error") + .0, + ParseErrorType::MalformedIndexExpr(..) + )); + + assert!(matches!( + *engine + .compile("#{a:1, b:2, c:3}[()]") + .expect_err("should error") + .0, + ParseErrorType::MalformedIndexExpr(..) + )); + + assert!(matches!( + *engine + .compile("#{a:1, b:2, c:3}[true && false]") + .expect_err("should error") + .0, + ParseErrorType::MalformedIndexExpr(..) + )); + + Ok(()) +} + #[test] fn test_map_assign() -> Result<(), Box> { let engine = Engine::new();