Skip to content

Commit 08d030f

Browse files
committed
More spec generators
1 parent 239a84c commit 08d030f

File tree

14 files changed

+179
-57
lines changed

14 files changed

+179
-57
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
return {
2+
module_name = 'ArmstrongNumbers',
3+
4+
generate_test = function(case)
5+
local template = [[
6+
assert.is_%s(ArmstrongNumbers.is_armstrong_number(%d))]]
7+
return template:format(case.expected, case.input.number)
8+
end
9+
}
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
local ArmstrongNumbers = require 'armstrong-numbers'
1+
local ArmstrongNumbers = require('armstrong-numbers')
22

3-
describe('armstrong-nunbers', function()
4-
it('Zero is an Armstrong number', function()
3+
describe('armstrong-numbers', function()
4+
it('zero is an armstrong number', function()
55
assert.is_true(ArmstrongNumbers.is_armstrong_number(0))
66
end)
77

8-
it('Single-digit numbers are Armstrong numbers', function()
8+
it('single-digit numbers are armstrong numbers', function()
99
assert.is_true(ArmstrongNumbers.is_armstrong_number(5))
1010
end)
1111

12-
it('There are no two-digit Armstrong numbers', function()
12+
it('there are no two-digit armstrong numbers', function()
1313
assert.is_false(ArmstrongNumbers.is_armstrong_number(10))
1414
end)
1515

16-
it('Three-digit number that is an Armstrong number', function()
16+
it('three-digit number that is an armstrong number', function()
1717
assert.is_true(ArmstrongNumbers.is_armstrong_number(153))
1818
end)
1919

20-
it('Three-digit number that is not an Armstrong number', function()
20+
it('three-digit number that is not an armstrong number', function()
2121
assert.is_false(ArmstrongNumbers.is_armstrong_number(100))
2222
end)
2323

24-
it('Four-digit number that is an Armstrong number', function()
24+
it('four-digit number that is an armstrong number', function()
2525
assert.is_true(ArmstrongNumbers.is_armstrong_number(9474))
2626
end)
2727

28-
it('Four-digit number that is not an Armstrong number', function()
28+
it('four-digit number that is not an armstrong number', function()
2929
assert.is_false(ArmstrongNumbers.is_armstrong_number(9475))
3030
end)
3131

32-
it('Seven-digit number that is an Armstrong number', function()
32+
it('seven-digit number that is an armstrong number', function()
3333
assert.is_true(ArmstrongNumbers.is_armstrong_number(9926315))
3434
end)
3535

36-
it('Seven-digit number that is not an Armstrong number', function()
36+
it('seven-digit number that is not an armstrong number', function()
3737
assert.is_false(ArmstrongNumbers.is_armstrong_number(9926314))
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 = 'Darts',
3+
4+
generate_test = function(case)
5+
local template = [[
6+
assert.equal(%s, Darts.%s(%s, %s))]]
7+
return template:format(case.expected, case.property, case.input.x, case.input.y)
8+
end
9+
}
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
1-
local Darts = require 'darts'
1+
local Darts = require('darts')
22

33
describe('darts', function()
4-
it('Missed target', function()
4+
it('missed target', function()
55
assert.equal(0, Darts.score(-9, 9))
66
end)
77

8-
it('On the outer circle', function()
8+
it('on the outer circle', function()
99
assert.equal(1, Darts.score(0, 10))
1010
end)
1111

12-
it('On the middle circle', function()
12+
it('on the middle circle', function()
1313
assert.equal(5, Darts.score(-5, 0))
1414
end)
1515

16-
it('On the inner circle', function()
16+
it('on the inner circle', function()
1717
assert.equal(10, Darts.score(0, -1))
1818
end)
1919

20-
it('Exactly on center', function()
20+
it('exactly on center', function()
2121
assert.equal(10, Darts.score(0, 0))
2222
end)
2323

24-
it('Near the center', function()
24+
it('near the center', function()
2525
assert.equal(10, Darts.score(-0.1, -0.1))
2626
end)
2727

28-
it('Just within the inner circle', function()
28+
it('just within the inner circle', function()
2929
assert.equal(10, Darts.score(0.7, 0.7))
3030
end)
3131

32-
it('Just outside the inner circle', function()
32+
it('just outside the inner circle', function()
3333
assert.equal(5, Darts.score(0.8, -0.8))
3434
end)
3535

36-
it('Just within the middle circle', function()
36+
it('just within the middle circle', function()
3737
assert.equal(5, Darts.score(-3.5, 3.5))
3838
end)
3939

40-
it('Just outside the middle circle', function()
40+
it('just outside the middle circle', function()
4141
assert.equal(1, Darts.score(-3.6, -3.6))
4242
end)
4343

44-
it('Just within the outer circle', function()
44+
it('just within the outer circle', function()
4545
assert.equal(1, Darts.score(-7.0, 7.0))
4646
end)
4747

48-
it('Just outside the outer circle', function()
48+
it('just outside the outer circle', function()
4949
assert.equal(0, Darts.score(7.1, -7.1))
5050
end)
5151

52-
it('Asymmetric position between the inner and middle circles', function()
52+
it('asymmetric position between the inner and middle circles', function()
5353
assert.equal(5, Darts.score(0.5, -4))
5454
end)
5555
end)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
local function snake_case(str)
2+
local s = str:gsub('%u', function(c)
3+
return '_' .. c:lower()
4+
end)
5+
if s:sub(1, 1) == '_' then
6+
s = s:sub(2)
7+
end
8+
return s
9+
end
10+
11+
return {
12+
module_name = 'diff',
13+
14+
generate_test = function(case)
15+
local template = [[
16+
assert.equal(%s, diff.%s(%s))]]
17+
return template:format(case.expected, snake_case(case.property), case.input.number)
18+
end
19+
}

exercises/practice/difference-of-squares/difference-of-squares_spec.lua

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,44 @@
11
local diff = require('difference-of-squares')
22

33
describe('difference-of-squares', function()
4-
describe('square_of_sum', function()
5-
it('should square the sum of the numbers up to the given number', function()
4+
describe('square the sum of the numbers up to the given number', function()
5+
it('square of sum 1', function()
6+
assert.equal(1, diff.square_of_sum(1))
7+
end)
8+
9+
it('square of sum 5', function()
610
assert.equal(225, diff.square_of_sum(5))
7-
assert.equal(3025, diff.square_of_sum(10))
11+
end)
12+
13+
it('square of sum 100', function()
814
assert.equal(25502500, diff.square_of_sum(100))
915
end)
1016
end)
1117

12-
describe('sum_of_squares', function()
13-
it('should sum the squares of the numbers up to the given number', function()
18+
describe('sum the squares of the numbers up to the given number', function()
19+
it('sum of squares 1', function()
20+
assert.equal(1, diff.sum_of_squares(1))
21+
end)
22+
23+
it('sum of squares 5', function()
1424
assert.equal(55, diff.sum_of_squares(5))
15-
assert.equal(385, diff.sum_of_squares(10))
25+
end)
26+
27+
it('sum of squares 100', function()
1628
assert.equal(338350, diff.sum_of_squares(100))
1729
end)
1830
end)
1931

20-
describe('difference_of_squares', function()
21-
it('should subtract sum of squares from square of sum', function()
22-
assert.equal(0, diff.difference_of_squares(0))
32+
describe('subtract sum of squares from square of sums', function()
33+
it('difference of squares 1', function()
34+
assert.equal(0, diff.difference_of_squares(1))
35+
end)
36+
37+
it('difference of squares 5', function()
2338
assert.equal(170, diff.difference_of_squares(5))
24-
assert.equal(2640, diff.difference_of_squares(10))
39+
end)
40+
41+
it('difference of squares 100', function()
2542
assert.equal(25164150, diff.difference_of_squares(100))
2643
end)
2744
end)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
local function snake_case(str)
2+
local s = str:gsub('%u', function(c)
3+
return '_' .. c:lower()
4+
end)
5+
if s:sub(1, 1) == '_' then
6+
s = s:sub(2)
7+
end
8+
return s
9+
end
10+
11+
return {
12+
module_name = 'EliudsEggs',
13+
14+
generate_test = function(case)
15+
local template = [[
16+
assert.equal(%s, EliudsEggs.%s(%s))]]
17+
return template:format(case.expected, snake_case(case.property), case.input.number)
18+
end
19+
}

exercises/practice/eliuds-eggs/eliuds-eggs_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local EliudsEggs = require 'eliuds-eggs'
1+
local EliudsEggs = require('eliuds-eggs')
22

33
describe('eliuds-eggs', function()
44
it('0 eggs', function()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
local function map(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 stringify(s)
10+
return "'" .. s .. "'"
11+
end
12+
13+
return {
14+
module_name = 'rcd',
15+
16+
generate_test = function(case)
17+
local template = [[
18+
assert.equal(%d, rcd.value({ %s }))]]
19+
return template:format(case.expected, table.concat(map(case.input.colors, stringify), ', '))
20+
end
21+
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
local rcd = require 'resistor-color-duo'
1+
local rcd = require('resistor-color-duo')
22

33
describe('resistor-color-duo', function()
4-
it('Brown and black', function()
4+
it('brown and black', function()
55
assert.equal(10, rcd.value({ 'brown', 'black' }))
66
end)
77

8-
it('Blue and grey', function()
8+
it('blue and grey', function()
99
assert.equal(68, rcd.value({ 'blue', 'grey' }))
1010
end)
1111

12-
it('Yellow and violet', function()
12+
it('yellow and violet', function()
1313
assert.equal(47, rcd.value({ 'yellow', 'violet' }))
1414
end)
1515

16-
it('White and red', function()
16+
it('white and red', function()
1717
assert.equal(92, rcd.value({ 'white', 'red' }))
1818
end)
1919

20-
it('Orange and orange', function()
20+
it('orange and orange', function()
2121
assert.equal(33, rcd.value({ 'orange', 'orange' }))
2222
end)
2323

24-
it('Ignore additional colors', function()
24+
it('ignore additional colors', function()
2525
assert.equal(51, rcd.value({ 'green', 'brown', 'orange' }))
2626
end)
2727

28-
it('Black and brown, one-digit', function()
28+
it('black and brown, one-digit', function()
2929
assert.equal(1, rcd.value({ 'black', 'brown' }))
3030
end)
3131
end)

0 commit comments

Comments
 (0)