← Lua Academy

Practical scripting · 65 / 69

Make a workflow decision

YOUR TASK

Make this happen

3 steps
  1. A workflow decision is easier to test as a pure function. Route paid orders with stock to production, paid orders without stock to waiting, and unpaid orders to payment.

  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.route = function(paid: boolean, stock: number): string
  return "production"
end

lesson.test.lua

local t = require("test")
local subject = require("cococo.subject")
t.it("payment comes first", function()
  local s = subject.load()
  t.expect(s.route(false, 5)):eq("payment")
end)
t.it("waits for stock", function()
  local s = subject.load()
  t.expect(s.route(true, 0)):eq("waiting")
end)
t.it("ready for production", function()
  local s = subject.load()
  t.expect(s.route(true, 5)):eq("production")
end)