From a2d59f1d77f74aadf431b5cc89ca0ea0de678a99 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Fri, 2 Oct 2020 00:36:29 +0300 Subject: [PATCH] test: Port Date format tests to Busted --- spec/date_spec.lua | 47 ++++++++++++++++++++++++++++++++++++++++----- tests/test-date.lua | 32 ------------------------------ 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/spec/date_spec.lua b/spec/date_spec.lua index 0a027449..1032de25 100644 --- a/spec/date_spec.lua +++ b/spec/date_spec.lua @@ -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) diff --git a/tests/test-date.lua b/tests/test-date.lua index 31d11125..6a91a098 100644 --- a/tests/test-date.lua +++ b/tests/test-date.lua @@ -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))