Add tests for index type checks.
This commit is contained in:
parent
04df4d2547
commit
591f7d7362
@ -1,5 +1,5 @@
|
|||||||
#![cfg(not(feature = "no_index"))]
|
#![cfg(not(feature = "no_index"))]
|
||||||
use rhai::{Array, Dynamic, Engine, EvalAltResult, INT};
|
use rhai::{Array, Dynamic, Engine, EvalAltResult, ParseErrorType, INT};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_arrays() -> Result<(), Box<EvalAltResult>> {
|
fn test_arrays() -> Result<(), Box<EvalAltResult>> {
|
||||||
@ -172,6 +172,53 @@ fn test_arrays() -> Result<(), Box<EvalAltResult>> {
|
|||||||
Ok(())
|
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]
|
#[test]
|
||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
fn test_array_with_structs() -> Result<(), Box<EvalAltResult>> {
|
fn test_array_with_structs() -> Result<(), Box<EvalAltResult>> {
|
||||||
|
@ -124,6 +124,56 @@ fn test_map_prop() -> Result<(), Box<EvalAltResult>> {
|
|||||||
Ok(())
|
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]
|
#[test]
|
||||||
fn test_map_assign() -> Result<(), Box<EvalAltResult>> {
|
fn test_map_assign() -> Result<(), Box<EvalAltResult>> {
|
||||||
let engine = Engine::new();
|
let engine = Engine::new();
|
||||||
|
Loading…
Reference in New Issue
Block a user