Skip to content

Commit d42266d

Browse files
BNAndrasBNAndrasryanplusplus
authored
Add spec generator for gigasecond (#634)
* Add spec generator for `gigasecond` * Minor tweaks to generator * Format * snake_case --------- Co-authored-by: BNAndras <bnandras@example.com> Co-authored-by: Ryan Hartlage <2488333+ryanplusplus@users.noreply.github.com>
1 parent 6b2d08e commit d42266d

File tree

3 files changed

+66
-17
lines changed

3 files changed

+66
-17
lines changed

exercises/practice/gigasecond/.meta/example.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
local gigasecond = {}
44

55
function gigasecond.anniversary(any_date)
6-
return os.date('%x', any_date + math.pow(10, 9))
6+
return os.date('!%x', any_date + math.pow(10, 9))
77
end
88

99
return gigasecond
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
local function components(moment)
2+
return moment:match("(%d%d%d%d)%-(%d%d)%-(%d%d)T?(%d*):?(%d*):?(%d*)")
3+
end
4+
5+
local function map_to_num(...)
6+
local t = { ... }
7+
for i, str in ipairs(t) do
8+
if str ~= nil and str ~= '' then
9+
t[i] = tonumber(str)
10+
else
11+
t[i] = 0
12+
end
13+
end
14+
return table.unpack(t)
15+
end
16+
17+
return {
18+
module_name = 'gigasecond',
19+
20+
generate_test = function(case)
21+
local year, month, day, hour, min, sec = map_to_num(components(case.input.moment))
22+
local e_year, e_month, e_day, e_hour, e_min, e_sec = map_to_num(components(case.expected))
23+
24+
local template = [[
25+
local moment_a = os.time({ year = %d, month = %d, day = %d, hour = %d, min = %d, sec = %d })
26+
local moment_b = os.time({ year = %d, month = %d, day = %d, hour = %d, min = %d, sec = %d })
27+
local actual = gigasecond.anniversary(moment_a)
28+
local expected = os.date('!%%x', moment_b)
29+
assert.are.equals(expected, actual)]]
30+
31+
return template:format(year, month, day, hour, min, sec, e_year, e_month, e_day, e_hour, e_min, e_sec)
32+
end
33+
}
Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,43 @@
11
local gigasecond = require('gigasecond')
22

33
describe('gigasecond', function()
4-
it('test 1', function()
5-
local actual = gigasecond.anniversary(os.time({ year = 2011, month = 3, day = 25, hour = 0, min = 0, sec = 0 }))
6-
local expectedDate = os.date('%x', os.time({ year = 2042, month = 12, day = 1, hour = 0, min = 0, sec = 0 }))
7-
assert.are.equals(expectedDate, actual)
4+
it('date only specification of time', function()
5+
local moment_a = os.time({ year = 2011, month = 4, day = 25, hour = 0, min = 0, sec = 0 })
6+
local moment_b = os.time({ year = 2043, month = 1, day = 1, hour = 1, min = 46, sec = 40 })
7+
local actual = gigasecond.anniversary(moment_a)
8+
local expected = os.date('!%x', moment_b)
9+
assert.are.equals(expected, actual)
810
end)
911

10-
it('test 2', function()
11-
local actual = gigasecond.anniversary(os.time({ year = 1977, month = 5, day = 13, hour = 0, min = 0, sec = 0 }))
12-
local expectedDate = os.date('%x', os.time({ year = 2009, month = 1, day = 19 }))
13-
assert.are.equals(expectedDate, actual)
12+
it('second test for date only specification of time', function()
13+
local moment_a = os.time({ year = 1977, month = 6, day = 13, hour = 0, min = 0, sec = 0 })
14+
local moment_b = os.time({ year = 2009, month = 2, day = 19, hour = 1, min = 46, sec = 40 })
15+
local actual = gigasecond.anniversary(moment_a)
16+
local expected = os.date('!%x', moment_b)
17+
assert.are.equals(expected, actual)
1418
end)
1519

16-
it('test 3', function()
17-
local actual = gigasecond.anniversary(os.time({ year = 1999, month = 7, day = 19 }))
18-
local expectedDate = os.date('%x', os.time({ year = 2031, month = 3, day = 27 }))
19-
assert.are.equals(expectedDate, actual)
20+
it('third test for date only specification of time', function()
21+
local moment_a = os.time({ year = 1959, month = 7, day = 19, hour = 0, min = 0, sec = 0 })
22+
local moment_b = os.time({ year = 1991, month = 3, day = 27, hour = 1, min = 46, sec = 40 })
23+
local actual = gigasecond.anniversary(moment_a)
24+
local expected = os.date('!%x', moment_b)
25+
assert.are.equals(expected, actual)
2026
end)
2127

22-
it('test 4', function()
23-
local actual = gigasecond.anniversary(os.time({ year = 1993, month = 8, day = 17 }))
24-
local expectedDate = os.date('%x', os.time({ year = 2025, month = 4, day = 25 }))
25-
assert.are.equals(expectedDate, actual)
28+
it('full time specified', function()
29+
local moment_a = os.time({ year = 2015, month = 1, day = 24, hour = 22, min = 0, sec = 0 })
30+
local moment_b = os.time({ year = 2046, month = 10, day = 2, hour = 23, min = 46, sec = 40 })
31+
local actual = gigasecond.anniversary(moment_a)
32+
local expected = os.date('!%x', moment_b)
33+
assert.are.equals(expected, actual)
34+
end)
35+
36+
it('full time with day roll-over', function()
37+
local moment_a = os.time({ year = 2015, month = 1, day = 24, hour = 23, min = 59, sec = 59 })
38+
local moment_b = os.time({ year = 2046, month = 10, day = 3, hour = 1, min = 46, sec = 39 })
39+
local actual = gigasecond.anniversary(moment_a)
40+
local expected = os.date('!%x', moment_b)
41+
assert.are.equals(expected, actual)
2642
end)
2743
end)

0 commit comments

Comments
 (0)