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(+) 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