| 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 | // Special-character names (§10.3.1.6, ADR-47) |
| 112 | // --------------------------------------------------------------------------- // |
| 113 | |
| 114 | #[test] |
| 115 | fn special_char_name_resolves_against_scope() { |
| 116 | let lua = vm(); |
| 117 | // `a-b` is the in-scope NAME when the table binds it, subtraction otherwise; |
| 118 | // `a - b` is the same candidate (spaces are not part of the name). |
| 119 | let n: f64 = lua |
| 120 | .load(r#"return feel.evaluate("a-b", { ["a-b"] = 5 }, nil, "symbols")"#) |
| 121 | .eval() |
| 122 | .unwrap(); |
| 123 | assert_eq!(n, 5.0); |
| 124 | let n: f64 = lua |
| 125 | .load(r#"return feel.evaluate("a - b", { ["a-b"] = 5 }, nil, "symbols")"#) |
| 126 | .eval() |
| 127 | .unwrap(); |
| 128 | assert_eq!(n, 5.0); |
| 129 | let n: f64 = lua |
| 130 | .load(r#"return feel.evaluate("a-b", { a = 7, b = 3 }, nil, "symbols")"#) |
| 131 | .eval() |
| 132 | .unwrap(); |
| 133 | assert_eq!(n, 4.0); |
| 134 | // `(a)-(b)` is the escape hatch: subtraction even when `a-b` is the name. |
| 135 | let n: f64 = lua |
| 136 | .load(r#"return feel.evaluate("(a)-(b)", { a = 7, b = 3, ["a-b"] = 100 }, nil, "symbols")"#) |
| 137 | .eval() |
| 138 | .unwrap(); |
| 139 | assert_eq!(n, 4.0); |
| 140 | // A hyphen name as a path base: `x-y.hour` fuses `x-y`, `.hour` stays a member. |
| 141 | let n: i64 = lua |
| 142 | .load(r#"return feel.evaluate("x-y.hour", { ["x-y"] = { hour = 3 } }, nil, "symbols")"#) |
| 143 | .eval() |
| 144 | .unwrap(); |
| 145 | assert_eq!(n, 3); |
| 146 | // A member word never fuses against the root scope: `x.y-z` is (x.y) - z. |
| 147 | let n: i64 = lua |
| 148 | .load(r#"return feel.evaluate("x.y-z", { x = { y = 10 }, z = 3, ["y-z"] = 1 }, nil, "symbols")"#) |
| 149 | .eval() |
| 150 | .unwrap(); |
| 151 | assert_eq!(n, 7); |
| 152 | } |
| 153 | |
| 154 | #[test] |
| 155 | fn special_char_name_resolves_host_function_from_registry() { |
| 156 | let lua = vm(); |
| 157 | // The ambient `functions` table is part of the parse-time scope (ADR-47): a |
| 158 | // hyphen-named host function is callable. |
| 159 | let n: i64 = lua |
| 160 | .load(r#"return feel.evaluate("my-fn(2)", {}, { ["my-fn"] = function(x) return x * 10 end }, "symbols")"#) |
| 161 | .eval() |
| 162 | .unwrap(); |
| 163 | assert_eq!(n, 20); |
| 164 | let v: Value = lua |
| 165 | .load(r#"return feel.evaluate("my-fn(2)", {}, nil, "symbols")"#) |
| 166 | .eval() |
| 167 | .unwrap(); |
| 168 | assert!(matches!(v, Value::Nil)); |
| 169 | } |
| 170 | |
| 171 | #[test] |
| 172 | fn compile_userdata_is_scope_adaptive() { |
| 173 | let lua = vm(); |
| 174 | // ADR-48: one compiled program follows each call's context. |
| 175 | let n: f64 = lua |
| 176 | .load(r#"local p = feel.compile("a-b", "symbols"); return p:evaluate({ ["a-b"] = 5 })"#) |
| 177 | .eval() |
| 178 | .unwrap(); |
| 179 | assert_eq!(n, 5.0); |
| 180 | let n: f64 = lua |
| 181 | .load(r#"local p = feel.compile("a-b", "symbols"); return p:evaluate({ a = 7, b = 3 })"#) |
| 182 | .eval() |
| 183 | .unwrap(); |
| 184 | assert_eq!(n, 4.0); |
| 185 | } |
| 186 | |
| 187 | #[test] |
| 188 | fn names_levels_backticks_and_members() { |
| 189 | let lua = vm(); |
| 190 | // Default (ADR-48, revised): quoted — `a-b` is subtraction. |
| 191 | let n: f64 = lua |
| 192 | .load(r#"return feel.evaluate("a-b", { a = 7, b = 3, ["a-b"] = 100 })"#) |
| 193 | .eval() |
| 194 | .unwrap(); |
| 195 | assert_eq!(n, 4.0); |
| 196 | let n: f64 = lua |
| 197 | .load(r#"return feel.evaluate("a-b", { a = 7, b = 3, ["a-b"] = 100 }, nil, "quoted")"#) |
| 198 | .eval() |
| 199 | .unwrap(); |
| 200 | assert_eq!(n, 4.0); |
| 201 | let n: f64 = lua |
| 202 | .load(r#"return feel.compile("a-b", "quoted"):evaluate({ a = 7, b = 3, ["a-b"] = 100 })"#) |
| 203 | .eval() |
| 204 | .unwrap(); |
| 205 | assert_eq!(n, 4.0); |
| 206 | let n: f64 = lua |
| 207 | .load(r#"return feel.evaluate("`Profit and loss`", { ["Profit and loss"] = 1 })"#) |
| 208 | .eval() |
| 209 | .unwrap(); |
| 210 | assert_eq!(n, 1.0); |
| 211 | let s: String = lua |
| 212 | .load(r#"return feel.evaluate("applicant.first-name", { applicant = { ["first-name"] = "Ada" } }, nil, "symbols")"#) |
| 213 | .eval() |
| 214 | .unwrap(); |
| 215 | assert_eq!(s, "Ada"); |
| 216 | // Selection maps over a sequence (§10.3.2.5) — plain and hyphen members. |
| 217 | let n: i64 = lua |
| 218 | .load(r#"return feel.evaluate("o.items.x", { o = { items = { { x = 1 }, { x = 2 } } } })[2]"#) |
| 219 | .eval() |
| 220 | .unwrap(); |
| 221 | assert_eq!(n, 2); |
| 222 | let n: i64 = lua |
| 223 | .load(r#"return feel.evaluate("orders.unit-price", { orders = { { ["unit-price"] = 3 } } }, nil, "symbols")[1]"#) |
| 224 | .eval() |
| 225 | .unwrap(); |
| 226 | assert_eq!(n, 3); |
| 227 | assert!(lua |
| 228 | .load(r#"return feel.evaluate("1", {}, nil, "full")"#) |
| 229 | .eval::<Value>() |
| 230 | .is_err()); |
| 231 | } |
| 232 | |
| 233 | // --------------------------------------------------------------------------- // |
| 234 | // Host callables (ADR-23): the core of this wrapper |
| 235 | // --------------------------------------------------------------------------- // |
| 236 | |
| 237 | #[test] |
| 238 | fn host_callable_bound_in_context() { |
| 239 | let lua = vm(); |
| 240 | let n: i64 = lua |
| 241 | .load(r#"return feel.evaluate("f(2, 3)", { f = function(a, b) return a + b end })"#) |
| 242 | .eval() |
| 243 | .unwrap(); |
| 244 | assert_eq!(n, 5); |
| 245 | } |
| 246 | |
| 247 | #[test] |
| 248 | fn named_args_become_trailing_options_table() { |
| 249 | let lua = vm(); |
| 250 | // FEEL `f(2, b: 3)` -> Lua `f(2, { b = 3 })` (Lua has no kwargs). |
| 251 | let n: i64 = lua |
| 252 | .load(r#"return feel.evaluate("f(2, b: 3)", { f = function(a, opts) return a + opts.b end })"#) |
| 253 | .eval() |
| 254 | .unwrap(); |
| 255 | assert_eq!(n, 5); |
| 256 | } |
| 257 | |
| 258 | #[test] |
| 259 | fn host_callable_via_functions_registry() { |
| 260 | let lua = vm(); |
| 261 | let n: i64 = lua |
| 262 | .load(r#"return feel.evaluate("add(2, 3)", nil, { add = function(a, b) return a + b end })"#) |
| 263 | .eval() |
| 264 | .unwrap(); |
| 265 | assert_eq!(n, 5); |
| 266 | } |
| 267 | |
| 268 | #[test] |
| 269 | fn context_binding_shadows_builtin() { |
| 270 | let lua = vm(); |
| 271 | // `count` is a builtin; a context binding of the same name wins (host override). |
| 272 | let n: i64 = lua |
| 273 | .load(r#"return feel.evaluate("count([1,2,3])", { count = function(x) return 99 end })"#) |
| 274 | .eval() |
| 275 | .unwrap(); |
| 276 | assert_eq!(n, 99); |
| 277 | } |
| 278 | |
| 279 | #[test] |
| 280 | fn returned_callable_passes_back_by_identity() { |
| 281 | let lua = vm(); |
| 282 | let same: bool = lua |
| 283 | .load( |
| 284 | r#" |
| 285 | local g = function() return 1 end |
| 286 | return feel.evaluate("f", { f = g }) == g |
| 287 | "#, |
| 288 | ) |
| 289 | .eval() |
| 290 | .unwrap(); |
| 291 | assert!(same); |
| 292 | } |
| 293 | |
| 294 | #[test] |
| 295 | fn returned_table_passes_back_by_identity() { |
| 296 | let lua = vm(); |
| 297 | let same: bool = lua |
| 298 | .load( |
| 299 | r#" |
| 300 | local obj = { a = 1 } |
| 301 | return feel.evaluate("o", { o = obj }) == obj |
| 302 | "#, |
| 303 | ) |
| 304 | .eval() |
| 305 | .unwrap(); |
| 306 | assert!(same); |
| 307 | } |
| 308 | |
| 309 | // --------------------------------------------------------------------------- // |
| 310 | // Laziness (ADR-21): on-demand member access, identity, cyclic-safe |
| 311 | // --------------------------------------------------------------------------- // |
| 312 | |
| 313 | #[test] |
| 314 | fn member_access_is_lazy_and_skips_untouched_siblings() { |
| 315 | let lua = vm(); |
| 316 | let (r, accessed): (i64, String) = lua |
| 317 | .load( |
| 318 | r#" |
| 319 | local accessed = {} |
| 320 | local backing = { a = 1, b = 2 } |
| 321 | local proxy = setmetatable({}, { __index = function(_, k) |
| 322 | accessed[#accessed + 1] = k |
| 323 | return backing[k] |
| 324 | end }) |
| 325 | local r = feel.evaluate("o.a", { o = proxy }) |
| 326 | return r, table.concat(accessed, ",") |
| 327 | "#, |
| 328 | ) |
| 329 | .eval() |
| 330 | .unwrap(); |
| 331 | assert_eq!(r, 1); |
| 332 | // Only `a` was read — the sibling `b` was never touched. |
| 333 | assert_eq!(accessed, "a"); |
| 334 | } |
| 335 | |
| 336 | #[test] |
| 337 | fn cyclic_table_navigates_a_finite_path() { |
| 338 | let lua = vm(); |
| 339 | let n: i64 = lua |
| 340 | .load( |
| 341 | r#" |
| 342 | local node = { value = 7 } |
| 343 | node.self = node |
| 344 | return feel.evaluate("o.self.self.value", { o = node }) |
| 345 | "#, |
| 346 | ) |
| 347 | .eval() |
| 348 | .unwrap(); |
| 349 | assert_eq!(n, 7); |
| 350 | } |
| 351 | |
| 352 | // --------------------------------------------------------------------------- // |
| 353 | // Temporal mapping (RD-8): ISO-8601 strings |
| 354 | // --------------------------------------------------------------------------- // |
| 355 | |
| 356 | #[test] |
| 357 | fn temporal_values_cross_out_as_iso_strings() { |
| 358 | let lua = vm(); |
| 359 | assert_eq!( |
| 360 | lua.load(r#"return feel.evaluate("date(2020, 1, 15)")"#).eval::<String>().unwrap(), |
| 361 | "2020-01-15" |
| 362 | ); |
| 363 | assert_eq!( |
| 364 | lua.load(r#"return feel.evaluate([[duration("P1Y2M")]])"#).eval::<String>().unwrap(), |
| 365 | "P1Y2M" |
| 366 | ); |
| 367 | } |
| 368 | |
| 369 | // --------------------------------------------------------------------------- // |
| 370 | // Error semantics (ADR-14): host error -> error-null, distinct from value-null |
| 371 | // --------------------------------------------------------------------------- // |
| 372 | |
| 373 | #[test] |
| 374 | fn host_error_is_error_null_lenient_nil_strict_raises() { |
| 375 | let lua = vm(); |
| 376 | // Lenient: a raised Lua error in a host callable surfaces as nil. |
| 377 | let v: Value = lua |
| 378 | .load(r#"return feel.evaluate("f()", { f = function() error("boom") end })"#) |
| 379 | .eval() |
| 380 | .unwrap(); |
| 381 | assert!(matches!(v, Value::Nil)); |
| 382 | // Strict: the same error-null is surfaced as a (pcall-catchable) Lua error. |
| 383 | let res = lua |
| 384 | .load(r#"return feel.evaluate_strict("f()", { f = function() error("boom") end })"#) |
| 385 | .eval::<Value>(); |
| 386 | assert!(res.is_err()); |
| 387 | } |
| 388 | |
| 389 | #[test] |
| 390 | fn host_error_null_propagates_stickily() { |
| 391 | let lua = vm(); |
| 392 | // `f() + 1` stays an error-null: lenient nil, strict raises. |
| 393 | let v: Value = lua |
| 394 | .load(r#"return feel.evaluate("f() + 1", { f = function() error("boom") end })"#) |
| 395 | .eval() |
| 396 | .unwrap(); |
| 397 | assert!(matches!(v, Value::Nil)); |
| 398 | let res = lua |
| 399 | .load(r#"return feel.evaluate_strict("f() + 1", { f = function() error("boom") end })"#) |
| 400 | .eval::<Value>(); |
| 401 | assert!(res.is_err()); |
| 402 | } |
| 403 | |
| 404 | #[test] |
| 405 | fn returned_nil_is_value_null_not_error_null() { |
| 406 | let lua = vm(); |
| 407 | // A callable that *returns* nil is a value-null, not an error -> strict does NOT raise. |
| 408 | let v: Value = lua |
| 409 | .load(r#"return feel.evaluate_strict("g()", { g = function() return nil end })"#) |
| 410 | .eval() |
| 411 | .unwrap(); |
| 412 | assert!(matches!(v, Value::Nil)); |
| 413 | } |
| 414 | |
| 415 | #[test] |
| 416 | fn failing_member_access_is_value_null_not_error_null() { |
| 417 | let lua = vm(); |
| 418 | // A raised `__index` during member access is a value-null (absent member), |
| 419 | // not an error -> strict does NOT raise. |
| 420 | let v: Value = lua |
| 421 | .load( |
| 422 | r#" |
| 423 | local proxy = setmetatable({}, { __index = function() error("no such field") end }) |
| 424 | return feel.evaluate_strict("o.missing", { o = proxy }) |
| 425 | "#, |
| 426 | ) |
| 427 | .eval() |
| 428 | .unwrap(); |
| 429 | assert!(matches!(v, Value::Nil)); |
| 430 | } |
| 431 | |
| 432 | // --------------------------------------------------------------------------- // |
| 433 | // compile() reuse |
| 434 | // --------------------------------------------------------------------------- // |
| 435 | |
| 436 | #[test] |
| 437 | fn compiled_expression_reused_across_contexts() { |
| 438 | let lua = vm(); |
| 439 | let (a, b): (i64, i64) = lua |
| 440 | .load( |
| 441 | r#" |
| 442 | local prog = feel.compile("a + 1") |
| 443 | return prog:evaluate({ a = 1 }), prog:evaluate({ a = 41 }) |
| 444 | "#, |
| 445 | ) |
| 446 | .eval() |
| 447 | .unwrap(); |
| 448 | assert_eq!((a, b), (2, 42)); |
| 449 | } |
| 450 | |
| 451 | // --------------------------------------------------------------------------- // |
| 452 | // Descendant `e ... name` (DMN 10.3.2.7, grammar rule 68) |
| 453 | // --------------------------------------------------------------------------- // |
| 454 | |
| 455 | #[test] |
| 456 | fn descendant_collects_across_a_list_of_tables() { |
| 457 | let lua = vm(); |
| 458 | let xs: Vec<i64> = lua |
| 459 | .load(r#"return feel.evaluate("[{x:1},{x:2}] ... x")"#) |
| 460 | .eval() |
| 461 | .unwrap(); |
| 462 | assert_eq!(xs, vec![1, 2]); |
| 463 | } |
| 464 | |
| 465 | #[test] |
| 466 | fn descendant_no_match_is_empty_list() { |
| 467 | let lua = vm(); |
| 468 | let xs: Vec<i64> = lua |
| 469 | .load(r#"return feel.evaluate("{a:1} ... z")"#) |
| 470 | .eval() |
| 471 | .unwrap(); |
| 472 | assert!(xs.is_empty()); |
| 473 | } |
| 474 | |
| 475 | #[test] |
| 476 | fn descendant_descends_into_nested_table() { |
| 477 | let lua = vm(); |
| 478 | let names: Vec<String> = lua |
| 479 | .load( |
| 480 | r#"return feel.evaluate("o ... name", { o = { customer = { name = "Ada" } } })"#, |
| 481 | ) |
| 482 | .eval() |
| 483 | .unwrap(); |
| 484 | assert_eq!(names, vec!["Ada".to_string()]); |
| 485 | } |
| 486 | |
| 487 | #[test] |
| 488 | fn descendant_terminates_on_a_cyclic_table() { |
| 489 | // Without the `Table::to_pointer`-based identity override this would loop |
| 490 | // forever; with it, the back-edge into the same table is cut after one re-entry. |
| 491 | let lua = vm(); |
| 492 | let xs: Vec<i64> = lua |
| 493 | .load( |
| 494 | r#" |
| 495 | local c = { value = 42 } |
| 496 | c.next = c -- self-referential (cyclic) Lua table |
| 497 | return feel.evaluate("c ... value", { c = c }) |
| 498 | "#, |
| 499 | ) |
| 500 | .eval() |
| 501 | .unwrap(); |
| 502 | assert_eq!(xs, vec![42, 42]); |
| 503 | } |