Annotate

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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local dbuffer = require "util.dbuffer";
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 describe("util.dbuffer", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 describe("#new", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 it("has a constructor", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 assert.Function(dbuffer.new);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 it("can be created", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 assert.truthy(dbuffer.new());
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 it("won't create an empty buffer", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 assert.falsy(dbuffer.new(0));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 it("won't create a negatively sized buffer", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 assert.falsy(dbuffer.new(-1));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 describe(":write", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local b = dbuffer.new();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 it("works", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 assert.truthy(b:write("hi"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 describe(":read", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 it("supports optional bytes parameter", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 -- should return the frontmost chunk
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 local b = dbuffer.new();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 assert.truthy(b:write("hello"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 assert.truthy(b:write(" "));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 assert.truthy(b:write("world"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 assert.equal("h", b:read(1));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 assert.equal("ello", b:read());
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 assert.equal(" ", b:read());
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 assert.equal("world", b:read());
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 describe(":discard", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 local b = dbuffer.new();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 it("works", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 assert.truthy(b:write("hello world"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 assert.truthy(b:discard(6));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 assert.equal(5, b:length());
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 assert.equal("world", b:read(5));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 describe(":collapse()", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 it("works on an empty buffer", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 local b = dbuffer.new();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 b:collapse();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 describe(":sub", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 -- Helper function to compare buffer:sub() with string:sub()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 local s = "hello world";
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 local function test_sub(b, x, y)
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 local string_result, buffer_result = s:sub(x, y), b:sub(x, y);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
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"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 end
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 it("works", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 local b = dbuffer.new();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 assert.truthy(b:write("hello world"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 assert.equals("hello", b:sub(1, 5));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 it("works after discard", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 local b = dbuffer.new(256);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 assert.truthy(b:write("foobar"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 assert.equals("foobar", b:sub(1, 6));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 assert.truthy(b:discard(3)); -- consume "foo"
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 assert.equals("bar", b:sub(1, 3));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 it("supports optional end parameter", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 local b = dbuffer.new();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 assert.truthy(b:write("hello world"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 assert.equals("hello world", b:sub(1));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 assert.equals("world", b:sub(-5));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 it("is equivalent to string:sub", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 local b = dbuffer.new(11);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 assert.truthy(b:write(s));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 for i = -13, 13 do
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 for j = -13, 13 do
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 test_sub(b, i, j);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 end
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 end
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 describe(":byte", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 -- Helper function to compare buffer:byte() with string:byte()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 local s = "hello world"
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 local function test_byte(b, x, y)
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 local string_result, buffer_result = {s:byte(x, y)}, {b:byte(x, y)};
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
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"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 end
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 it("is equivalent to string:byte", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 local b = dbuffer.new(11);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 assert.truthy(b:write(s));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 test_byte(b, 1);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 test_byte(b, 3);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 test_byte(b, -1);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 test_byte(b, -3);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 for i = -13, 13 do
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 for j = -13, 13 do
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 test_byte(b, i, j);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 end
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 end
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 it("works with characters > 127", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 local b = dbuffer.new();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 b:write(string.char(0, 140));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 local r = { b:byte(1, 2) };
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 assert.same({ 0, 140 }, r);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 it("works on an empty buffer", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 local b = dbuffer.new();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 assert.equal("", b:sub(1,1));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 end);