Annotate

spec/util_dbuffer_spec.lua @ 12919:7c0e5c7eff7c

mod_tokenauth: Fix misplaced closing parenthesis `type(x ~= y)` is always a string, thus truthy
author Kim Alvefur <zash@zash.se>
date Thu, 02 Mar 2023 22:34:29 +0100
parent 12764:bf6d2f9fad4d
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());
12764
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
9 assert.truthy(dbuffer.new(1));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
10 assert.truthy(dbuffer.new(1024));
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 it("won't create an empty buffer", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 assert.falsy(dbuffer.new(0));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 it("won't create a negatively sized buffer", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 assert.falsy(dbuffer.new(-1));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 describe(":write", function ()
12764
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
20 local b = dbuffer.new(10, 3);
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 it("works", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 assert.truthy(b:write("hi"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 end);
12764
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
24 it("fails when the buffer is full", function ()
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
25 local ret = b:write(" there world, this is a long piece of data");
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
26 assert.is_falsy(ret);
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
27 end);
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
28 it("works when max_chunks is reached", function ()
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
29 -- Chunks are an optimization, dbuffer should collapse chunks when needed
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
30 for _ = 1, 8 do
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
31 assert.truthy(b:write("!"));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
32 end
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
33 assert.falsy(b:write("!")); -- Length reached
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
34 end);
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 describe(":read", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 it("supports optional bytes parameter", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 -- should return the frontmost chunk
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 assert.truthy(b:write("hello"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 assert.truthy(b:write(" "));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 assert.truthy(b:write("world"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 assert.equal("h", b:read(1));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 assert.equal("ello", b:read());
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 assert.equal(" ", b:read());
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 assert.equal("world", b:read());
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 end);
12764
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
50 it("fails when there is not enough data in the buffer", function ()
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
51 local b = dbuffer.new(12);
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
52 b:write("hello");
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
53 b:write(" ");
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
54 b:write("world");
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
55 assert.is_falsy(b:read(12));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
56 assert.is_falsy(b:read(13));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
57 end);
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59
11636
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
60 describe(":read_until", function ()
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
61 it("works", function ()
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
62 local b = dbuffer.new();
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
63 b:write("hello\n");
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
64 b:write("world");
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
65 b:write("\n");
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
66 b:write("\n\n");
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
67 b:write("stuff");
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
68 b:write("more\nand more");
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
69
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
70 assert.equal(nil, b:read_until("."));
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
71 assert.equal(nil, b:read_until("%"));
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
72 assert.equal("hello\n", b:read_until("\n"));
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
73 assert.equal("world\n", b:read_until("\n"));
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
74 assert.equal("\n", b:read_until("\n"));
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
75 assert.equal("\n", b:read_until("\n"));
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
76 assert.equal("stu", b:read(3));
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
77 assert.equal("ffmore\n", b:read_until("\n"));
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
78 assert.equal(nil, b:read_until("\n"));
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
79 assert.equal("and more", b:read_chunk());
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
80 end);
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
81 end);
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
82
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 describe(":discard", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 local b = dbuffer.new();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 it("works", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 assert.truthy(b:write("hello world"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 assert.truthy(b:discard(6));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 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
89 assert.equal(5, b:len());
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 assert.equal("world", b:read(5));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 end);
12764
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
92 it("works across chunks", function ()
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
93 assert.truthy(b:write("hello"));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
94 assert.truthy(b:write(" "));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
95 assert.truthy(b:write("world"));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
96 assert.truthy(b:discard(3));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
97 assert.equal(8, b:length());
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
98 assert.truthy(b:discard(3));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
99 assert.equal(5, b:length());
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
100 assert.equal("world", b:read(5));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
101 end);
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
102 it("can discard the entire buffer", function ()
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
103 assert.equal(b:len(), 0);
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
104 assert.truthy(b:write("hello world"));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
105 assert.truthy(b:discard(11));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
106 assert.equal(0, b:len());
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
107 assert.truthy(b:write("hello world"));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
108 assert.truthy(b:discard(12));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
109 assert.equal(0, b:len());
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
110 assert.truthy(b:write("hello world"));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
111 assert.truthy(b:discard(128));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
112 assert.equal(0, b:len());
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
113 end);
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
114 it("works on an empty buffer", function ()
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
115 assert.truthy(dbuffer.new():discard());
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
116 assert.truthy(dbuffer.new():discard(0));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
117 assert.truthy(dbuffer.new():discard(1));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
118 end);
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 describe(":collapse()", function ()
12764
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
122 it("works", function ()
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
123 local b = dbuffer.new();
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
124 b:write("hello");
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
125 b:write(" ");
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
126 b:write("world");
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
127 b:collapse(6);
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
128 local ret, bytes = b:read_chunk();
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
129 assert.equal("hello ", ret);
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
130 assert.equal(6, bytes);
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
131 end);
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 it("works on an empty buffer", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 local b = dbuffer.new();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 b:collapse();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 describe(":sub", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 -- Helper function to compare buffer:sub() with string:sub()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 local s = "hello world";
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 local function test_sub(b, x, y)
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 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
143 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
144 end
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 it("works", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 local b = dbuffer.new();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 assert.truthy(b:write("hello world"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 assert.equals("hello", b:sub(1, 5));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 it("works after discard", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 local b = dbuffer.new(256);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 assert.truthy(b:write("foobar"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 assert.equals("foobar", b:sub(1, 6));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 assert.truthy(b:discard(3)); -- consume "foo"
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 assert.equals("bar", b:sub(1, 3));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160 it("supports optional end parameter", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 local b = dbuffer.new();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 assert.truthy(b:write("hello world"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 assert.equals("hello world", b:sub(1));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 assert.equals("world", b:sub(-5));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 it("is equivalent to string:sub", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 local b = dbuffer.new(11);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 assert.truthy(b:write(s));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 for i = -13, 13 do
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 for j = -13, 13 do
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 test_sub(b, i, j);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 end
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 end
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 end);
12764
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
176
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
177 it("works on an empty buffer", function ()
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
178 local b = dbuffer.new();
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
179 assert.equal("", b:sub(1, 12));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
180 end);
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 describe(":byte", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 -- Helper function to compare buffer:byte() with string:byte()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 local s = "hello world"
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 local function test_byte(b, x, y)
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 local string_result, buffer_result = {s:byte(x, y)}, {b:byte(x, y)};
12764
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
188 assert.same(
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
189 string_result,
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
190 buffer_result,
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
191 ("buffer:byte(%s, %s) does not match string:byte()"):format(x and ("%d"):format(x) or "nil", y and ("%d"):format(y) or "nil")
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
192 );
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 end
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 it("is equivalent to string:byte", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 local b = dbuffer.new(11);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 assert.truthy(b:write(s));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 test_byte(b, 1);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 test_byte(b, 3);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 test_byte(b, -1);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 test_byte(b, -3);
12764
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
202 test_byte(b, nil, 5);
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 for i = -13, 13 do
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 for j = -13, 13 do
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 test_byte(b, i, j);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 end
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 end
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 it("works with characters > 127", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 local b = dbuffer.new();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 b:write(string.char(0, 140));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 local r = { b:byte(1, 2) };
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 assert.same({ 0, 140 }, r);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 it("works on an empty buffer", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218 local b = dbuffer.new();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219 assert.equal("", b:sub(1,1));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
221 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222 end);