Use ? in tests.

This commit is contained in:
Stephen Chung 2023-02-13 08:59:58 +08:00
parent 7da20dd090
commit 7fc72e8c28
9 changed files with 41 additions and 57 deletions

View File

@ -515,14 +515,12 @@ fn test_arrays_map_reduce() -> Result<(), Box<EvalAltResult>> {
3
);
engine
.eval::<()>(
engine.eval::<()>(
"
let x = [1, 2, 3, 2, 1];
x.find(|v| v > 4)
",
)
.unwrap();
)?;
assert_eq!(
engine.eval::<INT>(
@ -534,14 +532,12 @@ fn test_arrays_map_reduce() -> Result<(), Box<EvalAltResult>> {
2
);
engine
.eval::<()>(
engine.eval::<()>(
"
let x = [#{alice: 1}, #{bob: 2}, #{clara: 3}];
x.find_map(|v| v.dave)
",
)
.unwrap();
)?;
Ok(())
}
@ -550,7 +546,7 @@ fn test_arrays_map_reduce() -> Result<(), Box<EvalAltResult>> {
fn test_arrays_elvis() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
engine.eval::<()>("let x = (); x?[2]").unwrap();
engine.eval::<()>("let x = (); x?[2]")?;
engine.run("let x = (); x?[2] = 42")?;

View File

@ -4,8 +4,8 @@ use rhai::{Engine, EvalAltResult};
fn test_bool_op1() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert!(engine.eval::<bool>("true && (false || true)").unwrap());
assert!(engine.eval::<bool>("true & (false | true)").unwrap());
assert!(engine.eval::<bool>("true && (false || true)")?);
assert!(engine.eval::<bool>("true & (false | true)")?);
Ok(())
}
@ -14,8 +14,8 @@ fn test_bool_op1() -> Result<(), Box<EvalAltResult>> {
fn test_bool_op2() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert!(!engine.eval::<bool>("false && (false || true)").unwrap());
assert!(!engine.eval::<bool>("false & (false | true)").unwrap());
assert!(!engine.eval::<bool>("false && (false || true)")?);
assert!(!engine.eval::<bool>("false & (false | true)")?);
Ok(())
}
@ -25,9 +25,9 @@ fn test_bool_op3() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert!(engine.eval::<bool>("true && (false || 123)").is_err());
assert!(engine.eval::<bool>("true && (true || { throw })").unwrap());
assert!(engine.eval::<bool>("true && (true || { throw })")?);
assert!(engine.eval::<bool>("123 && (false || true)").is_err());
assert!(!engine.eval::<bool>("false && (true || { throw })").unwrap());
assert!(!engine.eval::<bool>("false && (true || { throw })")?);
Ok(())
}
@ -36,23 +36,19 @@ fn test_bool_op3() -> Result<(), Box<EvalAltResult>> {
fn test_bool_op_short_circuit() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert!(engine
.eval::<bool>(
assert!(engine.eval::<bool>(
"
let x = true;
x || { throw; };
"
)
.unwrap());
)?);
assert!(!engine
.eval::<bool>(
assert!(!engine.eval::<bool>(
"
let x = false;
x && { throw; };
"
)
.unwrap());
)?);
Ok(())
}

View File

@ -369,9 +369,9 @@ fn test_closures_external() -> Result<(), Box<EvalAltResult>> {
let fn_ptr = engine.eval_ast::<FnPtr>(&ast)?;
let f = move |x: INT| -> String { fn_ptr.call(&engine, &ast, (x,)).unwrap() };
let f = move |x: INT| fn_ptr.call::<String>(&engine, &ast, (x,));
assert_eq!(f(42), "hello42");
assert_eq!(f(42)?, "hello42");
Ok(())
}
@ -383,20 +383,20 @@ fn test_closures_callback() -> Result<(), Box<EvalAltResult>> {
type SingleNode = Rc<dyn Node>;
trait Node {
fn run(&self, x: INT) -> INT;
fn run(&self, x: INT) -> Result<INT, Box<EvalAltResult>>;
}
struct PhaserNode {
func: Box<dyn Fn(INT) -> INT>,
func: Box<dyn Fn(INT) -> Result<INT, Box<EvalAltResult>>>,
}
impl Node for PhaserNode {
fn run(&self, x: INT) -> INT {
fn run(&self, x: INT) -> Result<INT, Box<EvalAltResult>> {
(self.func)(x)
}
}
fn phaser(callback: impl Fn(INT) -> INT + 'static) -> impl Node {
fn phaser(callback: impl Fn(INT) -> Result<INT, Box<EvalAltResult>> + 'static) -> impl Node {
PhaserNode {
func: Box::new(callback),
}
@ -419,7 +419,7 @@ fn test_closures_callback() -> Result<(), Box<EvalAltResult>> {
let engine = engine2.clone();
let ast = ast2.clone();
let callback = Box::new(move |x: INT| fp.call(&engine.borrow(), &ast, (x,)).unwrap());
let callback = Box::new(move |x: INT| fp.call(&engine.borrow(), &ast, (x,)));
Rc::new(phaser(callback)) as SingleNode
});
@ -428,7 +428,7 @@ fn test_closures_callback() -> Result<(), Box<EvalAltResult>> {
let cb = shared_engine.borrow().eval_ast::<SingleNode>(&ast)?;
assert_eq!(cb.run(21), 42);
assert_eq!(cb.run(21)?, 42);
Ok(())
}

View File

@ -21,7 +21,7 @@ fn test_comments() -> Result<(), Box<EvalAltResult>> {
42
);
engine.run("/* Hello world */").unwrap();
engine.run("/* Hello world */")?;
Ok(())
}

View File

@ -5,8 +5,8 @@ fn test_or_equals() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert_eq!(engine.eval::<INT>("let x = 16; x |= 74; x")?, 90);
assert!(engine.eval::<bool>("let x = true; x |= false; x").unwrap());
assert!(engine.eval::<bool>("let x = false; x |= true; x").unwrap());
assert!(engine.eval::<bool>("let x = true; x |= false; x")?);
assert!(engine.eval::<bool>("let x = false; x |= true; x")?);
Ok(())
}
@ -16,9 +16,9 @@ fn test_and_equals() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert_eq!(engine.eval::<INT>("let x = 16; x &= 31; x")?, 16);
assert!(!engine.eval::<bool>("let x = true; x &= false; x").unwrap());
assert!(!engine.eval::<bool>("let x = false; x &= true; x").unwrap());
assert!(engine.eval::<bool>("let x = true; x &= true; x").unwrap());
assert!(!engine.eval::<bool>("let x = true; x &= false; x")?);
assert!(!engine.eval::<bool>("let x = false; x &= true; x")?);
assert!(engine.eval::<bool>("let x = true; x &= true; x")?);
Ok(())
}

View File

@ -9,9 +9,7 @@ fn test_float() -> Result<(), Box<EvalAltResult>> {
assert!(engine.eval::<bool>("let x = 0.0; let y = 1.0; x < y")?);
assert!(!engine.eval::<bool>("let x = 0.0; let y = 1.0; x > y")?);
assert!(!engine
.eval::<bool>("let x = 0.; let y = 1.; x > y")
.unwrap());
assert!(!engine.eval::<bool>("let x = 0.; let y = 1.; x > y")?);
assert!((engine.eval::<FLOAT>("let x = 9.9999; x")? - 9.9999 as FLOAT).abs() < EPSILON);
Ok(())

View File

@ -399,11 +399,9 @@ fn test_get_set_indexer() -> Result<(), Box<EvalAltResult>> {
fn test_get_set_elvis() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
engine.eval::<()>("let x = (); x?.foo.bar.baz").unwrap();
engine.eval::<()>("let x = (); x?.foo(1,2,3)").unwrap();
engine
.eval::<()>("let x = #{a:()}; x.a?.foo.bar.baz")
.unwrap();
engine.eval::<()>("let x = (); x?.foo.bar.baz")?;
engine.eval::<()>("let x = (); x?.foo(1,2,3)")?;
engine.eval::<()>("let x = #{a:()}; x.a?.foo.bar.baz")?;
assert_eq!(engine.eval::<String>("let x = 'x'; x?.type_of()")?, "char");
Ok(())

View File

@ -4,14 +4,12 @@ use rhai::{Engine, EvalAltResult};
fn test_not() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert!(!engine
.eval::<bool>("let not_true = !true; not_true")
.unwrap());
assert!(!engine.eval::<bool>("let not_true = !true; not_true")?);
#[cfg(not(feature = "no_function"))]
assert!(engine.eval::<bool>("fn not(x) { !x } not(false)").unwrap());
assert!(engine.eval::<bool>("fn not(x) { !x } not(false)")?);
assert!(engine.eval::<bool>("!!!!true").unwrap());
assert!(engine.eval::<bool>("!!!!true")?);
Ok(())
}

View File

@ -10,9 +10,7 @@ fn test_unit() -> Result<(), Box<EvalAltResult>> {
#[test]
fn test_unit_eq() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert!(engine
.eval::<bool>("let x = (); let y = (); x == y")
.unwrap());
assert!(engine.eval::<bool>("let x = (); let y = (); x == y")?);
Ok(())
}