Delete countEquals predicate.

This commit is contained in:
jcamiel 2023-07-27 14:37:33 +02:00
parent 128134fe3b
commit 975fd95810
No known key found for this signature in database
GPG Key ID: 07FF11CFD55356CC
10 changed files with 17 additions and 196 deletions

View File

@ -183,7 +183,7 @@ pub mod tests {
use super::*;
use crate::http::xml_three_users_http_response;
// xpath //user countEquals 3
// `xpath "//user" count == 3`
pub fn assert_count_user() -> Assert {
let whitespace = Whitespace {
value: String::from(" "),
@ -193,10 +193,11 @@ pub mod tests {
not: false,
space0: whitespace.clone(),
predicate_func: PredicateFunc {
source_info: SourceInfo::new(1, 14, 1, 27),
value: PredicateFuncValue::CountEqual {
source_info: SourceInfo::new(1, 22, 1, 24),
value: PredicateFuncValue::Equal {
space0: whitespace.clone(),
value: PredicateValue::Integer(3),
operator: true,
},
},
};
@ -204,7 +205,13 @@ pub mod tests {
line_terminators: vec![],
space0: whitespace.clone(),
query: query::tests::xpath_users(),
filters: vec![],
filters: vec![(
whitespace.clone(),
Filter {
source_info: SourceInfo::new(1, 16, 1, 21),
value: FilterValue::Count,
},
)],
space1: whitespace.clone(),
predicate,
line_terminator0: LineTerminator {
@ -228,8 +235,8 @@ pub mod tests {
&xml_three_users_http_response(),
),
AssertResult::Explicit {
actual: Ok(Some(Value::Nodeset(3))),
source_info: SourceInfo::new(1, 14, 1, 27),
actual: Ok(Some(Value::Integer(3))),
source_info: SourceInfo::new(1, 22, 1, 24),
predicate_result: Some(Ok(())),
}
);

View File

@ -184,17 +184,6 @@ fn expected_no_value(
let value = eval_predicate_value(value, variables)?;
Ok(format!("less than or equals to <{}>", value.expected()))
}
// FIXME: we should remove this variant and use `count` filter.
PredicateFuncValue::CountEqual {
value: expected, ..
} => {
let expected = if let PredicateValue::Integer(expected) = expected {
expected
} else {
panic!();
};
Ok(format!("count equals to <{expected}>"))
}
PredicateFuncValue::StartWith {
value: expected, ..
} => {
@ -274,11 +263,6 @@ fn eval_predicate_func(
PredicateFuncValue::LessThanOrEqual {
value: expected, ..
} => eval_less_than_or_equal(expected, variables, value),
PredicateFuncValue::CountEqual {
value: PredicateValue::Integer(expected),
..
} => eval_count_equal(*expected, value),
PredicateFuncValue::CountEqual { .. } => panic!(), // should be catch by compilation
PredicateFuncValue::StartWith {
value: expected, ..
} => eval_start_with(expected, variables, value),
@ -365,36 +349,6 @@ fn eval_less_than_or_equal(
Ok(assert_values_less_or_equal(actual, &expected))
}
/// Evaluates if an `expected` count is equal to the count of an `actual` value.
fn eval_count_equal(expected: i64, actual: &Value) -> Result<AssertResult, Error> {
match actual {
Value::List(values) => Ok(AssertResult {
success: values.len() as i64 == expected,
actual: values.len().to_string(),
expected: expected.to_string(),
type_mismatch: false,
}),
Value::Nodeset(n) => Ok(AssertResult {
success: *n as i64 == expected,
actual: n.to_string(),
expected: expected.to_string(),
type_mismatch: false,
}),
Value::Bytes(data) => Ok(AssertResult {
success: data.len() as i64 == expected,
actual: data.len().to_string(),
expected: expected.to_string(),
type_mismatch: false,
}),
_ => Ok(AssertResult {
success: false,
actual: actual.display(),
expected: format!("count equals to <{expected}>"),
type_mismatch: true,
}),
}
}
/// Evaluates if an `expected` value (using a `variables` set) starts with an `actual` value.
/// This predicate works with string and bytes.
fn eval_start_with(
@ -1477,64 +1431,6 @@ mod tests {
);
}
#[test]
// FIXME: this test should be remove as `count` is now a filter
fn test_predicate_count_equals() {
// predicate: `count == 1`
// value: [42]
let expected = 1;
let value = Value::List(vec![Value::Integer(42)]);
let assert_result = eval_count_equal(expected, &value).unwrap();
assert!(assert_result.success);
assert!(!assert_result.type_mismatch);
assert_eq!(assert_result.actual.as_str(), "1");
assert_eq!(assert_result.expected.as_str(), "1");
// predicate: `count == 1`
// value: Nodeset(1)
let expected = 1;
let value = Value::Nodeset(1);
let assert_result = eval_count_equal(expected, &value).unwrap();
assert!(assert_result.success);
assert!(!assert_result.type_mismatch);
assert_eq!(assert_result.actual.as_str(), "1");
assert_eq!(assert_result.expected.as_str(), "1");
}
#[test]
// FIXME: this test should be remove as `count` is now a filter
fn test_predicate_count_equals_error() {
// predicate: `count == 10`
// value: true
let expected = 10;
let value = Value::Bool(true);
let assert_result = eval_count_equal(expected, &value).unwrap();
assert!(!assert_result.success);
assert!(assert_result.type_mismatch);
assert_eq!(assert_result.actual.as_str(), "bool <true>");
assert_eq!(assert_result.expected.as_str(), "count equals to <10>");
// predicate: `count == 1`
// value: []
let expected = 1;
let value = Value::List(vec![]);
let assert_result = eval_count_equal(expected, &value).unwrap();
assert!(!assert_result.success);
assert!(!assert_result.type_mismatch);
assert_eq!(assert_result.actual.as_str(), "0");
assert_eq!(assert_result.expected.as_str(), "1");
// predicate: `count == 1`
// value: Nodeset(3)
let expected = 1;
let value = Value::Nodeset(3);
let assert_result = eval_count_equal(expected, &value).unwrap();
assert!(!assert_result.success);
assert!(!assert_result.type_mismatch);
assert_eq!(assert_result.actual.as_str(), "3");
assert_eq!(assert_result.expected.as_str(), "1");
}
#[test]
fn test_predicate_is_empty_are_false() {
// predicate: `isEmpty`

View File

@ -389,13 +389,13 @@ mod tests {
&context_dir,
),
vec![AssertResult::Explicit {
actual: Ok(Some(Value::Nodeset(2))),
source_info: SourceInfo::new(1, 14, 1, 27),
actual: Ok(Some(Value::Integer(2))),
source_info: SourceInfo::new(1, 22, 1, 24),
predicate_result: Some(Err(Error {
source_info: SourceInfo::new(1, 0, 1, 0),
inner: RunnerError::AssertFailure {
actual: "2".to_string(),
expected: "3".to_string(),
actual: "int <2>".to_string(),
expected: "int <3>".to_string(),
type_mismatch: false,
},
assert: true,

View File

@ -456,12 +456,6 @@ pub enum PredicateFuncValue {
value: PredicateValue,
operator: bool,
},
// FIXME: `countEquals` predicate should be replace by `count` filter. This variant should be
// removed.
CountEqual {
space0: Whitespace,
value: PredicateValue,
},
StartWith {
space0: Whitespace,
value: PredicateValue,

View File

@ -222,7 +222,6 @@ impl PredicateFuncValue {
"lessThanOrEquals".to_string()
}
}
PredicateFuncValue::CountEqual { .. } => "countEquals".to_string(),
PredicateFuncValue::StartWith { .. } => "startsWith".to_string(),
PredicateFuncValue::EndWith { .. } => "endsWith".to_string(),
PredicateFuncValue::Contain { .. } => "contains".to_string(),

View File

@ -453,10 +453,6 @@ impl HtmlFormatter {
self.fmt_span_close();
match value {
PredicateFuncValue::CountEqual { space0, value, .. } => {
self.fmt_space(space0);
self.fmt_predicate_value(value);
}
PredicateFuncValue::Equal { space0, value, .. } => {
self.fmt_space(space0);
self.fmt_predicate_value(value);

View File

@ -76,7 +76,6 @@ fn predicate_func_value(reader: &mut Reader) -> ParseResult<'static, PredicateFu
greater_predicate,
less_or_equal_predicate,
less_predicate,
count_equal_predicate,
start_with_predicate,
end_with_predicate,
contain_predicate,
@ -265,21 +264,6 @@ fn less_or_equal_predicate(reader: &mut Reader) -> ParseResult<'static, Predicat
}
}
fn count_equal_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncValue> {
try_literal("countEquals", reader)?;
let space0 = one_or_more_spaces(reader)?;
let save = reader.state.clone();
let value = predicate_value(reader)?;
if !matches!(value, PredicateValue::Integer(_)) {
return Err(Error {
pos: save.pos,
recoverable: false,
inner: ParseError::PredicateValue {},
});
}
Ok(PredicateFuncValue::CountEqual { space0, value })
}
fn start_with_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncValue> {
try_literal("startsWith", reader)?;
let space0 = one_or_more_spaces(reader)?;
@ -447,21 +431,6 @@ mod tests {
);
}
#[test]
fn test_predicate_error() {
let mut reader = Reader::new("countEquals true");
let error = predicate(&mut reader).err().unwrap();
assert_eq!(
error.pos,
Pos {
line: 1,
column: 13,
}
);
assert!(!error.recoverable);
assert_eq!(error.inner, ParseError::PredicateValue {});
}
#[test]
fn test_predicate_func() {
let mut reader = Reader::new("tata equals 1");
@ -579,33 +548,6 @@ mod tests {
);
}
#[test]
fn test_count_equal_predicate() {
let mut reader = Reader::new("countEquals 2");
assert_eq!(
count_equal_predicate(&mut reader).unwrap(),
PredicateFuncValue::CountEqual {
value: PredicateValue::Integer(2),
space0: Whitespace {
value: String::from(" "),
source_info: SourceInfo::new(1, 12, 1, 13),
},
}
);
let mut reader = Reader::new("countEquals true");
let error = count_equal_predicate(&mut reader).err().unwrap();
assert_eq!(
error.pos,
Pos {
line: 1,
column: 13,
}
);
assert!(!error.recoverable);
assert_eq!(error.inner, ParseError::PredicateValue {});
}
#[test]
fn test_start_with_predicate() {
let mut reader = Reader::new("startsWith 2");

View File

@ -468,10 +468,6 @@ impl ToJson for Predicate {
));
add_predicate_value(&mut attributes, value);
}
PredicateFuncValue::CountEqual { value, .. } => {
attributes.push(("type".to_string(), JValue::String("count".to_string())));
add_predicate_value(&mut attributes, value);
}
PredicateFuncValue::StartWith { value, .. } => {
attributes.push(("type".to_string(), JValue::String("start-with".to_string())));
add_predicate_value(&mut attributes, value);

View File

@ -549,11 +549,6 @@ impl Tokenizable for PredicateFuncValue {
tokens.append(&mut space0.tokenize());
tokens.append(&mut value.tokenize());
}
PredicateFuncValue::CountEqual { space0, value } => {
tokens.push(Token::PredicateType(self.name()));
tokens.append(&mut space0.tokenize());
tokens.append(&mut value.tokenize());
}
PredicateFuncValue::StartWith { space0, value } => {
tokens.push(Token::PredicateType(self.name()));
tokens.append(&mut space0.tokenize());

View File

@ -388,10 +388,6 @@ fn lint_predicate_func_value(predicate_func_value: &PredicateFuncValue) -> Predi
space0: one_whitespace(),
value: lint_predicate_value(value),
},
PredicateFuncValue::CountEqual { value, .. } => PredicateFuncValue::CountEqual {
space0: one_whitespace(),
value: value.clone(),
},
PredicateFuncValue::IsInteger {} => PredicateFuncValue::IsInteger {},
PredicateFuncValue::IsFloat {} => PredicateFuncValue::IsFloat {},
PredicateFuncValue::IsBoolean {} => PredicateFuncValue::IsBoolean {},