Software /
code /
prosody
Annotate
spec/util_dbuffer_spec.lua @ 12402:8deb401a21f6
util.table: Backport table.move() from Lua 5.4
One difference is that 5.4 accepts "table-like" values (for this and other
table.*() functions), but that would require additional backporting work.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 18 Mar 2022 15:21:25 +0000 |
parent | 11637:19cddf92fcc2 |
child | 12764:bf6d2f9fad4d |
rev | line source |
---|---|
11105 | 1 local dbuffer = require "util.dbuffer"; |
2 describe("util.dbuffer", function () | |
3 describe("#new", function () | |
4 it("has a constructor", function () | |
5 assert.Function(dbuffer.new); | |
6 end); | |
7 it("can be created", function () | |
8 assert.truthy(dbuffer.new()); | |
9 end); | |
10 it("won't create an empty buffer", function () | |
11 assert.falsy(dbuffer.new(0)); | |
12 end); | |
13 it("won't create a negatively sized buffer", function () | |
14 assert.falsy(dbuffer.new(-1)); | |
15 end); | |
16 end); | |
17 describe(":write", function () | |
18 local b = dbuffer.new(); | |
19 it("works", function () | |
20 assert.truthy(b:write("hi")); | |
21 end); | |
22 end); | |
23 | |
24 describe(":read", function () | |
25 it("supports optional bytes parameter", function () | |
26 -- should return the frontmost chunk | |
27 local b = dbuffer.new(); | |
28 assert.truthy(b:write("hello")); | |
29 assert.truthy(b:write(" ")); | |
30 assert.truthy(b:write("world")); | |
31 assert.equal("h", b:read(1)); | |
32 | |
33 assert.equal("ello", b:read()); | |
34 assert.equal(" ", b:read()); | |
35 assert.equal("world", b:read()); | |
36 end); | |
37 end); | |
38 | |
11636
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
39 describe(":read_until", function () |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
40 it("works", function () |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
41 local b = dbuffer.new(); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
42 b:write("hello\n"); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
43 b:write("world"); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
44 b:write("\n"); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
45 b:write("\n\n"); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
46 b:write("stuff"); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
47 b:write("more\nand more"); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
48 |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
49 assert.equal(nil, b:read_until(".")); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
50 assert.equal(nil, b:read_until("%")); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
51 assert.equal("hello\n", b:read_until("\n")); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
52 assert.equal("world\n", b:read_until("\n")); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
53 assert.equal("\n", b:read_until("\n")); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
54 assert.equal("\n", b:read_until("\n")); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
55 assert.equal("stu", b:read(3)); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
56 assert.equal("ffmore\n", b:read_until("\n")); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
57 assert.equal(nil, b:read_until("\n")); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
58 assert.equal("and more", b:read_chunk()); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
59 end); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
60 end); |
11e0a0a08da3
util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents:
11158
diff
changeset
|
61 |
11105 | 62 describe(":discard", function () |
63 local b = dbuffer.new(); | |
64 it("works", function () | |
65 assert.truthy(b:write("hello world")); | |
66 assert.truthy(b:discard(6)); | |
67 assert.equal(5, b:length()); | |
11156
a8ef69f7fc35
util.dbuffer: Expose length as :len() method, like strings
Kim Alvefur <zash@zash.se>
parents:
11105
diff
changeset
|
68 assert.equal(5, b:len()); |
11105 | 69 assert.equal("world", b:read(5)); |
70 end); | |
71 end); | |
72 | |
73 describe(":collapse()", function () | |
74 it("works on an empty buffer", function () | |
75 local b = dbuffer.new(); | |
76 b:collapse(); | |
77 end); | |
78 end); | |
79 | |
80 describe(":sub", function () | |
81 -- Helper function to compare buffer:sub() with string:sub() | |
82 local s = "hello world"; | |
83 local function test_sub(b, x, y) | |
84 local string_result, buffer_result = s:sub(x, y), b:sub(x, y); | |
85 assert.equals(string_result, buffer_result, ("buffer:sub(%d, %s) does not match string:sub()"):format(x, y and ("%d"):format(y) or "nil")); | |
86 end | |
87 | |
88 it("works", function () | |
89 local b = dbuffer.new(); | |
90 assert.truthy(b:write("hello world")); | |
91 assert.equals("hello", b:sub(1, 5)); | |
92 end); | |
93 | |
94 it("works after discard", function () | |
95 local b = dbuffer.new(256); | |
96 assert.truthy(b:write("foobar")); | |
97 assert.equals("foobar", b:sub(1, 6)); | |
98 assert.truthy(b:discard(3)); -- consume "foo" | |
99 assert.equals("bar", b:sub(1, 3)); | |
100 end); | |
101 | |
102 it("supports optional end parameter", function () | |
103 local b = dbuffer.new(); | |
104 assert.truthy(b:write("hello world")); | |
105 assert.equals("hello world", b:sub(1)); | |
106 assert.equals("world", b:sub(-5)); | |
107 end); | |
108 | |
109 it("is equivalent to string:sub", function () | |
110 local b = dbuffer.new(11); | |
111 assert.truthy(b:write(s)); | |
112 for i = -13, 13 do | |
113 for j = -13, 13 do | |
114 test_sub(b, i, j); | |
115 end | |
116 end | |
117 end); | |
118 end); | |
119 | |
120 describe(":byte", function () | |
121 -- Helper function to compare buffer:byte() with string:byte() | |
122 local s = "hello world" | |
123 local function test_byte(b, x, y) | |
124 local string_result, buffer_result = {s:byte(x, y)}, {b:byte(x, y)}; | |
125 assert.same(string_result, buffer_result, ("buffer:byte(%d, %s) does not match string:byte()"):format(x, y and ("%d"):format(y) or "nil")); | |
126 end | |
127 | |
128 it("is equivalent to string:byte", function () | |
129 local b = dbuffer.new(11); | |
130 assert.truthy(b:write(s)); | |
131 test_byte(b, 1); | |
132 test_byte(b, 3); | |
133 test_byte(b, -1); | |
134 test_byte(b, -3); | |
135 for i = -13, 13 do | |
136 for j = -13, 13 do | |
137 test_byte(b, i, j); | |
138 end | |
139 end | |
140 end); | |
141 | |
142 it("works with characters > 127", function () | |
143 local b = dbuffer.new(); | |
144 b:write(string.char(0, 140)); | |
145 local r = { b:byte(1, 2) }; | |
146 assert.same({ 0, 140 }, r); | |
147 end); | |
148 | |
149 it("works on an empty buffer", function () | |
150 local b = dbuffer.new(); | |
151 assert.equal("", b:sub(1,1)); | |
152 end); | |
153 end); | |
154 end); |