Software /
code /
prosody
Annotate
spec/util_dbuffer_spec.lua @ 11000:d9aae4734f38
coding_style: Replace mention of git with hg
We use Mercurial, not git!
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 08 Jul 2020 23:22:28 +0200 |
parent | 10980:eaee72c7afbd |
child | 11028:d7a403060946 |
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 |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 describe(":sub", function () |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 -- Helper function to compare buffer:sub() with string:sub() |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 local s = "hello world"; |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 local function test_sub(b, x, y) |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 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
|
54 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
|
55 end |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 it("works", function () |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 local b = dbuffer.new(); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 assert.truthy(b:write("hello world")); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 assert.equals("hello", b:sub(1, 5)); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 end); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 it("supports optional end parameter", function () |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 local b = dbuffer.new(); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 assert.truthy(b:write("hello world")); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 assert.equals("hello world", b:sub(1)); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 assert.equals("world", b:sub(-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("is equivalent to string:sub", function () |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 local b = dbuffer.new(11); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 assert.truthy(b:write(s)); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 for i = -13, 13 do |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 for j = -13, 13 do |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 test_sub(b, i, j); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 end |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 end |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 end); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 end); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 describe(":byte", function () |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 -- Helper function to compare buffer:byte() with string:byte() |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 local s = "hello world" |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 local function test_byte(b, x, y) |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 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
|
86 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
|
87 end |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 it("is equivalent to string:byte", function () |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 local b = dbuffer.new(11); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 assert.truthy(b:write(s)); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 test_byte(b, 1); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 test_byte(b, 3); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 test_byte(b, -1); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 test_byte(b, -3); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 for i = -13, 13 do |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 for j = -13, 13 do |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 test_byte(b, i, j); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 end |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 end |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 end); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 it("works with characters > 127", function () |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 local b = dbuffer.new(); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 b:write(string.char(0, 140)); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 local r = { b:byte(1, 2) }; |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 assert.same({ 0, 140 }, r); |
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 end); |
39991e40d1dc
util.dbuffer: dynamic string buffer
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 end); |