Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions exercises/practice/bottle-song/.meta/spec_generator.lua
Original file line number Diff line number Diff line change
@@ -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
}
254 changes: 129 additions & 125 deletions exercises/practice/bottle-song/bottle-song_spec.lua
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 17 additions & 0 deletions exercises/practice/collatz-conjecture/.meta/spec_generator.lua
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 0 additions & 2 deletions exercises/practice/collatz-conjecture/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ 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"
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"
Expand Down
12 changes: 12 additions & 0 deletions exercises/practice/collatz-conjecture/collatz-conjecture_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions exercises/practice/proverb/.meta/spec_generator.lua
Original file line number Diff line number Diff line change
@@ -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
}
Loading
Loading