Skip to content

Commit

Permalink
test: simulate reading a map using __index
Browse files Browse the repository at this point in the history
See #591. This is not fully equivalent to a "maprecord"
because we don't yet implement support for __newindex.
But at least now the behavior of dot and index notation
are consistent.
  • Loading branch information
hishamhm committed Dec 26, 2022
1 parent 7bd8477 commit 2c51094
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions spec/metamethods/index_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ describe("metamethod __index", function()
f(Rec)
]])

it("can simulate reading a map using __index", util.check [[
local record R
metamethod __index: function(R, string): number
foo: string
bar: boolean
end
local r: R = {}
r.foo = "hello"
r.bar = true
print(123 + r["wat"] + r["yep"]) -- these get resolved by __index
print(123 + r.wat + r.hello) -- these get resolved by __index too
]])

-- this is failing because the definition and implementations are not being cross-checked
-- this causes the test to output an error on line 15, because the call doesn't match the
-- metamethod definition inside Rec.
Expand Down

0 comments on commit 2c51094

Please sign in to comment.