diff --git a/exercises/practice/gigasecond/.meta/example.lua b/exercises/practice/gigasecond/.meta/example.lua index d0da648..6eaeb9b 100644 --- a/exercises/practice/gigasecond/.meta/example.lua +++ b/exercises/practice/gigasecond/.meta/example.lua @@ -3,7 +3,7 @@ local gigasecond = {} function gigasecond.anniversary(any_date) - return os.date('%x', any_date + math.pow(10, 9)) + return os.date('!%x', any_date + math.pow(10, 9)) end return gigasecond diff --git a/exercises/practice/gigasecond/.meta/spec_generator.lua b/exercises/practice/gigasecond/.meta/spec_generator.lua new file mode 100644 index 0000000..9f5a6c6 --- /dev/null +++ b/exercises/practice/gigasecond/.meta/spec_generator.lua @@ -0,0 +1,33 @@ +local function components(moment) + return moment:match("(%d%d%d%d)%-(%d%d)%-(%d%d)T?(%d*):?(%d*):?(%d*)") +end + +local function map_to_num(...) + local t = { ... } + for i, str in ipairs(t) do + if str ~= nil and str ~= '' then + t[i] = tonumber(str) + else + t[i] = 0 + end + end + return table.unpack(t) +end + +return { + module_name = 'gigasecond', + + generate_test = function(case) + local year, month, day, hour, min, sec = map_to_num(components(case.input.moment)) + local e_year, e_month, e_day, e_hour, e_min, e_sec = map_to_num(components(case.expected)) + + local template = [[ + local moment_a = os.time({ year = %d, month = %d, day = %d, hour = %d, min = %d, sec = %d }) + local moment_b = os.time({ year = %d, month = %d, day = %d, hour = %d, min = %d, sec = %d }) + local actual = gigasecond.anniversary(moment_a) + local expected = os.date('!%%x', moment_b) + assert.are.equals(expected, actual)]] + + return template:format(year, month, day, hour, min, sec, e_year, e_month, e_day, e_hour, e_min, e_sec) + end +} diff --git a/exercises/practice/gigasecond/gigasecond_spec.lua b/exercises/practice/gigasecond/gigasecond_spec.lua index d5d12e1..eb33149 100644 --- a/exercises/practice/gigasecond/gigasecond_spec.lua +++ b/exercises/practice/gigasecond/gigasecond_spec.lua @@ -1,27 +1,43 @@ local gigasecond = require('gigasecond') describe('gigasecond', function() - it('test 1', function() - local actual = gigasecond.anniversary(os.time({ year = 2011, month = 3, day = 25, hour = 0, min = 0, sec = 0 })) - local expectedDate = os.date('%x', os.time({ year = 2042, month = 12, day = 1, hour = 0, min = 0, sec = 0 })) - assert.are.equals(expectedDate, actual) + it('date only specification of time', function() + local moment_a = os.time({ year = 2011, month = 4, day = 25, hour = 0, min = 0, sec = 0 }) + local moment_b = os.time({ year = 2043, month = 1, day = 1, hour = 1, min = 46, sec = 40 }) + local actual = gigasecond.anniversary(moment_a) + local expected = os.date('!%x', moment_b) + assert.are.equals(expected, actual) end) - it('test 2', function() - local actual = gigasecond.anniversary(os.time({ year = 1977, month = 5, day = 13, hour = 0, min = 0, sec = 0 })) - local expectedDate = os.date('%x', os.time({ year = 2009, month = 1, day = 19 })) - assert.are.equals(expectedDate, actual) + it('second test for date only specification of time', function() + local moment_a = os.time({ year = 1977, month = 6, day = 13, hour = 0, min = 0, sec = 0 }) + local moment_b = os.time({ year = 2009, month = 2, day = 19, hour = 1, min = 46, sec = 40 }) + local actual = gigasecond.anniversary(moment_a) + local expected = os.date('!%x', moment_b) + assert.are.equals(expected, actual) end) - it('test 3', function() - local actual = gigasecond.anniversary(os.time({ year = 1999, month = 7, day = 19 })) - local expectedDate = os.date('%x', os.time({ year = 2031, month = 3, day = 27 })) - assert.are.equals(expectedDate, actual) + it('third test for date only specification of time', function() + local moment_a = os.time({ year = 1959, month = 7, day = 19, hour = 0, min = 0, sec = 0 }) + local moment_b = os.time({ year = 1991, month = 3, day = 27, hour = 1, min = 46, sec = 40 }) + local actual = gigasecond.anniversary(moment_a) + local expected = os.date('!%x', moment_b) + assert.are.equals(expected, actual) end) - it('test 4', function() - local actual = gigasecond.anniversary(os.time({ year = 1993, month = 8, day = 17 })) - local expectedDate = os.date('%x', os.time({ year = 2025, month = 4, day = 25 })) - assert.are.equals(expectedDate, actual) + it('full time specified', function() + local moment_a = os.time({ year = 2015, month = 1, day = 24, hour = 22, min = 0, sec = 0 }) + local moment_b = os.time({ year = 2046, month = 10, day = 2, hour = 23, min = 46, sec = 40 }) + local actual = gigasecond.anniversary(moment_a) + local expected = os.date('!%x', moment_b) + assert.are.equals(expected, actual) + end) + + it('full time with day roll-over', function() + local moment_a = os.time({ year = 2015, month = 1, day = 24, hour = 23, min = 59, sec = 59 }) + local moment_b = os.time({ year = 2046, month = 10, day = 3, hour = 1, min = 46, sec = 39 }) + local actual = gigasecond.anniversary(moment_a) + local expected = os.date('!%x', moment_b) + assert.are.equals(expected, actual) end) end)