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

View File

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

View File

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

View File

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

View File

@ -5,8 +5,8 @@ fn test_or_equals() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new(); let engine = Engine::new();
assert_eq!(engine.eval::<INT>("let x = 16; x |= 74; x")?, 90); 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 = true; x |= false; x")?);
assert!(engine.eval::<bool>("let x = false; x |= true; x").unwrap()); assert!(engine.eval::<bool>("let x = false; x |= true; x")?);
Ok(()) Ok(())
} }
@ -16,9 +16,9 @@ fn test_and_equals() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new(); let engine = Engine::new();
assert_eq!(engine.eval::<INT>("let x = 16; x &= 31; x")?, 16); 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 = true; x &= false; x")?);
assert!(!engine.eval::<bool>("let x = false; x &= true; x").unwrap()); assert!(!engine.eval::<bool>("let x = false; x &= true; x")?);
assert!(engine.eval::<bool>("let x = true; x &= true; x").unwrap()); assert!(engine.eval::<bool>("let x = true; x &= true; x")?);
Ok(()) 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.0; let y = 1.0; x > y")?); assert!(!engine.eval::<bool>("let x = 0.0; let y = 1.0; x > y")?);
assert!(!engine assert!(!engine.eval::<bool>("let x = 0.; let y = 1.; x > y")?);
.eval::<bool>("let x = 0.; let y = 1.; x > y")
.unwrap());
assert!((engine.eval::<FLOAT>("let x = 9.9999; x")? - 9.9999 as FLOAT).abs() < EPSILON); assert!((engine.eval::<FLOAT>("let x = 9.9999; x")? - 9.9999 as FLOAT).abs() < EPSILON);
Ok(()) Ok(())

View File

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

View File

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

View File

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