← Lua Academy

Typed Lua · 27 / 69

Name your data

YOUR TASK

Make this happen

3 steps
  1. A record type describes fields and their types. An annotation is checked before execution; it documents the shape a function accepts. Implement a subtotal from quantity and unitPrice.

  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

type Line = { quantity: number, unitPrice: number }
exports.subtotal = function(line: Line): number
  return line.unitPrice
end

lesson.test.lua

local t = require("test")
local subject = require("cococo.subject")

t.it("quantity matters", function()
  local s = subject.load()
  t.expect(s.subtotal({ quantity = 3, unitPrice = 12 })):eq(36)
end)

t.it("zero quantity", function()
  local s = subject.load()
  t.expect(s.subtotal({ quantity = 0, unitPrice = 12 })):eq(0)
end)