Capstones · 67 / 69
Build an order summary
YOUR TASK
Make this happen
Combine records, arrays, loops and branches. Export summarize(lines), returning quantity and total. Each line has quantity and unitPrice. An empty order has zero quantity and zero total. Keep this calculation pure: it needs no platform calls.
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
type Line = { quantity: number, unitPrice: number }
exports.summarize = function(lines: { Line })
return { quantity = 0, total = 0 }
end
lesson.test.lua
local t = require("test")
local subject = require("cococo.subject")
t.it("summarizes multiple lines", function()
local s = subject.load()
t.expect(s.summarize({
{ quantity = 2, unitPrice = 10 },
{ quantity = 3, unitPrice = 4 },
})):deep_eq({ quantity = 5, total = 32 })
end)
t.it("empty order", function()
local s = subject.load()
t.expect(s.summarize({})):deep_eq({ quantity = 0, total = 0 })
end)