From 97c5fc1c210ff34f03c650f06a87e1a4dd59388c Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Tue, 13 Jul 2021 14:55:23 +0300 Subject: [PATCH] Add tests for evaluating operator tree and fix correct error for eval_number functions. --- src/tree/mod.rs | 4 +- tests/integration.rs | 385 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 343 insertions(+), 46 deletions(-) diff --git a/src/tree/mod.rs b/src/tree/mod.rs index ef5b344..3dff3b6 100644 --- a/src/tree/mod.rs +++ b/src/tree/mod.rs @@ -190,7 +190,7 @@ impl Node { match self.eval_with_context(context) { Ok(Value::Int(int)) => Ok(int as FloatType), Ok(Value::Float(float)) => Ok(float), - Ok(value) => Err(EvalexprError::expected_int(value)), + Ok(value) => Err(EvalexprError::expected_number(value)), Err(error) => Err(error), } } @@ -281,7 +281,7 @@ impl Node { match self.eval_with_context_mut(context) { Ok(Value::Int(int)) => Ok(int as FloatType), Ok(Value::Float(float)) => Ok(float), - Ok(value) => Err(EvalexprError::expected_int(value)), + Ok(value) => Err(EvalexprError::expected_number(value)), Err(error) => Err(error), } } diff --git a/tests/integration.rs b/tests/integration.rs index 74e0c95..8f38837 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -683,105 +683,402 @@ fn test_shortcut_functions() { // With detour via build_operator_tree assert_eq!( - build_operator_tree("\"???\"").unwrap().eval_string(), - Ok("???".to_owned()) + build_operator_tree("\"3.3\"").unwrap().eval_string(), + Ok("3.3".to_owned()) + ); + assert_eq!( + build_operator_tree("3.3").unwrap().eval_string(), + Err(EvalexprError::ExpectedString { + actual: Value::Float(3.3) + }) + ); + assert_eq!( + build_operator_tree("3..3").unwrap().eval_string(), + Err(EvalexprError::VariableIdentifierNotFound("3..3".to_owned())) ); assert_eq!( build_operator_tree("string") .unwrap() .eval_string_with_context(&context), - Ok("a string".to_string()) + Ok("a string".to_owned()) ); - assert_eq!(build_operator_tree("3.3").unwrap().eval_float(), Ok(3.3)); assert_eq!( build_operator_tree("3.3") .unwrap() - .eval_float_with_context(&context), - Ok(3.3) + .eval_string_with_context(&context), + Err(EvalexprError::ExpectedString { + actual: Value::Float(3.3) + }) ); - assert_eq!(build_operator_tree("3").unwrap().eval_int(), Ok(3)); assert_eq!( - build_operator_tree("3") + build_operator_tree("3..3") .unwrap() - .eval_int_with_context(&context), - Ok(3) + .eval_string_with_context(&context), + Err(EvalexprError::VariableIdentifierNotFound("3..3".to_owned())) ); - assert_eq!(build_operator_tree("3").unwrap().eval_number(), Ok(3.0)); - assert_eq!( - build_operator_tree("3") - .unwrap() - .eval_number_with_context(&context), - Ok(3.0) - ); - assert_eq!( - build_operator_tree("true").unwrap().eval_boolean(), - Ok(true) - ); - assert_eq!( - build_operator_tree("true") - .unwrap() - .eval_boolean_with_context(&context), - 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_context(&context), - Ok(vec![Value::Int(3), Value::Int(3)]) - ); - assert_eq!( - build_operator_tree("") - .unwrap() - .eval_empty_with_context(&context), - Ok(EMPTY_VALUE) - ); - assert_eq!( build_operator_tree("string") .unwrap() .eval_string_with_context_mut(&mut context), Ok("a string".to_string()) ); + assert_eq!( + build_operator_tree("3.3") + .unwrap() + .eval_string_with_context_mut(&mut context), + Err(EvalexprError::ExpectedString { + actual: Value::Float(3.3) + }) + ); + assert_eq!( + build_operator_tree("3..3") + .unwrap() + .eval_string_with_context_mut(&mut context), + Err(EvalexprError::VariableIdentifierNotFound("3..3".to_owned())) + ); + + assert_eq!(build_operator_tree("3.3").unwrap().eval_float(), Ok(3.3)); + assert_eq!( + build_operator_tree("33").unwrap().eval_float(), + Err(EvalexprError::ExpectedFloat { + actual: Value::Int(33) + }) + ); + assert_eq!( + build_operator_tree("asd()").unwrap().eval_float(), + Err(EvalexprError::FunctionIdentifierNotFound("asd".to_owned())) + ); + assert_eq!( + build_operator_tree("3.3") + .unwrap() + .eval_float_with_context(&context), + Ok(3.3) + ); + assert_eq!( + build_operator_tree("33") + .unwrap() + .eval_float_with_context(&context), + Err(EvalexprError::ExpectedFloat { + actual: Value::Int(33) + }) + ); + assert_eq!( + build_operator_tree("asd") + .unwrap() + .eval_float_with_context(&context), + Err(EvalexprError::VariableIdentifierNotFound("asd".to_owned())) + ); assert_eq!( build_operator_tree("3.3") .unwrap() .eval_float_with_context_mut(&mut context), Ok(3.3) ); + assert_eq!( + build_operator_tree("33") + .unwrap() + .eval_float_with_context_mut(&mut context), + Err(EvalexprError::ExpectedFloat { + actual: Value::Int(33) + }) + ); + assert_eq!( + build_operator_tree("asd") + .unwrap() + .eval_float_with_context_mut(&mut context), + Err(EvalexprError::VariableIdentifierNotFound("asd".to_owned())) + ); + + assert_eq!(build_operator_tree("3").unwrap().eval_int(), Ok(3)); + assert_eq!( + build_operator_tree("3.3").unwrap().eval_int(), + Err(EvalexprError::ExpectedInt { + actual: Value::Float(3.3) + }) + ); + assert_eq!( + build_operator_tree("(,);.").unwrap().eval_int(), + Err(EvalexprError::VariableIdentifierNotFound(".".to_owned())) + ); + assert_eq!( + build_operator_tree("3") + .unwrap() + .eval_int_with_context(&context), + Ok(3) + ); + assert_eq!( + build_operator_tree("3.3") + .unwrap() + .eval_int_with_context(&context), + Err(EvalexprError::ExpectedInt { + actual: Value::Float(3.3) + }) + ); + assert_eq!( + build_operator_tree("(,);.") + .unwrap() + .eval_int_with_context(&context), + Err(EvalexprError::VariableIdentifierNotFound(".".to_owned())) + ); assert_eq!( build_operator_tree("3") .unwrap() .eval_int_with_context_mut(&mut context), Ok(3) ); + assert_eq!( + build_operator_tree("3.3") + .unwrap() + .eval_int_with_context_mut(&mut context), + Err(EvalexprError::ExpectedInt { + actual: Value::Float(3.3) + }) + ); + assert_eq!( + build_operator_tree("(,);.") + .unwrap() + .eval_int_with_context_mut(&mut context), + Err(EvalexprError::VariableIdentifierNotFound(".".to_owned())) + ); + + assert_eq!(build_operator_tree("3").unwrap().eval_number(), Ok(3.0)); + assert_eq!( + build_operator_tree("true").unwrap().eval_number(), + Err(EvalexprError::ExpectedNumber { + actual: Value::Boolean(true) + }) + ); + assert_eq!( + build_operator_tree("abc").unwrap().eval_number(), + Err(EvalexprError::VariableIdentifierNotFound("abc".to_owned())) + ); + assert_eq!( + build_operator_tree("3") + .unwrap() + .eval_number_with_context(&context), + Ok(3.0) + ); + assert_eq!( + build_operator_tree("true") + .unwrap() + .eval_number_with_context(&context), + Err(EvalexprError::ExpectedNumber { + actual: Value::Boolean(true) + }) + ); + assert_eq!( + build_operator_tree("abc") + .unwrap() + .eval_number_with_context(&context), + Err(EvalexprError::VariableIdentifierNotFound("abc".to_owned())) + ); assert_eq!( build_operator_tree("3") .unwrap() .eval_number_with_context_mut(&mut context), Ok(3.0) ); + assert_eq!( + build_operator_tree("true") + .unwrap() + .eval_number_with_context_mut(&mut context), + Err(EvalexprError::ExpectedNumber { + actual: Value::Boolean(true) + }) + ); + assert_eq!( + build_operator_tree("abc") + .unwrap() + .eval_number_with_context_mut(&mut context), + Err(EvalexprError::VariableIdentifierNotFound("abc".to_owned())) + ); + + assert_eq!( + build_operator_tree("true").unwrap().eval_boolean(), + Ok(true) + ); + assert_eq!( + build_operator_tree("4").unwrap().eval_boolean(), + Err(EvalexprError::ExpectedBoolean { + actual: Value::Int(4) + }) + ); + assert_eq!( + build_operator_tree("trueee").unwrap().eval_boolean(), + Err(EvalexprError::VariableIdentifierNotFound( + "trueee".to_owned() + )) + ); + assert_eq!( + build_operator_tree("true") + .unwrap() + .eval_boolean_with_context(&context), + Ok(true) + ); + assert_eq!( + build_operator_tree("4") + .unwrap() + .eval_boolean_with_context(&context), + Err(EvalexprError::ExpectedBoolean { + actual: Value::Int(4) + }) + ); + assert_eq!( + build_operator_tree("trueee") + .unwrap() + .eval_boolean_with_context(&context), + Err(EvalexprError::VariableIdentifierNotFound( + "trueee".to_owned() + )) + ); assert_eq!( build_operator_tree("true") .unwrap() .eval_boolean_with_context_mut(&mut context), Ok(true) ); + assert_eq!( + build_operator_tree("4") + .unwrap() + .eval_boolean_with_context_mut(&mut context), + Err(EvalexprError::ExpectedBoolean { + actual: Value::Int(4) + }) + ); + assert_eq!( + build_operator_tree("trueee") + .unwrap() + .eval_boolean_with_context_mut(&mut context), + Err(EvalexprError::VariableIdentifierNotFound( + "trueee".to_owned() + )) + ); + + assert_eq!( + build_operator_tree("3,3").unwrap().eval_tuple(), + Ok(vec![Value::Int(3), Value::Int(3)]) + ); + assert_eq!( + build_operator_tree("33").unwrap().eval_tuple(), + Err(EvalexprError::ExpectedTuple { + actual: Value::Int(33) + }) + ); + assert_eq!( + build_operator_tree("3a3").unwrap().eval_tuple(), + Err(EvalexprError::VariableIdentifierNotFound("3a3".to_owned())) + ); + assert_eq!( + build_operator_tree("3,3") + .unwrap() + .eval_tuple_with_context(&context), + Ok(vec![Value::Int(3), Value::Int(3)]) + ); + assert_eq!( + build_operator_tree("33") + .unwrap() + .eval_tuple_with_context(&context), + Err(EvalexprError::ExpectedTuple { + actual: Value::Int(33) + }) + ); + assert_eq!( + build_operator_tree("3a3") + .unwrap() + .eval_tuple_with_context(&context), + Err(EvalexprError::VariableIdentifierNotFound("3a3".to_owned())) + ); assert_eq!( build_operator_tree("3,3") .unwrap() .eval_tuple_with_context_mut(&mut context), Ok(vec![Value::Int(3), Value::Int(3)]) ); + assert_eq!( + build_operator_tree("33") + .unwrap() + .eval_tuple_with_context_mut(&mut context), + Err(EvalexprError::ExpectedTuple { + actual: Value::Int(33) + }) + ); + assert_eq!( + build_operator_tree("3a3") + .unwrap() + .eval_tuple_with_context_mut(&mut context), + Err(EvalexprError::VariableIdentifierNotFound("3a3".to_owned())) + ); + + assert_eq!( + build_operator_tree("").unwrap().eval_empty(), + Ok(EMPTY_VALUE) + ); + assert_eq!( + build_operator_tree("()").unwrap().eval_empty(), + Ok(EMPTY_VALUE) + ); + assert_eq!( + build_operator_tree("(,)").unwrap().eval_empty(), + Err(EvalexprError::ExpectedEmpty { + actual: Value::Tuple(vec![Value::Empty, Value::Empty]) + }) + ); + assert_eq!( + build_operator_tree("xaq").unwrap().eval_empty(), + Err(EvalexprError::VariableIdentifierNotFound("xaq".to_owned())) + ); + assert_eq!( + build_operator_tree("") + .unwrap() + .eval_empty_with_context(&context), + Ok(EMPTY_VALUE) + ); + assert_eq!( + build_operator_tree("()") + .unwrap() + .eval_empty_with_context(&context), + Ok(EMPTY_VALUE) + ); + assert_eq!( + build_operator_tree("(,)") + .unwrap() + .eval_empty_with_context(&context), + Err(EvalexprError::ExpectedEmpty { + actual: Value::Tuple(vec![Value::Empty, Value::Empty]) + }) + ); + assert_eq!( + build_operator_tree("xaq") + .unwrap() + .eval_empty_with_context(&context), + Err(EvalexprError::VariableIdentifierNotFound("xaq".to_owned())) + ); assert_eq!( build_operator_tree("") .unwrap() .eval_empty_with_context_mut(&mut context), Ok(EMPTY_VALUE) ); + assert_eq!( + build_operator_tree("()") + .unwrap() + .eval_empty_with_context_mut(&mut context), + Ok(EMPTY_VALUE) + ); + assert_eq!( + build_operator_tree("(,)") + .unwrap() + .eval_empty_with_context_mut(&mut context), + Err(EvalexprError::ExpectedEmpty { + actual: Value::Tuple(vec![Value::Empty, Value::Empty]) + }) + ); + assert_eq!( + build_operator_tree("xaq") + .unwrap() + .eval_empty_with_context_mut(&mut context), + Err(EvalexprError::VariableIdentifierNotFound("xaq".to_owned())) + ); } #[test]