Tested configuration

This commit is contained in:
Sebastian Schmidt 2019-03-15 19:46:00 +02:00
parent b1abbd6cfd
commit 3351d19db3
2 changed files with 26 additions and 2 deletions

View File

@ -1,4 +1,4 @@
use configuration::EmptyConfiguration; use configuration::{EmptyConfiguration, Configuration};
use error::Error; use error::Error;
use value::Value; use value::Value;
@ -13,10 +13,16 @@ pub fn eval(string: &str) -> Result<Value, Error> {
tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval(&EmptyConfiguration) tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval(&EmptyConfiguration)
} }
pub fn eval_with_configuration(string: &str, configuration: &Configuration) -> Result<Value, Error> {
tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval(configuration)
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::{eval, value::Value}; use crate::{eval, value::Value};
use error::Error; use error::Error;
use configuration::HashMapConfiguration;
use eval_with_configuration;
#[test] #[test]
fn test_unary_examples() { fn test_unary_examples() {
@ -100,6 +106,24 @@ mod test {
assert_eq!(eval("5.0 <= 4.9 || !(4 > 3.5)"), Ok(Value::Boolean(false))); 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] #[test]
fn test_errors() { fn test_errors() {
assert_eq!( assert_eq!(

View File

@ -473,7 +473,7 @@ impl Operator for Identifier {
} }
fn argument_amount(&self) -> usize { fn argument_amount(&self) -> usize {
0 1
} }
fn eval(&self, arguments: &[Value], configuration: &Configuration) -> Result<Value, Error> { fn eval(&self, arguments: &[Value], configuration: &Configuration) -> Result<Value, Error> {