Annotate

spec/util_datetime_spec.lua @ 10459:7456eaa83b15

mod_s2s: Prevent unhandled stanza handler from complaining about stream features on aborted connections I have no idea why I wrote return false in e5945fb5b71f
author Kim Alvefur <zash@zash.se>
date Fri, 29 Nov 2019 18:15:23 +0100
parent 8392:ff8e122526f4
child 12628:b95da9a593be
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8392
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local util_datetime = require "util.datetime";
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 describe("util.datetime", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 it("should have been loaded", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 assert.is_table(util_datetime);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 describe("#date", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local date = util_datetime.date;
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 it("should exist", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 assert.is_function(date);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 it("should return a string", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 assert.is_string(date());
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 it("should look like a date", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 assert.truthy(string.find(date(), "^%d%d%d%d%-%d%d%-%d%d$"));
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 it("should work", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 assert.equals(date(1136239445), "2006-01-02");
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 describe("#time", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 local time = util_datetime.time;
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 it("should exist", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 assert.is_function(time);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 it("should return a string", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 assert.is_string(time());
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 it("should look like a timestamp", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 -- Note: Sub-second precision and timezones are ignored
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 assert.truthy(string.find(time(), "^%d%d:%d%d:%d%d"));
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 it("should work", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 assert.equals(time(1136239445), "22:04:05");
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 describe("#datetime", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 local datetime = util_datetime.datetime;
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 it("should exist", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 assert.is_function(datetime);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 it("should return a string", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 assert.is_string(datetime());
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 it("should look like a timestamp", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 -- Note: Sub-second precision and timezones are ignored
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 assert.truthy(string.find(datetime(), "^%d%d%d%d%-%d%d%-%d%dT%d%d:%d%d:%d%d"));
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 it("should work", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 assert.equals(datetime(1136239445), "2006-01-02T22:04:05Z");
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 describe("#legacy", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 local legacy = util_datetime.legacy;
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 it("should exist", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 assert.is_function(legacy);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 describe("#parse", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 local parse = util_datetime.parse;
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 it("should exist", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 assert.is_function(parse);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 it("should work", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 -- Timestamp used by Go
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 assert.equals(parse("2017-11-19T17:58:13Z"), 1511114293);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 assert.equals(parse("2017-11-19T18:58:50+0100"), 1511114330);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 assert.equals(parse("2006-01-02T15:04:05-0700"), 1136239445);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 it("should handle timezones", function ()
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 -- https://xmpp.org/extensions/xep-0082.html#example-2 and 3
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 assert.equals(parse("1969-07-21T02:56:15Z"), parse("1969-07-20T21:56:15-05:00"));
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 end);
ff8e122526f4 util.datetime: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 end);