lua: metatable dispatch (__index/__newindex/arith/cmp/__call/__len) +23 tests
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
This commit is contained in:
@@ -594,6 +594,71 @@ cat > "$TMPFILE" << 'EPOCHS'
|
||||
(epoch 770)
|
||||
(eval "(lua-eval-ast \"local t = {} local s = t s.x = 42 return t.x\")")
|
||||
|
||||
;; ── Phase 4: metatables ────────────────────────────────────────
|
||||
;; setmetatable / getmetatable
|
||||
(epoch 800)
|
||||
(eval "(lua-eval-ast \"local t = {} local r = setmetatable(t, {}) if r == t then return 1 else return 0 end\")")
|
||||
(epoch 801)
|
||||
(eval "(lua-eval-ast \"local mt = {} local t = setmetatable({}, mt) if getmetatable(t) == mt then return 1 else return 0 end\")")
|
||||
|
||||
;; type()
|
||||
(epoch 810)
|
||||
(eval "(lua-eval-ast \"return type(1)\")")
|
||||
(epoch 811)
|
||||
(eval "(lua-eval-ast \"return type(\\\"s\\\")\")")
|
||||
(epoch 812)
|
||||
(eval "(lua-eval-ast \"return type({})\")")
|
||||
(epoch 813)
|
||||
(eval "(lua-eval-ast \"return type(nil)\")")
|
||||
(epoch 814)
|
||||
(eval "(lua-eval-ast \"return type(true)\")")
|
||||
(epoch 815)
|
||||
(eval "(lua-eval-ast \"return type(function() end)\")")
|
||||
|
||||
;; __index
|
||||
(epoch 820)
|
||||
(eval "(lua-eval-ast \"local base = {x = 10} local t = setmetatable({}, {__index = base}) return t.x\")")
|
||||
(epoch 821)
|
||||
(eval "(lua-eval-ast \"local base = {x = 10} local t = setmetatable({y = 20}, {__index = base}) return t.x + t.y\")")
|
||||
(epoch 822)
|
||||
(eval "(lua-eval-ast \"local t = setmetatable({}, {__index = function(tbl, k) return k .. \\\"!\\\" end}) return t.hi\")")
|
||||
|
||||
;; __newindex
|
||||
(epoch 830)
|
||||
(eval "(lua-eval-ast \"local log = {} local t = setmetatable({}, {__newindex = function(tbl, k, v) log[1] = k end}) t.foo = 1 return log[1]\")")
|
||||
|
||||
;; Arithmetic metamethods
|
||||
(epoch 840)
|
||||
(eval "(lua-eval-ast \"local mt = {__add = function(a, b) return 99 end} local x = setmetatable({}, mt) return x + 1\")")
|
||||
(epoch 841)
|
||||
(eval "(lua-eval-ast \"local mt = {__add = function(a, b) return a.v + b.v end} local x = setmetatable({v = 3}, mt) local y = setmetatable({v = 4}, mt) return x + y\")")
|
||||
(epoch 842)
|
||||
(eval "(lua-eval-ast \"local mt = {__sub = function(a, b) return 7 end, __mul = function(a, b) return 11 end} local t = setmetatable({}, mt) return t - t + (t * t)\")")
|
||||
(epoch 843)
|
||||
(eval "(lua-eval-ast \"local mt = {__unm = function(a) return 42 end} local t = setmetatable({}, mt) return -t\")")
|
||||
(epoch 844)
|
||||
(eval "(lua-eval-ast \"local mt = {__concat = function(a, b) return \\\"cat\\\" end} local t = setmetatable({}, mt) return t .. \\\"x\\\"\")")
|
||||
|
||||
;; Comparison metamethods
|
||||
(epoch 850)
|
||||
(eval "(lua-eval-ast \"local mt = {__eq = function(a, b) return true end} local x = setmetatable({}, mt) local y = setmetatable({}, mt) if x == y then return 1 else return 0 end\")")
|
||||
(epoch 851)
|
||||
(eval "(lua-eval-ast \"local mt = {__lt = function(a, b) return true end} local x = setmetatable({}, mt) local y = setmetatable({}, mt) if x < y then return 1 else return 0 end\")")
|
||||
(epoch 852)
|
||||
(eval "(lua-eval-ast \"local mt = {__le = function(a, b) return true end} local x = setmetatable({}, mt) local y = setmetatable({}, mt) if x <= y then return 1 else return 0 end\")")
|
||||
|
||||
;; __call
|
||||
(epoch 860)
|
||||
(eval "(lua-eval-ast \"local mt = {__call = function(t, x) return x + 100 end} local obj = setmetatable({}, mt) return obj(5)\")")
|
||||
|
||||
;; __len
|
||||
(epoch 870)
|
||||
(eval "(lua-eval-ast \"local mt = {__len = function(t) return 99 end} local t = setmetatable({}, mt) return #t\")")
|
||||
|
||||
;; Classic OO pattern
|
||||
(epoch 880)
|
||||
(eval "(lua-eval-ast \"local Animal = {} Animal.__index = Animal function Animal:new(name) local o = setmetatable({name = name}, self) return o end function Animal:getName() return self.name end local a = Animal:new(\\\"Rex\\\") return a:getName()\")")
|
||||
|
||||
EPOCHS
|
||||
|
||||
OUTPUT=$(timeout 60 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
|
||||
@@ -899,6 +964,31 @@ check 760 "t[k]=v reads via t.foo" '88'
|
||||
check 761 "t[concat]=v reads via t.xy" '7'
|
||||
check 770 "reference semantics t=s" '42'
|
||||
|
||||
# ── Phase 4: metatables ───────────────────────────────────────
|
||||
check 800 "setmetatable returns t" '1'
|
||||
check 801 "getmetatable roundtrip" '1'
|
||||
check 810 "type(1)" '"number"'
|
||||
check 811 "type(string)" '"string"'
|
||||
check 812 "type({})" '"table"'
|
||||
check 813 "type(nil)" '"nil"'
|
||||
check 814 "type(true)" '"boolean"'
|
||||
check 815 "type(function)" '"function"'
|
||||
check 820 "__index table lookup" '10'
|
||||
check 821 "__index chain + self" '30'
|
||||
check 822 "__index as function" '"hi!"'
|
||||
check 830 "__newindex fires" '"foo"'
|
||||
check 840 "__add table+number" '99'
|
||||
check 841 "__add two tables" '7'
|
||||
check 842 "__sub + __mul chain" '18'
|
||||
check 843 "__unm" '42'
|
||||
check 844 "__concat" '"cat"'
|
||||
check 850 "__eq" '1'
|
||||
check 851 "__lt" '1'
|
||||
check 852 "__le" '1'
|
||||
check 860 "__call" '105'
|
||||
check 870 "__len" '99'
|
||||
check 880 "OO pattern self:m()" '"Rex"'
|
||||
|
||||
TOTAL=$((PASS + FAIL))
|
||||
if [ $FAIL -eq 0 ]; then
|
||||
echo "ok $PASS/$TOTAL Lua-on-SX tests passed"
|
||||
|
||||
Reference in New Issue
Block a user