Skip to content
This repository was archived by the owner on Jul 12, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/createEnum.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,25 @@ local function createEnum(enumName, variantValues)
variants[variantName] = createEnumVariant(enum, variantName, value)
end

local enumItems = {}

for _, variant in pairs(variants) do
enumItems[#enumItems + 1] = variant
end

getmetatable(enum)[typeKey] = "Enum"

getmetatable(enum).__tostring = function()
return enumName
end

getmetatable(enum).__index = function(self, key)
if key == "GetEnumItems" then
return function()
return enumItems
end
end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of a bolt-on solution, we should probably make a table containing the enum variant's methods and explicitly index into it instead of comparing the key with a constant.

Additionally, we should return the same function every time, otherwise Foo.GetEnumItems ~= Foo.GetEnumItems!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix it. [2]

Copy link
Contributor Author

@Kampfkarren Kampfkarren Aug 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So about that...
image
I'm not sure if you want to replicate this strange behavior or not.


local variant = variants[key]

if variant == nil then
Expand Down
18 changes: 18 additions & 0 deletions lib/createEnumGroup_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,22 @@ describe("createEnumGroup", function()
tostring(group.whatever)
end)
end)

it("should return enum items with GetEnumItems", function()
local values = {
Doge = 1,
Funny = 2,
}

local Foo = createEnum("Foo", values)

local enumItems = Foo:GetEnumItems()
assert.equal(#enumItems, 2)

for index=1,2 do
local enumItem = enumItems[index]

assert.not_nil(values[enumItem.Name])
end
end)
end)