Refine position display in error messages.
This commit is contained in:
@@ -1,19 +1,17 @@
|
||||
#![cfg(not(feature = "no_function"))]
|
||||
use rhai::{Engine, EvalAltResult, Func, ParseErrorType, Scope, INT};
|
||||
use rhai::{Engine, EvalAltResult, Func, ParseError, ParseErrorType, Scope, INT};
|
||||
|
||||
#[test]
|
||||
fn test_fn() -> Result<(), Box<EvalAltResult>> {
|
||||
let engine = Engine::new();
|
||||
|
||||
// Expect duplicated parameters error
|
||||
match engine
|
||||
.compile("fn hello(x, x) { x }")
|
||||
.expect_err("should be error")
|
||||
.error_type()
|
||||
{
|
||||
ParseErrorType::FnDuplicatedParam(f, p) if f == "hello" && p == "x" => (),
|
||||
_ => assert!(false, "wrong error"),
|
||||
}
|
||||
assert!(matches!(
|
||||
engine
|
||||
.compile("fn hello(x, x) { x }")
|
||||
.expect_err("should be error"),
|
||||
ParseError(x, _) if *x == ParseErrorType::FnDuplicatedParam("hello".to_string(), "x".to_string())
|
||||
));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@@ -7,14 +7,16 @@ fn test_constant() -> Result<(), Box<EvalAltResult>> {
|
||||
assert_eq!(engine.eval::<INT>("const x = 123; x")?, 123);
|
||||
|
||||
assert!(matches!(
|
||||
*engine.eval::<INT>("const x = 123; x = 42;").expect_err("expects error"),
|
||||
EvalAltResult::ErrorParsing(err) if err.error_type() == &ParseErrorType::AssignmentToConstant("x".to_string())
|
||||
*engine
|
||||
.eval::<INT>("const x = 123; x = 42;")
|
||||
.expect_err("expects error"),
|
||||
EvalAltResult::ErrorParsing(ParseErrorType::AssignmentToConstant(x), _) if x == "x"
|
||||
));
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
assert!(matches!(
|
||||
*engine.eval::<INT>("const x = [1, 2, 3, 4, 5]; x[2] = 42;").expect_err("expects error"),
|
||||
EvalAltResult::ErrorParsing(err) if err.error_type() == &ParseErrorType::AssignmentToConstant("x".to_string())
|
||||
EvalAltResult::ErrorParsing(ParseErrorType::AssignmentToConstant(x), _) if x == "x"
|
||||
));
|
||||
|
||||
Ok(())
|
||||
|
@@ -21,6 +21,8 @@ fn test_expressions() -> Result<(), Box<EvalAltResult>> {
|
||||
assert!(engine.eval_expression::<()>("x = 42").is_err());
|
||||
assert!(engine.compile_expression("let x = 42").is_err());
|
||||
|
||||
engine.compile("40 + { let x = 2; x }")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
use rhai::{Engine, EvalAltResult, INT};
|
||||
use rhai::{Engine, EvalAltResult, ParseError, ParseErrorType, INT};
|
||||
|
||||
#[test]
|
||||
fn test_loop() -> Result<(), Box<EvalAltResult>> {
|
||||
@@ -26,5 +26,15 @@ fn test_loop() -> Result<(), Box<EvalAltResult>> {
|
||||
21
|
||||
);
|
||||
|
||||
assert!(matches!(
|
||||
engine.compile("let x = 0; break;").expect_err("should error"),
|
||||
ParseError(x, _) if *x == ParseErrorType::LoopBreak
|
||||
));
|
||||
|
||||
assert!(matches!(
|
||||
engine.compile("let x = 0; if x > 0 { continue; }").expect_err("should error"),
|
||||
ParseError(x, _) if *x == ParseErrorType::LoopBreak
|
||||
));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@@ -1,5 +1,7 @@
|
||||
#![cfg(not(feature = "no_module"))]
|
||||
use rhai::{module_resolvers, Engine, EvalAltResult, Module, Scope, INT};
|
||||
use rhai::{
|
||||
module_resolvers, Engine, EvalAltResult, Module, ParseError, ParseErrorType, Scope, INT,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_module() {
|
||||
@@ -231,3 +233,20 @@ fn test_module_from_ast() -> Result<(), Box<EvalAltResult>> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_module_export() -> Result<(), Box<EvalAltResult>> {
|
||||
let engine = Engine::new();
|
||||
|
||||
assert!(matches!(
|
||||
engine.compile(r"let x = 10; { export x; }").expect_err("should error"),
|
||||
ParseError(x, _) if *x == ParseErrorType::WrongExport
|
||||
));
|
||||
|
||||
assert!(matches!(
|
||||
engine.compile(r"fn abc(x) { export x; }").expect_err("should error"),
|
||||
ParseError(x, _) if *x == ParseErrorType::WrongExport
|
||||
));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user