From c84e96122d47be962ce0af3160cecc9a045c54ec Mon Sep 17 00:00:00 2001 From: Ryan Hartlage <2488333+ryanplusplus@users.noreply.github.com> Date: Mon, 23 Mar 2026 20:38:35 -0400 Subject: [PATCH] Add additional spec generators --- .../practice/leap/.meta/spec_generator.lua | 9 +++ exercises/practice/leap/leap_spec.lua | 10 +-- .../raindrops/.meta/spec_generator.lua | 9 +++ .../practice/raindrops/raindrops_spec.lua | 30 ++++----- .../practice/resistor-color/.meta/example.lua | 32 +++++---- .../resistor-color/.meta/spec_generator.lua | 29 ++++++++ .../practice/resistor-color/.meta/tests.toml | 1 - .../resistor-color/resistor-color_spec.lua | 67 ++++++++----------- 8 files changed, 115 insertions(+), 72 deletions(-) create mode 100644 exercises/practice/leap/.meta/spec_generator.lua create mode 100644 exercises/practice/raindrops/.meta/spec_generator.lua create mode 100644 exercises/practice/resistor-color/.meta/spec_generator.lua diff --git a/exercises/practice/leap/.meta/spec_generator.lua b/exercises/practice/leap/.meta/spec_generator.lua new file mode 100644 index 00000000..4f9d32bd --- /dev/null +++ b/exercises/practice/leap/.meta/spec_generator.lua @@ -0,0 +1,9 @@ +return { + module_name = 'is_leap_year', + + generate_test = function(case) + local template = [[ + assert.is_%s(is_leap_year(%d))]] + return template:format(case.expected, case.input.year) + end +} diff --git a/exercises/practice/leap/leap_spec.lua b/exercises/practice/leap/leap_spec.lua index fcc84725..f1c9309c 100644 --- a/exercises/practice/leap/leap_spec.lua +++ b/exercises/practice/leap/leap_spec.lua @@ -1,15 +1,15 @@ local is_leap_year = require('leap') describe('leap', function() - it('year not divisible by 4 is common year', function() + it('year not divisible by 4 in common year', function() assert.is_false(is_leap_year(2015)) end) - it('year divisible by 2, not divisible by 4 is common year', function() + it('year divisible by 2, not divisible by 4 in common year', function() assert.is_false(is_leap_year(1970)) end) - it('year divisible by 4, not divisible by 100 is leap year', function() + it('year divisible by 4, not divisible by 100 in leap year', function() assert.is_true(is_leap_year(1996)) end) @@ -17,7 +17,7 @@ describe('leap', function() assert.is_true(is_leap_year(1960)) end) - it('year divisible by 100, not divisible by 400 is common year', function() + it('year divisible by 100, not divisible by 400 in common year', function() assert.is_false(is_leap_year(2100)) end) @@ -33,7 +33,7 @@ describe('leap', function() assert.is_true(is_leap_year(2400)) end) - it('year divisible by 200, not divisible by 400 is common year', function() + it('year divisible by 200, not divisible by 400 in common year', function() assert.is_false(is_leap_year(1800)) end) end) diff --git a/exercises/practice/raindrops/.meta/spec_generator.lua b/exercises/practice/raindrops/.meta/spec_generator.lua new file mode 100644 index 00000000..c30a437f --- /dev/null +++ b/exercises/practice/raindrops/.meta/spec_generator.lua @@ -0,0 +1,9 @@ +return { + module_name = 'raindrops', + + generate_test = function(case) + local template = [[ + assert.equal('%s', raindrops(%d))]] + return template:format(case.expected, case.input.number) + end +} diff --git a/exercises/practice/raindrops/raindrops_spec.lua b/exercises/practice/raindrops/raindrops_spec.lua index 7429131c..4558526c 100644 --- a/exercises/practice/raindrops/raindrops_spec.lua +++ b/exercises/practice/raindrops/raindrops_spec.lua @@ -5,19 +5,19 @@ describe('raindrops', function() assert.equal('1', raindrops(1)) end) - it('the sound for 3 is Pling', function() + it('the sound for 3 is pling', function() assert.equal('Pling', raindrops(3)) end) - it('the sound for 5 is Plang', function() + it('the sound for 5 is plang', function() assert.equal('Plang', raindrops(5)) end) - it('the sound for 7 is Plong', function() + it('the sound for 7 is plong', function() assert.equal('Plong', raindrops(7)) end) - it('the sound for 6 is Pling as it has a factor 3', function() + it('the sound for 6 is pling as it has a factor 3', function() assert.equal('Pling', raindrops(6)) end) @@ -25,39 +25,39 @@ describe('raindrops', function() assert.equal('8', raindrops(8)) end) - it('the sound for 9 is Pling as it has a factor 3', function() + it('the sound for 9 is pling as it has a factor 3', function() assert.equal('Pling', raindrops(9)) end) - it('the sound for 10 is Plang as it has a factor 5', function() + it('the sound for 10 is plang as it has a factor 5', function() assert.equal('Plang', raindrops(10)) end) - it('the sound for 14 is Plong as it has a factor 7', function() + it('the sound for 14 is plong as it has a factor of 7', function() assert.equal('Plong', raindrops(14)) end) - it('the sound for 15 is PlingPlang as it has a factor 3 and 5', function() + it('the sound for 15 is plingplang as it has factors 3 and 5', function() assert.equal('PlingPlang', raindrops(15)) end) - it('the sound for 21 is PlingPlong as it has factors 3 and 7', function() + it('the sound for 21 is plingplong as it has factors 3 and 7', function() assert.equal('PlingPlong', raindrops(21)) end) - it('the sound for 25 is Plang as it has a factor 5', function() + it('the sound for 25 is plang as it has a factor 5', function() assert.equal('Plang', raindrops(25)) end) - it('the sound for 27 is Pling as it has a factor 3', function() + it('the sound for 27 is pling as it has a factor 3', function() assert.equal('Pling', raindrops(27)) end) - it('the sound for 35 is PlangPlong as it has factors 5 and 7', function() + it('the sound for 35 is plangplong as it has factors 5 and 7', function() assert.equal('PlangPlong', raindrops(35)) end) - it('the sound for 49 is Plong as it has a factor 7', function() + it('the sound for 49 is plong as it has a factor 7', function() assert.equal('Plong', raindrops(49)) end) @@ -65,11 +65,11 @@ describe('raindrops', function() assert.equal('52', raindrops(52)) end) - it('the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7', function() + it('the sound for 105 is plingplangplong as it has factors 3, 5 and 7', function() assert.equal('PlingPlangPlong', raindrops(105)) end) - it('the sound for 3125 is Plang as it has a factor 5', function() + it('the sound for 3125 is plang as it has a factor 5', function() assert.equal('Plang', raindrops(3125)) end) end) diff --git a/exercises/practice/resistor-color/.meta/example.lua b/exercises/practice/resistor-color/.meta/example.lua index 2c19157a..324b949c 100644 --- a/exercises/practice/resistor-color/.meta/example.lua +++ b/exercises/practice/resistor-color/.meta/example.lua @@ -1,18 +1,26 @@ -local value = { - black = 0, - brown = 1, - red = 2, - orange = 3, - yellow = 4, - green = 5, - blue = 6, - violet = 7, - grey = 8, - white = 9 +local colors = { + 'black', -- + 'brown', -- + 'red', -- + 'orange', -- + 'yellow', -- + 'green', -- + 'blue', -- + 'violet', -- + 'grey', -- + 'white' } return { + colors = function() + return colors + end, + color_code = function(color) - return value[color] + for i, v in ipairs(colors) do + if v == color then + return i - 1 + end + end end } diff --git a/exercises/practice/resistor-color/.meta/spec_generator.lua b/exercises/practice/resistor-color/.meta/spec_generator.lua new file mode 100644 index 00000000..01b2d96e --- /dev/null +++ b/exercises/practice/resistor-color/.meta/spec_generator.lua @@ -0,0 +1,29 @@ +local map = function(t, f) + local mapped = {} + for i, v in ipairs(t) do + mapped[i] = f(v) + end + return mapped +end + +local function render_table(t) + return table.concat(map(t, function(element) + return "'" .. element .. "'" + end), ', --\n') +end + +return { + module_name = 'rc', + + generate_test = function(case) + if case.property == 'colors' then + local template = [[ + assert.same({ %s }, rc.colors())]] + return template:format(render_table(case.expected)) + else + local template = [[ + assert.equal(%d, rc.color_code('%s'))]] + return template:format(case.expected, case.input.color) + end + end +} diff --git a/exercises/practice/resistor-color/.meta/tests.toml b/exercises/practice/resistor-color/.meta/tests.toml index 65423a05..9d4ee973 100644 --- a/exercises/practice/resistor-color/.meta/tests.toml +++ b/exercises/practice/resistor-color/.meta/tests.toml @@ -20,4 +20,3 @@ description = "Color codes -> Orange" [581d68fa-f968-4be2-9f9d-880f2fb73cf7] description = "Colors" -include = false diff --git a/exercises/practice/resistor-color/resistor-color_spec.lua b/exercises/practice/resistor-color/resistor-color_spec.lua index 5b163163..ec23e014 100644 --- a/exercises/practice/resistor-color/resistor-color_spec.lua +++ b/exercises/practice/resistor-color/resistor-color_spec.lua @@ -1,43 +1,32 @@ -local rc = require 'resistor-color' +local rc = require('resistor-color') describe('resistor-color', function() - it('Black', function() - assert.equal(0, rc.color_code('black')) - end) - - it('Brown', function() - assert.equal(1, rc.color_code('brown')) - end) - - it('Red', function() - assert.equal(2, rc.color_code('red')) - end) - - it('Orange', function() - assert.equal(3, rc.color_code('orange')) - end) - - it('Yellow', function() - assert.equal(4, rc.color_code('yellow')) - end) - - it('Green', function() - assert.equal(5, rc.color_code('green')) - end) - - it('Blue', function() - assert.equal(6, rc.color_code('blue')) - end) - - it('Violet', function() - assert.equal(7, rc.color_code('violet')) - end) - - it('Grey', function() - assert.equal(8, rc.color_code('grey')) - end) - - it('White', function() - assert.equal(9, rc.color_code('white')) + describe('color codes', function() + it('black', function() + assert.equal(0, rc.color_code('black')) + end) + + it('white', function() + assert.equal(9, rc.color_code('white')) + end) + + it('orange', function() + assert.equal(3, rc.color_code('orange')) + end) + end) + + it('colors', function() + assert.same({ + 'black', -- + 'brown', -- + 'red', -- + 'orange', -- + 'yellow', -- + 'green', -- + 'blue', -- + 'violet', -- + 'grey', -- + 'white' + }, rc.colors()) end) end)