| 1 | //! Integration tests for the Lua wrapper (ADR-22). |
| 2 | //! |
| 3 | //! These embed a real (vendored) Lua VM, build the `feel` module into it via |
| 4 | //! `build_module`, and drive it from Lua scripts — exercising the boundary exactly |
| 5 | //! as `require("feel")` would, but self-contained (no external interpreter or |
| 6 | //! separate module build). Mirrors feel-py's pytest suite and feel-wasm's smoke.mjs. |
| 7 | |
| 8 | use feel::build_module; |
| 9 | use feel::mlua::{Lua, Value}; |
| 10 | |
| 11 | /// A fresh Lua VM with the `feel` module bound to the `feel` global. |
| 12 | fn vm() -> Lua { |
| 13 | let lua = Lua::new(); |
| 14 | let module = build_module(&lua).expect("build feel module"); |
| 15 | lua.globals().set("feel", module).expect("bind feel global"); |
| 16 | lua |
| 17 | } |
| 18 | |
| 19 | // --------------------------------------------------------------------------- // |
| 20 | // Scalars, paths, control flow, contexts |
| 21 | // --------------------------------------------------------------------------- // |
| 22 | |
| 23 | #[test] |
| 24 | fn scalars_and_arithmetic() { |
| 25 | let lua = vm(); |
| 26 | assert_eq!( |
| 27 | lua.load(r#"return feel.evaluate("1 + 1")"#).eval::<i64>().unwrap(), |
| 28 | 2 |
| 29 | ); |
| 30 | assert_eq!( |
| 31 | lua.load(r#"return feel.evaluate("2 * 3 + 1")"#).eval::<i64>().unwrap(), |
| 32 | 7 |
| 33 | ); |
| 34 | assert_eq!( |
| 35 | lua.load(r#"return feel.evaluate([["foo"]])"#).eval::<String>().unwrap(), |
| 36 | "foo" |
| 37 | ); |
| 38 | assert!(lua.load(r#"return feel.evaluate("1 < 2")"#).eval::<bool>().unwrap()); |
| 39 | assert_eq!( |
| 40 | lua.load(r#"return feel.evaluate("3 / 2")"#).eval::<f64>().unwrap(), |
| 41 | 1.5 |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | #[test] |
| 46 | fn path_access_into_nested_table() { |
| 47 | let lua = vm(); |
| 48 | let n: i64 = lua |
| 49 | .load(r#"return feel.evaluate("person.age", { person = { age = 42 } })"#) |
| 50 | .eval() |
| 51 | .unwrap(); |
| 52 | assert_eq!(n, 42); |
| 53 | // An absent member is a value-null (-> nil). |
| 54 | let v: Value = lua |
| 55 | .load(r#"return feel.evaluate("person.missing", { person = { age = 42 } })"#) |
| 56 | .eval() |
| 57 | .unwrap(); |
| 58 | assert!(matches!(v, Value::Nil)); |
| 59 | } |
| 60 | |
| 61 | #[test] |
| 62 | fn for_loop_and_list_result() { |
| 63 | let lua = vm(); |
| 64 | let xs: Vec<i64> = lua |
| 65 | .load(r#"return feel.evaluate("for i in [1,2,3] return i * 2")"#) |
| 66 | .eval() |
| 67 | .unwrap(); |
| 68 | assert_eq!(xs, vec![2, 4, 6]); |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | fn sequence_table_in_context_forces_to_list() { |
| 73 | let lua = vm(); |
| 74 | // A Lua sequence used by a list builtin is forced one level into a FEEL list. |
| 75 | let n: i64 = lua |
| 76 | .load(r#"return feel.evaluate("sum(xs)", { xs = {1, 2, 3} })"#) |
| 77 | .eval() |
| 78 | .unwrap(); |
| 79 | assert_eq!(n, 6); |
| 80 | } |
| 81 | |
| 82 | // --------------------------------------------------------------------------- // |
| 83 | // Lenient vs strict, parse errors |
| 84 | // --------------------------------------------------------------------------- // |
| 85 | |
| 86 | #[test] |
| 87 | fn lenient_returns_nil_on_type_error() { |
| 88 | let lua = vm(); |
| 89 | let v: Value = lua.load(r#"return feel.evaluate([[1 + "x"]])"#).eval().unwrap(); |
| 90 | assert!(matches!(v, Value::Nil)); |
| 91 | } |
| 92 | |
| 93 | #[test] |
| 94 | fn strict_raises_on_error_null() { |
| 95 | let lua = vm(); |
| 96 | let res = lua.load(r#"return feel.evaluate_strict([[1 + "x"]])"#).eval::<Value>(); |
| 97 | assert!(res.is_err()); |
| 98 | } |
| 99 | |
| 100 | #[test] |
| 101 | fn parse_error_raises() { |
| 102 | let lua = vm(); |
| 103 | assert!(lua.load(r#"return feel.evaluate("1 +")"#).eval::<Value>().is_err()); |
| 104 | assert!(lua |
| 105 | .load(r#"return feel.evaluate("if x then")"#) |
| 106 | .eval::<Value>() |
| 107 | .is_err()); |
| 108 | } |
| 109 | |
| 110 | // --------------------------------------------------------------------------- // |
| 111 | // Host callables (ADR-23): the core of this wrapper |
| 112 | // --------------------------------------------------------------------------- // |
| 113 | |
| 114 | #[test] |
| 115 | fn host_callable_bound_in_context() { |
| 116 | let lua = vm(); |
| 117 | let n: i64 = lua |
| 118 | .load(r#"return feel.evaluate("f(2, 3)", { f = function(a, b) return a + b end })"#) |
| 119 | .eval() |
| 120 | .unwrap(); |
| 121 | assert_eq!(n, 5); |
| 122 | } |
| 123 | |
| 124 | #[test] |
| 125 | fn named_args_become_trailing_options_table() { |
| 126 | let lua = vm(); |
| 127 | // FEEL `f(2, b: 3)` -> Lua `f(2, { b = 3 })` (Lua has no kwargs). |
| 128 | let n: i64 = lua |
| 129 | .load(r#"return feel.evaluate("f(2, b: 3)", { f = function(a, opts) return a + opts.b end })"#) |
| 130 | .eval() |
| 131 | .unwrap(); |
| 132 | assert_eq!(n, 5); |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn host_callable_via_functions_registry() { |
| 137 | let lua = vm(); |
| 138 | let n: i64 = lua |
| 139 | .load(r#"return feel.evaluate("add(2, 3)", nil, { add = function(a, b) return a + b end })"#) |
| 140 | .eval() |
| 141 | .unwrap(); |
| 142 | assert_eq!(n, 5); |
| 143 | } |
| 144 | |
| 145 | #[test] |
| 146 | fn context_binding_shadows_builtin() { |
| 147 | let lua = vm(); |
| 148 | // `count` is a builtin; a context binding of the same name wins (host override). |
| 149 | let n: i64 = lua |
| 150 | .load(r#"return feel.evaluate("count([1,2,3])", { count = function(x) return 99 end })"#) |
| 151 | .eval() |
| 152 | .unwrap(); |
| 153 | assert_eq!(n, 99); |
| 154 | } |
| 155 | |
| 156 | #[test] |
| 157 | fn returned_callable_passes_back_by_identity() { |
| 158 | let lua = vm(); |
| 159 | let same: bool = lua |
| 160 | .load( |
| 161 | r#" |
| 162 | local g = function() return 1 end |
| 163 | return feel.evaluate("f", { f = g }) == g |
| 164 | "#, |
| 165 | ) |
| 166 | .eval() |
| 167 | .unwrap(); |
| 168 | assert!(same); |
| 169 | } |
| 170 | |
| 171 | #[test] |
| 172 | fn returned_table_passes_back_by_identity() { |
| 173 | let lua = vm(); |
| 174 | let same: bool = lua |
| 175 | .load( |
| 176 | r#" |
| 177 | local obj = { a = 1 } |
| 178 | return feel.evaluate("o", { o = obj }) == obj |
| 179 | "#, |
| 180 | ) |
| 181 | .eval() |
| 182 | .unwrap(); |
| 183 | assert!(same); |
| 184 | } |
| 185 | |
| 186 | // --------------------------------------------------------------------------- // |
| 187 | // Laziness (ADR-21): on-demand member access, identity, cyclic-safe |
| 188 | // --------------------------------------------------------------------------- // |
| 189 | |
| 190 | #[test] |
| 191 | fn member_access_is_lazy_and_skips_untouched_siblings() { |
| 192 | let lua = vm(); |
| 193 | let (r, accessed): (i64, String) = lua |
| 194 | .load( |
| 195 | r#" |
| 196 | local accessed = {} |
| 197 | local backing = { a = 1, b = 2 } |
| 198 | local proxy = setmetatable({}, { __index = function(_, k) |
| 199 | accessed[#accessed + 1] = k |
| 200 | return backing[k] |
| 201 | end }) |
| 202 | local r = feel.evaluate("o.a", { o = proxy }) |
| 203 | return r, table.concat(accessed, ",") |
| 204 | "#, |
| 205 | ) |
| 206 | .eval() |
| 207 | .unwrap(); |
| 208 | assert_eq!(r, 1); |
| 209 | // Only `a` was read — the sibling `b` was never touched. |
| 210 | assert_eq!(accessed, "a"); |
| 211 | } |
| 212 | |
| 213 | #[test] |
| 214 | fn cyclic_table_navigates_a_finite_path() { |
| 215 | let lua = vm(); |
| 216 | let n: i64 = lua |
| 217 | .load( |
| 218 | r#" |
| 219 | local node = { value = 7 } |
| 220 | node.self = node |
| 221 | return feel.evaluate("o.self.self.value", { o = node }) |
| 222 | "#, |
| 223 | ) |
| 224 | .eval() |
| 225 | .unwrap(); |
| 226 | assert_eq!(n, 7); |
| 227 | } |
| 228 | |
| 229 | // --------------------------------------------------------------------------- // |
| 230 | // Temporal mapping (RD-8): ISO-8601 strings |
| 231 | // --------------------------------------------------------------------------- // |
| 232 | |
| 233 | #[test] |
| 234 | fn temporal_values_cross_out_as_iso_strings() { |
| 235 | let lua = vm(); |
| 236 | assert_eq!( |
| 237 | lua.load(r#"return feel.evaluate("date(2020, 1, 15)")"#).eval::<String>().unwrap(), |
| 238 | "2020-01-15" |
| 239 | ); |
| 240 | assert_eq!( |
| 241 | lua.load(r#"return feel.evaluate([[duration("P1Y2M")]])"#).eval::<String>().unwrap(), |
| 242 | "P1Y2M" |
| 243 | ); |
| 244 | } |
| 245 | |
| 246 | // --------------------------------------------------------------------------- // |
| 247 | // Error semantics (ADR-14): host error -> error-null, distinct from value-null |
| 248 | // --------------------------------------------------------------------------- // |
| 249 | |
| 250 | #[test] |
| 251 | fn host_error_is_error_null_lenient_nil_strict_raises() { |
| 252 | let lua = vm(); |
| 253 | // Lenient: a raised Lua error in a host callable surfaces as nil. |
| 254 | let v: Value = lua |
| 255 | .load(r#"return feel.evaluate("f()", { f = function() error("boom") end })"#) |
| 256 | .eval() |
| 257 | .unwrap(); |
| 258 | assert!(matches!(v, Value::Nil)); |
| 259 | // Strict: the same error-null is surfaced as a (pcall-catchable) Lua error. |
| 260 | let res = lua |
| 261 | .load(r#"return feel.evaluate_strict("f()", { f = function() error("boom") end })"#) |
| 262 | .eval::<Value>(); |
| 263 | assert!(res.is_err()); |
| 264 | } |
| 265 | |
| 266 | #[test] |
| 267 | fn host_error_null_propagates_stickily() { |
| 268 | let lua = vm(); |
| 269 | // `f() + 1` stays an error-null: lenient nil, strict raises. |
| 270 | let v: Value = lua |
| 271 | .load(r#"return feel.evaluate("f() + 1", { f = function() error("boom") end })"#) |
| 272 | .eval() |
| 273 | .unwrap(); |
| 274 | assert!(matches!(v, Value::Nil)); |
| 275 | let res = lua |
| 276 | .load(r#"return feel.evaluate_strict("f() + 1", { f = function() error("boom") end })"#) |
| 277 | .eval::<Value>(); |
| 278 | assert!(res.is_err()); |
| 279 | } |
| 280 | |
| 281 | #[test] |
| 282 | fn returned_nil_is_value_null_not_error_null() { |
| 283 | let lua = vm(); |
| 284 | // A callable that *returns* nil is a value-null, not an error -> strict does NOT raise. |
| 285 | let v: Value = lua |
| 286 | .load(r#"return feel.evaluate_strict("g()", { g = function() return nil end })"#) |
| 287 | .eval() |
| 288 | .unwrap(); |
| 289 | assert!(matches!(v, Value::Nil)); |
| 290 | } |
| 291 | |
| 292 | #[test] |
| 293 | fn failing_member_access_is_value_null_not_error_null() { |
| 294 | let lua = vm(); |
| 295 | // A raised `__index` during member access is a value-null (absent member), |
| 296 | // not an error -> strict does NOT raise. |
| 297 | let v: Value = lua |
| 298 | .load( |
| 299 | r#" |
| 300 | local proxy = setmetatable({}, { __index = function() error("no such field") end }) |
| 301 | return feel.evaluate_strict("o.missing", { o = proxy }) |
| 302 | "#, |
| 303 | ) |
| 304 | .eval() |
| 305 | .unwrap(); |
| 306 | assert!(matches!(v, Value::Nil)); |
| 307 | } |
| 308 | |
| 309 | // --------------------------------------------------------------------------- // |
| 310 | // compile() reuse |
| 311 | // --------------------------------------------------------------------------- // |
| 312 | |
| 313 | #[test] |
| 314 | fn compiled_expression_reused_across_contexts() { |
| 315 | let lua = vm(); |
| 316 | let (a, b): (i64, i64) = lua |
| 317 | .load( |
| 318 | r#" |
| 319 | local prog = feel.compile("a + 1") |
| 320 | return prog:evaluate({ a = 1 }), prog:evaluate({ a = 41 }) |
| 321 | "#, |
| 322 | ) |
| 323 | .eval() |
| 324 | .unwrap(); |
| 325 | assert_eq!((a, b), (2, 42)); |
| 326 | } |
| 327 | |
| 328 | // --------------------------------------------------------------------------- // |
| 329 | // Descendant `e ... name` (DMN 10.3.2.7, grammar rule 68) |
| 330 | // --------------------------------------------------------------------------- // |
| 331 | |
| 332 | #[test] |
| 333 | fn descendant_collects_across_a_list_of_tables() { |
| 334 | let lua = vm(); |
| 335 | let xs: Vec<i64> = lua |
| 336 | .load(r#"return feel.evaluate("[{x:1},{x:2}] ... x")"#) |
| 337 | .eval() |
| 338 | .unwrap(); |
| 339 | assert_eq!(xs, vec![1, 2]); |
| 340 | } |
| 341 | |
| 342 | #[test] |
| 343 | fn descendant_no_match_is_empty_list() { |
| 344 | let lua = vm(); |
| 345 | let xs: Vec<i64> = lua |
| 346 | .load(r#"return feel.evaluate("{a:1} ... z")"#) |
| 347 | .eval() |
| 348 | .unwrap(); |
| 349 | assert!(xs.is_empty()); |
| 350 | } |
| 351 | |
| 352 | #[test] |
| 353 | fn descendant_descends_into_nested_table() { |
| 354 | let lua = vm(); |
| 355 | let names: Vec<String> = lua |
| 356 | .load( |
| 357 | r#"return feel.evaluate("o ... name", { o = { customer = { name = "Ada" } } })"#, |
| 358 | ) |
| 359 | .eval() |
| 360 | .unwrap(); |
| 361 | assert_eq!(names, vec!["Ada".to_string()]); |
| 362 | } |
| 363 | |
| 364 | #[test] |
| 365 | fn descendant_terminates_on_a_cyclic_table() { |
| 366 | // Without the `Table::to_pointer`-based identity override this would loop |
| 367 | // forever; with it, the back-edge into the same table is cut after one re-entry. |
| 368 | let lua = vm(); |
| 369 | let xs: Vec<i64> = lua |
| 370 | .load( |
| 371 | r#" |
| 372 | local c = { value = 42 } |
| 373 | c.next = c -- self-referential (cyclic) Lua table |
| 374 | return feel.evaluate("c ... value", { c = c }) |
| 375 | "#, |
| 376 | ) |
| 377 | .eval() |
| 378 | .unwrap(); |
| 379 | assert_eq!(xs, vec![42, 42]); |
| 380 | } |