Use matches! in examples.

This commit is contained in:
Stephen Chung 2020-03-14 23:40:30 +08:00
parent b9e4040635
commit 6e076c409d
6 changed files with 98 additions and 84 deletions

View File

@ -62,10 +62,11 @@ fn main() {
match input.as_str().trim() {
"exit" | "quit" => break, // quit
"ast" => {
// print the last AST
match &ast {
Some(ast) => println!("{:#?}", ast),
None => println!("()"),
if matches!(&ast, Some(_)) {
// print the last AST
println!("{:#?}", ast.as_ref().unwrap());
} else {
println!("()");
}
continue;
}

View File

@ -6,18 +6,16 @@ fn test_constant() -> Result<(), EvalAltResult> {
assert_eq!(engine.eval::<i64>("const x = 123; x")?, 123);
match engine.eval::<i64>("const x = 123; x = 42;") {
Err(EvalAltResult::ErrorAssignmentToConstant(var, _)) if var == "x" => (),
Err(err) => return Err(err),
Ok(_) => panic!("expecting compilation error"),
}
assert!(
matches!(engine.eval::<i64>("const x = 123; x = 42;").expect_err("expects error"),
EvalAltResult::ErrorAssignmentToConstant(var, _) if var == "x")
);
#[cfg(not(feature = "no_index"))]
match engine.eval::<i64>("const x = [1, 2, 3, 4, 5]; x[2] = 42;") {
Err(EvalAltResult::ErrorAssignmentToConstant(var, _)) if var == "x" => (),
Err(err) => return Err(err),
Ok(_) => panic!("expecting compilation error"),
}
assert!(
matches!(engine.eval::<i64>("const x = [1, 2, 3, 4, 5]; x[2] = 42;").expect_err("expects error"),
EvalAltResult::ErrorAssignmentToConstant(var, _) if var == "x")
);
Ok(())
}

View File

@ -6,12 +6,9 @@ fn test_decrement() -> Result<(), EvalAltResult> {
assert_eq!(engine.eval::<INT>("let x = 10; x -= 7; x")?, 3);
let r = engine.eval::<String>("let s = \"test\"; s -= \"ing\"; s");
match r {
Err(EvalAltResult::ErrorFunctionNotFound(err, _)) if err == "- (string, string)" => (),
_ => panic!(),
}
assert!(matches!(engine
.eval::<String>(r#"let s = "test"; s -= "ing"; s"#)
.expect_err("expects error"), EvalAltResult::ErrorFunctionNotFound(err, _) if err == "- (string, string)"));
Ok(())
}

View File

@ -24,54 +24,76 @@ fn test_math() -> Result<(), EvalAltResult> {
{
#[cfg(not(feature = "only_i32"))]
{
match engine.eval::<INT>("(-9223372036854775808).abs()") {
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
r => panic!("should return overflow error: {:?}", r),
}
match engine.eval::<INT>("9223372036854775807 + 1") {
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
r => panic!("should return overflow error: {:?}", r),
}
match engine.eval::<INT>("-9223372036854775808 - 1") {
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
r => panic!("should return underflow error: {:?}", r),
}
match engine.eval::<INT>("9223372036854775807 * 9223372036854775807") {
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
r => panic!("should return overflow error: {:?}", r),
}
match engine.eval::<INT>("9223372036854775807 / 0") {
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
r => panic!("should return division by zero error: {:?}", r),
}
match engine.eval::<INT>("9223372036854775807 % 0") {
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
r => panic!("should return division by zero error: {:?}", r),
}
assert!(matches!(
engine
.eval::<INT>("(-9223372036854775808).abs()")
.expect_err("expects negation overflow"),
EvalAltResult::ErrorArithmetic(_, _)
));
assert!(matches!(
engine
.eval::<INT>("9223372036854775807 + 1")
.expect_err("expects overflow"),
EvalAltResult::ErrorArithmetic(_, _)
));
assert!(matches!(
engine
.eval::<INT>("-9223372036854775808 - 1")
.expect_err("expects underflow"),
EvalAltResult::ErrorArithmetic(_, _)
));
assert!(matches!(
engine
.eval::<INT>("9223372036854775807 * 9223372036854775807")
.expect_err("expects overflow"),
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")]
{
match engine.eval::<INT>("2147483647 + 1") {
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
r => panic!("should return overflow error: {:?}", r),
}
match engine.eval::<INT>("-2147483648 - 1") {
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
r => panic!("should return underflow error: {:?}", r),
}
match engine.eval::<INT>("2147483647 * 2147483647") {
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
r => panic!("should return overflow error: {:?}", r),
}
match engine.eval::<INT>("2147483647 / 0") {
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
r => panic!("should return division by zero error: {:?}", r),
}
match engine.eval::<INT>("2147483647 % 0") {
Err(EvalAltResult::ErrorArithmetic(_, _)) => (),
r => panic!("should return division by zero error: {:?}", r),
}
assert!(matches!(
engine
.eval::<INT>("2147483647 + 1")
.expect_err("expects overflow"),
EvalAltResult::ErrorArithmetic(_, _)
));
assert!(matches!(
engine
.eval::<INT>("-2147483648 - 1")
.expect_err("expects underflow"),
EvalAltResult::ErrorArithmetic(_, _)
));
assert!(matches!(
engine
.eval::<INT>("2147483647 * 2147483647")
.expect_err("expects overflow"),
EvalAltResult::ErrorArithmetic(_, _)
));
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(_, _)
));
}
}

View File

@ -5,12 +5,10 @@ use rhai::{Engine, EvalAltResult, RegisterFn, INT};
fn test_mismatched_op() {
let mut engine = Engine::new();
let r = engine.eval::<INT>("60 + \"hello\"");
match r {
Err(EvalAltResult::ErrorMismatchOutputType(err, _)) if err == "string" => (),
_ => panic!(),
}
assert!(
matches!(engine.eval::<INT>(r#"60 + "hello""#).expect_err("expects error"),
EvalAltResult::ErrorMismatchOutputType(err, _) if err == "string")
);
}
#[test]
@ -30,14 +28,16 @@ fn test_mismatched_op_custom_type() {
engine.register_type_with_name::<TestStruct>("TestStruct");
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 {
#[cfg(feature = "only_i32")]
Err(EvalAltResult::ErrorFunctionNotFound(err, _)) if err == "+ (i32, TestStruct)" => (),
EvalAltResult::ErrorFunctionNotFound(err, _) if err == "+ (i32, TestStruct)" => (),
#[cfg(not(feature = "only_i32"))]
Err(EvalAltResult::ErrorFunctionNotFound(err, _)) if err == "+ (i64, TestStruct)" => (),
EvalAltResult::ErrorFunctionNotFound(err, _) if err == "+ (i64, TestStruct)" => (),
_ => panic!(),
}

View File

@ -1,18 +1,14 @@
use rhai::{Engine, EvalAltResult, INT};
use rhai::{Engine, EvalAltResult};
#[test]
fn test_throw() {
let mut engine = Engine::new();
match engine.eval::<INT>(r#"if true { throw "hello" }"#) {
Ok(_) => panic!("not an error"),
Err(EvalAltResult::ErrorRuntime(s, _)) if s == "hello" => (),
Err(err) => panic!("wrong error: {}", err),
}
assert!(matches!(
engine.eval::<()>(r#"if true { throw "hello" }"#).expect_err("expects error"),
EvalAltResult::ErrorRuntime(s, _) if s == "hello"));
match engine.eval::<INT>(r#"throw;"#) {
Ok(_) => panic!("not an error"),
Err(EvalAltResult::ErrorRuntime(s, _)) if s == "" => (),
Err(err) => panic!("wrong error: {}", err),
}
assert!(matches!(
engine.eval::<()>(r#"throw;"#).expect_err("expects error"),
EvalAltResult::ErrorRuntime(s, _) if s == ""));
}