Public repository: timo / feel

Reader

An interpreter of FEEL, written in Rust, with integrations for PostgreSQL, WASM, Lua, and Python.

feel / crates / feel-lua / smoke.lua · 3.0 KB · at da382279c66c · raw

1
-- End-to-end smoke test for the feel-lua loadable module (ADR-22), the Lua
2
-- analogue of feel-wasm/smoke.mjs. Build the module, then point Lua's C path at
3
-- it and run this script:
4
--
5
--   cargo build -p feel-lua --no-default-features --features lua54,module
6
--   LUA_CPATH='target/debug/lib?.so;;' lua5.4 crates/feel-lua/smoke.lua
7
--
8
-- (`lib?.so` expands `?` -> `feel`, i.e. `libfeel.so`; no rename needed.)
9
10
local feel = require("feel")
11
12
local function eq(actual, expected, label)
13
  if actual ~= expected then
14
    error(string.format("FAIL %s: expected %s, got %s", label, tostring(expected), tostring(actual)))
15
  end
16
  print(string.format("ok  %-42s = %s", label, tostring(actual)))
17
end
18
19
-- Scalars, arithmetic, control flow.
20
eq(feel.evaluate("1 + 1"), 2, "1 + 1")
21
eq(feel.evaluate("2 * 3 + 1"), 7, "2 * 3 + 1")
22
eq(feel.evaluate([["foo"]]), "foo", 'string "foo"')
23
eq(feel.evaluate("1 < 2"), true, "1 < 2")
24
25
-- Lists round-trip as sequence tables.
26
local doubled = feel.evaluate("for i in [1,2,3] return i * 2")
27
eq(table.concat(doubled, ","), "2,4,6", "for-loop list")
28
29
-- Lazy path access into nested tables (firing __index on demand).
30
eq(feel.evaluate("person.age", { person = { age = 42 } }), 42, "person.age")
31
32
-- Host callable bound directly in the context.
33
eq(feel.evaluate("f(2, 3)", { f = function(a, b) return a + b end }), 5, "host f(2,3)")
34
35
-- FEEL named arguments arrive as a single trailing options table.
36
eq(
37
  feel.evaluate("f(2, b: 3)", { f = function(a, opts) return a + opts.b end }),
38
  5,
39
  "named args -> {b=3}"
40
)
41
42
-- Ambient function registry (the third argument).
43
eq(feel.evaluate("add(2, 3)", nil, { add = function(a, b) return a + b end }), 5, "registry add(2,3)")
44
45
-- A context binding shadows a built-in (host override).
46
eq(feel.evaluate("count([1,2,3])", { count = function(_) return 99 end }), 99, "shadow builtin count")
47
48
-- Identity pass-through: a table/function handed in comes back unchanged.
49
local obj = { a = 1 }
50
eq(feel.evaluate("o", { o = obj }) == obj, true, "table identity")
51
52
-- Temporal values cross out as ISO-8601 strings (RD-8).
53
eq(feel.evaluate("date(2020, 1, 15)"), "2020-01-15", "date -> ISO")
54
eq(feel.evaluate([[duration("P1Y2M")]]), "P1Y2M", "duration -> ISO")
55
56
-- Error semantics: a raised host error is an error-null -> nil (lenient).
57
eq(feel.evaluate("f()", { f = function() error("boom") end }), nil, "host error -> nil (lenient)")
58
59
-- Strict surfaces the error-null as a catchable Lua error.
60
local ok = pcall(function()
61
  return feel.evaluate_strict("f()", { f = function() error("boom") end })
62
end)
63
eq(ok, false, "host error -> raised (strict)")
64
65
-- A regular `return nil` is a value-null, not an error -> strict does NOT raise.
66
local ok2 = pcall(function()
67
  return feel.evaluate_strict("g()", { g = function() return nil end })
68
end)
69
eq(ok2, true, "return nil -> value-null (strict ok)")
70
71
-- compile() once, evaluate against many contexts.
72
local prog = feel.compile("a + 1")
73
eq(prog:evaluate({ a = 1 }), 2, "compiled a+1 (a=1)")
74
eq(prog:evaluate({ a = 41 }), 42, "compiled a+1 (a=41)")
75
76
print("\nall feel-lua smoke checks passed")