diff --git a/exercises/practice/bottle-song/.meta/spec_generator.lua b/exercises/practice/bottle-song/.meta/spec_generator.lua new file mode 100644 index 00000000..e940cd56 --- /dev/null +++ b/exercises/practice/bottle-song/.meta/spec_generator.lua @@ -0,0 +1,16 @@ +local utils = require 'utils' + +return { + module_name = 'BottleSong', + + generate_test = function(case) + local template = [[ + local expected = table.concat({ %s }) + assert.equal(expected, BottleSong.%s(%s, %s))]] + + local expected = table.concat(utils.map(case.expected, function(x) + return utils.stringify(x .. '\n') + end), ', ') + return template:format(expected, utils.snake_case(case.property), case.input.startBottles, case.input.takeDown) + end +} diff --git a/exercises/practice/bottle-song/bottle-song_spec.lua b/exercises/practice/bottle-song/bottle-song_spec.lua index 1a0f0562..fd12ea04 100644 --- a/exercises/practice/bottle-song/bottle-song_spec.lua +++ b/exercises/practice/bottle-song/bottle-song_spec.lua @@ -1,137 +1,141 @@ -local BottleSong = require 'bottle-song' +local BottleSong = require('bottle-song') -describe('lyrics', function() - describe('single verse', function() - it('first generic verse', function() - expected = table.concat({ - "Ten green bottles hanging on the wall,\n", - "Ten green bottles hanging on the wall,\n", - "And if one green bottle should accidentally fall,\n", - "There'll be nine green bottles hanging on the wall.\n" - }) - assert.equal(expected, BottleSong.recite(10, 1)) - end) +describe('bottle-song', function() + describe('verse', function() + describe('single verse', function() + it('first generic verse', function() + local expected = table.concat({ + 'Ten green bottles hanging on the wall,\n', + 'Ten green bottles hanging on the wall,\n', + 'And if one green bottle should accidentally fall,\n', + "There'll be nine green bottles hanging on the wall.\n" + }) + assert.equal(expected, BottleSong.recite(10, 1)) + end) - it('last generic verse', function() - expected = table.concat({ - "Three green bottles hanging on the wall,\n", - "Three green bottles hanging on the wall,\n", - "And if one green bottle should accidentally fall,\n", - "There'll be two green bottles hanging on the wall.\n" - }) - assert.equal(expected, BottleSong.recite(3, 1)) - end) + it('last generic verse', function() + local expected = table.concat({ + 'Three green bottles hanging on the wall,\n', + 'Three green bottles hanging on the wall,\n', + 'And if one green bottle should accidentally fall,\n', + "There'll be two green bottles hanging on the wall.\n" + }) + assert.equal(expected, BottleSong.recite(3, 1)) + end) - it('verse with 2 bottles', function() - expected = table.concat({ - "Two green bottles hanging on the wall,\n", - "Two green bottles hanging on the wall,\n", - "And if one green bottle should accidentally fall,\n", - "There'll be one green bottle hanging on the wall.\n" - }) - assert.equal(expected, BottleSong.recite(2, 1)) - end) + it('verse with 2 bottles', function() + local expected = table.concat({ + 'Two green bottles hanging on the wall,\n', + 'Two green bottles hanging on the wall,\n', + 'And if one green bottle should accidentally fall,\n', + "There'll be one green bottle hanging on the wall.\n" + }) + assert.equal(expected, BottleSong.recite(2, 1)) + end) - it('verse with 1 bottle', function() - expected = table.concat({ - "One green bottle hanging on the wall,\n", - "One green bottle hanging on the wall,\n", - "And if one green bottle should accidentally fall,\n", - "There'll be no green bottles hanging on the wall.\n" - }) - assert.equal(expected, BottleSong.recite(1, 1)) + it('verse with 1 bottle', function() + local expected = table.concat({ + 'One green bottle hanging on the wall,\n', + 'One green bottle hanging on the wall,\n', + 'And if one green bottle should accidentally fall,\n', + "There'll be no green bottles hanging on the wall.\n" + }) + assert.equal(expected, BottleSong.recite(1, 1)) + end) end) end) - describe('multiple verses', function() - it('first two verses', function() - expected = table.concat({ - "Ten green bottles hanging on the wall,\n", - "Ten green bottles hanging on the wall,\n", - "And if one green bottle should accidentally fall,\n", - "There'll be nine green bottles hanging on the wall.\n", - "\n", - "Nine green bottles hanging on the wall,\n", - "Nine green bottles hanging on the wall,\n", - "And if one green bottle should accidentally fall,\n", - "There'll be eight green bottles hanging on the wall.\n" - }) - assert.equal(expected, BottleSong.recite(10, 2)) - end) + describe('lyrics', function() + describe('multiple verses', function() + it('first two verses', function() + local expected = table.concat({ + 'Ten green bottles hanging on the wall,\n', + 'Ten green bottles hanging on the wall,\n', + 'And if one green bottle should accidentally fall,\n', + "There'll be nine green bottles hanging on the wall.\n", + '\n', + 'Nine green bottles hanging on the wall,\n', + 'Nine green bottles hanging on the wall,\n', + 'And if one green bottle should accidentally fall,\n', + "There'll be eight green bottles hanging on the wall.\n" + }) + assert.equal(expected, BottleSong.recite(10, 2)) + end) - it('last three verses', function() - expected = table.concat({ - "Three green bottles hanging on the wall,\n", - "Three green bottles hanging on the wall,\n", - "And if one green bottle should accidentally fall,\n", - "There'll be two green bottles hanging on the wall.\n", - "\n", - "Two green bottles hanging on the wall,\n", - "Two green bottles hanging on the wall,\n", - "And if one green bottle should accidentally fall,\n", - "There'll be one green bottle hanging on the wall.\n", - "\n", - "One green bottle hanging on the wall,\n", - "One green bottle hanging on the wall,\n", - "And if one green bottle should accidentally fall,\n", - "There'll be no green bottles hanging on the wall.\n" - }) - assert.equal(expected, BottleSong.recite(3, 3)) - end) + it('last three verses', function() + local expected = table.concat({ + 'Three green bottles hanging on the wall,\n', + 'Three green bottles hanging on the wall,\n', + 'And if one green bottle should accidentally fall,\n', + "There'll be two green bottles hanging on the wall.\n", + '\n', + 'Two green bottles hanging on the wall,\n', + 'Two green bottles hanging on the wall,\n', + 'And if one green bottle should accidentally fall,\n', + "There'll be one green bottle hanging on the wall.\n", + '\n', + 'One green bottle hanging on the wall,\n', + 'One green bottle hanging on the wall,\n', + 'And if one green bottle should accidentally fall,\n', + "There'll be no green bottles hanging on the wall.\n" + }) + assert.equal(expected, BottleSong.recite(3, 3)) + end) - it('all verses', function() - expected = table.concat({ - "Ten green bottles hanging on the wall,\n", - "Ten green bottles hanging on the wall,\n", - "And if one green bottle should accidentally fall,\n", - "There'll be nine green bottles hanging on the wall.\n", - "\n", - "Nine green bottles hanging on the wall,\n", - "Nine green bottles hanging on the wall,\n", - "And if one green bottle should accidentally fall,\n", - "There'll be eight green bottles hanging on the wall.\n", - "\n", - "Eight green bottles hanging on the wall,\n", - "Eight green bottles hanging on the wall,\n", - "And if one green bottle should accidentally fall,\n", - "There'll be seven green bottles hanging on the wall.\n", - "\n", - "Seven green bottles hanging on the wall,\n", - "Seven green bottles hanging on the wall,\n", - "And if one green bottle should accidentally fall,\n", - "There'll be six green bottles hanging on the wall.\n", - "\n", - "Six green bottles hanging on the wall,\n", - "Six green bottles hanging on the wall,\n", - "And if one green bottle should accidentally fall,\n", - "There'll be five green bottles hanging on the wall.\n", - "\n", - "Five green bottles hanging on the wall,\n", - "Five green bottles hanging on the wall,\n", - "And if one green bottle should accidentally fall,\n", - "There'll be four green bottles hanging on the wall.\n", - "\n", - "Four green bottles hanging on the wall,\n", - "Four green bottles hanging on the wall,\n", - "And if one green bottle should accidentally fall,\n", - "There'll be three green bottles hanging on the wall.\n", - "\n", - "Three green bottles hanging on the wall,\n", - "Three green bottles hanging on the wall,\n", - "And if one green bottle should accidentally fall,\n", - "There'll be two green bottles hanging on the wall.\n", - "\n", - "Two green bottles hanging on the wall,\n", - "Two green bottles hanging on the wall,\n", - "And if one green bottle should accidentally fall,\n", - "There'll be one green bottle hanging on the wall.\n", - "\n", - "One green bottle hanging on the wall,\n", - "One green bottle hanging on the wall,\n", - "And if one green bottle should accidentally fall,\n", - "There'll be no green bottles hanging on the wall.\n" - }) - assert.equal(expected, BottleSong.recite(10, 10)) + it('all verses', function() + local expected = table.concat({ + 'Ten green bottles hanging on the wall,\n', + 'Ten green bottles hanging on the wall,\n', + 'And if one green bottle should accidentally fall,\n', + "There'll be nine green bottles hanging on the wall.\n", + '\n', + 'Nine green bottles hanging on the wall,\n', + 'Nine green bottles hanging on the wall,\n', + 'And if one green bottle should accidentally fall,\n', + "There'll be eight green bottles hanging on the wall.\n", + '\n', + 'Eight green bottles hanging on the wall,\n', + 'Eight green bottles hanging on the wall,\n', + 'And if one green bottle should accidentally fall,\n', + "There'll be seven green bottles hanging on the wall.\n", + '\n', + 'Seven green bottles hanging on the wall,\n', + 'Seven green bottles hanging on the wall,\n', + 'And if one green bottle should accidentally fall,\n', + "There'll be six green bottles hanging on the wall.\n", + '\n', + 'Six green bottles hanging on the wall,\n', + 'Six green bottles hanging on the wall,\n', + 'And if one green bottle should accidentally fall,\n', + "There'll be five green bottles hanging on the wall.\n", + '\n', + 'Five green bottles hanging on the wall,\n', + 'Five green bottles hanging on the wall,\n', + 'And if one green bottle should accidentally fall,\n', + "There'll be four green bottles hanging on the wall.\n", + '\n', + 'Four green bottles hanging on the wall,\n', + 'Four green bottles hanging on the wall,\n', + 'And if one green bottle should accidentally fall,\n', + "There'll be three green bottles hanging on the wall.\n", + '\n', + 'Three green bottles hanging on the wall,\n', + 'Three green bottles hanging on the wall,\n', + 'And if one green bottle should accidentally fall,\n', + "There'll be two green bottles hanging on the wall.\n", + '\n', + 'Two green bottles hanging on the wall,\n', + 'Two green bottles hanging on the wall,\n', + 'And if one green bottle should accidentally fall,\n', + "There'll be one green bottle hanging on the wall.\n", + '\n', + 'One green bottle hanging on the wall,\n', + 'One green bottle hanging on the wall,\n', + 'And if one green bottle should accidentally fall,\n', + "There'll be no green bottles hanging on the wall.\n" + }) + assert.equal(expected, BottleSong.recite(10, 10)) + end) end) end) end) diff --git a/exercises/practice/collatz-conjecture/.meta/spec_generator.lua b/exercises/practice/collatz-conjecture/.meta/spec_generator.lua new file mode 100644 index 00000000..42dff7ea --- /dev/null +++ b/exercises/practice/collatz-conjecture/.meta/spec_generator.lua @@ -0,0 +1,17 @@ +local utils = require 'utils' + +return { + module_name = 'conjecture', + + generate_test = function(case) + if type(case.expected) == 'table' then + local template = [[ + assert.has_error(function() conjecture(%s) end)]] + return template:format(case.input.number) + else + local template = [[ + assert.are.equal(%s, conjecture(%s))]] + return template:format(case.expected, case.input.number) + end + end +} diff --git a/exercises/practice/collatz-conjecture/.meta/tests.toml b/exercises/practice/collatz-conjecture/.meta/tests.toml index cc34e168..0014abd1 100644 --- a/exercises/practice/collatz-conjecture/.meta/tests.toml +++ b/exercises/practice/collatz-conjecture/.meta/tests.toml @@ -23,7 +23,6 @@ description = "large number of even and odd steps" [7d4750e6-def9-4b86-aec7-9f7eb44f95a3] description = "zero is an error" -include = false [2187673d-77d6-4543-975e-66df6c50e2da] description = "zero is an error" @@ -31,7 +30,6 @@ reimplements = "7d4750e6-def9-4b86-aec7-9f7eb44f95a3" [c6c795bf-a288-45e9-86a1-841359ad426d] description = "negative value is an error" -include = false [ec11f479-56bc-47fd-a434-bcd7a31a7a2e] description = "negative value is an error" diff --git a/exercises/practice/collatz-conjecture/collatz-conjecture_spec.lua b/exercises/practice/collatz-conjecture/collatz-conjecture_spec.lua index de1da5ae..9f2e28bf 100644 --- a/exercises/practice/collatz-conjecture/collatz-conjecture_spec.lua +++ b/exercises/practice/collatz-conjecture/collatz-conjecture_spec.lua @@ -23,6 +23,18 @@ describe('collatz-conjecture', function() end) end) + it('zero is an error', function() + assert.has_error(function() + conjecture(0) + end) + end) + + it('negative value is an error', function() + assert.has_error(function() + conjecture(-15) + end) + end) + it('negative value is an error', function() assert.has_error(function() conjecture(-15) diff --git a/exercises/practice/proverb/.meta/spec_generator.lua b/exercises/practice/proverb/.meta/spec_generator.lua new file mode 100644 index 00000000..625eeb85 --- /dev/null +++ b/exercises/practice/proverb/.meta/spec_generator.lua @@ -0,0 +1,18 @@ +local utils = require 'utils' + +return { + module_name = 'Proverb', + + generate_test = function(case) + local template = [[ + local strings = { %s } + local expected = table.concat({ %s }) + assert.equal(expected, Proverb.%s(strings))]] + + local strings = table.concat(utils.map(case.input.strings, utils.stringify), ', ') + local expected = table.concat(utils.map(case.expected, function(x) + return utils.stringify(x .. '\n') + end), ', ') + return template:format(strings, expected, utils.snake_case(case.property)) + end +} diff --git a/exercises/practice/proverb/proverb_spec.lua b/exercises/practice/proverb/proverb_spec.lua index 6be92cfd..37490d3d 100644 --- a/exercises/practice/proverb/proverb_spec.lua +++ b/exercises/practice/proverb/proverb_spec.lua @@ -1,4 +1,4 @@ -local Proverb = require 'proverb' +local Proverb = require('proverb') describe('proverb', function() it('zero pieces', function() @@ -9,22 +9,22 @@ describe('proverb', function() it('one piece', function() local strings = { 'nail' } - local expected = table.concat({ "And all for the want of a nail.\n" }) + local expected = table.concat({ 'And all for the want of a nail.\n' }) assert.equal(expected, Proverb.recite(strings)) end) it('two pieces', function() local strings = { 'nail', 'shoe' } - local expected = table.concat({ "For want of a nail the shoe was lost.\n", "And all for the want of a nail.\n" }) + local expected = table.concat({ 'For want of a nail the shoe was lost.\n', 'And all for the want of a nail.\n' }) assert.equal(expected, Proverb.recite(strings)) end) it('three pieces', function() local strings = { 'nail', 'shoe', 'horse' } local expected = table.concat({ - "For want of a nail the shoe was lost.\n", - "For want of a shoe the horse was lost.\n", - "And all for the want of a nail.\n" + 'For want of a nail the shoe was lost.\n', + 'For want of a shoe the horse was lost.\n', + 'And all for the want of a nail.\n' }) assert.equal(expected, Proverb.recite(strings)) end) @@ -32,13 +32,13 @@ describe('proverb', function() it('full proverb', function() local strings = { 'nail', 'shoe', 'horse', 'rider', 'message', 'battle', 'kingdom' } local expected = table.concat({ - "For want of a nail the shoe was lost.\n", - "For want of a shoe the horse was lost.\n", - "For want of a horse the rider was lost.\n", - "For want of a rider the message was lost.\n", - "For want of a message the battle was lost.\n", - "For want of a battle the kingdom was lost.\n", - "And all for the want of a nail.\n" + 'For want of a nail the shoe was lost.\n', + 'For want of a shoe the horse was lost.\n', + 'For want of a horse the rider was lost.\n', + 'For want of a rider the message was lost.\n', + 'For want of a message the battle was lost.\n', + 'For want of a battle the kingdom was lost.\n', + 'And all for the want of a nail.\n' }) assert.equal(expected, Proverb.recite(strings)) end) @@ -46,10 +46,10 @@ describe('proverb', function() it('four pieces modernized', function() local strings = { 'pin', 'gun', 'soldier', 'battle' } local expected = table.concat({ - "For want of a pin the gun was lost.\n", - "For want of a gun the soldier was lost.\n", - "For want of a soldier the battle was lost.\n", - "And all for the want of a pin.\n" + 'For want of a pin the gun was lost.\n', + 'For want of a gun the soldier was lost.\n', + 'For want of a soldier the battle was lost.\n', + 'And all for the want of a pin.\n' }) assert.equal(expected, Proverb.recite(strings)) end)