Annotate

spec/util_dbuffer_spec.lua @ 11055:5fb95410f89c

util.jid: Add test coverage for XEP-0106: JID Escaping functions
author Kim Alvefur <zash@zash.se>
date Fri, 28 Aug 2020 18:43:37 +0200
parent 11046:64713f21ff0e
child 11114:6a608ecb3471
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10973
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local dbuffer = require "util.dbuffer";
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 describe("util.dbuffer", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 describe("#new", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 it("has a constructor", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 assert.Function(dbuffer.new);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 it("can be created", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 assert.truthy(dbuffer.new());
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 it("won't create an empty buffer", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 assert.falsy(dbuffer.new(0));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 it("won't create a negatively sized buffer", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 assert.falsy(dbuffer.new(-1));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 describe(":write", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local b = dbuffer.new();
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 it("works", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 assert.truthy(b:write("hi"));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
10980
eaee72c7afbd util.dbuffer: If no bytes parameter passed to read, return remainder of frontmost chunk
Matthew Wild <mwild1@gmail.com>
parents: 10977
diff changeset
24 describe(":read", function ()
eaee72c7afbd util.dbuffer: If no bytes parameter passed to read, return remainder of frontmost chunk
Matthew Wild <mwild1@gmail.com>
parents: 10977
diff changeset
25 it("supports optional bytes parameter", function ()
eaee72c7afbd util.dbuffer: If no bytes parameter passed to read, return remainder of frontmost chunk
Matthew Wild <mwild1@gmail.com>
parents: 10977
diff changeset
26 -- should return the frontmost chunk
eaee72c7afbd util.dbuffer: If no bytes parameter passed to read, return remainder of frontmost chunk
Matthew Wild <mwild1@gmail.com>
parents: 10977
diff changeset
27 local b = dbuffer.new();
eaee72c7afbd util.dbuffer: If no bytes parameter passed to read, return remainder of frontmost chunk
Matthew Wild <mwild1@gmail.com>
parents: 10977
diff changeset
28 assert.truthy(b:write("hello"));
eaee72c7afbd util.dbuffer: If no bytes parameter passed to read, return remainder of frontmost chunk
Matthew Wild <mwild1@gmail.com>
parents: 10977
diff changeset
29 assert.truthy(b:write(" "));
eaee72c7afbd util.dbuffer: If no bytes parameter passed to read, return remainder of frontmost chunk
Matthew Wild <mwild1@gmail.com>
parents: 10977
diff changeset
30 assert.truthy(b:write("world"));
eaee72c7afbd util.dbuffer: If no bytes parameter passed to read, return remainder of frontmost chunk
Matthew Wild <mwild1@gmail.com>
parents: 10977
diff changeset
31 assert.equal("h", b:read(1));
eaee72c7afbd util.dbuffer: If no bytes parameter passed to read, return remainder of frontmost chunk
Matthew Wild <mwild1@gmail.com>
parents: 10977
diff changeset
32
eaee72c7afbd util.dbuffer: If no bytes parameter passed to read, return remainder of frontmost chunk
Matthew Wild <mwild1@gmail.com>
parents: 10977
diff changeset
33 assert.equal("ello", b:read());
eaee72c7afbd util.dbuffer: If no bytes parameter passed to read, return remainder of frontmost chunk
Matthew Wild <mwild1@gmail.com>
parents: 10977
diff changeset
34 assert.equal(" ", b:read());
eaee72c7afbd util.dbuffer: If no bytes parameter passed to read, return remainder of frontmost chunk
Matthew Wild <mwild1@gmail.com>
parents: 10977
diff changeset
35 assert.equal("world", b:read());
eaee72c7afbd util.dbuffer: If no bytes parameter passed to read, return remainder of frontmost chunk
Matthew Wild <mwild1@gmail.com>
parents: 10977
diff changeset
36 end);
eaee72c7afbd util.dbuffer: If no bytes parameter passed to read, return remainder of frontmost chunk
Matthew Wild <mwild1@gmail.com>
parents: 10977
diff changeset
37 end);
eaee72c7afbd util.dbuffer: If no bytes parameter passed to read, return remainder of frontmost chunk
Matthew Wild <mwild1@gmail.com>
parents: 10977
diff changeset
38
10973
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 describe(":discard", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 local b = dbuffer.new();
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 it("works", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 assert.truthy(b:write("hello world"));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 assert.truthy(b:discard(6));
10977
29b9b679bdbc util.dbuffer: Don't use # operator in tests, Lua 5.1 doesn't support __len
Matthew Wild <mwild1@gmail.com>
parents: 10973
diff changeset
44 assert.equal(5, b:length());
10973
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 assert.equal("world", b:read(5));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
11028
d7a403060946 util.dbuffer: Fix traceback when :collapse() is called on empty buffer
Matthew Wild <mwild1@gmail.com>
parents: 10980
diff changeset
49 describe(":collapse()", function ()
d7a403060946 util.dbuffer: Fix traceback when :collapse() is called on empty buffer
Matthew Wild <mwild1@gmail.com>
parents: 10980
diff changeset
50 it("works on an empty buffer", function ()
d7a403060946 util.dbuffer: Fix traceback when :collapse() is called on empty buffer
Matthew Wild <mwild1@gmail.com>
parents: 10980
diff changeset
51 local b = dbuffer.new();
d7a403060946 util.dbuffer: Fix traceback when :collapse() is called on empty buffer
Matthew Wild <mwild1@gmail.com>
parents: 10980
diff changeset
52 b:collapse();
d7a403060946 util.dbuffer: Fix traceback when :collapse() is called on empty buffer
Matthew Wild <mwild1@gmail.com>
parents: 10980
diff changeset
53 end);
d7a403060946 util.dbuffer: Fix traceback when :collapse() is called on empty buffer
Matthew Wild <mwild1@gmail.com>
parents: 10980
diff changeset
54 end);
d7a403060946 util.dbuffer: Fix traceback when :collapse() is called on empty buffer
Matthew Wild <mwild1@gmail.com>
parents: 10980
diff changeset
55
10973
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 describe(":sub", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 -- Helper function to compare buffer:sub() with string:sub()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 local s = "hello world";
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 local function test_sub(b, x, y)
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 local string_result, buffer_result = s:sub(x, y), b:sub(x, y);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 assert.equals(string_result, buffer_result, ("buffer:sub(%d, %s) does not match string:sub()"):format(x, y and ("%d"):format(y) or "nil"));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 it("works", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 local b = dbuffer.new();
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 assert.truthy(b:write("hello world"));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 assert.equals("hello", b:sub(1, 5));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69
11045
8c6bace13229 util.dbuffer: Fix :sub() not working with partially-consumed chunks (thanks Zash for test case)
Matthew Wild <mwild1@gmail.com>
parents: 11044
diff changeset
70 it("works after discard", function ()
11044
7b720a815519 util.dbuffer: Add failing test case involving :sub after :discard
Kim Alvefur <zash@zash.se>
parents: 11028
diff changeset
71 local b = dbuffer.new(256);
11046
64713f21ff0e util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents: 11045
diff changeset
72 assert.truthy(b:write("foobar"));
11044
7b720a815519 util.dbuffer: Add failing test case involving :sub after :discard
Kim Alvefur <zash@zash.se>
parents: 11028
diff changeset
73 assert.equals("foobar", b:sub(1, 6));
11046
64713f21ff0e util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents: 11045
diff changeset
74 assert.truthy(b:discard(3)); -- consume "foo"
11044
7b720a815519 util.dbuffer: Add failing test case involving :sub after :discard
Kim Alvefur <zash@zash.se>
parents: 11028
diff changeset
75 assert.equals("bar", b:sub(1, 3));
7b720a815519 util.dbuffer: Add failing test case involving :sub after :discard
Kim Alvefur <zash@zash.se>
parents: 11028
diff changeset
76 end);
7b720a815519 util.dbuffer: Add failing test case involving :sub after :discard
Kim Alvefur <zash@zash.se>
parents: 11028
diff changeset
77
10973
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 it("supports optional end parameter", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 local b = dbuffer.new();
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 assert.truthy(b:write("hello world"));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 assert.equals("hello world", b:sub(1));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 assert.equals("world", b:sub(-5));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 it("is equivalent to string:sub", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 local b = dbuffer.new(11);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 assert.truthy(b:write(s));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 for i = -13, 13 do
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 for j = -13, 13 do
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 test_sub(b, i, j);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 end
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 end
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 describe(":byte", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 -- Helper function to compare buffer:byte() with string:byte()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 local s = "hello world"
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 local function test_byte(b, x, y)
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 local string_result, buffer_result = {s:byte(x, y)}, {b:byte(x, y)};
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 assert.same(string_result, buffer_result, ("buffer:byte(%d, %s) does not match string:byte()"):format(x, y and ("%d"):format(y) or "nil"));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 end
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 it("is equivalent to string:byte", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 local b = dbuffer.new(11);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 assert.truthy(b:write(s));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 test_byte(b, 1);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 test_byte(b, 3);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 test_byte(b, -1);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 test_byte(b, -3);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 for i = -13, 13 do
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 for j = -13, 13 do
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 test_byte(b, i, j);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 end
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 end
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 it("works with characters > 127", function ()
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 local b = dbuffer.new();
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 b:write(string.char(0, 140));
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 local r = { b:byte(1, 2) };
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 assert.same({ 0, 140 }, r);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 end);
11028
d7a403060946 util.dbuffer: Fix traceback when :collapse() is called on empty buffer
Matthew Wild <mwild1@gmail.com>
parents: 10980
diff changeset
124
d7a403060946 util.dbuffer: Fix traceback when :collapse() is called on empty buffer
Matthew Wild <mwild1@gmail.com>
parents: 10980
diff changeset
125 it("works on an empty buffer", function ()
d7a403060946 util.dbuffer: Fix traceback when :collapse() is called on empty buffer
Matthew Wild <mwild1@gmail.com>
parents: 10980
diff changeset
126 local b = dbuffer.new();
d7a403060946 util.dbuffer: Fix traceback when :collapse() is called on empty buffer
Matthew Wild <mwild1@gmail.com>
parents: 10980
diff changeset
127 assert.equal("", b:sub(1,1));
d7a403060946 util.dbuffer: Fix traceback when :collapse() is called on empty buffer
Matthew Wild <mwild1@gmail.com>
parents: 10980
diff changeset
128 end);
10973
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 end);
39991e40d1dc util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 end);