Annotate

spec/util_rsm_spec.lua @ 12446:e54b8a5e35ad 0.12

util.random: Test whether util.crand works before using it (fix #1734) util.crand can be configured at compile time to use the Linux getrandom() system call, available from Linux 3.17, but it is still possible to load it with an older kernel lacking that system call, where attempting to use it throws an ENOSYS error. By testing for this on load we can fall back to /dev/urandom in this case.
author Kim Alvefur <zash@zash.se>
date Sat, 02 Apr 2022 16:33:27 +0200
parent 11427:83f5499d1f10
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10760
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local rsm = require "util.rsm";
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local xml = require "util.xml";
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local function strip(s)
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 return (s:gsub(">%s+<", "><"));
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 end
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 describe("util.rsm", function ()
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 describe("parse", function ()
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 it("works", function ()
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local test = xml.parse(strip([[
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 <set xmlns='http://jabber.org/protocol/rsm'>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 <max>10</max>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 </set>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 ]]));
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 assert.same({ max = 10 }, rsm.parse(test));
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 end);
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 it("works", function ()
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 local test = xml.parse(strip([[
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 <set xmlns='http://jabber.org/protocol/rsm'>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 <first index='0'>saint@example.org</first>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 <last>peterpan@neverland.lit</last>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 <count>800</count>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 </set>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 ]]));
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 assert.same({ first = { index = 0, "saint@example.org" }, last = "peterpan@neverland.lit", count = 800 }, rsm.parse(test));
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 end);
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 it("works", function ()
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 local test = xml.parse(strip([[
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 <set xmlns='http://jabber.org/protocol/rsm'>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 <max>10</max>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 <before>peter@pixyland.org</before>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 </set>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 ]]));
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 assert.same({ max = 10, before = "peter@pixyland.org" }, rsm.parse(test));
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 end);
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39
11427
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
40 it("all fields works", function()
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
41 local test = assert(xml.parse(strip([[
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
42 <set xmlns='http://jabber.org/protocol/rsm'>
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
43 <after>a</after>
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
44 <before>b</before>
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
45 <count>10</count>
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
46 <first index='1'>f</first>
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
47 <index>5</index>
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
48 <last>z</last>
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
49 <max>100</max>
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
50 </set>
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
51 ]])));
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
52 assert.same({
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
53 after = "a";
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
54 before = "b";
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
55 count = 10;
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
56 first = {index = 1; "f"};
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
57 index = 5;
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
58 last = "z";
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
59 max = 100;
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
60 }, rsm.parse(test));
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
61 end);
10760
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 end);
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 describe("generate", function ()
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 it("works", function ()
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 local test = xml.parse(strip([[
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 <set xmlns='http://jabber.org/protocol/rsm'>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 <max>10</max>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 </set>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 ]]));
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 local res = rsm.generate({ max = 10 });
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 assert.same(test:get_child_text("max"), res:get_child_text("max"));
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 end);
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 it("works", function ()
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 local test = xml.parse(strip([[
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 <set xmlns='http://jabber.org/protocol/rsm'>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 <first index='0'>saint@example.org</first>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 <last>peterpan@neverland.lit</last>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 <count>800</count>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 </set>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 ]]));
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 local res = rsm.generate({ first = { index = 0, "saint@example.org" }, last = "peterpan@neverland.lit", count = 800 });
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 assert.same(test:get_child("first").attr.index, res:get_child("first").attr.index);
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 assert.same(test:get_child_text("first"), res:get_child_text("first"));
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 assert.same(test:get_child_text("last"), res:get_child_text("last"));
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 assert.same(test:get_child_text("count"), res:get_child_text("count"));
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 end);
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 it("works", function ()
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 local test = xml.parse(strip([[
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 <set xmlns='http://jabber.org/protocol/rsm'>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 <max>10</max>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 <before>peter@pixyland.org</before>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 </set>
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 ]]));
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 local res = rsm.generate({ max = 10, before = "peter@pixyland.org" });
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 assert.same(test:get_child_text("max"), res:get_child_text("max"));
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 assert.same(test:get_child_text("before"), res:get_child_text("before"));
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 end);
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101
10762
4fc224c97986 util.rsm: Test that Lua 5.3 floats are not encoded with decimal point
Kim Alvefur <zash@zash.se>
parents: 10760
diff changeset
102 it("handles floats", function ()
4fc224c97986 util.rsm: Test that Lua 5.3 floats are not encoded with decimal point
Kim Alvefur <zash@zash.se>
parents: 10760
diff changeset
103 local r1 = rsm.generate({ max = 10.0, count = 100.0, first = { index = 1.0, "foo" } });
4fc224c97986 util.rsm: Test that Lua 5.3 floats are not encoded with decimal point
Kim Alvefur <zash@zash.se>
parents: 10760
diff changeset
104 assert.equal("10", r1:get_child_text("max"));
4fc224c97986 util.rsm: Test that Lua 5.3 floats are not encoded with decimal point
Kim Alvefur <zash@zash.se>
parents: 10760
diff changeset
105 assert.equal("100", r1:get_child_text("count"));
4fc224c97986 util.rsm: Test that Lua 5.3 floats are not encoded with decimal point
Kim Alvefur <zash@zash.se>
parents: 10760
diff changeset
106 assert.equal("1", r1:get_child("first").attr.index);
4fc224c97986 util.rsm: Test that Lua 5.3 floats are not encoded with decimal point
Kim Alvefur <zash@zash.se>
parents: 10760
diff changeset
107 end);
4fc224c97986 util.rsm: Test that Lua 5.3 floats are not encoded with decimal point
Kim Alvefur <zash@zash.se>
parents: 10760
diff changeset
108
11427
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
109
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
110 it("all fields works", function ()
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
111 local res = rsm.generate({
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
112 after = "a";
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
113 before = "b";
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
114 count = 10;
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
115 first = {index = 1; "f"};
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
116 index = 5;
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
117 last = "z";
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
118 max = 100;
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
119 });
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
120 assert.equal("a", res:get_child_text("after"));
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
121 assert.equal("b", res:get_child_text("before"));
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
122 assert.equal("10", res:get_child_text("count"));
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
123 assert.equal("f", res:get_child_text("first"));
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
124 assert.equal("1", res:get_child("first").attr.index);
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
125 assert.equal("5", res:get_child_text("index"));
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
126 assert.equal("z", res:get_child_text("last"));
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
127 assert.equal("100", res:get_child_text("max"));
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
128 end);
10760
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 end);
11427
83f5499d1f10 util.rsm: Increase test coverage
Kim Alvefur <zash@zash.se>
parents: 10762
diff changeset
130
10760
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 end);
c9d1d9c80e1e util.rsm: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132