Use matches! in examples.
This commit is contained in:
parent
b9e4040635
commit
6e076c409d
@ -62,10 +62,11 @@ fn main() {
|
|||||||
match input.as_str().trim() {
|
match input.as_str().trim() {
|
||||||
"exit" | "quit" => break, // quit
|
"exit" | "quit" => break, // quit
|
||||||
"ast" => {
|
"ast" => {
|
||||||
|
if matches!(&ast, Some(_)) {
|
||||||
// print the last AST
|
// print the last AST
|
||||||
match &ast {
|
println!("{:#?}", ast.as_ref().unwrap());
|
||||||
Some(ast) => println!("{:#?}", ast),
|
} else {
|
||||||
None => println!("()"),
|
println!("()");
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -6,18 +6,16 @@ fn test_constant() -> Result<(), EvalAltResult> {
|
|||||||
|
|
||||||
assert_eq!(engine.eval::<i64>("const x = 123; x")?, 123);
|
assert_eq!(engine.eval::<i64>("const x = 123; x")?, 123);
|
||||||
|
|
||||||
match engine.eval::<i64>("const x = 123; x = 42;") {
|
assert!(
|
||||||
Err(EvalAltResult::ErrorAssignmentToConstant(var, _)) if var == "x" => (),
|
matches!(engine.eval::<i64>("const x = 123; x = 42;").expect_err("expects error"),
|
||||||
Err(err) => return Err(err),
|
EvalAltResult::ErrorAssignmentToConstant(var, _) if var == "x")
|
||||||
Ok(_) => panic!("expecting compilation error"),
|
);
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
match engine.eval::<i64>("const x = [1, 2, 3, 4, 5]; x[2] = 42;") {
|
assert!(
|
||||||
Err(EvalAltResult::ErrorAssignmentToConstant(var, _)) if var == "x" => (),
|
matches!(engine.eval::<i64>("const x = [1, 2, 3, 4, 5]; x[2] = 42;").expect_err("expects error"),
|
||||||
Err(err) => return Err(err),
|
EvalAltResult::ErrorAssignmentToConstant(var, _) if var == "x")
|
||||||
Ok(_) => panic!("expecting compilation error"),
|
);
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -6,12 +6,9 @@ fn test_decrement() -> Result<(), EvalAltResult> {
|
|||||||
|
|
||||||
assert_eq!(engine.eval::<INT>("let x = 10; x -= 7; x")?, 3);
|
assert_eq!(engine.eval::<INT>("let x = 10; x -= 7; x")?, 3);
|
||||||
|
|
||||||
let r = engine.eval::<String>("let s = \"test\"; s -= \"ing\"; s");
|
assert!(matches!(engine
|
||||||
|
.eval::<String>(r#"let s = "test"; s -= "ing"; s"#)
|
||||||
match r {
|
.expect_err("expects error"), EvalAltResult::ErrorFunctionNotFound(err, _) if err == "- (string, string)"));
|
||||||
Err(EvalAltResult::ErrorFunctionNotFound(err, _)) if err == "- (string, string)" => (),
|
|
||||||
_ => panic!(),
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
110
tests/math.rs
110
tests/math.rs
@ -24,54 +24,76 @@ fn test_math() -> Result<(), EvalAltResult> {
|
|||||||
{
|
{
|
||||||
#[cfg(not(feature = "only_i32"))]
|
#[cfg(not(feature = "only_i32"))]
|
||||||
{
|
{
|
||||||
match engine.eval::<INT>("(-9223372036854775808).abs()") {
|
assert!(matches!(
|
||||||
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
|
engine
|
||||||
r => panic!("should return overflow error: {:?}", r),
|
.eval::<INT>("(-9223372036854775808).abs()")
|
||||||
}
|
.expect_err("expects negation overflow"),
|
||||||
match engine.eval::<INT>("9223372036854775807 + 1") {
|
EvalAltResult::ErrorArithmetic(_, _)
|
||||||
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
|
));
|
||||||
r => panic!("should return overflow error: {:?}", r),
|
assert!(matches!(
|
||||||
}
|
engine
|
||||||
match engine.eval::<INT>("-9223372036854775808 - 1") {
|
.eval::<INT>("9223372036854775807 + 1")
|
||||||
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
|
.expect_err("expects overflow"),
|
||||||
r => panic!("should return underflow error: {:?}", r),
|
EvalAltResult::ErrorArithmetic(_, _)
|
||||||
}
|
));
|
||||||
match engine.eval::<INT>("9223372036854775807 * 9223372036854775807") {
|
assert!(matches!(
|
||||||
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
|
engine
|
||||||
r => panic!("should return overflow error: {:?}", r),
|
.eval::<INT>("-9223372036854775808 - 1")
|
||||||
}
|
.expect_err("expects underflow"),
|
||||||
match engine.eval::<INT>("9223372036854775807 / 0") {
|
EvalAltResult::ErrorArithmetic(_, _)
|
||||||
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
|
));
|
||||||
r => panic!("should return division by zero error: {:?}", r),
|
assert!(matches!(
|
||||||
}
|
engine
|
||||||
match engine.eval::<INT>("9223372036854775807 % 0") {
|
.eval::<INT>("9223372036854775807 * 9223372036854775807")
|
||||||
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
|
.expect_err("expects overflow"),
|
||||||
r => panic!("should return division by zero error: {:?}", r),
|
EvalAltResult::ErrorArithmetic(_, _)
|
||||||
}
|
));
|
||||||
|
assert!(matches!(
|
||||||
|
engine
|
||||||
|
.eval::<INT>("9223372036854775807 / 0")
|
||||||
|
.expect_err("expects division by zero"),
|
||||||
|
EvalAltResult::ErrorArithmetic(_, _)
|
||||||
|
));
|
||||||
|
assert!(matches!(
|
||||||
|
engine
|
||||||
|
.eval::<INT>("9223372036854775807 % 0")
|
||||||
|
.expect_err("expects division by zero"),
|
||||||
|
EvalAltResult::ErrorArithmetic(_, _)
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "only_i32")]
|
#[cfg(feature = "only_i32")]
|
||||||
{
|
{
|
||||||
match engine.eval::<INT>("2147483647 + 1") {
|
assert!(matches!(
|
||||||
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
|
engine
|
||||||
r => panic!("should return overflow error: {:?}", r),
|
.eval::<INT>("2147483647 + 1")
|
||||||
}
|
.expect_err("expects overflow"),
|
||||||
match engine.eval::<INT>("-2147483648 - 1") {
|
EvalAltResult::ErrorArithmetic(_, _)
|
||||||
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
|
));
|
||||||
r => panic!("should return underflow error: {:?}", r),
|
assert!(matches!(
|
||||||
}
|
engine
|
||||||
match engine.eval::<INT>("2147483647 * 2147483647") {
|
.eval::<INT>("-2147483648 - 1")
|
||||||
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
|
.expect_err("expects underflow"),
|
||||||
r => panic!("should return overflow error: {:?}", r),
|
EvalAltResult::ErrorArithmetic(_, _)
|
||||||
}
|
));
|
||||||
match engine.eval::<INT>("2147483647 / 0") {
|
assert!(matches!(
|
||||||
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
|
engine
|
||||||
r => panic!("should return division by zero error: {:?}", r),
|
.eval::<INT>("2147483647 * 2147483647")
|
||||||
}
|
.expect_err("expects overflow"),
|
||||||
match engine.eval::<INT>("2147483647 % 0") {
|
EvalAltResult::ErrorArithmetic(_, _)
|
||||||
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
|
));
|
||||||
r => panic!("should return division by zero error: {:?}", r),
|
assert!(matches!(
|
||||||
}
|
engine
|
||||||
|
.eval::<INT>("2147483647 / 0")
|
||||||
|
.expect_err("expects division by zero"),
|
||||||
|
EvalAltResult::ErrorArithmetic(_, _)
|
||||||
|
));
|
||||||
|
assert!(matches!(
|
||||||
|
engine
|
||||||
|
.eval::<INT>("2147483647 % 0")
|
||||||
|
.expect_err("expects division by zero"),
|
||||||
|
EvalAltResult::ErrorArithmetic(_, _)
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,12 +5,10 @@ use rhai::{Engine, EvalAltResult, RegisterFn, INT};
|
|||||||
fn test_mismatched_op() {
|
fn test_mismatched_op() {
|
||||||
let mut engine = Engine::new();
|
let mut engine = Engine::new();
|
||||||
|
|
||||||
let r = engine.eval::<INT>("60 + \"hello\"");
|
assert!(
|
||||||
|
matches!(engine.eval::<INT>(r#"60 + "hello""#).expect_err("expects error"),
|
||||||
match r {
|
EvalAltResult::ErrorMismatchOutputType(err, _) if err == "string")
|
||||||
Err(EvalAltResult::ErrorMismatchOutputType(err, _)) if err == "string" => (),
|
);
|
||||||
_ => panic!(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -30,14 +28,16 @@ fn test_mismatched_op_custom_type() {
|
|||||||
engine.register_type_with_name::<TestStruct>("TestStruct");
|
engine.register_type_with_name::<TestStruct>("TestStruct");
|
||||||
engine.register_fn("new_ts", TestStruct::new);
|
engine.register_fn("new_ts", TestStruct::new);
|
||||||
|
|
||||||
let r = engine.eval::<INT>("60 + new_ts()");
|
let r = engine
|
||||||
|
.eval::<INT>("60 + new_ts()")
|
||||||
|
.expect_err("expects error");
|
||||||
|
|
||||||
match r {
|
match r {
|
||||||
#[cfg(feature = "only_i32")]
|
#[cfg(feature = "only_i32")]
|
||||||
Err(EvalAltResult::ErrorFunctionNotFound(err, _)) if err == "+ (i32, TestStruct)" => (),
|
EvalAltResult::ErrorFunctionNotFound(err, _) if err == "+ (i32, TestStruct)" => (),
|
||||||
|
|
||||||
#[cfg(not(feature = "only_i32"))]
|
#[cfg(not(feature = "only_i32"))]
|
||||||
Err(EvalAltResult::ErrorFunctionNotFound(err, _)) if err == "+ (i64, TestStruct)" => (),
|
EvalAltResult::ErrorFunctionNotFound(err, _) if err == "+ (i64, TestStruct)" => (),
|
||||||
|
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,14 @@
|
|||||||
use rhai::{Engine, EvalAltResult, INT};
|
use rhai::{Engine, EvalAltResult};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_throw() {
|
fn test_throw() {
|
||||||
let mut engine = Engine::new();
|
let mut engine = Engine::new();
|
||||||
|
|
||||||
match engine.eval::<INT>(r#"if true { throw "hello" }"#) {
|
assert!(matches!(
|
||||||
Ok(_) => panic!("not an error"),
|
engine.eval::<()>(r#"if true { throw "hello" }"#).expect_err("expects error"),
|
||||||
Err(EvalAltResult::ErrorRuntime(s, _)) if s == "hello" => (),
|
EvalAltResult::ErrorRuntime(s, _) if s == "hello"));
|
||||||
Err(err) => panic!("wrong error: {}", err),
|
|
||||||
}
|
|
||||||
|
|
||||||
match engine.eval::<INT>(r#"throw;"#) {
|
assert!(matches!(
|
||||||
Ok(_) => panic!("not an error"),
|
engine.eval::<()>(r#"throw;"#).expect_err("expects error"),
|
||||||
Err(EvalAltResult::ErrorRuntime(s, _)) if s == "" => (),
|
EvalAltResult::ErrorRuntime(s, _) if s == ""));
|
||||||
Err(err) => panic!("wrong error: {}", err),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user