Skip to content

Commit

Permalink
test: Port Date format tests to Busted
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque authored and Tieske committed Oct 9, 2020
1 parent 943a700 commit 9ef4b2f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 37 deletions.
47 changes: 42 additions & 5 deletions spec/date_spec.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,48 @@
local Date = require("pl.Date")

describe("pl.Date:__tostring()", function ()
describe("pl.Date", function ()

describe("function", function ()

describe("Format()", function ()

it("should output parsable inputs", function ()
local function assert_date_format(expected, format)
local df = Date.Format(format)
local d = df:parse(expected)
assert.is.equal(expected, df:tostring(d))
end
assert_date_format('02/04/10', 'dd/mm/yy')
assert_date_format('04/02/2010', 'mm/dd/yyyy')
assert_date_format('2011-02-20', 'yyyy-mm-dd')
assert_date_format('20070320', 'yyyymmdd')
assert_date_format('23:10', 'HH:MM')
end)

it("should parse 'slack' fields", function ()
local df = Date.Format("m/d/yy")
-- TODO: Re-enable when issue #359 fixed
-- assert.is.equal('01/05/99', df:tostring(df:parse('1/5/99')))
assert.is.equal('01/05/01', df:tostring(df:parse('1/5/01')))
assert.is.equal('01/05/32', df:tostring(df:parse('1/5/32')))
end)

end)

end)

describe("meta method", function ()

describe("__tostring()", function ()

it("should be suitable for serialization", function ()
local df = Date.Format()
local du = df:parse("2008-07-05")
assert.is.equal(du, du:toUTC())
end)

end)

it("should be suitable for serialization", function ()
local df = Date.Format()
local du = df:parse("2008-07-05")
assert.is.equal(du, du:toUTC())
end)

end)
32 changes: 0 additions & 32 deletions tests/test-date.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,6 @@ local T = require 'pl.test'.tuple

local Date = require 'pl.Date'

--[[
d = Date()
print(d)
print(d:year())
d:day(20)
print(d)
d:add {day = 2}
print(d:day())
d = Date() -- 'now'
print(d:last_day():day())
print(d:month(7):last_day())
--]]

function check_df(fmt,str,no_check)
local df = Date.Format(fmt)
local d = df:parse(str)
--print(str,d)
if not no_check then
asserteq(df:tostring(d),str)
end
end

check_df('dd/mm/yy','02/04/10')
check_df('mm/dd/yyyy','04/02/2010')
check_df('yyyy-mm-dd','2011-02-20')
check_df('yyyymmdd','20070320')

-- use single fields for 'slack' parsing
check_df('m/d/yyyy','1/5/2001',true)

check_df('HH:MM','23:10')

iso = Date.Format 'yyyy-mm-dd' -- ISO date
d = iso:parse '2010-04-10'
asserteq(T(d:day(),d:month(),d:year()),T(10,4,2010))
Expand Down

0 comments on commit 9ef4b2f

Please sign in to comment.