← Lua Academy

Typed Lua · 29 / 69

Model a status

YOUR TASK

Make this happen

3 steps
  1. A literal union restricts a parameter to a small set of values. Use the declared states to decide whether an order may ship.

  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 Status = "paid" | "pending" | "cancelled"
exports.canShip = function(status: Status): boolean
  return true
end

lesson.test.lua

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

t.it("paid orders ship", function()
  local s = subject.load()
  t.expect(s.canShip("paid")):eq(true)
end)

t.it("pending orders wait", function()
  local s = subject.load()
  t.expect(s.canShip("pending")):eq(false)
end)

t.it("cancelled orders stay stopped", function()
  local s = subject.load()
  t.expect(s.canShip("cancelled")):eq(false)
end)