Software /
code /
prosody
Annotate
spec/util_dbuffer_spec.lua @ 11028:d7a403060946
util.dbuffer: Fix traceback when :collapse() is called on empty buffer
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 20 Aug 2020 15:22:19 +0100 |
parent | 10980:eaee72c7afbd |
child | 11044:7b720a815519 |
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 |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 it("supports optional end parameter", function () |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 local b = dbuffer.new(); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 assert.truthy(b:write("hello world")); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 assert.equals("hello world", b:sub(1)); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 assert.equals("world", b:sub(-5)); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 end); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 it("is equivalent to string:sub", function () |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 local b = dbuffer.new(11); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 assert.truthy(b:write(s)); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 for i = -13, 13 do |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 for j = -13, 13 do |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 test_sub(b, i, j); |
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 end |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 end); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 end); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 describe(":byte", function () |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 -- Helper function to compare buffer:byte() with string:byte() |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 local s = "hello world" |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 local function test_byte(b, x, y) |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 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
|
93 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
|
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 it("is equivalent to string:byte", function () |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 local b = dbuffer.new(11); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 assert.truthy(b:write(s)); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 test_byte(b, 1); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 test_byte(b, 3); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 test_byte(b, -1); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 test_byte(b, -3); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 for i = -13, 13 do |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 for j = -13, 13 do |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 test_byte(b, i, j); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 end |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 end |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 end); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 it("works with characters > 127", function () |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 local b = dbuffer.new(); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 b:write(string.char(0, 140)); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 local r = { b:byte(1, 2) }; |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 assert.same({ 0, 140 }, r); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 end); |
11028
d7a403060946
util.dbuffer: Fix traceback when :collapse() is called on empty buffer
Matthew Wild <mwild1@gmail.com>
parents:
10980
diff
changeset
|
116 |
d7a403060946
util.dbuffer: Fix traceback when :collapse() is called on empty buffer
Matthew Wild <mwild1@gmail.com>
parents:
10980
diff
changeset
|
117 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
|
118 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
|
119 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
|
120 end); |
10973
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 end); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 end); |