diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-09-29 13:52:56 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-09-29 13:52:56 +0200 |
| commit | 4710b5224f24992dabb2a2dd9230cac586f20ffd (patch) | |
| tree | 3cd5c2cb4e390b76bab8968722be3deefdf2688c /tests | |
| parent | 38b60098e42b4a899eb574f65fa14d4fdbe82f9b (diff) | |
| download | ele-4710b5224f24992dabb2a2dd9230cac586f20ffd.tar.gz ele-4710b5224f24992dabb2a2dd9230cac586f20ffd.zip | |
Fix literal matching, comparisons, and CLI error handling
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/cli_tests.rs | 187 | ||||
| -rw-r--r-- | tests/eval_tests.rs | 94 |
2 files changed, 281 insertions, 0 deletions
diff --git a/tests/cli_tests.rs b/tests/cli_tests.rs new file mode 100644 index 0000000..e86a29a --- /dev/null +++ b/tests/cli_tests.rs @@ -0,0 +1,187 @@ +use std::io::Write; +use std::process::{Command, Output, Stdio}; + +fn run(expr: &str, input: &str, args: &[&str]) -> Output { + let mut child = Command::new(env!("CARGO_BIN_EXE_aip-filter")) + .args(args) + .arg(expr) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + child + .stdin + .take() + .unwrap() + .write_all(input.as_bytes()) + .unwrap(); + child.wait_with_output().unwrap() +} + +fn matches(expr: &str, record: &str, expected: bool) { + let input = format!("{record}\n"); + let output = run(expr, &input, &[]); + assert!( + output.status.success(), + "{expr}: {}", + String::from_utf8_lossy(&output.stderr) + ); + assert_eq!( + String::from_utf8(output.stdout).unwrap(), + if expected { input } else { String::new() }, + "{expr} against {record}" + ); +} + +#[test] +fn bare_literals_search_nested_values() { + for record in [ + r#"{"msg":"hello foobar"}"#, + r#"{"nested":{"msg":"hello foobar"}}"#, + r#"{"events":[{"msg":"hello foobar"}]}"#, + r#"{"events":[["hello foobar"]]}"#, + r#"["hello foobar"]"#, + r#""hello foobar""#, + ] { + matches("foobar", record, true); + matches("FOOBAR", record, true); + matches("absent", record, false); + } + matches("foobar", r#"{"foobar":"unrelated"}"#, false); + matches("foobar", r#"{"foobar":"","msg":"hello foobar"}"#, true); + matches("foo.bar", r#"{"msg":"hello foo.bar"}"#, true); + matches("active", r#"{"active":true}"#, true); + matches("active", r#"{"active":false}"#, false); +} + +#[test] +fn quoted_literals_do_not_resolve_fields() { + matches(r#""msg""#, r#"{"msg":"unrelated"}"#, false); + matches(r#""active""#, r#"{"active":false,"msg":"active"}"#, true); +} + +#[test] +fn unicode_wildcards_do_not_panic() { + for expr in [ + r#"name = "x*""#, + r#"name = "*x""#, + r#"name : "x*""#, + r#"name : "*x""#, + ] { + matches(expr, r#"{"name":"é"}"#, false); + } + matches(r#"name = "é*""#, r#"{"name":"étude"}"#, true); + matches(r#"name != "op*""#, r#"{"name":"open"}"#, false); + matches(r#"name != "op*""#, r#"{"name":"closed"}"#, true); + matches(r#"names != "op*""#, r#"{"names":["open"]}"#, false); +} + +#[test] +fn has_checks_later_phrases_and_unicode_boundaries() { + matches( + r#"msg:"hello world""#, + r#"{"msg":"xhello world; hello world"}"#, + true, + ); + matches(r#"msg:"a a""#, r#"{"msg":"xa a a"}"#, true); + matches("msg:a", r#"{"msg":"İa"}"#, false); + matches("msg:a", r#"{"msg":"İ a"}"#, true); + matches("msg:crash", r#"{"msg":"crashes"}"#, false); +} + +#[test] +fn numeric_comparisons_do_not_truncate_or_round_integers() { + for (expr, record, expected) in [ + ("n = 5.9", r#"{"n":5}"#, false), + ("n < 5.9", r#"{"n":5}"#, true), + ("n > -5.9", r#"{"n":-5}"#, true), + ("n = -5", r#"{"n":-5}"#, true), + ("n = 9007199254740992.0", r#"{"n":9007199254740993}"#, false), + ("n = 9007199254740993", r#"{"n":9007199254740992.0}"#, false), + ("n > -1", r#"{"n":18446744073709551615}"#, true), + ( + "n < 18446744073709551616.0", + r#"{"n":18446744073709551615}"#, + true, + ), + ( + "n < -9223372036854775808.0", + r#"{"n":-9223372036854775808}"#, + false, + ), + ("n:0.0", r#"{"n":[0.00000000000000001]}"#, false), + ("n:5.0", r#"{"n":[5]}"#, true), + ] { + matches(expr, record, expected); + } +} + +#[test] +fn has_and_boolean_values() { + matches("flags:garbage", r#"{"flags":[false]}"#, false); + matches("flags:false", r#"{"flags":[false]}"#, true); + matches("flags:FALSE", r#"{"flags":[false]}"#, true); + matches("flags:0", r#"{"flags":[false]}"#, true); + matches("tags:*", r#"{"tags":[0]}"#, true); + matches("tags:*", r#"{"tags":[]}"#, false); + matches("meta:owner", r#"{"meta":{"owner":"alice"}}"#, true); + matches("meta:*", r#"{"meta":{"owner":"alice"}}"#, true); + matches("meta:*", r#"{"meta":{}}"#, false); + matches("meta.owner=alice", r#"{"meta":{"owner":"alice"}}"#, true); +} + +#[test] +fn empty_filter_and_print_mode() { + let input = "{\"msg\":\"foobar\"}\n"; + let output = run("", input, &[]); + assert!(output.status.success()); + assert_eq!(output.stdout, input.as_bytes()); + let output = run("", input, &["--print"]); + assert!(output.status.success()); + assert!(output.stdout.is_empty()); + + let path = format!("{}/README.md", env!("CARGO_MANIFEST_DIR")); + let output = Command::new(env!("CARGO_BIN_EXE_aip-filter")) + .args(["", &path]) + .stdin(Stdio::null()) + .output() + .unwrap(); + assert!(output.status.success()); + assert_eq!(output.stdout, std::fs::read(path).unwrap()); +} + +#[test] +fn invalid_json_is_reported_and_returns_failure() { + let output = run("foobar", "not json\n{\"msg\":\"foobar\"}\n", &[]); + assert!(!output.status.success()); + assert_eq!(output.stdout, b"{\"msg\":\"foobar\"}\n"); + assert!( + String::from_utf8(output.stderr) + .unwrap() + .contains("<stdin>:1: invalid JSON") + ); +} + +#[cfg(unix)] +#[test] +fn output_flush_errors_are_reported() { + use std::os::fd::OwnedFd; + use std::os::unix::net::UnixStream; + let (writer, reader) = UnixStream::pair().unwrap(); + drop(reader); + let mut child = Command::new(env!("CARGO_BIN_EXE_aip-filter")) + .arg("foobar") + .stdin(Stdio::piped()) + .stdout(Stdio::from(OwnedFd::from(writer))) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + child + .stdin + .take() + .unwrap() + .write_all(b"{\"msg\":\"foobar\"}\n") + .unwrap(); + assert!(!child.wait_with_output().unwrap().status.success()); +} diff --git a/tests/eval_tests.rs b/tests/eval_tests.rs new file mode 100644 index 0000000..885b497 --- /dev/null +++ b/tests/eval_tests.rs @@ -0,0 +1,94 @@ +use aip_filter::{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}" + ); + } +} |
