Add tests for to_number fn
@@ -5,7 +5,6 @@ use tornado_common_api::{Number, Value};
pub fn to_number(variable_name: &str, value: &mut Value) -> Result<(), MatcherError> {
match value {
Value::Text(text) => {
- //*value = Value::Text(trimmed.to_owned());
if let Ok(u_value) = text.parse::<u64>() {
*value = Value::Number(Number::PosInt(u_value));
Ok(())
@@ -34,3 +33,35 @@ pub fn to_number(variable_name: &str, value: &mut Value) -> Result<(), MatcherEr
}),
}
}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ #[test]
+ fn to_number_modifier_should_return_a_positive_number() {
+ let mut input = Value::Text("12".to_owned());
+ to_number("", &mut input).unwrap();
+ assert_eq!(Value::Number(Number::PosInt(12)), input);
+ }
+
+ #[test]
+ fn to_number_modifier_should_return_a_negative_number() {
+ let mut input = Value::Text("-3412".to_owned());
+ to_number("", &mut input).unwrap();
+ assert_eq!(Value::Number(Number::NegInt(-3412)), input);
+ }
+
+ #[test]
+ fn to_number_modifier_should_return_a_float() {
+ let mut input = Value::Text("3.14".to_owned());
+ to_number("", &mut input).unwrap();
+ assert_eq!(Value::Number(Number::Float(3.14)), input);
+ }
+
+ #[test]
+ fn to_number_modifier_should_return_a_error() {
+ let mut input = Value::Text("something".to_owned());
+ assert!(to_number("", &mut input).is_err());
+ }
+}
