From 1c939a8b74d77cc25b7da84767454747865b938e Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Sun, 18 Aug 2024 22:00:40 -0300 Subject: [PATCH] tests: add test case using self See #756. --- spec/subtyping/self_spec.lua | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 spec/subtyping/self_spec.lua diff --git a/spec/subtyping/self_spec.lua b/spec/subtyping/self_spec.lua new file mode 100644 index 000000000..bf9b2aa19 --- /dev/null +++ b/spec/subtyping/self_spec.lua @@ -0,0 +1,43 @@ +local util = require("spec.util") + +describe("subtyping of self", function() + it("self type resolves from abstract interface to concrete records (#756)", util.check([[ + local interface SoundMaker + make_sound: function(self) + end + + local record Animal is SoundMaker + species: string + end + + function Animal:create(species: string): Animal + return setmetatable({ species = species }, { __index = Animal }) + end + + function Animal:make_sound() + print("Animal sound") + end + + local record Person is SoundMaker + name: string + end + + function Person:create(name: string): Person + return setmetatable({ name = name }, { __index = Person }) + end + + function Person:make_sound() + print("Person sound") + end + + local things: {SoundMaker} = { + Animal:create("Dog"), + Person:create("John") + } + + for _, thing in ipairs(things) do + thing:make_sound() + end + ]])) +end) +