Add tests for index type checks.

This commit is contained in:
Stephen Chung 2022-05-17 15:20:32 +08:00
parent 04df4d2547
commit 591f7d7362
2 changed files with 98 additions and 1 deletions

View File

@ -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<EvalAltResult>> {
@ -172,6 +172,53 @@ fn test_arrays() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[test]
fn test_array_index_types() -> Result<(), Box<EvalAltResult>> {
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<EvalAltResult>> {

View File

@ -124,6 +124,56 @@ fn test_map_prop() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[test]
fn test_map_index_types() -> Result<(), Box<EvalAltResult>> {
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<EvalAltResult>> {
let engine = Engine::new();