Software /
code /
prosody
Annotate
spec/util_dbuffer_spec.lua @ 11490:34d4e4a01ef8
util.prosodyctl: Install plugins for current Lua version
Fixes that luarocks defaults to installing the rock for its own runtime
version of Lua.
This only works with luarocks 3.x, it does nothing on 2.x as currently
available from Debian.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 16 Mar 2021 02:44:22 +0100 |
parent | 11158:3a72cb126d6c |
child | 11636:11e0a0a08da3 |
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 | |
39 describe(":discard", function () | |
40 local b = dbuffer.new(); | |
41 it("works", function () | |
42 assert.truthy(b:write("hello world")); | |
43 assert.truthy(b:discard(6)); | |
44 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
|
45 assert.equal(5, b:len()); |
11105 | 46 assert.equal("world", b:read(5)); |
47 end); | |
48 end); | |
49 | |
50 describe(":collapse()", function () | |
51 it("works on an empty buffer", function () | |
52 local b = dbuffer.new(); | |
53 b:collapse(); | |
54 end); | |
55 end); | |
56 | |
57 describe(":sub", function () | |
58 -- Helper function to compare buffer:sub() with string:sub() | |
59 local s = "hello world"; | |
60 local function test_sub(b, x, y) | |
61 local string_result, buffer_result = s:sub(x, y), b:sub(x, y); | |
62 assert.equals(string_result, buffer_result, ("buffer:sub(%d, %s) does not match string:sub()"):format(x, y and ("%d"):format(y) or "nil")); | |
63 end | |
64 | |
65 it("works", function () | |
66 local b = dbuffer.new(); | |
67 assert.truthy(b:write("hello world")); | |
68 assert.equals("hello", b:sub(1, 5)); | |
69 end); | |
70 | |
71 it("works after discard", function () | |
72 local b = dbuffer.new(256); | |
73 assert.truthy(b:write("foobar")); | |
74 assert.equals("foobar", b:sub(1, 6)); | |
75 assert.truthy(b:discard(3)); -- consume "foo" | |
76 assert.equals("bar", b:sub(1, 3)); | |
77 end); | |
78 | |
79 it("supports optional end parameter", function () | |
80 local b = dbuffer.new(); | |
81 assert.truthy(b:write("hello world")); | |
82 assert.equals("hello world", b:sub(1)); | |
83 assert.equals("world", b:sub(-5)); | |
84 end); | |
85 | |
86 it("is equivalent to string:sub", function () | |
87 local b = dbuffer.new(11); | |
88 assert.truthy(b:write(s)); | |
89 for i = -13, 13 do | |
90 for j = -13, 13 do | |
91 test_sub(b, i, j); | |
92 end | |
93 end | |
94 end); | |
95 end); | |
96 | |
97 describe(":byte", function () | |
98 -- Helper function to compare buffer:byte() with string:byte() | |
99 local s = "hello world" | |
100 local function test_byte(b, x, y) | |
101 local string_result, buffer_result = {s:byte(x, y)}, {b:byte(x, y)}; | |
102 assert.same(string_result, buffer_result, ("buffer:byte(%d, %s) does not match string:byte()"):format(x, y and ("%d"):format(y) or "nil")); | |
103 end | |
104 | |
105 it("is equivalent to string:byte", function () | |
106 local b = dbuffer.new(11); | |
107 assert.truthy(b:write(s)); | |
108 test_byte(b, 1); | |
109 test_byte(b, 3); | |
110 test_byte(b, -1); | |
111 test_byte(b, -3); | |
112 for i = -13, 13 do | |
113 for j = -13, 13 do | |
114 test_byte(b, i, j); | |
115 end | |
116 end | |
117 end); | |
118 | |
119 it("works with characters > 127", function () | |
120 local b = dbuffer.new(); | |
121 b:write(string.char(0, 140)); | |
122 local r = { b:byte(1, 2) }; | |
123 assert.same({ 0, 140 }, r); | |
124 end); | |
125 | |
126 it("works on an empty buffer", function () | |
127 local b = dbuffer.new(); | |
128 assert.equal("", b:sub(1,1)); | |
129 end); | |
130 end); | |
131 end); |