← Lua Academy

Practical scripting · 64 / 69

Preserve source records

YOUR TASK

Make this happen

3 steps
  1. A normalisation pipeline should not unexpectedly mutate its input. Build a new record with an uppercase code and retain the quantity.

  2. Read the cases in lesson.test.lua, then choose Check solution to see what needs to change.

  3. Update subject.lua until every test passes. Keep the supplied tests unchanged.

Read the starter and tests

subject.lua

exports.normalise = function(line: { code: string, quantity: number })
  line.code = string.upper(line.code)
  return line
end

lesson.test.lua

local t = require("test")
local subject = require("cococo.subject")
t.it("normalises output", function()
  local s = subject.load()
  t.expect(s.normalise({ code = "ab", quantity = 2 }))
    :deep_eq({ code = "AB", quantity = 2 })
end)
t.it("input stays unchanged", function()
  local s = subject.load()
  local line = { code = "ab", quantity = 2 }
  s.normalise(line)
  t.expect(line.code):eq("ab")
end)