Add predicates for types

This commit is contained in:
Fabrice Reix 2020-11-28 18:36:51 +01:00
parent 6d5239a3d2
commit 367a26bf11
7 changed files with 165 additions and 4 deletions

View File

@ -284,6 +284,11 @@ fn expected(
let expected = eval_template(expected, variables)?;
Ok(format!("matches regex <{}>", expected))
}
PredicateFuncValue::IsInteger {} => Ok("integer".to_string()),
PredicateFuncValue::IsFloat {} => Ok("float".to_string()),
PredicateFuncValue::IsBoolean {} => Ok("boolean".to_string()),
PredicateFuncValue::IsString {} => Ok("string".to_string()),
PredicateFuncValue::IsCollection {} => Ok("collection".to_string()),
PredicateFuncValue::Exist {} => Ok("something".to_string()),
}
}
@ -498,6 +503,41 @@ fn eval_something(
}
}
// types
PredicateFuncValue::IsInteger {} => Ok(AssertResult {
success: matches!(value, Value::Integer(_)),
actual: value.display(),
expected: "integer".to_string(),
type_mismatch: false,
}),
PredicateFuncValue::IsFloat {} => Ok(AssertResult {
success: matches!(value, Value::Float(_, _)),
actual: value.display(),
expected: "float".to_string(),
type_mismatch: false,
}),
PredicateFuncValue::IsBoolean {} => Ok(AssertResult {
success: matches!(value, Value::Bool(_)),
actual: value.display(),
expected: "boolean".to_string(),
type_mismatch: false,
}),
PredicateFuncValue::IsString {} => Ok(AssertResult {
success: matches!(value, Value::String(_)),
actual: value.display(),
expected: "string".to_string(),
type_mismatch: false,
}),
PredicateFuncValue::IsCollection {} => Ok(AssertResult {
success: matches!(value, Value::Bytes(_))
|| matches!(value, Value::List(_))
|| matches!(value, Value::Nodeset(_))
|| matches!(value, Value::Object(_)),
actual: value.display(),
expected: "collection".to_string(),
type_mismatch: false,
}),
// exists
PredicateFuncValue::Exist {} => match value {
Value::Nodeset(0) => Ok(AssertResult {
@ -1165,7 +1205,7 @@ mod tests {
success: true,
type_mismatch: false,
actual: "int <2>".to_string(),
expected: "int <1>".to_string()
expected: "int <1>".to_string(),
}
);
assert_eq!(
@ -1174,7 +1214,7 @@ mod tests {
success: false,
type_mismatch: false,
actual: "int <1>".to_string(),
expected: "int <1>".to_string()
expected: "int <1>".to_string(),
}
);
assert_eq!(
@ -1183,7 +1223,7 @@ mod tests {
success: true,
type_mismatch: false,
actual: "float <1.1>".to_string(),
expected: "int <1>".to_string()
expected: "int <1>".to_string(),
}
);
assert_eq!(
@ -1192,7 +1232,7 @@ mod tests {
success: false,
type_mismatch: false,
actual: "float <1.1>".to_string(),
expected: "int <2>".to_string()
expected: "int <2>".to_string(),
}
);
}
@ -1299,6 +1339,38 @@ mod tests {
assert_eq!(assert_result.expected.as_str(), "count equals to <1>");
}
#[test]
fn test_predicate_type() {
let variables = HashMap::new();
let assert_result = eval_something(
PredicateFunc {
value: PredicateFuncValue::IsInteger {},
source_info: SourceInfo::init(0, 0, 0, 0),
},
&variables,
Value::Integer(1),
)
.unwrap();
assert_eq!(assert_result.success, true);
assert_eq!(assert_result.type_mismatch, false);
assert_eq!(assert_result.actual.as_str(), "int <1>");
assert_eq!(assert_result.expected.as_str(), "integer");
let assert_result = eval_something(
PredicateFunc {
value: PredicateFuncValue::IsInteger {},
source_info: SourceInfo::init(0, 0, 0, 0),
},
&variables,
Value::Float(1, 0),
)
.unwrap();
assert_eq!(assert_result.success, false);
assert_eq!(assert_result.type_mismatch, false);
assert_eq!(assert_result.actual.as_str(), "float <1.0>");
assert_eq!(assert_result.expected.as_str(), "integer");
}
#[test]
fn test_predicate_not_with_different_types() {
// equals predicate does not generate a type error with an integer value

View File

@ -422,6 +422,11 @@ pub enum PredicateFuncValue {
IncludeNull { space0: Whitespace },
IncludeExpression { space0: Whitespace, value: Expr },
Match { space0: Whitespace, value: Template },
IsInteger {},
IsFloat {},
IsBoolean {},
IsString {},
IsCollection {},
Exist {},
}

View File

@ -72,6 +72,11 @@ fn predicate_func_value(reader: &mut Reader) -> ParseResult<'static, PredicateFu
contain_predicate,
include_predicate,
match_predicate,
integer_predicate,
float_predicate,
boolean_predicate,
string_predicate,
collection_predicate,
exist_predicate,
],
reader,
@ -289,6 +294,31 @@ fn match_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncVal
Ok(PredicateFuncValue::Match { space0, value })
}
fn integer_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncValue> {
try_literal("isInteger", reader)?;
Ok(PredicateFuncValue::IsInteger {})
}
fn float_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncValue> {
try_literal("isFloat", reader)?;
Ok(PredicateFuncValue::IsFloat {})
}
fn boolean_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncValue> {
try_literal("isBoolean", reader)?;
Ok(PredicateFuncValue::IsBoolean {})
}
fn string_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncValue> {
try_literal("isString", reader)?;
Ok(PredicateFuncValue::IsString {})
}
fn collection_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncValue> {
try_literal("isCollection", reader)?;
Ok(PredicateFuncValue::IsCollection {})
}
fn exist_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncValue> {
try_literal("exists", reader)?;
Ok(PredicateFuncValue::Exist {})

View File

@ -591,6 +591,21 @@ impl Htmlable for PredicateFuncValue {
buffer.push_str(space0.to_html().as_str());
buffer.push_str(format!("<span class=\"boolean\">{}</span>", value).as_str());
}
PredicateFuncValue::IsInteger {} => {
buffer.push_str("<span class=\"predicate-type\">isInteger</span>");
}
PredicateFuncValue::IsFloat {} => {
buffer.push_str("<span class=\"predicate-type\">isFloat</span>");
}
PredicateFuncValue::IsBoolean {} => {
buffer.push_str("<span class=\"predicate-type\">isBoolean</span>");
}
PredicateFuncValue::IsString {} => {
buffer.push_str("<span class=\"predicate-type\">isString</span>");
}
PredicateFuncValue::IsCollection {} => {
buffer.push_str("<span class=\"predicate-type\">isCollection</span>");
}
PredicateFuncValue::Exist {} => {
buffer.push_str("<span class=\"predicate-type\">exists</span>");
}

View File

@ -438,6 +438,24 @@ impl ToJson for Predicate {
attributes.push(("type".to_string(), JValue::String("match".to_string())));
attributes.push(("value".to_string(), JValue::String(value.to_string())));
}
PredicateFuncValue::IsInteger {} => {
attributes.push(("type".to_string(), JValue::String("isInteger".to_string())));
}
PredicateFuncValue::IsFloat {} => {
attributes.push(("type".to_string(), JValue::String("isFloat".to_string())));
}
PredicateFuncValue::IsBoolean {} => {
attributes.push(("type".to_string(), JValue::String("isBoolean".to_string())));
}
PredicateFuncValue::IsString {} => {
attributes.push(("type".to_string(), JValue::String("isString".to_string())));
}
PredicateFuncValue::IsCollection {} => {
attributes.push((
"type".to_string(),
JValue::String("isCollection".to_string()),
));
}
PredicateFuncValue::Exist {} => {
attributes.push(("type".to_string(), JValue::String("exist".to_string())));
}

View File

@ -652,6 +652,22 @@ impl Tokenizable for PredicateFuncValue {
add_tokens(&mut tokens, space0.tokenize());
add_tokens(&mut tokens, value.tokenize());
}
PredicateFuncValue::IsInteger {} => {
tokens.push(Token::PredicateType(String::from("isInteger")));
}
PredicateFuncValue::IsFloat {} => {
tokens.push(Token::PredicateType(String::from("isFloat")));
}
PredicateFuncValue::IsBoolean {} => {
tokens.push(Token::PredicateType(String::from("isBoolean")));
}
PredicateFuncValue::IsString {} => {
tokens.push(Token::PredicateType(String::from("isString")));
}
PredicateFuncValue::IsCollection {} => {
tokens.push(Token::PredicateType(String::from("isCollection")));
}
PredicateFuncValue::Exist {} => {
tokens.push(Token::PredicateType(String::from("exists")));
}

View File

@ -508,6 +508,11 @@ impl Lintable<PredicateFuncValue> for PredicateFuncValue {
space0: one_whitespace(),
value: value.clone(),
},
PredicateFuncValue::IsInteger {} => PredicateFuncValue::IsInteger {},
PredicateFuncValue::IsFloat {} => PredicateFuncValue::IsFloat {},
PredicateFuncValue::IsBoolean {} => PredicateFuncValue::IsBoolean {},
PredicateFuncValue::IsString {} => PredicateFuncValue::IsString {},
PredicateFuncValue::IsCollection {} => PredicateFuncValue::IsCollection {},
PredicateFuncValue::Exist {} => PredicateFuncValue::Exist {},
}
}