From d79609ddc922ef12032c738fdb8e602818f244e0 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 14 Aug 2022 16:51:10 +0200 Subject: util.datetime: Fix argument order in tests The expected value goes first. --- spec/util_datetime_spec.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'spec/util_datetime_spec.lua') diff --git a/spec/util_datetime_spec.lua b/spec/util_datetime_spec.lua index 497ab7d3..aff55c7b 100644 --- a/spec/util_datetime_spec.lua +++ b/spec/util_datetime_spec.lua @@ -16,7 +16,7 @@ describe("util.datetime", function () assert.truthy(string.find(date(), "^%d%d%d%d%-%d%d%-%d%d$")); end); it("should work", function () - assert.equals(date(1136239445), "2006-01-02"); + assert.equals("2006-01-02", date(1136239445)); end); end); describe("#time", function () @@ -32,7 +32,7 @@ describe("util.datetime", function () assert.truthy(string.find(time(), "^%d%d:%d%d:%d%d")); end); it("should work", function () - assert.equals(time(1136239445), "22:04:05"); + assert.equals("22:04:05", time(1136239445)); end); end); describe("#datetime", function () @@ -48,7 +48,7 @@ describe("util.datetime", function () assert.truthy(string.find(datetime(), "^%d%d%d%d%-%d%d%-%d%dT%d%d:%d%d:%d%d")); end); it("should work", function () - assert.equals(datetime(1136239445), "2006-01-02T22:04:05Z"); + assert.equals("2006-01-02T22:04:05Z", datetime(1136239445)); end); end); describe("#legacy", function () @@ -64,9 +64,9 @@ describe("util.datetime", function () end); it("should work", function () -- Timestamp used by Go - assert.equals(parse("2017-11-19T17:58:13Z"), 1511114293); - assert.equals(parse("2017-11-19T18:58:50+0100"), 1511114330); - assert.equals(parse("2006-01-02T15:04:05-0700"), 1136239445); + assert.equals(1511114293, parse("2017-11-19T17:58:13Z")); + assert.equals(1511114330, parse("2017-11-19T18:58:50+0100")); + assert.equals(1136239445, parse("2006-01-02T15:04:05-0700")); end); it("should handle timezones", function () -- https://xmpp.org/extensions/xep-0082.html#example-2 and 3 -- cgit v1.2.3 From c85c18b03ab627ee81273d4c0f0009e1a5cc2d03 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 14 Aug 2022 16:57:31 +0200 Subject: util.datetime: Add support for sub-second precision timestamps Lua since 5.3 raises a fuss when time functions are handed a number with a fractional part and the underlying C functions are all based on integer seconds without support for more precision. --- spec/util_datetime_spec.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'spec/util_datetime_spec.lua') diff --git a/spec/util_datetime_spec.lua b/spec/util_datetime_spec.lua index aff55c7b..a35a1037 100644 --- a/spec/util_datetime_spec.lua +++ b/spec/util_datetime_spec.lua @@ -18,6 +18,9 @@ describe("util.datetime", function () it("should work", function () assert.equals("2006-01-02", date(1136239445)); end); + it("should ignore fractional parts", function () + assert.equals("2006-01-02", date(1136239445.5)); + end); end); describe("#time", function () local time = util_datetime.time; @@ -34,6 +37,9 @@ describe("util.datetime", function () it("should work", function () assert.equals("22:04:05", time(1136239445)); end); + it("should handle precision", function () + assert.equal("14:46:32.158200", time(1660488392.1582)) + end) end); describe("#datetime", function () local datetime = util_datetime.datetime; @@ -50,6 +56,9 @@ describe("util.datetime", function () it("should work", function () assert.equals("2006-01-02T22:04:05Z", datetime(1136239445)); end); + it("should handle precision", function () + assert.equal("2022-08-14T14:46:32.158200Z", datetime(1660488392.1582)) + end) end); describe("#legacy", function () local legacy = util_datetime.legacy; @@ -72,5 +81,9 @@ describe("util.datetime", function () -- https://xmpp.org/extensions/xep-0082.html#example-2 and 3 assert.equals(parse("1969-07-21T02:56:15Z"), parse("1969-07-20T21:56:15-05:00")); end); + it("should handle precision", function () + -- floating point comparison is not an exact science + assert.truthy(math.abs(1660488392.1582 - parse("2022-08-14T14:46:32.158200Z")) < 0.001) + end) end); end); -- cgit v1.2.3 From f8f90ea923315383477b936e1ec109ec30eb2dd3 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sat, 8 Oct 2022 23:55:22 +0100 Subject: util.datetime: Add some missing test cases You guessed it, mutation testing. --- spec/util_datetime_spec.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'spec/util_datetime_spec.lua') diff --git a/spec/util_datetime_spec.lua b/spec/util_datetime_spec.lua index a35a1037..960e8aef 100644 --- a/spec/util_datetime_spec.lua +++ b/spec/util_datetime_spec.lua @@ -38,7 +38,10 @@ describe("util.datetime", function () assert.equals("22:04:05", time(1136239445)); end); it("should handle precision", function () + assert.equal("14:46:31.158200", time(1660488391.1582)) assert.equal("14:46:32.158200", time(1660488392.1582)) + assert.equal("14:46:33.158200", time(1660488393.1582)) + assert.equal("14:46:33.999900", time(1660488393.9999)) end) end); describe("#datetime", function () @@ -57,7 +60,10 @@ describe("util.datetime", function () assert.equals("2006-01-02T22:04:05Z", datetime(1136239445)); end); it("should handle precision", function () + assert.equal("2022-08-14T14:46:31.158200Z", datetime(1660488391.1582)) assert.equal("2022-08-14T14:46:32.158200Z", datetime(1660488392.1582)) + assert.equal("2022-08-14T14:46:33.158200Z", datetime(1660488393.1582)) + assert.equal("2022-08-14T14:46:33.999900Z", datetime(1660488393.9999)) end) end); describe("#legacy", function () @@ -65,6 +71,9 @@ describe("util.datetime", function () it("should exist", function () assert.is_function(legacy); end); + it("should not add precision", function () + assert.equal("20220814T14:46:31", legacy(1660488391.1582)); + end); end); describe("#parse", function () local parse = util_datetime.parse; @@ -76,6 +85,7 @@ describe("util.datetime", function () assert.equals(1511114293, parse("2017-11-19T17:58:13Z")); assert.equals(1511114330, parse("2017-11-19T18:58:50+0100")); assert.equals(1136239445, parse("2006-01-02T15:04:05-0700")); + assert.equals(1136239445, parse("2006-01-02T15:04:05-07")); end); it("should handle timezones", function () -- https://xmpp.org/extensions/xep-0082.html#example-2 and 3 @@ -85,5 +95,10 @@ describe("util.datetime", function () -- floating point comparison is not an exact science assert.truthy(math.abs(1660488392.1582 - parse("2022-08-14T14:46:32.158200Z")) < 0.001) end) + it("should return nil when given invalid inputs", function () + assert.is_nil(parse(nil)); + assert.is_nil(parse("hello world")); + assert.is_nil(parse("2017-11-19T18:58:50$0100")); + end); end); end); -- cgit v1.2.3