util.datetime: Add some missing test cases
authorMatthew Wild <mwild1@gmail.com>
Sat, 08 Oct 2022 23:55:22 +0100
changeset 12759 a09dacf660d2
parent 12758 a92ca737d05f
child 12765 82915c755d90
util.datetime: Add some missing test cases You guessed it, mutation testing.
spec/util_datetime_spec.lua
--- a/spec/util_datetime_spec.lua	Sat Oct 08 20:39:14 2022 +0100
+++ b/spec/util_datetime_spec.lua	Sat Oct 08 23:55:22 2022 +0100
@@ -38,7 +38,10 @@
 			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 @@
 			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 @@
 		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 @@
 			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 @@
 			-- 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);