Skip to content

Commit 7f21eb9

Browse files
authored
Spec generators for bottle-song, collatz-conjecture, proverb (#652)
1 parent 7f3d4cd commit 7f21eb9

7 files changed

Lines changed: 209 additions & 144 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
local utils = require 'utils'
2+
3+
return {
4+
module_name = 'BottleSong',
5+
6+
generate_test = function(case)
7+
local template = [[
8+
local expected = table.concat({ %s })
9+
assert.equal(expected, BottleSong.%s(%s, %s))]]
10+
11+
local expected = table.concat(utils.map(case.expected, function(x)
12+
return utils.stringify(x .. '\n')
13+
end), ', ')
14+
return template:format(expected, utils.snake_case(case.property), case.input.startBottles, case.input.takeDown)
15+
end
16+
}
Lines changed: 129 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,141 @@
1-
local BottleSong = require 'bottle-song'
1+
local BottleSong = require('bottle-song')
22

3-
describe('lyrics', function()
4-
describe('single verse', function()
5-
it('first generic verse', function()
6-
expected = table.concat({
7-
"Ten green bottles hanging on the wall,\n",
8-
"Ten green bottles hanging on the wall,\n",
9-
"And if one green bottle should accidentally fall,\n",
10-
"There'll be nine green bottles hanging on the wall.\n"
11-
})
12-
assert.equal(expected, BottleSong.recite(10, 1))
13-
end)
3+
describe('bottle-song', function()
4+
describe('verse', function()
5+
describe('single verse', function()
6+
it('first generic verse', function()
7+
local expected = table.concat({
8+
'Ten green bottles hanging on the wall,\n',
9+
'Ten green bottles hanging on the wall,\n',
10+
'And if one green bottle should accidentally fall,\n',
11+
"There'll be nine green bottles hanging on the wall.\n"
12+
})
13+
assert.equal(expected, BottleSong.recite(10, 1))
14+
end)
1415

15-
it('last generic verse', function()
16-
expected = table.concat({
17-
"Three green bottles hanging on the wall,\n",
18-
"Three green bottles hanging on the wall,\n",
19-
"And if one green bottle should accidentally fall,\n",
20-
"There'll be two green bottles hanging on the wall.\n"
21-
})
22-
assert.equal(expected, BottleSong.recite(3, 1))
23-
end)
16+
it('last generic verse', function()
17+
local expected = table.concat({
18+
'Three green bottles hanging on the wall,\n',
19+
'Three green bottles hanging on the wall,\n',
20+
'And if one green bottle should accidentally fall,\n',
21+
"There'll be two green bottles hanging on the wall.\n"
22+
})
23+
assert.equal(expected, BottleSong.recite(3, 1))
24+
end)
2425

25-
it('verse with 2 bottles', function()
26-
expected = table.concat({
27-
"Two green bottles hanging on the wall,\n",
28-
"Two green bottles hanging on the wall,\n",
29-
"And if one green bottle should accidentally fall,\n",
30-
"There'll be one green bottle hanging on the wall.\n"
31-
})
32-
assert.equal(expected, BottleSong.recite(2, 1))
33-
end)
26+
it('verse with 2 bottles', function()
27+
local expected = table.concat({
28+
'Two green bottles hanging on the wall,\n',
29+
'Two green bottles hanging on the wall,\n',
30+
'And if one green bottle should accidentally fall,\n',
31+
"There'll be one green bottle hanging on the wall.\n"
32+
})
33+
assert.equal(expected, BottleSong.recite(2, 1))
34+
end)
3435

35-
it('verse with 1 bottle', function()
36-
expected = table.concat({
37-
"One green bottle hanging on the wall,\n",
38-
"One green bottle hanging on the wall,\n",
39-
"And if one green bottle should accidentally fall,\n",
40-
"There'll be no green bottles hanging on the wall.\n"
41-
})
42-
assert.equal(expected, BottleSong.recite(1, 1))
36+
it('verse with 1 bottle', function()
37+
local expected = table.concat({
38+
'One green bottle hanging on the wall,\n',
39+
'One green bottle hanging on the wall,\n',
40+
'And if one green bottle should accidentally fall,\n',
41+
"There'll be no green bottles hanging on the wall.\n"
42+
})
43+
assert.equal(expected, BottleSong.recite(1, 1))
44+
end)
4345
end)
4446
end)
4547

46-
describe('multiple verses', function()
47-
it('first two verses', function()
48-
expected = table.concat({
49-
"Ten green bottles hanging on the wall,\n",
50-
"Ten green bottles hanging on the wall,\n",
51-
"And if one green bottle should accidentally fall,\n",
52-
"There'll be nine green bottles hanging on the wall.\n",
53-
"\n",
54-
"Nine green bottles hanging on the wall,\n",
55-
"Nine green bottles hanging on the wall,\n",
56-
"And if one green bottle should accidentally fall,\n",
57-
"There'll be eight green bottles hanging on the wall.\n"
58-
})
59-
assert.equal(expected, BottleSong.recite(10, 2))
60-
end)
48+
describe('lyrics', function()
49+
describe('multiple verses', function()
50+
it('first two verses', function()
51+
local expected = table.concat({
52+
'Ten green bottles hanging on the wall,\n',
53+
'Ten green bottles hanging on the wall,\n',
54+
'And if one green bottle should accidentally fall,\n',
55+
"There'll be nine green bottles hanging on the wall.\n",
56+
'\n',
57+
'Nine green bottles hanging on the wall,\n',
58+
'Nine green bottles hanging on the wall,\n',
59+
'And if one green bottle should accidentally fall,\n',
60+
"There'll be eight green bottles hanging on the wall.\n"
61+
})
62+
assert.equal(expected, BottleSong.recite(10, 2))
63+
end)
6164

62-
it('last three verses', function()
63-
expected = table.concat({
64-
"Three green bottles hanging on the wall,\n",
65-
"Three green bottles hanging on the wall,\n",
66-
"And if one green bottle should accidentally fall,\n",
67-
"There'll be two green bottles hanging on the wall.\n",
68-
"\n",
69-
"Two green bottles hanging on the wall,\n",
70-
"Two green bottles hanging on the wall,\n",
71-
"And if one green bottle should accidentally fall,\n",
72-
"There'll be one green bottle hanging on the wall.\n",
73-
"\n",
74-
"One green bottle hanging on the wall,\n",
75-
"One green bottle hanging on the wall,\n",
76-
"And if one green bottle should accidentally fall,\n",
77-
"There'll be no green bottles hanging on the wall.\n"
78-
})
79-
assert.equal(expected, BottleSong.recite(3, 3))
80-
end)
65+
it('last three verses', function()
66+
local expected = table.concat({
67+
'Three green bottles hanging on the wall,\n',
68+
'Three green bottles hanging on the wall,\n',
69+
'And if one green bottle should accidentally fall,\n',
70+
"There'll be two green bottles hanging on the wall.\n",
71+
'\n',
72+
'Two green bottles hanging on the wall,\n',
73+
'Two green bottles hanging on the wall,\n',
74+
'And if one green bottle should accidentally fall,\n',
75+
"There'll be one green bottle hanging on the wall.\n",
76+
'\n',
77+
'One green bottle hanging on the wall,\n',
78+
'One green bottle hanging on the wall,\n',
79+
'And if one green bottle should accidentally fall,\n',
80+
"There'll be no green bottles hanging on the wall.\n"
81+
})
82+
assert.equal(expected, BottleSong.recite(3, 3))
83+
end)
8184

82-
it('all verses', function()
83-
expected = table.concat({
84-
"Ten green bottles hanging on the wall,\n",
85-
"Ten green bottles hanging on the wall,\n",
86-
"And if one green bottle should accidentally fall,\n",
87-
"There'll be nine green bottles hanging on the wall.\n",
88-
"\n",
89-
"Nine green bottles hanging on the wall,\n",
90-
"Nine green bottles hanging on the wall,\n",
91-
"And if one green bottle should accidentally fall,\n",
92-
"There'll be eight green bottles hanging on the wall.\n",
93-
"\n",
94-
"Eight green bottles hanging on the wall,\n",
95-
"Eight green bottles hanging on the wall,\n",
96-
"And if one green bottle should accidentally fall,\n",
97-
"There'll be seven green bottles hanging on the wall.\n",
98-
"\n",
99-
"Seven green bottles hanging on the wall,\n",
100-
"Seven green bottles hanging on the wall,\n",
101-
"And if one green bottle should accidentally fall,\n",
102-
"There'll be six green bottles hanging on the wall.\n",
103-
"\n",
104-
"Six green bottles hanging on the wall,\n",
105-
"Six green bottles hanging on the wall,\n",
106-
"And if one green bottle should accidentally fall,\n",
107-
"There'll be five green bottles hanging on the wall.\n",
108-
"\n",
109-
"Five green bottles hanging on the wall,\n",
110-
"Five green bottles hanging on the wall,\n",
111-
"And if one green bottle should accidentally fall,\n",
112-
"There'll be four green bottles hanging on the wall.\n",
113-
"\n",
114-
"Four green bottles hanging on the wall,\n",
115-
"Four green bottles hanging on the wall,\n",
116-
"And if one green bottle should accidentally fall,\n",
117-
"There'll be three green bottles hanging on the wall.\n",
118-
"\n",
119-
"Three green bottles hanging on the wall,\n",
120-
"Three green bottles hanging on the wall,\n",
121-
"And if one green bottle should accidentally fall,\n",
122-
"There'll be two green bottles hanging on the wall.\n",
123-
"\n",
124-
"Two green bottles hanging on the wall,\n",
125-
"Two green bottles hanging on the wall,\n",
126-
"And if one green bottle should accidentally fall,\n",
127-
"There'll be one green bottle hanging on the wall.\n",
128-
"\n",
129-
"One green bottle hanging on the wall,\n",
130-
"One green bottle hanging on the wall,\n",
131-
"And if one green bottle should accidentally fall,\n",
132-
"There'll be no green bottles hanging on the wall.\n"
133-
})
134-
assert.equal(expected, BottleSong.recite(10, 10))
85+
it('all verses', function()
86+
local expected = table.concat({
87+
'Ten green bottles hanging on the wall,\n',
88+
'Ten green bottles hanging on the wall,\n',
89+
'And if one green bottle should accidentally fall,\n',
90+
"There'll be nine green bottles hanging on the wall.\n",
91+
'\n',
92+
'Nine green bottles hanging on the wall,\n',
93+
'Nine green bottles hanging on the wall,\n',
94+
'And if one green bottle should accidentally fall,\n',
95+
"There'll be eight green bottles hanging on the wall.\n",
96+
'\n',
97+
'Eight green bottles hanging on the wall,\n',
98+
'Eight green bottles hanging on the wall,\n',
99+
'And if one green bottle should accidentally fall,\n',
100+
"There'll be seven green bottles hanging on the wall.\n",
101+
'\n',
102+
'Seven green bottles hanging on the wall,\n',
103+
'Seven green bottles hanging on the wall,\n',
104+
'And if one green bottle should accidentally fall,\n',
105+
"There'll be six green bottles hanging on the wall.\n",
106+
'\n',
107+
'Six green bottles hanging on the wall,\n',
108+
'Six green bottles hanging on the wall,\n',
109+
'And if one green bottle should accidentally fall,\n',
110+
"There'll be five green bottles hanging on the wall.\n",
111+
'\n',
112+
'Five green bottles hanging on the wall,\n',
113+
'Five green bottles hanging on the wall,\n',
114+
'And if one green bottle should accidentally fall,\n',
115+
"There'll be four green bottles hanging on the wall.\n",
116+
'\n',
117+
'Four green bottles hanging on the wall,\n',
118+
'Four green bottles hanging on the wall,\n',
119+
'And if one green bottle should accidentally fall,\n',
120+
"There'll be three green bottles hanging on the wall.\n",
121+
'\n',
122+
'Three green bottles hanging on the wall,\n',
123+
'Three green bottles hanging on the wall,\n',
124+
'And if one green bottle should accidentally fall,\n',
125+
"There'll be two green bottles hanging on the wall.\n",
126+
'\n',
127+
'Two green bottles hanging on the wall,\n',
128+
'Two green bottles hanging on the wall,\n',
129+
'And if one green bottle should accidentally fall,\n',
130+
"There'll be one green bottle hanging on the wall.\n",
131+
'\n',
132+
'One green bottle hanging on the wall,\n',
133+
'One green bottle hanging on the wall,\n',
134+
'And if one green bottle should accidentally fall,\n',
135+
"There'll be no green bottles hanging on the wall.\n"
136+
})
137+
assert.equal(expected, BottleSong.recite(10, 10))
138+
end)
135139
end)
136140
end)
137141
end)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
local utils = require 'utils'
2+
3+
return {
4+
module_name = 'conjecture',
5+
6+
generate_test = function(case)
7+
if type(case.expected) == 'table' then
8+
local template = [[
9+
assert.has_error(function() conjecture(%s) end)]]
10+
return template:format(case.input.number)
11+
else
12+
local template = [[
13+
assert.are.equal(%s, conjecture(%s))]]
14+
return template:format(case.expected, case.input.number)
15+
end
16+
end
17+
}

exercises/practice/collatz-conjecture/.meta/tests.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ description = "large number of even and odd steps"
2323

2424
[7d4750e6-def9-4b86-aec7-9f7eb44f95a3]
2525
description = "zero is an error"
26-
include = false
2726

2827
[2187673d-77d6-4543-975e-66df6c50e2da]
2928
description = "zero is an error"
3029
reimplements = "7d4750e6-def9-4b86-aec7-9f7eb44f95a3"
3130

3231
[c6c795bf-a288-45e9-86a1-841359ad426d]
3332
description = "negative value is an error"
34-
include = false
3533

3634
[ec11f479-56bc-47fd-a434-bcd7a31a7a2e]
3735
description = "negative value is an error"

exercises/practice/collatz-conjecture/collatz-conjecture_spec.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ describe('collatz-conjecture', function()
2323
end)
2424
end)
2525

26+
it('zero is an error', function()
27+
assert.has_error(function()
28+
conjecture(0)
29+
end)
30+
end)
31+
32+
it('negative value is an error', function()
33+
assert.has_error(function()
34+
conjecture(-15)
35+
end)
36+
end)
37+
2638
it('negative value is an error', function()
2739
assert.has_error(function()
2840
conjecture(-15)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
local utils = require 'utils'
2+
3+
return {
4+
module_name = 'Proverb',
5+
6+
generate_test = function(case)
7+
local template = [[
8+
local strings = { %s }
9+
local expected = table.concat({ %s })
10+
assert.equal(expected, Proverb.%s(strings))]]
11+
12+
local strings = table.concat(utils.map(case.input.strings, utils.stringify), ', ')
13+
local expected = table.concat(utils.map(case.expected, function(x)
14+
return utils.stringify(x .. '\n')
15+
end), ', ')
16+
return template:format(strings, expected, utils.snake_case(case.property))
17+
end
18+
}

0 commit comments

Comments
 (0)