From 3351d19db32f1b2d2932e4d8ca43ebe76762d7b4 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Fri, 15 Mar 2019 19:46:00 +0200 Subject: [PATCH] Tested configuration --- src/lib.rs | 26 +++++++++++++++++++++++++- src/operator/mod.rs | 2 +- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index edcbd1a..9410c6a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -use configuration::EmptyConfiguration; +use configuration::{EmptyConfiguration, Configuration}; use error::Error; use value::Value; @@ -13,10 +13,16 @@ pub fn eval(string: &str) -> Result { tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval(&EmptyConfiguration) } +pub fn eval_with_configuration(string: &str, configuration: &Configuration) -> Result { + tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval(configuration) +} + #[cfg(test)] mod test { use crate::{eval, value::Value}; use error::Error; + use configuration::HashMapConfiguration; + use eval_with_configuration; #[test] fn test_unary_examples() { @@ -100,6 +106,24 @@ mod test { assert_eq!(eval("5.0 <= 4.9 || !(4 > 3.5)"), Ok(Value::Boolean(false))); } + #[test] + fn test_with_configuration() { + let mut configuration = HashMapConfiguration::new(); + configuration.insert("tr".to_string(), Value::Boolean(true)); + configuration.insert("fa".to_string(), Value::Boolean(false)); + configuration.insert("five".to_string(), Value::Int(5)); + configuration.insert("six".to_string(), Value::Int(6)); + configuration.insert("half".to_string(), Value::Float(0.5)); + configuration.insert("zero".to_string(), Value::Int(0)); + + assert_eq!(eval_with_configuration("tr", &configuration), Ok(Value::Boolean(true))); + assert_eq!(eval_with_configuration("fa", &configuration), Ok(Value::Boolean(false))); + assert_eq!(eval_with_configuration("tr && false", &configuration), Ok(Value::Boolean(false))); + assert_eq!(eval_with_configuration("five + six", &configuration), Ok(Value::Int(11))); + assert_eq!(eval_with_configuration("five * half", &configuration), Ok(Value::Float(2.5))); + assert_eq!(eval_with_configuration("five < six && true", &configuration), Ok(Value::Boolean(true))); + } + #[test] fn test_errors() { assert_eq!( diff --git a/src/operator/mod.rs b/src/operator/mod.rs index dcccf83..e5e774c 100644 --- a/src/operator/mod.rs +++ b/src/operator/mod.rs @@ -473,7 +473,7 @@ impl Operator for Identifier { } fn argument_amount(&self) -> usize { - 0 + 1 } fn eval(&self, arguments: &[Value], configuration: &Configuration) -> Result {