-
-
Notifications
You must be signed in to change notification settings - Fork 60
Add spec generator for gigasecond
#634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had no idea this was a thing. For anyone else that is unfamiliar, this tells it to use UTC instead of local time.