diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-09-29 14:34:42 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-09-29 14:34:42 +0200 |
| commit | 0ebdd46766744d649c93f4023d7f20cbfde51c1e (patch) | |
| tree | c1ce87732fa766b591e380e1fb0b0a3f86d77807 /tests | |
| parent | 66e86c9693a7184944cdd2a0dbf1514e9d0125dc (diff) | |
| download | ele-0ebdd46766744d649c93f4023d7f20cbfde51c1e.tar.gz ele-0ebdd46766744d649c93f4023d7f20cbfde51c1e.zip | |
Implement map-key presence and repeated has traversal
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/cli_tests.rs | 3 | ||||
| -rw-r--r-- | tests/has_path_tests.rs | 98 |
2 files changed, 101 insertions, 0 deletions
diff --git a/tests/cli_tests.rs b/tests/cli_tests.rs index 9e0f848..c283e19 100644 --- a/tests/cli_tests.rs +++ b/tests/cli_tests.rs @@ -128,6 +128,9 @@ fn has_and_boolean_values() { matches("meta:*", r#"{"meta":{"owner":"alice"}}"#, true); matches("meta:*", r#"{"meta":{}}"#, false); matches("meta.owner=alice", r#"{"meta":{"owner":"alice"}}"#, true); + matches("meta.zero:*", r#"{"meta":{"zero":0}}"#, true); + matches("r.foo:42", r#"{"r":[{"foo":42}]}"#, true); + matches("r.foo:42", r#"{"r":[{"foo":0}]}"#, false); } #[test] diff --git a/tests/has_path_tests.rs b/tests/has_path_tests.rs new file mode 100644 index 0000000..459eaba --- /dev/null +++ b/tests/has_path_tests.rs @@ -0,0 +1,98 @@ +use aip_filter::{Filterable, Value, parse}; +use serde_json::json; + +fn matches(record: &impl Filterable, text: &str) -> bool { + parse(text) + .unwrap() + .unwrap() + .compile() + .unwrap() + .evaluate(record) +} + +#[test] +fn map_presence_does_not_depend_on_the_value() { + for value in [ + json!(0), + json!(false), + json!(""), + json!(null), + json!([]), + json!({}), + ] { + let record = json!({"m": {"foo": value}}); + assert!(matches(&record, "m:foo")); + assert!(matches(&record, "m.foo:*"), "{record}"); + assert!(matches(&record, "m.foo:(* OR absent)")); + assert!(!matches(&record, "m.foo:(NOT *)")); + assert!(!matches(&record, "m.missing:*")); + assert!(!matches(&record, "m.missing:(NOT *)")); + } + assert!(!matches(&json!({"m": {}}), "m:*")); + assert!(!matches(&json!({"r": []}), "r:*")); + assert!(!matches(&json!({"n": 0}), "n:*")); + assert!(matches(&json!({"m": {"foo": 0}}), "m.foo:(0 AND *)")); + assert!(matches(&json!({"m": {"FOO": 1, "foo": 0}}), "m.foo:0")); +} + +#[test] +fn has_traverses_repeated_values_without_array_indices() { + let record = json!({"r": [{"foo": 42}, {"foo": 43}, {"other": 0}, null]}); + assert!(matches(&record, "r.foo:42")); + assert!(matches(&record, "r.foo:43")); + assert!(matches(&record, "r.foo:*")); + assert!(matches(&record, "r.foo:42 AND r.foo:43")); + assert!(!matches(&record, "r.foo:(42 AND 43)")); + assert!(!matches(&record, "r.foo:99")); + assert!(!matches(&record, "r.0.foo:42")); + assert!(!matches(&record, "r.foo = 42")); + assert!(!matches(&record, "r.foo != 42")); + assert!(!matches(&record, "r.foo > 0")); + assert!(!matches(&json!({"r": []}), "r.foo:*")); + assert!(!matches(&json!({"r": [{"other": 0}]}), "r.foo:(NOT *)")); + assert!(matches(&json!({"r": [{"foo": 0}]}), "r.foo:*")); + assert!(matches( + &json!({"r": [{"foo": [{"bar": 42}]}]}), + "r.foo.bar:42" + )); +} + +#[test] +fn owned_collections_follow_the_same_has_rules() { + struct Record; + impl Filterable for Record { + fn field(&self, name: &str) -> Option<Value<'_>> { + match name { + "m" => Some(Value::Map(vec![("foo", Value::Bool(false))])), + "r" => Some(Value::List(vec![Value::Map(vec![("foo", Value::Int(42))])])), + _ => None, + } + } + } + assert!(matches(&Record, "m.foo:*")); + assert!(matches(&Record, "r.foo:42")); + assert!(!matches(&Record, "r.foo = 42")); +} + +#[test] +fn custom_message_paths_keep_non_default_presence_rules() { + struct Message(i64); + impl Filterable for Message { + fn field(&self, _: &str) -> Option<Value<'_>> { + None + } + fn field_path(&self, path: &[&str]) -> Option<Value<'_>> { + match path { + ["inner", "value"] => Some(Value::Int(self.0)), + ["inner", "map"] => Some(Value::Map(vec![("value", Value::Int(self.0))])), + ["inner", "map", "value"] => Some(Value::Int(self.0)), + _ => None, + } + } + } + assert!(!matches(&Message(0), "inner.value:*")); + assert!(matches(&Message(42), "inner.value:*")); + assert!(matches(&Message(42), "inner.value:42")); + assert!(matches(&Message(0), "inner.map.value:*")); + assert!(matches(&Message(0), "inner.map.value:0")); +} |
