From c73650964142c5ded3118febf1f8a80d8cca2f08 Mon Sep 17 00:00:00 2001 From: BNAndras Date: Fri, 27 Feb 2026 10:43:22 -0800 Subject: [PATCH 1/4] Add spec generator for `gigasecond` --- .../practice/gigasecond/.meta/example.lua | 2 +- .../gigasecond/.meta/spec_generator.lua | 36 ++++++++++++++ .../practice/gigasecond/gigasecond_spec.lua | 48 ++++++++++++------- 3 files changed, 69 insertions(+), 17 deletions(-) create mode 100644 exercises/practice/gigasecond/.meta/spec_generator.lua diff --git a/exercises/practice/gigasecond/.meta/example.lua b/exercises/practice/gigasecond/.meta/example.lua index d0da6489..6eaeb9b7 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 00000000..3836878b --- /dev/null +++ b/exercises/practice/gigasecond/.meta/spec_generator.lua @@ -0,0 +1,36 @@ +local function components(moment) + return moment:match("(%d%d%d%d)%-(%d%d)%-(%d%d)T?(%d*):?(%d*):?(%d*)") +end + +local function to_num(str) + if str ~= nil and str ~= '' then + return tonumber(str) + end + return 0 +end + +return { + module_name = 'gigasecond', + + generate_test = function(case) + local year, month, day, hour, min, sec = components(case.input.moment) + year, month, day = to_num(year), to_num(month), to_num(day) + hour, min, sec = to_num(hour), to_num(min), to_num(sec) + + local e_year, e_month, e_day, e_hour, e_min, e_sec = components(case.expected) + e_year, e_month, e_day = to_num(e_year), to_num(e_month), to_num(e_day) + e_hour, e_min, e_sec = to_num(e_hour), to_num(e_min), to_num(e_sec) + + local template = [[ + local momentA = os.time({ year = %d, month = %d, day = %d, hour = %d, min = %d, sec = %d }) + local momentB = os.time({ year = %d, month = %d, day = %d, hour = %d, min = %d, sec = %d }) + local result = gigasecond.anniversary(momentA) + local expected = os.date('!%%x', momentB) + assert.are.equals(expected, result)]] + + 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 d5d12e10..08f57106 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 momentA = os.time({ year = 2011, month = 4, day = 25, hour = 0, min = 0, sec = 0 }) + local momentB = os.time({ year = 2043, month = 1, day = 1, hour = 1, min = 46, sec = 40 }) + local result = gigasecond.anniversary(momentA) + local expected = os.date('!%x', momentB) + assert.are.equals(expected, result) 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 momentA = os.time({ year = 1977, month = 6, day = 13, hour = 0, min = 0, sec = 0 }) + local momentB = os.time({ year = 2009, month = 2, day = 19, hour = 1, min = 46, sec = 40 }) + local result = gigasecond.anniversary(momentA) + local expected = os.date('!%x', momentB) + assert.are.equals(expected, result) 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 momentA = os.time({ year = 1959, month = 7, day = 19, hour = 0, min = 0, sec = 0 }) + local momentB = os.time({ year = 1991, month = 3, day = 27, hour = 1, min = 46, sec = 40 }) + local result = gigasecond.anniversary(momentA) + local expected = os.date('!%x', momentB) + assert.are.equals(expected, result) 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 momentA = os.time({ year = 2015, month = 1, day = 24, hour = 22, min = 0, sec = 0 }) + local momentB = os.time({ year = 2046, month = 10, day = 2, hour = 23, min = 46, sec = 40 }) + local result = gigasecond.anniversary(momentA) + local expected = os.date('!%x', momentB) + assert.are.equals(expected, result) + end) + + it('full time with day roll-over', function() + local momentA = os.time({ year = 2015, month = 1, day = 24, hour = 23, min = 59, sec = 59 }) + local momentB = os.time({ year = 2046, month = 10, day = 3, hour = 1, min = 46, sec = 39 }) + local result = gigasecond.anniversary(momentA) + local expected = os.date('!%x', momentB) + assert.are.equals(expected, result) end) end) From 35c4044de3a087f41506d9e6dafd76990efc03e9 Mon Sep 17 00:00:00 2001 From: BNAndras Date: Fri, 27 Feb 2026 10:49:10 -0800 Subject: [PATCH 2/4] Minor tweaks to generator --- .../gigasecond/.meta/spec_generator.lua | 26 +++++++++---------- .../practice/gigasecond/gigasecond_spec.lua | 20 +++++++------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/exercises/practice/gigasecond/.meta/spec_generator.lua b/exercises/practice/gigasecond/.meta/spec_generator.lua index 3836878b..59a94f6e 100644 --- a/exercises/practice/gigasecond/.meta/spec_generator.lua +++ b/exercises/practice/gigasecond/.meta/spec_generator.lua @@ -2,31 +2,31 @@ local function components(moment) return moment:match("(%d%d%d%d)%-(%d%d)%-(%d%d)T?(%d*):?(%d*):?(%d*)") end -local function to_num(str) - if str ~= nil and str ~= '' then - return tonumber(str) +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 0 + return table.unpack(t) end return { module_name = 'gigasecond', generate_test = function(case) - local year, month, day, hour, min, sec = components(case.input.moment) - year, month, day = to_num(year), to_num(month), to_num(day) - hour, min, sec = to_num(hour), to_num(min), to_num(sec) - - local e_year, e_month, e_day, e_hour, e_min, e_sec = components(case.expected) - e_year, e_month, e_day = to_num(e_year), to_num(e_month), to_num(e_day) - e_hour, e_min, e_sec = to_num(e_hour), to_num(e_min), to_num(e_sec) + 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 momentA = os.time({ year = %d, month = %d, day = %d, hour = %d, min = %d, sec = %d }) local momentB = os.time({ year = %d, month = %d, day = %d, hour = %d, min = %d, sec = %d }) - local result = gigasecond.anniversary(momentA) + local actual = gigasecond.anniversary(momentA) local expected = os.date('!%%x', momentB) - assert.are.equals(expected, result)]] + assert.are.equals(expected, actual)]] return template:format( year, month, day, hour, min, sec, diff --git a/exercises/practice/gigasecond/gigasecond_spec.lua b/exercises/practice/gigasecond/gigasecond_spec.lua index 08f57106..c4191290 100644 --- a/exercises/practice/gigasecond/gigasecond_spec.lua +++ b/exercises/practice/gigasecond/gigasecond_spec.lua @@ -4,40 +4,40 @@ describe('gigasecond', function() it('date only specification of time', function() local momentA = os.time({ year = 2011, month = 4, day = 25, hour = 0, min = 0, sec = 0 }) local momentB = os.time({ year = 2043, month = 1, day = 1, hour = 1, min = 46, sec = 40 }) - local result = gigasecond.anniversary(momentA) + local actual = gigasecond.anniversary(momentA) local expected = os.date('!%x', momentB) - assert.are.equals(expected, result) + assert.are.equals(expected, actual) end) it('second test for date only specification of time', function() local momentA = os.time({ year = 1977, month = 6, day = 13, hour = 0, min = 0, sec = 0 }) local momentB = os.time({ year = 2009, month = 2, day = 19, hour = 1, min = 46, sec = 40 }) - local result = gigasecond.anniversary(momentA) + local actual = gigasecond.anniversary(momentA) local expected = os.date('!%x', momentB) - assert.are.equals(expected, result) + assert.are.equals(expected, actual) end) it('third test for date only specification of time', function() local momentA = os.time({ year = 1959, month = 7, day = 19, hour = 0, min = 0, sec = 0 }) local momentB = os.time({ year = 1991, month = 3, day = 27, hour = 1, min = 46, sec = 40 }) - local result = gigasecond.anniversary(momentA) + local actual = gigasecond.anniversary(momentA) local expected = os.date('!%x', momentB) - assert.are.equals(expected, result) + assert.are.equals(expected, actual) end) it('full time specified', function() local momentA = os.time({ year = 2015, month = 1, day = 24, hour = 22, min = 0, sec = 0 }) local momentB = os.time({ year = 2046, month = 10, day = 2, hour = 23, min = 46, sec = 40 }) - local result = gigasecond.anniversary(momentA) + local actual = gigasecond.anniversary(momentA) local expected = os.date('!%x', momentB) - assert.are.equals(expected, result) + assert.are.equals(expected, actual) end) it('full time with day roll-over', function() local momentA = os.time({ year = 2015, month = 1, day = 24, hour = 23, min = 59, sec = 59 }) local momentB = os.time({ year = 2046, month = 10, day = 3, hour = 1, min = 46, sec = 39 }) - local result = gigasecond.anniversary(momentA) + local actual = gigasecond.anniversary(momentA) local expected = os.date('!%x', momentB) - assert.are.equals(expected, result) + assert.are.equals(expected, actual) end) end) From e9e9c1d9108f087ccc70107583b896d9de4367cf Mon Sep 17 00:00:00 2001 From: Ryan Hartlage <2488333+ryanplusplus@users.noreply.github.com> Date: Fri, 27 Feb 2026 14:20:36 -0500 Subject: [PATCH 3/4] Format --- exercises/practice/gigasecond/.meta/spec_generator.lua | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/exercises/practice/gigasecond/.meta/spec_generator.lua b/exercises/practice/gigasecond/.meta/spec_generator.lua index 59a94f6e..3f0f9cd9 100644 --- a/exercises/practice/gigasecond/.meta/spec_generator.lua +++ b/exercises/practice/gigasecond/.meta/spec_generator.lua @@ -3,7 +3,7 @@ local function components(moment) end local function map_to_num(...) - local t = {...} + local t = { ... } for i, str in ipairs(t) do if str ~= nil and str ~= '' then t[i] = tonumber(str) @@ -28,9 +28,6 @@ return { local expected = os.date('!%%x', momentB) 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 - ) + return template:format(year, month, day, hour, min, sec, e_year, e_month, e_day, e_hour, e_min, e_sec) end } From d12dd34937e99f44049753f46cf99bf2b8ef2c63 Mon Sep 17 00:00:00 2001 From: Ryan Hartlage <2488333+ryanplusplus@users.noreply.github.com> Date: Sat, 28 Feb 2026 12:35:07 -0500 Subject: [PATCH 4/4] snake_case --- .../gigasecond/.meta/spec_generator.lua | 8 ++-- .../practice/gigasecond/gigasecond_spec.lua | 40 +++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/exercises/practice/gigasecond/.meta/spec_generator.lua b/exercises/practice/gigasecond/.meta/spec_generator.lua index 3f0f9cd9..9f5a6c63 100644 --- a/exercises/practice/gigasecond/.meta/spec_generator.lua +++ b/exercises/practice/gigasecond/.meta/spec_generator.lua @@ -22,10 +22,10 @@ return { local e_year, e_month, e_day, e_hour, e_min, e_sec = map_to_num(components(case.expected)) local template = [[ - local momentA = os.time({ year = %d, month = %d, day = %d, hour = %d, min = %d, sec = %d }) - local momentB = os.time({ year = %d, month = %d, day = %d, hour = %d, min = %d, sec = %d }) - local actual = gigasecond.anniversary(momentA) - local expected = os.date('!%%x', momentB) + 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) diff --git a/exercises/practice/gigasecond/gigasecond_spec.lua b/exercises/practice/gigasecond/gigasecond_spec.lua index c4191290..eb331496 100644 --- a/exercises/practice/gigasecond/gigasecond_spec.lua +++ b/exercises/practice/gigasecond/gigasecond_spec.lua @@ -2,42 +2,42 @@ local gigasecond = require('gigasecond') describe('gigasecond', function() it('date only specification of time', function() - local momentA = os.time({ year = 2011, month = 4, day = 25, hour = 0, min = 0, sec = 0 }) - local momentB = os.time({ year = 2043, month = 1, day = 1, hour = 1, min = 46, sec = 40 }) - local actual = gigasecond.anniversary(momentA) - local expected = os.date('!%x', momentB) + 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('second test for date only specification of time', function() - local momentA = os.time({ year = 1977, month = 6, day = 13, hour = 0, min = 0, sec = 0 }) - local momentB = os.time({ year = 2009, month = 2, day = 19, hour = 1, min = 46, sec = 40 }) - local actual = gigasecond.anniversary(momentA) - local expected = os.date('!%x', momentB) + 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('third test for date only specification of time', function() - local momentA = os.time({ year = 1959, month = 7, day = 19, hour = 0, min = 0, sec = 0 }) - local momentB = os.time({ year = 1991, month = 3, day = 27, hour = 1, min = 46, sec = 40 }) - local actual = gigasecond.anniversary(momentA) - local expected = os.date('!%x', momentB) + 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('full time specified', function() - local momentA = os.time({ year = 2015, month = 1, day = 24, hour = 22, min = 0, sec = 0 }) - local momentB = os.time({ year = 2046, month = 10, day = 2, hour = 23, min = 46, sec = 40 }) - local actual = gigasecond.anniversary(momentA) - local expected = os.date('!%x', momentB) + 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 momentA = os.time({ year = 2015, month = 1, day = 24, hour = 23, min = 59, sec = 59 }) - local momentB = os.time({ year = 2046, month = 10, day = 3, hour = 1, min = 46, sec = 39 }) - local actual = gigasecond.anniversary(momentA) - local expected = os.date('!%x', momentB) + 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)