← Lua Academy

Test-driven development · 45 / 69

Cover a table of examples

YOUR TASK

Make this happen

3 steps
  1. t.each creates a named case for each row of data. Read the input/output table before implementing clamp: values below zero become zero, values above ten become ten, and the middle stays unchanged.

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

lesson.test.lua

local t = require("test")
local subject = require("cococo.subject")
t.each(
  "clamp",
  { { -1, 0 }, { 0, 0 }, { 5, 5 }, { 10, 10 }, { 11, 10 } },
  function(input, expected)
    local s = subject.load()
    t.expect(s.clamp(input)):eq(expected)
  end
)