Use ? operator in tests.
This commit is contained in:
parent
63482d5a79
commit
5b5fd162be
@ -1,4 +1,4 @@
|
|||||||
use rhai::{Engine, RegisterFn};
|
use rhai::{Engine, EvalAltResult, RegisterFn};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct TestStruct {
|
struct TestStruct {
|
||||||
@ -15,7 +15,7 @@ impl TestStruct {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() -> Result<(), EvalAltResult> {
|
||||||
let mut engine = Engine::new();
|
let mut engine = Engine::new();
|
||||||
|
|
||||||
engine.register_type::<TestStruct>();
|
engine.register_type::<TestStruct>();
|
||||||
@ -23,7 +23,9 @@ fn main() {
|
|||||||
engine.register_fn("update", TestStruct::update);
|
engine.register_fn("update", TestStruct::update);
|
||||||
engine.register_fn("new_ts", TestStruct::new);
|
engine.register_fn("new_ts", TestStruct::new);
|
||||||
|
|
||||||
if let Ok(result) = engine.eval::<TestStruct>("let x = new_ts(); x.update(); x") {
|
let result = engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")?;
|
||||||
|
|
||||||
println!("result: {}", result.x); // prints 1001
|
println!("result: {}", result.x); // prints 1001
|
||||||
}
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ use std::any::TypeId;
|
|||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
|
/// # fn main() -> Result<(), rhai::EvalAltResult> {
|
||||||
/// use rhai::{Engine, RegisterFn};
|
/// use rhai::{Engine, RegisterFn};
|
||||||
///
|
///
|
||||||
/// // Normal function
|
/// // Normal function
|
||||||
@ -23,9 +24,11 @@ use std::any::TypeId;
|
|||||||
/// // You must use the trait rhai::RegisterFn to get this method.
|
/// // You must use the trait rhai::RegisterFn to get this method.
|
||||||
/// engine.register_fn("add", add);
|
/// engine.register_fn("add", add);
|
||||||
///
|
///
|
||||||
/// if let Ok(result) = engine.eval::<i64>("add(40, 2)") {
|
/// let result = engine.eval::<i64>("add(40, 2)")?;
|
||||||
|
///
|
||||||
/// println!("Answer: {}", result); // prints 42
|
/// println!("Answer: {}", result); // prints 42
|
||||||
/// }
|
/// # Ok(())
|
||||||
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub trait RegisterFn<FN, ARGS, RET> {
|
pub trait RegisterFn<FN, ARGS, RET> {
|
||||||
/// Register a custom function with the `Engine`.
|
/// Register a custom function with the `Engine`.
|
||||||
@ -37,7 +40,8 @@ pub trait RegisterFn<FN, ARGS, RET> {
|
|||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use rhai::{Engine, RegisterDynamicFn, Dynamic};
|
/// # fn main() -> Result<(), rhai::EvalAltResult> {
|
||||||
|
/// use rhai::{Engine, Dynamic, RegisterDynamicFn};
|
||||||
///
|
///
|
||||||
/// // Function that returns a Dynamic value
|
/// // Function that returns a Dynamic value
|
||||||
/// fn get_an_any(x: i64) -> Dynamic {
|
/// fn get_an_any(x: i64) -> Dynamic {
|
||||||
@ -49,9 +53,11 @@ pub trait RegisterFn<FN, ARGS, RET> {
|
|||||||
/// // You must use the trait rhai::RegisterDynamicFn to get this method.
|
/// // You must use the trait rhai::RegisterDynamicFn to get this method.
|
||||||
/// engine.register_dynamic_fn("get_an_any", get_an_any);
|
/// engine.register_dynamic_fn("get_an_any", get_an_any);
|
||||||
///
|
///
|
||||||
/// if let Ok(result) = engine.eval::<i64>("get_an_any(42)") {
|
/// let result = engine.eval::<i64>("get_an_any(42)")?;
|
||||||
|
///
|
||||||
/// println!("Answer: {}", result); // prints 42
|
/// println!("Answer: {}", result); // prints 42
|
||||||
/// }
|
/// # Ok(())
|
||||||
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub trait RegisterDynamicFn<FN, ARGS> {
|
pub trait RegisterDynamicFn<FN, ARGS> {
|
||||||
/// Register a custom function returning `Dynamic` values with the `Engine`.
|
/// Register a custom function returning `Dynamic` values with the `Engine`.
|
||||||
@ -63,6 +69,7 @@ pub trait RegisterDynamicFn<FN, ARGS> {
|
|||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
|
/// # fn main() -> Result<(), rhai::EvalAltResult> {
|
||||||
/// use rhai::{Engine, RegisterFn};
|
/// use rhai::{Engine, RegisterFn};
|
||||||
///
|
///
|
||||||
/// // Normal function
|
/// // Normal function
|
||||||
@ -75,9 +82,11 @@ pub trait RegisterDynamicFn<FN, ARGS> {
|
|||||||
/// // You must use the trait rhai::RegisterFn to get this method.
|
/// // You must use the trait rhai::RegisterFn to get this method.
|
||||||
/// engine.register_fn("add", add);
|
/// engine.register_fn("add", add);
|
||||||
///
|
///
|
||||||
/// if let Ok(result) = engine.eval::<i64>("add(40, 2)") {
|
/// let result = engine.eval::<i64>("add(40, 2)")?;
|
||||||
|
///
|
||||||
/// println!("Answer: {}", result); // prints 42
|
/// println!("Answer: {}", result); // prints 42
|
||||||
/// }
|
/// # Ok(())
|
||||||
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub trait RegisterResultFn<FN, ARGS, RET> {
|
pub trait RegisterResultFn<FN, ARGS, RET> {
|
||||||
/// Register a custom function with the `Engine`.
|
/// Register a custom function with the `Engine`.
|
||||||
|
12
src/lib.rs
12
src/lib.rs
@ -17,15 +17,22 @@
|
|||||||
//! And the Rust part:
|
//! And the Rust part:
|
||||||
//!
|
//!
|
||||||
//! ```rust,no_run
|
//! ```rust,no_run
|
||||||
//! use rhai::{Engine, RegisterFn};
|
//! use rhai::{Engine, EvalAltResult, RegisterFn};
|
||||||
//!
|
//!
|
||||||
|
//! fn main() -> Result<(), EvalAltResult>
|
||||||
|
//! {
|
||||||
//! fn compute_something(x: i64) -> bool {
|
//! fn compute_something(x: i64) -> bool {
|
||||||
//! (x % 40) == 0
|
//! (x % 40) == 0
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! let mut engine = Engine::new();
|
//! let mut engine = Engine::new();
|
||||||
|
//!
|
||||||
//! engine.register_fn("compute_something", compute_something);
|
//! engine.register_fn("compute_something", compute_something);
|
||||||
//! assert_eq!(engine.eval_file::<bool>("my_script.rhai").unwrap(), true);
|
//!
|
||||||
|
//! assert_eq!(engine.eval_file::<bool>("my_script.rhai")?, true);
|
||||||
|
//!
|
||||||
|
//! Ok(())
|
||||||
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! [Check out the README on GitHub for more information!](https://github.com/jonathandturner/rhai)
|
//! [Check out the README on GitHub for more information!](https://github.com/jonathandturner/rhai)
|
||||||
@ -61,6 +68,7 @@ mod call;
|
|||||||
mod engine;
|
mod engine;
|
||||||
mod error;
|
mod error;
|
||||||
mod fn_register;
|
mod fn_register;
|
||||||
|
mod optimize;
|
||||||
mod parser;
|
mod parser;
|
||||||
mod result;
|
mod result;
|
||||||
mod scope;
|
mod scope;
|
||||||
|
@ -9,13 +9,17 @@ use std::borrow::Cow;
|
|||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
|
/// # fn main() -> Result<(), rhai::EvalAltResult> {
|
||||||
/// use rhai::{Engine, Scope};
|
/// use rhai::{Engine, Scope};
|
||||||
///
|
///
|
||||||
/// let mut engine = Engine::new();
|
/// let mut engine = Engine::new();
|
||||||
/// let mut my_scope = Scope::new();
|
/// let mut my_scope = Scope::new();
|
||||||
///
|
///
|
||||||
/// assert!(engine.eval_with_scope::<()>(&mut my_scope, "let x = 5;").is_ok());
|
/// engine.eval_with_scope::<()>(&mut my_scope, "let x = 5;")?;
|
||||||
/// assert_eq!(engine.eval_with_scope::<i64>(&mut my_scope, "x + 1").unwrap(), 6);
|
///
|
||||||
|
/// assert_eq!(engine.eval_with_scope::<i64>(&mut my_scope, "x + 1")?, 6);
|
||||||
|
/// # Ok(())
|
||||||
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// When searching for variables, newly-added variables are found before similarly-named but older variables,
|
/// When searching for variables, newly-added variables are found before similarly-named but older variables,
|
||||||
|
@ -4,7 +4,7 @@ use rhai::{Engine, EvalAltResult};
|
|||||||
fn test_engine_call_fn() -> Result<(), EvalAltResult> {
|
fn test_engine_call_fn() -> Result<(), EvalAltResult> {
|
||||||
let mut engine = Engine::new();
|
let mut engine = Engine::new();
|
||||||
|
|
||||||
let ast = Engine::compile("fn hello(x, y) { x.len() + y }")?;
|
let ast = engine.compile("fn hello(x, y) { x.len() + y }")?;
|
||||||
|
|
||||||
let result: i64 = engine.call_fn("hello", &ast, (&mut String::from("abc"), &mut 123_i64))?;
|
let result: i64 = engine.call_fn("hello", &ast, (&mut String::from("abc"), &mut 123_i64))?;
|
||||||
|
|
||||||
|
@ -40,9 +40,9 @@ fn test_scope_eval() -> Result<(), EvalAltResult> {
|
|||||||
.expect("y and z not found?");
|
.expect("y and z not found?");
|
||||||
|
|
||||||
// Second invocation using the same state
|
// Second invocation using the same state
|
||||||
if let Ok(result) = engine.eval_with_scope::<i64>(&mut scope, "x") {
|
let result = engine.eval_with_scope::<i64>(&mut scope, "x")?;
|
||||||
|
|
||||||
println!("result: {}", result); // should print 966
|
println!("result: {}", result); // should print 966
|
||||||
}
|
|
||||||
|
|
||||||
// Variable y is changed in the script
|
// Variable y is changed in the script
|
||||||
assert_eq!(scope.get_value::<i64>("y").unwrap(), 1);
|
assert_eq!(scope.get_value::<i64>("y").unwrap(), 1);
|
||||||
|
Loading…
Reference in New Issue
Block a user