From d4359d71bfe092dfc074e815c6134e7925fd75ab Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Wed, 20 Mar 2019 17:06:47 +0200 Subject: [PATCH] Document the shortcut functions for direct evaluation into types Related to #15 --- src/lib.rs | 12 ++++++++++++ tests/integration.rs | 23 +++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 5b892df..58a5afb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,10 @@ //! use evalexpr::*; //! //! assert_eq!(eval("1 + 2 + 3"), Ok(Value::from(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 - 2 * 3"), Ok(Value::from(-5))); //! assert_eq!(eval("1.0 + 2 * 3"), Ok(Value::from(7.0))); //! assert_eq!(eval("true && 4 > 2"), Ok(Value::from(true))); @@ -55,6 +59,10 @@ //! }))); //! //! assert_eq!(eval_with_configuration("five + 8 > f(twelve)", &configuration), Ok(Value::from(true))); +//! // `eval_with_configuration` returns a variant of the `Value` enum, +//! // while `eval_[type]_with_configuration` returns the respective type directly. +//! // Both can be used interchangeably. +//! assert_eq!(eval_boolean_with_configuration("five + 8 > f(twelve)", &configuration), Ok(true)); //! assert_eq!(eval_with_configuration("avg(2, 4) == 3", &configuration), Ok(Value::from(true))); //! ``` //! @@ -73,6 +81,10 @@ //! //! configuration.insert_variable("c", 8); //! assert_eq!(precompiled.eval_with_configuration(&configuration), Ok(Value::from(false))); +//! // `Node::eval_with_configuration` returns a variant of the `Value` enum, +//! // while `Node::eval_[type]_with_configuration` returns the respective type directly. +//! // Both can be used interchangeably. +//! assert_eq!(precompiled.eval_boolean_with_configuration(&configuration), Ok(false)); //! ``` //! //! ## Features diff --git a/tests/integration.rs b/tests/integration.rs index b1c7b93..62b20c3 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -364,4 +364,27 @@ fn test_shortcut_functions() { eval_tuple_with_configuration("3,3", &configuration), Ok(vec![Value::Int(3), Value::Int(3)]) ); + + // assert_eq!(build_operator_tree("???").unwrap().eval_string()); + assert_eq!( + build_operator_tree("string").unwrap().eval_string_with_configuration(&configuration), + Ok("a string".to_string()) + ); + assert_eq!(build_operator_tree("3.3").unwrap().eval_float(), Ok(3.3)); + assert_eq!( + build_operator_tree("3.3").unwrap().eval_float_with_configuration(&configuration), + Ok(3.3) + ); + assert_eq!(build_operator_tree("3").unwrap().eval_int(), Ok(3)); + assert_eq!(build_operator_tree("3").unwrap().eval_int_with_configuration(&configuration), Ok(3)); + assert_eq!(build_operator_tree("true").unwrap().eval_boolean(), Ok(true)); + assert_eq!( + build_operator_tree("true").unwrap().eval_boolean_with_configuration(&configuration), + Ok(true) + ); + assert_eq!(build_operator_tree("3,3").unwrap().eval_tuple(), Ok(vec![Value::Int(3), Value::Int(3)])); + assert_eq!( + build_operator_tree("3,3").unwrap().eval_tuple_with_configuration(&configuration), + Ok(vec![Value::Int(3), Value::Int(3)]) + ); }