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
9 changes: 9 additions & 0 deletions exercises/practice/armstrong-numbers/.meta/spec_generator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
return {
module_name = 'ArmstrongNumbers',

generate_test = function(case)
local template = [[
assert.is_%s(ArmstrongNumbers.is_armstrong_number(%d))]]
return template:format(case.expected, case.input.number)
end
}
22 changes: 11 additions & 11 deletions exercises/practice/armstrong-numbers/armstrong-numbers_spec.lua
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
local ArmstrongNumbers = require 'armstrong-numbers'
local ArmstrongNumbers = require('armstrong-numbers')

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

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

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

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

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

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

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

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

it('Seven-digit number that is not an Armstrong number', function()
it('seven-digit number that is not an armstrong number', function()
assert.is_false(ArmstrongNumbers.is_armstrong_number(9926314))
end)
end)
9 changes: 9 additions & 0 deletions exercises/practice/darts/.meta/spec_generator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
return {
module_name = 'Darts',

generate_test = function(case)
local template = [[
assert.equal(%s, Darts.%s(%s, %s))]]
return template:format(case.expected, case.property, case.input.x, case.input.y)
end
}
28 changes: 14 additions & 14 deletions exercises/practice/darts/darts_spec.lua
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
local Darts = require 'darts'
local Darts = require('darts')

describe('darts', function()
it('Missed target', function()
it('missed target', function()
assert.equal(0, Darts.score(-9, 9))
end)

it('On the outer circle', function()
it('on the outer circle', function()
assert.equal(1, Darts.score(0, 10))
end)

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

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

it('Exactly on center', function()
it('exactly on center', function()
assert.equal(10, Darts.score(0, 0))
end)

it('Near the center', function()
it('near the center', function()
assert.equal(10, Darts.score(-0.1, -0.1))
end)

it('Just within the inner circle', function()
it('just within the inner circle', function()
assert.equal(10, Darts.score(0.7, 0.7))
end)

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

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

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

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

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

it('Asymmetric position between the inner and middle circles', function()
it('asymmetric position between the inner and middle circles', function()
assert.equal(5, Darts.score(0.5, -4))
end)
end)
19 changes: 19 additions & 0 deletions exercises/practice/difference-of-squares/.meta/spec_generator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
local function snake_case(str)
local s = str:gsub('%u', function(c)
return '_' .. c:lower()
end)
if s:sub(1, 1) == '_' then
s = s:sub(2)
end
return s
end

return {
module_name = 'diff',

generate_test = function(case)
local template = [[
assert.equal(%s, diff.%s(%s))]]
return template:format(case.expected, snake_case(case.property), case.input.number)
end
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
local diff = require('difference-of-squares')

describe('difference-of-squares', function()
describe('square_of_sum', function()
it('should square the sum of the numbers up to the given number', function()
describe('square the sum of the numbers up to the given number', function()
it('square of sum 1', function()
assert.equal(1, diff.square_of_sum(1))
end)

it('square of sum 5', function()
assert.equal(225, diff.square_of_sum(5))
assert.equal(3025, diff.square_of_sum(10))
end)

it('square of sum 100', function()
assert.equal(25502500, diff.square_of_sum(100))
end)
end)

describe('sum_of_squares', function()
it('should sum the squares of the numbers up to the given number', function()
describe('sum the squares of the numbers up to the given number', function()
it('sum of squares 1', function()
assert.equal(1, diff.sum_of_squares(1))
end)

it('sum of squares 5', function()
assert.equal(55, diff.sum_of_squares(5))
assert.equal(385, diff.sum_of_squares(10))
end)

it('sum of squares 100', function()
assert.equal(338350, diff.sum_of_squares(100))
end)
end)

describe('difference_of_squares', function()
it('should subtract sum of squares from square of sum', function()
assert.equal(0, diff.difference_of_squares(0))
describe('subtract sum of squares from square of sums', function()
it('difference of squares 1', function()
assert.equal(0, diff.difference_of_squares(1))
end)

it('difference of squares 5', function()
assert.equal(170, diff.difference_of_squares(5))
assert.equal(2640, diff.difference_of_squares(10))
end)

it('difference of squares 100', function()
assert.equal(25164150, diff.difference_of_squares(100))
end)
end)
Expand Down
19 changes: 19 additions & 0 deletions exercises/practice/eliuds-eggs/.meta/spec_generator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
local function snake_case(str)
local s = str:gsub('%u', function(c)
return '_' .. c:lower()
end)
if s:sub(1, 1) == '_' then
s = s:sub(2)
end
return s
end

return {
module_name = 'EliudsEggs',

generate_test = function(case)
local template = [[
assert.equal(%s, EliudsEggs.%s(%s))]]
return template:format(case.expected, snake_case(case.property), case.input.number)
end
}
2 changes: 1 addition & 1 deletion exercises/practice/eliuds-eggs/eliuds-eggs_spec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local EliudsEggs = require 'eliuds-eggs'
local EliudsEggs = require('eliuds-eggs')

describe('eliuds-eggs', function()
it('0 eggs', function()
Expand Down
21 changes: 21 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/spec_generator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local function map(t, f)
local mapped = {}
for i, v in ipairs(t) do
mapped[i] = f(v)
end
return mapped
end

local function stringify(s)
return "'" .. s .. "'"
end

return {
module_name = 'rcd',

generate_test = function(case)
local template = [[
assert.equal(%d, rcd.value({ %s }))]]
return template:format(case.expected, table.concat(map(case.input.colors, stringify), ', '))
end
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
local rcd = require 'resistor-color-duo'
local rcd = require('resistor-color-duo')

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

it('Blue and grey', function()
it('blue and grey', function()
assert.equal(68, rcd.value({ 'blue', 'grey' }))
end)

it('Yellow and violet', function()
it('yellow and violet', function()
assert.equal(47, rcd.value({ 'yellow', 'violet' }))
end)

it('White and red', function()
it('white and red', function()
assert.equal(92, rcd.value({ 'white', 'red' }))
end)

it('Orange and orange', function()
it('orange and orange', function()
assert.equal(33, rcd.value({ 'orange', 'orange' }))
end)

it('Ignore additional colors', function()
it('ignore additional colors', function()
assert.equal(51, rcd.value({ 'green', 'brown', 'orange' }))
end)

it('Black and brown, one-digit', function()
it('black and brown, one-digit', function()
assert.equal(1, rcd.value({ 'black', 'brown' }))
end)
end)
2 changes: 1 addition & 1 deletion exercises/practice/resistor-color-trio/.meta/example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ local value = {
}

return {
decode = function(c1, c2, c3)
label = function(c1, c2, c3)
local value = (value[c1] * 10 + value[c2]) * 10 ^ value[c3]

if value >= 1e9 then
Expand Down
28 changes: 28 additions & 0 deletions exercises/practice/resistor-color-trio/.meta/spec_generator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
local function map(t, f)
local mapped = {}
for i, v in ipairs(t) do
mapped[i] = f(v)
end
return mapped
end

local function stringify(s)
return "'" .. s .. "'"
end

return {
module_name = 'rct',

generate_test = function(case)
local template = [[
local value, unit = rct.%s(%s)
assert.are.equal(%s, value)
assert.are.equal('%s', unit)]]
return template:format(table.unpack({
case.property,
table.concat(map(case.input.colors, stringify), ', '),
case.expected.value,
case.expected.unit
}))
end
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
return {
decode = function(c1, c2, c3)
label = function(c1, c2, c3)
end
}
Loading
Loading