Comparison

spec/util_dbuffer_spec.lua @ 11105:3c0940f3cf74 0.11

util.dbuffer: Simplify test case An earlier theory involved the bug being related to collapsing multiple items, so it exercised that too. Also correct the comment, it referred to the space in "hello world" in an earlier version before the test string was changed to "foobar", which was what was tested in a REPL
author Kim Alvefur <zash@zash.se>
date Mon, 24 Aug 2020 17:28:48 +0200
child 11114:6a608ecb3471
child 11156:a8ef69f7fc35
comparison
equal deleted inserted replaced
11104:6632acc96cf6 11105:3c0940f3cf74
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());
45 assert.equal("world", b:read(5));
46 end);
47 end);
48
49 describe(":collapse()", function ()
50 it("works on an empty buffer", function ()
51 local b = dbuffer.new();
52 b:collapse();
53 end);
54 end);
55
56 describe(":sub", function ()
57 -- Helper function to compare buffer:sub() with string:sub()
58 local s = "hello world";
59 local function test_sub(b, x, y)
60 local string_result, buffer_result = s:sub(x, y), b:sub(x, y);
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"));
62 end
63
64 it("works", function ()
65 local b = dbuffer.new();
66 assert.truthy(b:write("hello world"));
67 assert.equals("hello", b:sub(1, 5));
68 end);
69
70 it("works after discard", function ()
71 local b = dbuffer.new(256);
72 assert.truthy(b:write("foobar"));
73 assert.equals("foobar", b:sub(1, 6));
74 assert.truthy(b:discard(3)); -- consume "foo"
75 assert.equals("bar", b:sub(1, 3));
76 end);
77
78 it("supports optional end parameter", function ()
79 local b = dbuffer.new();
80 assert.truthy(b:write("hello world"));
81 assert.equals("hello world", b:sub(1));
82 assert.equals("world", b:sub(-5));
83 end);
84
85 it("is equivalent to string:sub", function ()
86 local b = dbuffer.new(11);
87 assert.truthy(b:write(s));
88 for i = -13, 13 do
89 for j = -13, 13 do
90 test_sub(b, i, j);
91 end
92 end
93 end);
94 end);
95
96 describe(":byte", function ()
97 -- Helper function to compare buffer:byte() with string:byte()
98 local s = "hello world"
99 local function test_byte(b, x, y)
100 local string_result, buffer_result = {s:byte(x, y)}, {b:byte(x, y)};
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"));
102 end
103
104 it("is equivalent to string:byte", function ()
105 local b = dbuffer.new(11);
106 assert.truthy(b:write(s));
107 test_byte(b, 1);
108 test_byte(b, 3);
109 test_byte(b, -1);
110 test_byte(b, -3);
111 for i = -13, 13 do
112 for j = -13, 13 do
113 test_byte(b, i, j);
114 end
115 end
116 end);
117
118 it("works with characters > 127", function ()
119 local b = dbuffer.new();
120 b:write(string.char(0, 140));
121 local r = { b:byte(1, 2) };
122 assert.same({ 0, 140 }, r);
123 end);
124
125 it("works on an empty buffer", function ()
126 local b = dbuffer.new();
127 assert.equal("", b:sub(1,1));
128 end);
129 end);
130 end);