Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
metatable cloning (#16)
Browse files Browse the repository at this point in the history
Co-authored-by: C <[email protected]>
  • Loading branch information
Rusty483 and cxmeel authored Aug 31, 2023
1 parent 10140be commit dcdb17c
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 61 deletions.
8 changes: 3 additions & 5 deletions src/Array/copyDeep.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
```
]=]
local function copyDeep<T>(array: { T }): { T }
local result = {}
local result = table.clone(array)

for _, value in ipairs(array) do
for index, value in array do
if type(value) == "table" then
table.insert(result, copyDeep(value))
else
table.insert(result, value)
result[index] = copyDeep(value) :: any
end
end

Expand Down
14 changes: 14 additions & 0 deletions src/Array/copyDeep.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,18 @@ return function()
expect(new[2]).to.equal(2)
expect(new[3]).never.to.equal(array[3])
end)

it("should copy metatables", function()
local meta = setmetatable({}, { __index = function() end })
local array = { { 1, 2, 3 }, { 4, 5, 6 }, meta }

local new = CopyDeep(array)

expect(new).to.be.a("table")
expect(new).never.to.equal(array)

expect(function()
return getmetatable(new[3])
end).to.be.a("function")
end)
end
4 changes: 1 addition & 3 deletions src/Dictionary/copyDeep.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
```
]=]
local function copyDeep<T>(dictionary: T): T
local new = {}
local new = table.clone(dictionary)

for key, value in pairs(dictionary) do
if type(value) == "table" then
new[key] = copyDeep(value)
else
new[key] = value
end
end

Expand Down
18 changes: 18 additions & 0 deletions src/Dictionary/copyDeep.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,22 @@ return function()

expect(new.hello.world).to.equal("goodbye")
end)

it("should copy metatables", function()
local meta = setmetatable({}, { __index = function() end })
local dictionary = {
hello = {},
world = {},
meta = meta,
}

local new = CopyDeep(dictionary)

expect(new).to.be.a("table")
expect(new).never.to.equal(dictionary)

expect(function()
return getmetatable(new[3])
end).to.be.a("function")
end)
end
67 changes: 67 additions & 0 deletions testez.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# TestEZ
[[afterAll.args]]
type = "function"

[[afterEach.args]]
type = "function"

[[beforeAll.args]]
type = "function"

[[beforeEach.args]]
type = "function"

[[describe.args]]
type = "string"

[[describe.args]]
type = "function"

[[describeFOCUS.args]]
type = "string"

[[describeFOCUS.args]]
type = "function"

[[describeSKIP.args]]
type = "string"

[[describeSKIP.args]]
type = "function"

[[expect.args]]
type = "any"

[[FIXME.args]]
type = "string"
required = false

[FOCUS]
args = []

[[it.args]]
type = "string"

[[it.args]]
type = "function"

[[itFIXME.args]]
type = "string"

[[itFIXME.args]]
type = "function"

[[itFOCUS.args]]
type = "string"

[[itFOCUS.args]]
type = "function"

[[itSKIP.args]]
type = "string"

[[itSKIP.args]]
type = "function"

[SKIP]
args = []
53 changes: 0 additions & 53 deletions testez.yml

This file was deleted.

0 comments on commit dcdb17c

Please sign in to comment.