Add tests for evaluating operator tree and fix correct error for eval_number functions.

This commit is contained in:
Sebastian Schmidt 2021-07-13 14:55:23 +03:00
parent 56a5f7b0e8
commit 97c5fc1c21
2 changed files with 343 additions and 46 deletions

View File

@ -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),
}
}

View File

@ -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]