← Lua Academy

Test-driven development · 44 / 69

Test a rejected input

YOUR TASK

Make this happen

3 steps
  1. A function may reject invalid input with error. The raises matcher calls a supplied function and expects a catchable error. Resource exhaustion is not a catchable validation error.

  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.requirePositive = function(n: number): number
  return n
end

lesson.test.lua

local t = require("test")
local subject = require("cococo.subject")
t.it("rejects zero", function()
  local s = subject.load()
  t.expect(function()
    s.requirePositive(0)
  end):raises()
end)
t.it("accepts positive", function()
  local s = subject.load()
  t.expect(s.requirePositive(3)):eq(3)
end)