Skip to content

Commit 239a84c

Browse files
authored
Add additional spec generators (#638)
1 parent 4bb9a39 commit 239a84c

8 files changed

Lines changed: 115 additions & 72 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
return {
2+
module_name = 'is_leap_year',
3+
4+
generate_test = function(case)
5+
local template = [[
6+
assert.is_%s(is_leap_year(%d))]]
7+
return template:format(case.expected, case.input.year)
8+
end
9+
}

exercises/practice/leap/leap_spec.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
local is_leap_year = require('leap')
22

33
describe('leap', function()
4-
it('year not divisible by 4 is common year', function()
4+
it('year not divisible by 4 in common year', function()
55
assert.is_false(is_leap_year(2015))
66
end)
77

8-
it('year divisible by 2, not divisible by 4 is common year', function()
8+
it('year divisible by 2, not divisible by 4 in common year', function()
99
assert.is_false(is_leap_year(1970))
1010
end)
1111

12-
it('year divisible by 4, not divisible by 100 is leap year', function()
12+
it('year divisible by 4, not divisible by 100 in leap year', function()
1313
assert.is_true(is_leap_year(1996))
1414
end)
1515

1616
it('year divisible by 4 and 5 is still a leap year', function()
1717
assert.is_true(is_leap_year(1960))
1818
end)
1919

20-
it('year divisible by 100, not divisible by 400 is common year', function()
20+
it('year divisible by 100, not divisible by 400 in common year', function()
2121
assert.is_false(is_leap_year(2100))
2222
end)
2323

@@ -33,7 +33,7 @@ describe('leap', function()
3333
assert.is_true(is_leap_year(2400))
3434
end)
3535

36-
it('year divisible by 200, not divisible by 400 is common year', function()
36+
it('year divisible by 200, not divisible by 400 in common year', function()
3737
assert.is_false(is_leap_year(1800))
3838
end)
3939
end)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
return {
2+
module_name = 'raindrops',
3+
4+
generate_test = function(case)
5+
local template = [[
6+
assert.equal('%s', raindrops(%d))]]
7+
return template:format(case.expected, case.input.number)
8+
end
9+
}

exercises/practice/raindrops/raindrops_spec.lua

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,71 +5,71 @@ describe('raindrops', function()
55
assert.equal('1', raindrops(1))
66
end)
77

8-
it('the sound for 3 is Pling', function()
8+
it('the sound for 3 is pling', function()
99
assert.equal('Pling', raindrops(3))
1010
end)
1111

12-
it('the sound for 5 is Plang', function()
12+
it('the sound for 5 is plang', function()
1313
assert.equal('Plang', raindrops(5))
1414
end)
1515

16-
it('the sound for 7 is Plong', function()
16+
it('the sound for 7 is plong', function()
1717
assert.equal('Plong', raindrops(7))
1818
end)
1919

20-
it('the sound for 6 is Pling as it has a factor 3', function()
20+
it('the sound for 6 is pling as it has a factor 3', function()
2121
assert.equal('Pling', raindrops(6))
2222
end)
2323

2424
it('2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base', function()
2525
assert.equal('8', raindrops(8))
2626
end)
2727

28-
it('the sound for 9 is Pling as it has a factor 3', function()
28+
it('the sound for 9 is pling as it has a factor 3', function()
2929
assert.equal('Pling', raindrops(9))
3030
end)
3131

32-
it('the sound for 10 is Plang as it has a factor 5', function()
32+
it('the sound for 10 is plang as it has a factor 5', function()
3333
assert.equal('Plang', raindrops(10))
3434
end)
3535

36-
it('the sound for 14 is Plong as it has a factor 7', function()
36+
it('the sound for 14 is plong as it has a factor of 7', function()
3737
assert.equal('Plong', raindrops(14))
3838
end)
3939

40-
it('the sound for 15 is PlingPlang as it has a factor 3 and 5', function()
40+
it('the sound for 15 is plingplang as it has factors 3 and 5', function()
4141
assert.equal('PlingPlang', raindrops(15))
4242
end)
4343

44-
it('the sound for 21 is PlingPlong as it has factors 3 and 7', function()
44+
it('the sound for 21 is plingplong as it has factors 3 and 7', function()
4545
assert.equal('PlingPlong', raindrops(21))
4646
end)
4747

48-
it('the sound for 25 is Plang as it has a factor 5', function()
48+
it('the sound for 25 is plang as it has a factor 5', function()
4949
assert.equal('Plang', raindrops(25))
5050
end)
5151

52-
it('the sound for 27 is Pling as it has a factor 3', function()
52+
it('the sound for 27 is pling as it has a factor 3', function()
5353
assert.equal('Pling', raindrops(27))
5454
end)
5555

56-
it('the sound for 35 is PlangPlong as it has factors 5 and 7', function()
56+
it('the sound for 35 is plangplong as it has factors 5 and 7', function()
5757
assert.equal('PlangPlong', raindrops(35))
5858
end)
5959

60-
it('the sound for 49 is Plong as it has a factor 7', function()
60+
it('the sound for 49 is plong as it has a factor 7', function()
6161
assert.equal('Plong', raindrops(49))
6262
end)
6363

6464
it('the sound for 52 is 52', function()
6565
assert.equal('52', raindrops(52))
6666
end)
6767

68-
it('the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7', function()
68+
it('the sound for 105 is plingplangplong as it has factors 3, 5 and 7', function()
6969
assert.equal('PlingPlangPlong', raindrops(105))
7070
end)
7171

72-
it('the sound for 3125 is Plang as it has a factor 5', function()
72+
it('the sound for 3125 is plang as it has a factor 5', function()
7373
assert.equal('Plang', raindrops(3125))
7474
end)
7575
end)
Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
local value = {
2-
black = 0,
3-
brown = 1,
4-
red = 2,
5-
orange = 3,
6-
yellow = 4,
7-
green = 5,
8-
blue = 6,
9-
violet = 7,
10-
grey = 8,
11-
white = 9
1+
local colors = {
2+
'black', --
3+
'brown', --
4+
'red', --
5+
'orange', --
6+
'yellow', --
7+
'green', --
8+
'blue', --
9+
'violet', --
10+
'grey', --
11+
'white'
1212
}
1313

1414
return {
15+
colors = function()
16+
return colors
17+
end,
18+
1519
color_code = function(color)
16-
return value[color]
20+
for i, v in ipairs(colors) do
21+
if v == color then
22+
return i - 1
23+
end
24+
end
1725
end
1826
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
local map = function(t, f)
2+
local mapped = {}
3+
for i, v in ipairs(t) do
4+
mapped[i] = f(v)
5+
end
6+
return mapped
7+
end
8+
9+
local function render_table(t)
10+
return table.concat(map(t, function(element)
11+
return "'" .. element .. "'"
12+
end), ', --\n')
13+
end
14+
15+
return {
16+
module_name = 'rc',
17+
18+
generate_test = function(case)
19+
if case.property == 'colors' then
20+
local template = [[
21+
assert.same({ %s }, rc.colors())]]
22+
return template:format(render_table(case.expected))
23+
else
24+
local template = [[
25+
assert.equal(%d, rc.color_code('%s'))]]
26+
return template:format(case.expected, case.input.color)
27+
end
28+
end
29+
}

exercises/practice/resistor-color/.meta/tests.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,3 @@ description = "Color codes -> Orange"
2020

2121
[581d68fa-f968-4be2-9f9d-880f2fb73cf7]
2222
description = "Colors"
23-
include = false
Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,32 @@
1-
local rc = require 'resistor-color'
1+
local rc = require('resistor-color')
22

33
describe('resistor-color', function()
4-
it('Black', function()
5-
assert.equal(0, rc.color_code('black'))
6-
end)
7-
8-
it('Brown', function()
9-
assert.equal(1, rc.color_code('brown'))
10-
end)
11-
12-
it('Red', function()
13-
assert.equal(2, rc.color_code('red'))
14-
end)
15-
16-
it('Orange', function()
17-
assert.equal(3, rc.color_code('orange'))
18-
end)
19-
20-
it('Yellow', function()
21-
assert.equal(4, rc.color_code('yellow'))
22-
end)
23-
24-
it('Green', function()
25-
assert.equal(5, rc.color_code('green'))
26-
end)
27-
28-
it('Blue', function()
29-
assert.equal(6, rc.color_code('blue'))
30-
end)
31-
32-
it('Violet', function()
33-
assert.equal(7, rc.color_code('violet'))
34-
end)
35-
36-
it('Grey', function()
37-
assert.equal(8, rc.color_code('grey'))
38-
end)
39-
40-
it('White', function()
41-
assert.equal(9, rc.color_code('white'))
4+
describe('color codes', function()
5+
it('black', function()
6+
assert.equal(0, rc.color_code('black'))
7+
end)
8+
9+
it('white', function()
10+
assert.equal(9, rc.color_code('white'))
11+
end)
12+
13+
it('orange', function()
14+
assert.equal(3, rc.color_code('orange'))
15+
end)
16+
end)
17+
18+
it('colors', function()
19+
assert.same({
20+
'black', --
21+
'brown', --
22+
'red', --
23+
'orange', --
24+
'yellow', --
25+
'green', --
26+
'blue', --
27+
'violet', --
28+
'grey', --
29+
'white'
30+
}, rc.colors())
4231
end)
4332
end)

0 commit comments

Comments
 (0)