Format code.

This commit is contained in:
Sebastian Schmidt 2021-06-22 11:43:58 +02:00
parent bd9a314baa
commit 6a30bd24e2
4 changed files with 147 additions and 31 deletions

View File

@ -166,4 +166,4 @@ macro_rules! context_map {
$crate::context_map!((&mut context) $($tt)*)
.map(|_| context)
}};
}
}

View File

@ -515,9 +515,9 @@ pub use crate::{
function::Function,
interface::*,
operator::Operator,
token::PartialToken,
tree::Node,
value::{value_type::ValueType, EmptyType, FloatType, IntType, TupleType, Value, EMPTY_VALUE},
token::PartialToken,
};
mod context;

View File

@ -767,40 +767,145 @@ fn test_type_errors_in_binary_operators() {
fn test_empty_context() {
let context = EmptyContext;
assert_eq!(context.get_value("abc"), None);
assert_eq!(context.call_function("abc", &Value::Empty), Err(EvalexprError::FunctionIdentifierNotFound("abc".to_owned())));
assert_eq!(
context.call_function("abc", &Value::Empty),
Err(EvalexprError::FunctionIdentifierNotFound("abc".to_owned()))
);
}
#[test]
fn test_hashmap_context_type_safety() {
let mut context = context_map! {"a" => 5, "b" => 5.0}.unwrap();
assert_eq!(eval_with_context_mut("a = 4", &mut context), Ok(Value::Empty));
assert_eq!(eval_with_context_mut("a = 4.0", &mut context), Err(EvalexprError::ExpectedInt {actual: Value::Float(4.0)}));
assert_eq!(eval_with_context_mut("a += 4.0", &mut context), Err(EvalexprError::ExpectedInt {actual: Value::Float(8.0)}));
assert_eq!(eval_with_context_mut("a -= 4.0", &mut context), Err(EvalexprError::ExpectedInt {actual: Value::Float(0.0)}));
assert_eq!(eval_with_context_mut("a *= 4.0", &mut context), Err(EvalexprError::ExpectedInt {actual: Value::Float(16.0)}));
assert_eq!(eval_with_context_mut("a /= 4.0", &mut context), Err(EvalexprError::ExpectedInt {actual: Value::Float(1.0)}));
assert_eq!(eval_with_context_mut("a %= 4.0", &mut context), Err(EvalexprError::ExpectedInt {actual: Value::Float(0.0)}));
assert_eq!(eval_with_context_mut("a ^= 4.0", &mut context), Err(EvalexprError::ExpectedInt {actual: Value::Float(256.0)}));
assert_eq!(
eval_with_context_mut("a = 4", &mut context),
Ok(Value::Empty)
);
assert_eq!(
eval_with_context_mut("a = 4.0", &mut context),
Err(EvalexprError::ExpectedInt {
actual: Value::Float(4.0)
})
);
assert_eq!(
eval_with_context_mut("a += 4.0", &mut context),
Err(EvalexprError::ExpectedInt {
actual: Value::Float(8.0)
})
);
assert_eq!(
eval_with_context_mut("a -= 4.0", &mut context),
Err(EvalexprError::ExpectedInt {
actual: Value::Float(0.0)
})
);
assert_eq!(
eval_with_context_mut("a *= 4.0", &mut context),
Err(EvalexprError::ExpectedInt {
actual: Value::Float(16.0)
})
);
assert_eq!(
eval_with_context_mut("a /= 4.0", &mut context),
Err(EvalexprError::ExpectedInt {
actual: Value::Float(1.0)
})
);
assert_eq!(
eval_with_context_mut("a %= 4.0", &mut context),
Err(EvalexprError::ExpectedInt {
actual: Value::Float(0.0)
})
);
assert_eq!(
eval_with_context_mut("a ^= 4.0", &mut context),
Err(EvalexprError::ExpectedInt {
actual: Value::Float(256.0)
})
);
assert_eq!(eval_with_context_mut("b = 4.0", &mut context), Ok(Value::Empty));
assert_eq!(eval_with_context_mut("b = 4", &mut context), Err(EvalexprError::ExpectedFloat {actual: Value::Int(4)}));
assert_eq!(eval_with_context_mut("b += 4", &mut context), Ok(Value::Empty));
assert_eq!(eval_with_context_mut("b -= 4", &mut context), Ok(Value::Empty));
assert_eq!(eval_with_context_mut("b *= 4", &mut context), Ok(Value::Empty));
assert_eq!(eval_with_context_mut("b /= 4", &mut context), Ok(Value::Empty));
assert_eq!(eval_with_context_mut("b %= 4", &mut context), Ok(Value::Empty));
assert_eq!(eval_with_context_mut("b ^= 4", &mut context), Ok(Value::Empty));
assert_eq!(
eval_with_context_mut("b = 4.0", &mut context),
Ok(Value::Empty)
);
assert_eq!(
eval_with_context_mut("b = 4", &mut context),
Err(EvalexprError::ExpectedFloat {
actual: Value::Int(4)
})
);
assert_eq!(
eval_with_context_mut("b += 4", &mut context),
Ok(Value::Empty)
);
assert_eq!(
eval_with_context_mut("b -= 4", &mut context),
Ok(Value::Empty)
);
assert_eq!(
eval_with_context_mut("b *= 4", &mut context),
Ok(Value::Empty)
);
assert_eq!(
eval_with_context_mut("b /= 4", &mut context),
Ok(Value::Empty)
);
assert_eq!(
eval_with_context_mut("b %= 4", &mut context),
Ok(Value::Empty)
);
assert_eq!(
eval_with_context_mut("b ^= 4", &mut context),
Ok(Value::Empty)
);
}
#[test]
fn test_error_constructors() {
assert_eq!(eval("a = true + \"4\""), Err(EvalexprError::ExpectedNumberOrString { actual: Value::Boolean(true) }));
assert_eq!(eval("a = true && \"4\""), Err(EvalexprError::ExpectedBoolean { actual: Value::from("4") }));
assert_eq!(eval_tuple("4"), Err(EvalexprError::ExpectedTuple { actual: Value::Int(4) }));
assert_eq!(Value::Tuple(vec![Value::Int(4), Value::Int(5)]).as_fixed_len_tuple(3), Err(EvalexprError::ExpectedFixedLenTuple { expected_len: 3, actual: Value::Tuple(vec![Value::Int(4), Value::Int(5)]) }));
assert_eq!(eval_empty("4"), Err(EvalexprError::ExpectedEmpty {actual: Value::Int(4)}));
assert_eq!(eval("&"), Err(EvalexprError::UnmatchedPartialToken { first: PartialToken::Ampersand, second: None }));
assert_eq!(
eval("a = true + \"4\""),
Err(EvalexprError::ExpectedNumberOrString {
actual: Value::Boolean(true)
})
);
assert_eq!(
eval("a = true && \"4\""),
Err(EvalexprError::ExpectedBoolean {
actual: Value::from("4")
})
);
assert_eq!(
eval_tuple("4"),
Err(EvalexprError::ExpectedTuple {
actual: Value::Int(4)
})
);
assert_eq!(
Value::Tuple(vec![Value::Int(4), Value::Int(5)]).as_fixed_len_tuple(3),
Err(EvalexprError::ExpectedFixedLenTuple {
expected_len: 3,
actual: Value::Tuple(vec![Value::Int(4), Value::Int(5)])
})
);
assert_eq!(
eval_empty("4"),
Err(EvalexprError::ExpectedEmpty {
actual: Value::Int(4)
})
);
assert_eq!(
eval("&"),
Err(EvalexprError::UnmatchedPartialToken {
first: PartialToken::Ampersand,
second: None
})
);
assert_eq!(expect_function_argument_amount(2, 2), Ok(()));
assert_eq!(expect_function_argument_amount(2, 3), Err(EvalexprError::WrongFunctionArgumentAmount { expected: 3, actual: 2 }));
}
assert_eq!(
expect_function_argument_amount(2, 3),
Err(EvalexprError::WrongFunctionArgumentAmount {
expected: 3,
actual: 2
})
);
}

View File

@ -13,9 +13,20 @@ fn test_serde() {
}
}
#[test]
fn test_serde_errors() {
assert_eq!(ron::de::from_str::<Node>("[\"5==5\"]"), Err(ron::de::Error::Parser(ron::de::ParseError::ExpectedString, ron::de::Position{col:1,line:1})));
assert_eq!(ron::de::from_str::<Node>("\"&\""), Err(ron::de::Error::Message("Found a partial token '&' that should be followed by another partial token.".to_owned())));
}
assert_eq!(
ron::de::from_str::<Node>("[\"5==5\"]"),
Err(ron::de::Error::Parser(
ron::de::ParseError::ExpectedString,
ron::de::Position { col: 1, line: 1 }
))
);
assert_eq!(
ron::de::from_str::<Node>("\"&\""),
Err(ron::de::Error::Message(
"Found a partial token '&' that should be followed by another partial token."
.to_owned()
))
);
}