1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
use ele::{Filterable, Value, parse};
struct Record<'a> {
value: Value<'a>,
}
impl Filterable for Record<'_> {
fn field(&self, name: &str) -> Option<Value<'_>> {
(name == "value").then(|| self.value.clone())
}
fn all_field_values(&self) -> Vec<Value<'_>> {
vec![self.value.clone()]
}
}
fn matches(value: Value<'_>, filter: &str) -> bool {
parse(filter).unwrap().unwrap().evaluate(&Record { value })
}
#[test]
fn global_matching_for_library_records() {
let value = Value::Map(vec![
("ignored_key", Value::Null),
(
"events",
Value::List(vec![Value::StringOwned("FOObar".into())]),
),
]);
assert!(matches(value.clone(), "foobar"));
assert!(!matches(value.clone(), "ignored_key"));
assert!(value.matches_global("foobar"));
assert!(Value::String("ÉTUDE").matches_global("étude"));
assert!(Value::String("").matches_global(""));
assert!(!Value::Null.matches_global("null"));
}
#[test]
fn global_matching_can_avoid_collecting_values() {
struct Direct;
impl Filterable for Direct {
fn field(&self, _: &str) -> Option<Value<'_>> {
None
}
fn all_field_values(&self) -> Vec<Value<'_>> {
panic!("global matching must use the override");
}
fn matches_global(&self, search: &str) -> bool {
Value::String("hello foobar").matches_global(search)
}
}
assert!(parse("foobar").unwrap().unwrap().evaluate(&Direct));
}
#[test]
fn integer_float_boundaries() {
for (integer, float, order) in [
(Value::Int(i64::MIN), i64::MIN as f64, "="),
(Value::Int(i64::MAX), i64::MAX as f64, "<"),
(Value::Uint(u64::MAX), u64::MAX as f64, "<"),
(Value::Int(0), -0.0, "="),
(Value::Uint(0), -0.5, ">"),
(Value::Int(-5), -5.9, ">"),
(Value::Int(5), 5.9, "<"),
(Value::Int(1), f64::INFINITY, "<"),
(Value::Uint(1), f64::NEG_INFINITY, ">"),
] {
assert!(matches(integer, &format!("value {order} \"{float:?}\"")));
}
assert!(!matches(Value::Int(0), "value = NaN"));
assert!(!matches(Value::Float(f64::NAN), "value > 0"));
assert!(matches(Value::Float(f64::INFINITY), "value = inf"));
assert!(matches(Value::Float(f64::NEG_INFINITY), "value < -1"));
}
#[test]
fn composite_comparisons_and_negation() {
for (filter, expected) in [
("value:(open OR closed)", true),
("value:(open AND closed)", false),
("value:(NOT closed)", true),
(r#"value =~ ("^op" OR "^cl")"#, true),
(r#"value !~ ("^cl" AND "^sh")"#, true),
(r#"value != "op*""#, false),
] {
assert_eq!(
matches(Value::StringOwned("open".into()), filter),
expected,
"{filter}"
);
}
}
|