| 1 | //! Fixture-driven test closing the loop: |
| 2 | //! TCK XML -> tools/ingest_tck.py -> tests/fixtures/*.json -> feel-core. |
| 3 | //! |
| 4 | //! Every fixture case is asserted according to its `comparison` mode: |
| 5 | //! * `exact` -> structural equality |
| 6 | //! * `f64_epsilon` -> numeric tolerance (our documented decimal128->f64 deviation) |
| 7 | //! |
| 8 | //! An optional `coerce_to` field (a FEEL type string, e.g. `"string"`, |
| 9 | //! `"list<number>"`) triggers §10.3.2.9.4 implicit conversion of the evaluated |
| 10 | //! result before comparison. This models the decision-variable / literal-expression |
| 11 | //! `typeRef` coercion that the 0082-feel-coercion suite exercises. |
| 12 | |
| 13 | use std::fs; |
| 14 | use std::path::{Path, PathBuf}; |
| 15 | |
| 16 | use feel_core::{coerce, evaluate, parse_type, Context, Value}; |
| 17 | |
| 18 | fn fixtures_dir() -> PathBuf { |
| 19 | Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures") |
| 20 | } |
| 21 | |
| 22 | fn fixture_files() -> Vec<PathBuf> { |
| 23 | let dir = fixtures_dir(); |
| 24 | let mut files: Vec<PathBuf> = fs::read_dir(&dir) |
| 25 | .unwrap_or_else(|e| panic!("cannot read fixtures dir {}: {e}", dir.display())) |
| 26 | .filter_map(|entry| entry.ok().map(|e| e.path())) |
| 27 | .filter(|p| p.extension().map(|x| x == "json").unwrap_or(false)) |
| 28 | .collect(); |
| 29 | files.sort(); |
| 30 | files |
| 31 | } |
| 32 | |
| 33 | #[test] |
| 34 | fn tck_fixtures_pass() { |
| 35 | let files = fixture_files(); |
| 36 | assert!( |
| 37 | !files.is_empty(), |
| 38 | "no fixtures in {} — run tools/ingest_tck.py first", |
| 39 | fixtures_dir().display() |
| 40 | ); |
| 41 | |
| 42 | let mut checked = 0usize; |
| 43 | for file in &files { |
| 44 | let text = fs::read_to_string(file).expect("read fixture"); |
| 45 | let cases: serde_json::Value = serde_json::from_str(&text).expect("parse fixture JSON"); |
| 46 | let cases = cases.as_array().expect("fixture must be a JSON array"); |
| 47 | |
| 48 | for case in cases { |
| 49 | checked += 1; |
| 50 | assert_case(case); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // Visible with `cargo test -p feel-core -- --nocapture`. |
| 55 | eprintln!("verified {checked} TCK fixture case(s) across {} file(s)", files.len()); |
| 56 | assert!(checked > 0, "fixtures contained no cases"); |
| 57 | } |
| 58 | |
| 59 | fn assert_case(case: &serde_json::Value) { |
| 60 | let id = case["id"].as_str().unwrap_or("<no-id>"); |
| 61 | let expr = case["expression"] |
| 62 | .as_str() |
| 63 | .unwrap_or_else(|| panic!("[{id}] missing `expression`")); |
| 64 | |
| 65 | // The regex builtins are dialect-dependent and need a host provider (ADR-16). |
| 66 | // A `--no-default-features` build ships no provider, so those cases evaluate |
| 67 | // to null; skip them here (they are validated under the default `rust-regex`). |
| 68 | #[cfg(not(feature = "rust-regex"))] |
| 69 | if expr.contains("matches(") || expr.contains("replace(") || expr.contains("split(") { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | let comparison = case["comparison"].as_str().unwrap_or("exact"); |
| 74 | |
| 75 | let ctx = context_from_case(case, id); |
| 76 | // Collapse error provenance: TCK expectations are spec-shaped (a type error |
| 77 | // is just `null`), so we compare against the lenient view (ADR-14). |
| 78 | let evaluated = evaluate(expr, &ctx) |
| 79 | .unwrap_or_else(|e| panic!("[{id}] evaluating `{expr}` failed: {e}")) |
| 80 | .into_lenient(); |
| 81 | |
| 82 | // Optional §10.3.2.9.4 coercion: apply when the fixture carries a target type |
| 83 | // (models the decision-variable / literal-expression `typeRef` boundary). |
| 84 | let got = if let Some(type_str) = case["coerce_to"].as_str() { |
| 85 | let ty = parse_type(type_str) |
| 86 | .unwrap_or_else(|| panic!("[{id}] unparseable coerce_to: {type_str:?}")); |
| 87 | coerce(evaluated, &ty) |
| 88 | } else { |
| 89 | evaluated |
| 90 | }; |
| 91 | |
| 92 | match comparison { |
| 93 | "exact" => { |
| 94 | let expected = expected_value(case, id); |
| 95 | assert_eq!(got, expected, "[{id}] `{expr}`: exact comparison failed"); |
| 96 | } |
| 97 | "f64_epsilon" => { |
| 98 | let epsilon = case["epsilon"] |
| 99 | .as_f64() |
| 100 | .unwrap_or_else(|| panic!("[{id}] f64_epsilon case missing numeric `epsilon`")); |
| 101 | let expected = case["expected"] |
| 102 | .as_f64() |
| 103 | .unwrap_or_else(|| panic!("[{id}] f64_epsilon case `expected` is not numeric")); |
| 104 | match got { |
| 105 | Value::Number(actual) => assert!( |
| 106 | (actual - expected).abs() <= epsilon, |
| 107 | "[{id}] `{expr}`: {actual} not within {epsilon} of {expected}" |
| 108 | ), |
| 109 | other => panic!("[{id}] `{expr}`: expected a Number, got {other:?}"), |
| 110 | } |
| 111 | } |
| 112 | other => panic!("[{id}] unknown comparison mode `{other}`"), |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /// Build the evaluation [`Context`] for a case from its optional `context` |
| 117 | /// object (the ingested `<inputNode>` values). Absent or `null` -> empty. |
| 118 | fn context_from_case(case: &serde_json::Value, id: &str) -> Context { |
| 119 | let mut ctx = Context::empty(); |
| 120 | match &case["context"] { |
| 121 | serde_json::Value::Object(map) => { |
| 122 | for (key, val) in map { |
| 123 | ctx.insert(key.clone(), json_to_value(val)); |
| 124 | } |
| 125 | } |
| 126 | serde_json::Value::Null => {} |
| 127 | other => panic!("[{id}] `context` must be a JSON object, got {other}"), |
| 128 | } |
| 129 | ctx |
| 130 | } |
| 131 | |
| 132 | /// Map a fixture's (`expected`, `expected_type`) pair to a [`Value`]. |
| 133 | fn expected_value(case: &serde_json::Value, id: &str) -> Value { |
| 134 | let expected = &case["expected"]; |
| 135 | match case["expected_type"].as_str() { |
| 136 | Some("boolean") => Value::Boolean( |
| 137 | expected |
| 138 | .as_bool() |
| 139 | .unwrap_or_else(|| panic!("[{id}] expected boolean")), |
| 140 | ), |
| 141 | Some("number") => Value::Number( |
| 142 | expected |
| 143 | .as_f64() |
| 144 | .unwrap_or_else(|| panic!("[{id}] expected number")), |
| 145 | ), |
| 146 | Some("string") => Value::String( |
| 147 | expected |
| 148 | .as_str() |
| 149 | .unwrap_or_else(|| panic!("[{id}] expected string")) |
| 150 | .to_string(), |
| 151 | ), |
| 152 | Some("list") | Some("context") => json_to_value(expected), |
| 153 | Some("null") => Value::Null, |
| 154 | // Temporal expectations are the canonical ISO-8601 string (ADR-18); parse |
| 155 | // them into a temporal Value via feel-core's own parser so the comparison |
| 156 | // is structural and exact. |
| 157 | Some(kind @ ("date" | "time" | "date and time" | "duration")) => { |
| 158 | let text = expected |
| 159 | .as_str() |
| 160 | .unwrap_or_else(|| panic!("[{id}] temporal `expected` is not a string")); |
| 161 | feel_core::temporal_from_iso(kind, text) |
| 162 | .unwrap_or_else(|| panic!("[{id}] cannot parse temporal {kind} {text:?}")) |
| 163 | } |
| 164 | // Fall back to the JSON shape if the type tag is absent. |
| 165 | _ => json_to_value(expected), |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /// Recursively map a fixture's JSON value to a [`Value`] (used for list results, |
| 170 | /// which may be nested). Note: list comparison is structural/exact — numeric |
| 171 | /// items inside a list are not given the scalar `f64_epsilon` tolerance. |
| 172 | fn json_to_value(json: &serde_json::Value) -> Value { |
| 173 | match json { |
| 174 | serde_json::Value::Bool(b) => Value::Boolean(*b), |
| 175 | serde_json::Value::Number(n) => Value::Number(n.as_f64().expect("finite number")), |
| 176 | serde_json::Value::String(s) => Value::String(s.clone()), |
| 177 | serde_json::Value::Array(a) => Value::List(a.iter().map(json_to_value).collect()), |
| 178 | // A tagged temporal input `{"@type": …, "@value": …}` (ADR-18) binds as a |
| 179 | // typed temporal Value rather than a plain context. |
| 180 | serde_json::Value::Object(map) => { |
| 181 | if let (Some(serde_json::Value::String(kind)), Some(serde_json::Value::String(text))) = |
| 182 | (map.get("@type"), map.get("@value")) |
| 183 | { |
| 184 | return feel_core::temporal_from_iso(kind, text) |
| 185 | .unwrap_or_else(|| panic!("cannot parse temporal {kind} {text:?}")); |
| 186 | } |
| 187 | Value::Context(map.iter().map(|(k, v)| (k.clone(), json_to_value(v))).collect()) |
| 188 | } |
| 189 | serde_json::Value::Null => Value::Null, |
| 190 | } |
| 191 | } |