From 08d030fe3e8d4ca4b59c60c93a931e175e292643 Mon Sep 17 00:00:00 2001 From: Ryan Hartlage <2488333+ryanplusplus@users.noreply.github.com> Date: Fri, 3 Apr 2026 15:36:56 -0400 Subject: [PATCH] More spec generators --- .../.meta/spec_generator.lua | 9 +++++ .../armstrong-numbers_spec.lua | 22 +++++------ .../practice/darts/.meta/spec_generator.lua | 9 +++++ exercises/practice/darts/darts_spec.lua | 28 +++++++------- .../.meta/spec_generator.lua | 19 ++++++++++ .../difference-of-squares_spec.lua | 37 ++++++++++++++----- .../eliuds-eggs/.meta/spec_generator.lua | 19 ++++++++++ .../practice/eliuds-eggs/eliuds-eggs_spec.lua | 2 +- .../.meta/spec_generator.lua | 21 +++++++++++ .../resistor-color-duo_spec.lua | 16 ++++---- .../resistor-color-trio/.meta/example.lua | 2 +- .../.meta/spec_generator.lua | 28 ++++++++++++++ .../resistor-color-trio.lua | 2 +- .../resistor-color-trio_spec.lua | 22 +++++------ 14 files changed, 179 insertions(+), 57 deletions(-) create mode 100644 exercises/practice/armstrong-numbers/.meta/spec_generator.lua create mode 100644 exercises/practice/darts/.meta/spec_generator.lua create mode 100644 exercises/practice/difference-of-squares/.meta/spec_generator.lua create mode 100644 exercises/practice/eliuds-eggs/.meta/spec_generator.lua create mode 100644 exercises/practice/resistor-color-duo/.meta/spec_generator.lua create mode 100644 exercises/practice/resistor-color-trio/.meta/spec_generator.lua diff --git a/exercises/practice/armstrong-numbers/.meta/spec_generator.lua b/exercises/practice/armstrong-numbers/.meta/spec_generator.lua new file mode 100644 index 00000000..3556a63b --- /dev/null +++ b/exercises/practice/armstrong-numbers/.meta/spec_generator.lua @@ -0,0 +1,9 @@ +return { + module_name = 'ArmstrongNumbers', + + generate_test = function(case) + local template = [[ + assert.is_%s(ArmstrongNumbers.is_armstrong_number(%d))]] + return template:format(case.expected, case.input.number) + end +} diff --git a/exercises/practice/armstrong-numbers/armstrong-numbers_spec.lua b/exercises/practice/armstrong-numbers/armstrong-numbers_spec.lua index dc27814d..93a729d9 100644 --- a/exercises/practice/armstrong-numbers/armstrong-numbers_spec.lua +++ b/exercises/practice/armstrong-numbers/armstrong-numbers_spec.lua @@ -1,39 +1,39 @@ -local ArmstrongNumbers = require 'armstrong-numbers' +local ArmstrongNumbers = require('armstrong-numbers') -describe('armstrong-nunbers', function() - it('Zero is an Armstrong number', function() +describe('armstrong-numbers', function() + it('zero is an armstrong number', function() assert.is_true(ArmstrongNumbers.is_armstrong_number(0)) end) - it('Single-digit numbers are Armstrong numbers', function() + it('single-digit numbers are armstrong numbers', function() assert.is_true(ArmstrongNumbers.is_armstrong_number(5)) end) - it('There are no two-digit Armstrong numbers', function() + it('there are no two-digit armstrong numbers', function() assert.is_false(ArmstrongNumbers.is_armstrong_number(10)) end) - it('Three-digit number that is an Armstrong number', function() + it('three-digit number that is an armstrong number', function() assert.is_true(ArmstrongNumbers.is_armstrong_number(153)) end) - it('Three-digit number that is not an Armstrong number', function() + it('three-digit number that is not an armstrong number', function() assert.is_false(ArmstrongNumbers.is_armstrong_number(100)) end) - it('Four-digit number that is an Armstrong number', function() + it('four-digit number that is an armstrong number', function() assert.is_true(ArmstrongNumbers.is_armstrong_number(9474)) end) - it('Four-digit number that is not an Armstrong number', function() + it('four-digit number that is not an armstrong number', function() assert.is_false(ArmstrongNumbers.is_armstrong_number(9475)) end) - it('Seven-digit number that is an Armstrong number', function() + it('seven-digit number that is an armstrong number', function() assert.is_true(ArmstrongNumbers.is_armstrong_number(9926315)) end) - it('Seven-digit number that is not an Armstrong number', function() + it('seven-digit number that is not an armstrong number', function() assert.is_false(ArmstrongNumbers.is_armstrong_number(9926314)) end) end) diff --git a/exercises/practice/darts/.meta/spec_generator.lua b/exercises/practice/darts/.meta/spec_generator.lua new file mode 100644 index 00000000..55170148 --- /dev/null +++ b/exercises/practice/darts/.meta/spec_generator.lua @@ -0,0 +1,9 @@ +return { + module_name = 'Darts', + + generate_test = function(case) + local template = [[ + assert.equal(%s, Darts.%s(%s, %s))]] + return template:format(case.expected, case.property, case.input.x, case.input.y) + end +} diff --git a/exercises/practice/darts/darts_spec.lua b/exercises/practice/darts/darts_spec.lua index 64b00043..20a16b98 100644 --- a/exercises/practice/darts/darts_spec.lua +++ b/exercises/practice/darts/darts_spec.lua @@ -1,55 +1,55 @@ -local Darts = require 'darts' +local Darts = require('darts') describe('darts', function() - it('Missed target', function() + it('missed target', function() assert.equal(0, Darts.score(-9, 9)) end) - it('On the outer circle', function() + it('on the outer circle', function() assert.equal(1, Darts.score(0, 10)) end) - it('On the middle circle', function() + it('on the middle circle', function() assert.equal(5, Darts.score(-5, 0)) end) - it('On the inner circle', function() + it('on the inner circle', function() assert.equal(10, Darts.score(0, -1)) end) - it('Exactly on center', function() + it('exactly on center', function() assert.equal(10, Darts.score(0, 0)) end) - it('Near the center', function() + it('near the center', function() assert.equal(10, Darts.score(-0.1, -0.1)) end) - it('Just within the inner circle', function() + it('just within the inner circle', function() assert.equal(10, Darts.score(0.7, 0.7)) end) - it('Just outside the inner circle', function() + it('just outside the inner circle', function() assert.equal(5, Darts.score(0.8, -0.8)) end) - it('Just within the middle circle', function() + it('just within the middle circle', function() assert.equal(5, Darts.score(-3.5, 3.5)) end) - it('Just outside the middle circle', function() + it('just outside the middle circle', function() assert.equal(1, Darts.score(-3.6, -3.6)) end) - it('Just within the outer circle', function() + it('just within the outer circle', function() assert.equal(1, Darts.score(-7.0, 7.0)) end) - it('Just outside the outer circle', function() + it('just outside the outer circle', function() assert.equal(0, Darts.score(7.1, -7.1)) end) - it('Asymmetric position between the inner and middle circles', function() + it('asymmetric position between the inner and middle circles', function() assert.equal(5, Darts.score(0.5, -4)) end) end) diff --git a/exercises/practice/difference-of-squares/.meta/spec_generator.lua b/exercises/practice/difference-of-squares/.meta/spec_generator.lua new file mode 100644 index 00000000..fd08d262 --- /dev/null +++ b/exercises/practice/difference-of-squares/.meta/spec_generator.lua @@ -0,0 +1,19 @@ +local function snake_case(str) + local s = str:gsub('%u', function(c) + return '_' .. c:lower() + end) + if s:sub(1, 1) == '_' then + s = s:sub(2) + end + return s +end + +return { + module_name = 'diff', + + generate_test = function(case) + local template = [[ + assert.equal(%s, diff.%s(%s))]] + return template:format(case.expected, snake_case(case.property), case.input.number) + end +} diff --git a/exercises/practice/difference-of-squares/difference-of-squares_spec.lua b/exercises/practice/difference-of-squares/difference-of-squares_spec.lua index 91dfddd1..7a4057b4 100644 --- a/exercises/practice/difference-of-squares/difference-of-squares_spec.lua +++ b/exercises/practice/difference-of-squares/difference-of-squares_spec.lua @@ -1,27 +1,44 @@ local diff = require('difference-of-squares') describe('difference-of-squares', function() - describe('square_of_sum', function() - it('should square the sum of the numbers up to the given number', function() + describe('square the sum of the numbers up to the given number', function() + it('square of sum 1', function() + assert.equal(1, diff.square_of_sum(1)) + end) + + it('square of sum 5', function() assert.equal(225, diff.square_of_sum(5)) - assert.equal(3025, diff.square_of_sum(10)) + end) + + it('square of sum 100', function() assert.equal(25502500, diff.square_of_sum(100)) end) end) - describe('sum_of_squares', function() - it('should sum the squares of the numbers up to the given number', function() + describe('sum the squares of the numbers up to the given number', function() + it('sum of squares 1', function() + assert.equal(1, diff.sum_of_squares(1)) + end) + + it('sum of squares 5', function() assert.equal(55, diff.sum_of_squares(5)) - assert.equal(385, diff.sum_of_squares(10)) + end) + + it('sum of squares 100', function() assert.equal(338350, diff.sum_of_squares(100)) end) end) - describe('difference_of_squares', function() - it('should subtract sum of squares from square of sum', function() - assert.equal(0, diff.difference_of_squares(0)) + describe('subtract sum of squares from square of sums', function() + it('difference of squares 1', function() + assert.equal(0, diff.difference_of_squares(1)) + end) + + it('difference of squares 5', function() assert.equal(170, diff.difference_of_squares(5)) - assert.equal(2640, diff.difference_of_squares(10)) + end) + + it('difference of squares 100', function() assert.equal(25164150, diff.difference_of_squares(100)) end) end) diff --git a/exercises/practice/eliuds-eggs/.meta/spec_generator.lua b/exercises/practice/eliuds-eggs/.meta/spec_generator.lua new file mode 100644 index 00000000..698909cd --- /dev/null +++ b/exercises/practice/eliuds-eggs/.meta/spec_generator.lua @@ -0,0 +1,19 @@ +local function snake_case(str) + local s = str:gsub('%u', function(c) + return '_' .. c:lower() + end) + if s:sub(1, 1) == '_' then + s = s:sub(2) + end + return s +end + +return { + module_name = 'EliudsEggs', + + generate_test = function(case) + local template = [[ + assert.equal(%s, EliudsEggs.%s(%s))]] + return template:format(case.expected, snake_case(case.property), case.input.number) + end +} diff --git a/exercises/practice/eliuds-eggs/eliuds-eggs_spec.lua b/exercises/practice/eliuds-eggs/eliuds-eggs_spec.lua index fbee8cce..cf1a48ce 100644 --- a/exercises/practice/eliuds-eggs/eliuds-eggs_spec.lua +++ b/exercises/practice/eliuds-eggs/eliuds-eggs_spec.lua @@ -1,4 +1,4 @@ -local EliudsEggs = require 'eliuds-eggs' +local EliudsEggs = require('eliuds-eggs') describe('eliuds-eggs', function() it('0 eggs', function() diff --git a/exercises/practice/resistor-color-duo/.meta/spec_generator.lua b/exercises/practice/resistor-color-duo/.meta/spec_generator.lua new file mode 100644 index 00000000..6d0de7d3 --- /dev/null +++ b/exercises/practice/resistor-color-duo/.meta/spec_generator.lua @@ -0,0 +1,21 @@ +local function map(t, f) + local mapped = {} + for i, v in ipairs(t) do + mapped[i] = f(v) + end + return mapped +end + +local function stringify(s) + return "'" .. s .. "'" +end + +return { + module_name = 'rcd', + + generate_test = function(case) + local template = [[ + assert.equal(%d, rcd.value({ %s }))]] + return template:format(case.expected, table.concat(map(case.input.colors, stringify), ', ')) + end +} diff --git a/exercises/practice/resistor-color-duo/resistor-color-duo_spec.lua b/exercises/practice/resistor-color-duo/resistor-color-duo_spec.lua index 44a86a26..e90a9da6 100644 --- a/exercises/practice/resistor-color-duo/resistor-color-duo_spec.lua +++ b/exercises/practice/resistor-color-duo/resistor-color-duo_spec.lua @@ -1,31 +1,31 @@ -local rcd = require 'resistor-color-duo' +local rcd = require('resistor-color-duo') describe('resistor-color-duo', function() - it('Brown and black', function() + it('brown and black', function() assert.equal(10, rcd.value({ 'brown', 'black' })) end) - it('Blue and grey', function() + it('blue and grey', function() assert.equal(68, rcd.value({ 'blue', 'grey' })) end) - it('Yellow and violet', function() + it('yellow and violet', function() assert.equal(47, rcd.value({ 'yellow', 'violet' })) end) - it('White and red', function() + it('white and red', function() assert.equal(92, rcd.value({ 'white', 'red' })) end) - it('Orange and orange', function() + it('orange and orange', function() assert.equal(33, rcd.value({ 'orange', 'orange' })) end) - it('Ignore additional colors', function() + it('ignore additional colors', function() assert.equal(51, rcd.value({ 'green', 'brown', 'orange' })) end) - it('Black and brown, one-digit', function() + it('black and brown, one-digit', function() assert.equal(1, rcd.value({ 'black', 'brown' })) end) end) diff --git a/exercises/practice/resistor-color-trio/.meta/example.lua b/exercises/practice/resistor-color-trio/.meta/example.lua index 193c7556..0dc38138 100644 --- a/exercises/practice/resistor-color-trio/.meta/example.lua +++ b/exercises/practice/resistor-color-trio/.meta/example.lua @@ -12,7 +12,7 @@ local value = { } return { - decode = function(c1, c2, c3) + label = function(c1, c2, c3) local value = (value[c1] * 10 + value[c2]) * 10 ^ value[c3] if value >= 1e9 then diff --git a/exercises/practice/resistor-color-trio/.meta/spec_generator.lua b/exercises/practice/resistor-color-trio/.meta/spec_generator.lua new file mode 100644 index 00000000..b75bf626 --- /dev/null +++ b/exercises/practice/resistor-color-trio/.meta/spec_generator.lua @@ -0,0 +1,28 @@ +local function map(t, f) + local mapped = {} + for i, v in ipairs(t) do + mapped[i] = f(v) + end + return mapped +end + +local function stringify(s) + return "'" .. s .. "'" +end + +return { + module_name = 'rct', + + generate_test = function(case) + local template = [[ + local value, unit = rct.%s(%s) + assert.are.equal(%s, value) + assert.are.equal('%s', unit)]] + return template:format(table.unpack({ + case.property, + table.concat(map(case.input.colors, stringify), ', '), + case.expected.value, + case.expected.unit + })) + end +} diff --git a/exercises/practice/resistor-color-trio/resistor-color-trio.lua b/exercises/practice/resistor-color-trio/resistor-color-trio.lua index c7559a05..06ac3e20 100644 --- a/exercises/practice/resistor-color-trio/resistor-color-trio.lua +++ b/exercises/practice/resistor-color-trio/resistor-color-trio.lua @@ -1,4 +1,4 @@ return { - decode = function(c1, c2, c3) + label = function(c1, c2, c3) end } diff --git a/exercises/practice/resistor-color-trio/resistor-color-trio_spec.lua b/exercises/practice/resistor-color-trio/resistor-color-trio_spec.lua index a34c4404..bcaa02fb 100644 --- a/exercises/practice/resistor-color-trio/resistor-color-trio_spec.lua +++ b/exercises/practice/resistor-color-trio/resistor-color-trio_spec.lua @@ -1,62 +1,62 @@ -local rct = require 'resistor-color-trio' +local rct = require('resistor-color-trio') describe('resistor-color-trio', function() it('orange and orange and black', function() - local value, unit = rct.decode('orange', 'orange', 'black') + local value, unit = rct.label('orange', 'orange', 'black') assert.are.equal(33, value) assert.are.equal('ohms', unit) end) it('blue and grey and brown', function() - local value, unit = rct.decode('blue', 'grey', 'brown') + local value, unit = rct.label('blue', 'grey', 'brown') assert.are.equal(680, value) assert.are.equal('ohms', unit) end) it('red and black and red', function() - local value, unit = rct.decode('red', 'black', 'red') + local value, unit = rct.label('red', 'black', 'red') assert.are.equal(2, value) assert.are.equal('kiloohms', unit) end) it('green and brown and orange', function() - local value, unit = rct.decode('green', 'brown', 'orange') + local value, unit = rct.label('green', 'brown', 'orange') assert.are.equal(51, value) assert.are.equal('kiloohms', unit) end) it('yellow and violet and yellow', function() - local value, unit = rct.decode('yellow', 'violet', 'yellow') + local value, unit = rct.label('yellow', 'violet', 'yellow') assert.are.equal(470, value) assert.are.equal('kiloohms', unit) end) it('blue and violet and blue', function() - local value, unit = rct.decode('blue', 'violet', 'blue') + local value, unit = rct.label('blue', 'violet', 'blue') assert.are.equal(67, value) assert.are.equal('megaohms', unit) end) it('minimum possible value', function() - local value, unit = rct.decode('black', 'black', 'black') + local value, unit = rct.label('black', 'black', 'black') assert.are.equal(0, value) assert.are.equal('ohms', unit) end) it('maximum possible value', function() - local value, unit = rct.decode('white', 'white', 'white') + local value, unit = rct.label('white', 'white', 'white') assert.are.equal(99, value) assert.are.equal('gigaohms', unit) end) it('first two colors make an invalid octal number', function() - local value, unit = rct.decode('black', 'grey', 'black') + local value, unit = rct.label('black', 'grey', 'black') assert.are.equal(8, value) assert.are.equal('ohms', unit) end) it('ignore extra colors', function() - local value, unit = rct.decode('blue', 'green', 'yellow', 'orange') + local value, unit = rct.label('blue', 'green', 'yellow', 'orange') assert.are.equal(650, value) assert.are.equal('kiloohms', unit) end)