Working with data · 18 / 69
Copies and aliases
YOUR TASK
Make this happen
Assigning a table to another variable shares the same table. Create a fresh record when an update must leave the original unchanged.
Read the cases in
lesson.test.lua, then choose Check solution to see what needs to change.Update
subject.luauntil every test passes. Keep the supplied tests unchanged.
Read the starter and tests
subject.lua
exports.rename = function(item: { name: string }, name: string)
local copy = item
copy.name = name
return copy
end
lesson.test.lua
local t = require("test")
local subject = require("cococo.subject")
t.it("returns new name", function()
local s = subject.load()
t.expect(s.rename({ name = "old" }, "new").name):eq("new")
end)
t.it("preserves original", function()
local s = subject.load()
local item = { name = "old" }
s.rename(item, "new")
t.expect(item.name):eq("old")
end)