Skip to content

Commit

Permalink
Update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ISibboI committed Oct 17, 2024
1 parent 3b19f2f commit 2b4b351
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 76 deletions.
12 changes: 6 additions & 6 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl<NumericTypes: EvalexprNumericTypes> HashMapContext<NumericTypes> {
/// ```rust
/// # use evalexpr::*;
///
/// let mut context = HashMapContext::new();
/// let mut context = HashMapContext::<DefaultNumericTypes>::new();
/// context.set_value("abc".into(), "def".into()).unwrap();
/// assert_eq!(context.get_value("abc"), Some(&("def".into())));
/// context.clear_variables();
Expand All @@ -273,7 +273,7 @@ impl<NumericTypes: EvalexprNumericTypes> HashMapContext<NumericTypes> {
/// ```rust
/// # use evalexpr::*;
///
/// let mut context = HashMapContext::new();
/// let mut context = HashMapContext::<DefaultNumericTypes>::new();
/// context.set_value("abc".into(), "def".into()).unwrap();
/// assert_eq!(context.get_value("abc"), Some(&("def".into())));
/// context.clear();
Expand Down Expand Up @@ -391,12 +391,12 @@ impl<NumericTypes: EvalexprNumericTypes> Default for HashMapContext<NumericTypes
/// ```rust
/// use evalexpr::*;
///
/// let ctx = evalexpr::context_map! {
/// "x" => 8,
/// "f" => Function::new(|_| Ok(42.into()))
/// let ctx: HashMapContext<DefaultNumericTypes> = context_map! {
/// "x" => int 8,
/// "f" => Function::new(|_| Ok(Value::from_int(42)))
/// }.unwrap(); // Do proper error handling here
///
/// assert_eq!(eval_with_context("x + f()", &ctx), Ok(50.into()));
/// assert_eq!(eval_with_context("x + f()", &ctx), Ok(Value::from_int(50)));
/// ```
#[macro_export]
macro_rules! context_map {
Expand Down
4 changes: 2 additions & 2 deletions src/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ where
/// ```rust
/// use evalexpr::*;
///
/// let mut context = HashMapContext::new();
/// let mut context = HashMapContext::<DefaultNumericTypes>::new();
/// context.set_function("id".into(), Function::new(|argument| {
/// Ok(argument.clone())
/// })).unwrap(); // Do proper error handling here
/// assert_eq!(eval_with_context("id(4)", &context), Ok(Value::from(4)));
/// assert_eq!(eval_with_context("id(4)", &context), Ok(Value::from_int(4)));
/// ```
pub struct Function<NumericTypes: EvalexprNumericTypes> {
function: Box<dyn ClonableFn<NumericTypes>>,
Expand Down
36 changes: 18 additions & 18 deletions src/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
/// ```rust
/// use evalexpr::*;
///
/// assert_eq!(eval("1 + 2 + 3"), Ok(Value::from(6)));
/// assert_eq!(eval("1 + 2 + 3"), Ok(Value::from_int(6)));
/// ```
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
Expand All @@ -31,11 +31,11 @@ pub fn eval(string: &str) -> EvalexprResultValue {
/// ```rust
/// use evalexpr::*;
///
/// let mut context = HashMapContext::new();
/// context.set_value("one".into(), 1.into()).unwrap(); // Do proper error handling here
/// context.set_value("two".into(), 2.into()).unwrap(); // Do proper error handling here
/// context.set_value("three".into(), 3.into()).unwrap(); // Do proper error handling here
/// assert_eq!(eval_with_context("one + two + three", &context), Ok(Value::from(6)));
/// let mut context = HashMapContext::<DefaultNumericTypes>::new();
/// context.set_value("one".into(), Value::from_int(1)).unwrap(); // Do proper error handling here
/// context.set_value("two".into(), Value::from_int(2)).unwrap(); // Do proper error handling here
/// context.set_value("three".into(), Value::from_int(3)).unwrap(); // Do proper error handling here
/// assert_eq!(eval_with_context("one + two + three", &context), Ok(Value::from_int(6)));
/// ```
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
Expand All @@ -53,11 +53,11 @@ pub fn eval_with_context<C: Context>(
/// ```rust
/// use evalexpr::*;
///
/// let mut context = HashMapContext::new();
/// context.set_value("one".into(), 1.into()).unwrap(); // Do proper error handling here
/// context.set_value("two".into(), 2.into()).unwrap(); // Do proper error handling here
/// context.set_value("three".into(), 3.into()).unwrap(); // Do proper error handling here
/// assert_eq!(eval_with_context_mut("one + two + three", &mut context), Ok(Value::from(6)));
/// let mut context = HashMapContext::<DefaultNumericTypes>::new();
/// context.set_value("one".into(), Value::from_int(1)).unwrap(); // Do proper error handling here
/// context.set_value("two".into(), Value::from_int(2)).unwrap(); // Do proper error handling here
/// context.set_value("three".into(), Value::from_int(3)).unwrap(); // Do proper error handling here
/// assert_eq!(eval_with_context_mut("one + two + three", &mut context), Ok(Value::from_int(6)));
/// ```
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
Expand All @@ -80,15 +80,15 @@ pub fn eval_with_context_mut<C: ContextWithMutableVariables>(
///
/// let precomputed = build_operator_tree("one + two + three").unwrap(); // Do proper error handling here
///
/// let mut context = HashMapContext::new();
/// context.set_value("one".into(), 1.into()).unwrap(); // Do proper error handling here
/// context.set_value("two".into(), 2.into()).unwrap(); // Do proper error handling here
/// context.set_value("three".into(), 3.into()).unwrap(); // Do proper error handling here
/// let mut context = HashMapContext::<DefaultNumericTypes>::new();
/// context.set_value("one".into(), Value::from_int(1)).unwrap(); // Do proper error handling here
/// context.set_value("two".into(), Value::from_int(2)).unwrap(); // Do proper error handling here
/// context.set_value("three".into(), Value::from_int(3)).unwrap(); // Do proper error handling here
///
/// assert_eq!(precomputed.eval_with_context(&context), Ok(Value::from(6)));
/// assert_eq!(precomputed.eval_with_context(&context), Ok(Value::from_int(6)));
///
/// context.set_value("three".into(), 5.into()).unwrap(); // Do proper error handling here
/// assert_eq!(precomputed.eval_with_context(&context), Ok(Value::from(8)));
/// context.set_value("three".into(), Value::from_int(5)).unwrap(); // Do proper error handling here
/// assert_eq!(precomputed.eval_with_context(&context), Ok(Value::from_int(8)));
/// ```
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
Expand Down
74 changes: 37 additions & 37 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
//! ```rust
//! use evalexpr::*;
//!
//! assert_eq!(eval("1 + 2 + 3"), Ok(Value::from(6)));
//! assert_eq!(eval("1 + 2 + 3"), Ok(Value::from_int(6)));
//! // `eval` returns a variant of the `Value` enum,
//! // while `eval_[type]` returns the respective type directly.
//! // Both can be used interchangeably.
//! assert_eq!(eval_int("1 + 2 + 3"), Ok(6));
//! assert_eq!(eval("1 /* inline comments are supported */ - 2 * 3 // as are end-of-line comments"), Ok(Value::from(-5)));
//! assert_eq!(eval("1.0 + 2 * 3"), Ok(Value::from(7.0)));
//! assert_eq!(eval("1 /* inline comments are supported */ - 2 * 3 // as are end-of-line comments"), Ok(Value::from_int(-5)));
//! assert_eq!(eval("1.0 + 2 * 3"), Ok(Value::from_float(7.0)));
//! assert_eq!(eval("true && 4 > 2"), Ok(Value::from(true)));
//! ```
//!
Expand All @@ -28,14 +28,14 @@
//! ```rust
//! use evalexpr::*;
//!
//! let mut context = HashMapContext::new();
//! let mut context = HashMapContext::<DefaultNumericTypes>::new();
//! // Assign 5 to a like this
//! assert_eq!(eval_empty_with_context_mut("a = 5", &mut context), Ok(EMPTY_VALUE));
//! // The HashMapContext is type safe, so this will fail now
//! assert_eq!(eval_empty_with_context_mut("a = 5.0", &mut context),
//! Err(EvalexprError::expected_int(Value::from(5.0))));
//! Err(EvalexprError::expected_int(Value::from_float(5.0))));
//! // We can check which value the context stores for a like this
//! assert_eq!(context.get_value("a"), Some(&Value::from(5)));
//! assert_eq!(context.get_value("a"), Some(&Value::from_int(5)));
//! // And use the value in another expression like this
//! assert_eq!(eval_int_with_context_mut("a = a + 2; a", &mut context), Ok(7));
//! // It is also possible to save a bit of typing by using an operator-assignment operator
Expand All @@ -47,9 +47,9 @@
//! ```rust
//! use evalexpr::*;
//!
//! let context = context_map! {
//! "five" => 5,
//! "twelve" => 12,
//! let context: HashMapContext<DefaultNumericTypes> = context_map! {
//! "five" => int 5,
//! "twelve" => int 12,
//! "f" => Function::new(|argument| {
//! if let Ok(int) = argument.as_int() {
//! Ok(Value::Int(int / 2))
Expand Down Expand Up @@ -83,16 +83,16 @@
//! ```rust
//! use evalexpr::*;
//!
//! let precompiled = build_operator_tree("a * b - c > 5").unwrap(); // Do proper error handling here
//! let precompiled = build_operator_tree::<DefaultNumericTypes>("a * b - c > 5").unwrap(); // Do proper error handling here
//!
//! let mut context = context_map! {
//! "a" => 6,
//! "b" => 2,
//! "c" => 3
//! "a" => int 6,
//! "b" => int 2,
//! "c" => int 3,
//! }.unwrap(); // Do proper error handling here
//! assert_eq!(precompiled.eval_with_context(&context), Ok(Value::from(true)));
//!
//! context.set_value("c".into(), 8.into()).unwrap(); // Do proper error handling here
//! context.set_value("c".into(), Value::from_int(8)).unwrap(); // Do proper error handling here
//! assert_eq!(precompiled.eval_with_context(&context), Ok(Value::from(false)));
//! // `Node::eval_with_context` returns a variant of the `Value` enum,
//! // while `Node::eval_[type]_with_context` returns the respective type directly.
Expand Down Expand Up @@ -165,9 +165,9 @@
//! ```rust
//! use evalexpr::*;
//!
//! assert_eq!(eval("1 / 2"), Ok(Value::from(0)));
//! assert_eq!(eval("1.0 / 2"), Ok(Value::from(0.5)));
//! assert_eq!(eval("2^2"), Ok(Value::from(4.0)));
//! assert_eq!(eval("1 / 2"), Ok(Value::from_int(0)));
//! assert_eq!(eval("1.0 / 2"), Ok(Value::from_float(0.5)));
//! assert_eq!(eval("2^2"), Ok(Value::from_float(4.0)));
//! ```
//!
//! #### The Aggregation Operator
Expand All @@ -181,7 +181,7 @@
//! use evalexpr::*;
//!
//! assert_eq!(eval("1, \"b\", 3"),
//! Ok(Value::from(vec![Value::from(1), Value::from("b"), Value::from(3)])));
//! Ok(Value::from(vec![Value::from_int(1), Value::from("b"), Value::from_int(3)])));
//! ```
//!
//! To create nested tuples, use parentheses:
Expand All @@ -190,8 +190,8 @@
//! use evalexpr::*;
//!
//! assert_eq!(eval("1, 2, (true, \"b\")"), Ok(Value::from(vec![
//! Value::from(1),
//! Value::from(2),
//! Value::from_int(1),
//! Value::from_int(2),
//! Value::from(vec![
//! Value::from(true),
//! Value::from("b")
Expand All @@ -210,13 +210,13 @@
//! ```rust
//! use evalexpr::*;
//!
//! let mut context = HashMapContext::new();
//! let mut context = HashMapContext::<DefaultNumericTypes>::new();
//! assert_eq!(eval_with_context("a = 5", &context), Err(EvalexprError::ContextNotMutable));
//! assert_eq!(eval_empty_with_context_mut("a = 5", &mut context), Ok(EMPTY_VALUE));
//! assert_eq!(eval_empty_with_context_mut("a = 5.0", &mut context),
//! Err(EvalexprError::expected_int(5.0.into())));
//! Err(EvalexprError::expected_int(Value::from_float(5.0))));
//! assert_eq!(eval_int_with_context("a", &context), Ok(5));
//! assert_eq!(context.get_value("a"), Some(5.into()).as_ref());
//! assert_eq!(context.get_value("a"), Some(Value::from_int(5)).as_ref());
//! ```
//!
//! For each binary operator, there exists an equivalent operator-assignment operator.
Expand All @@ -241,9 +241,9 @@
//! ```rust
//! use evalexpr::*;
//!
//! let mut context = HashMapContext::new();
//! let mut context = HashMapContext::<DefaultNumericTypes>::new();
//! assert_eq!(eval("1;2;3;4;"), Ok(Value::Empty));
//! assert_eq!(eval("1;2;3;4"), Ok(4.into()));
//! assert_eq!(eval("1;2;3;4"), Ok(Value::from_int(4)));
//!
//! // Initialization of variables via script
//! assert_eq!(eval_empty_with_context_mut("hp = 1; max_hp = 5; heal_amount = 3;", &mut context),
Expand Down Expand Up @@ -271,15 +271,15 @@
//! // The context is not preserved between eval calls
//! assert_eq!(eval("a"), Err(EvalexprError::VariableIdentifierNotFound("a".to_string())));
//!
//! let mut context = HashMapContext::new();
//! let mut context = HashMapContext::<DefaultNumericTypes>::new();
//! assert_eq!(eval_with_context_mut("a = 5;", &mut context), Ok(Value::from(())));
//! // Assignments require mutable contexts
//! assert_eq!(eval_with_context("a = 6", &context), Err(EvalexprError::ContextNotMutable));
//! // The HashMapContext is type safe
//! assert_eq!(eval_with_context_mut("a = 5.5", &mut context),
//! Err(EvalexprError::ExpectedInt { actual: Value::from(5.5) }));
//! Err(EvalexprError::ExpectedInt { actual: Value::from_float(5.5) }));
//! // Reading a variable does not require a mutable context
//! assert_eq!(eval_with_context("a", &context), Ok(Value::from(5)));
//! assert_eq!(eval_with_context("a", &context), Ok(Value::from_int(5)));
//!
//! ```
//!
Expand All @@ -299,16 +299,16 @@
//! ```rust
//! use evalexpr::*;
//!
//! let mut context = HashMapContext::new();
//! let mut context = HashMapContext::<DefaultNumericTypes>::new();
//! // We can set variables in code like this...
//! context.set_value("a".into(), 5.into());
//! context.set_value("a".into(), Value::from_int(5));
//! // ...and read from them in expressions
//! assert_eq!(eval_int_with_context("a", &context), Ok(5));
//! // We can write or overwrite variables in expressions...
//! assert_eq!(eval_with_context_mut("a = 10; b = 1.0;", &mut context), Ok(().into()));
//! // ...and read the value in code like this
//! assert_eq!(context.get_value("a"), Some(&Value::from(10)));
//! assert_eq!(context.get_value("b"), Some(&Value::from(1.0)));
//! assert_eq!(context.get_value("a"), Some(&Value::from_int(10)));
//! assert_eq!(context.get_value("b"), Some(&Value::from_float(1.0)));
//! ```
//!
//! Contexts are also required for user-defined functions.
Expand All @@ -317,8 +317,8 @@
//! ```rust
//! use evalexpr::*;
//!
//! let context = context_map!{
//! "f" => Function::new(|args| Ok(Value::from(args.as_int()? + 5))),
//! let context: HashMapContext<DefaultNumericTypes> = context_map!{
//! "f" => Function::new(|args| Ok(Value::from_int(args.as_int()? + 5))),
//! }.unwrap_or_else(|error| panic!("Error creating context: {}", error));
//! assert_eq!(eval_int_with_context("f 5", &context), Ok(10));
//! ```
Expand All @@ -332,8 +332,8 @@
//!
//! ```rust
//! use evalexpr::*;
//! let mut context = HashMapContext::new();
//! assert_eq!(eval_with_context("max(1,3)",&context),Ok(Value::from(3)));
//! let mut context = HashMapContext::<DefaultNumericTypes>::new();
//! assert_eq!(eval_with_context("max(1,3)",&context),Ok(Value::from_int(3)));
//! context.set_builtin_functions_disabled(true).unwrap(); // Do proper error handling here
//! assert_eq!(eval_with_context("max(1,3)",&context),Err(EvalexprError::FunctionIdentifierNotFound(String::from("max"))));
//! ```
Expand Down Expand Up @@ -550,7 +550,7 @@
//! // In ron format, strings are surrounded by "
//! let serialized_free = "\"five * five\"";
//! match ron::de::from_str::<Node>(serialized_free) {
//! Ok(free) => assert_eq!(free.eval_with_context(&context), Ok(Value::from(25))),
//! Ok(free) => assert_eq!(free.eval_with_context(&context), Ok(Value::from_int(25))),
//! Err(error) => {
//! () // Handle error
//! }
Expand Down
Loading

0 comments on commit 2b4b351

Please sign in to comment.