Comparison

spec/util_dbuffer_spec.lua @ 11636:11e0a0a08da3

util.dbuffer: Add read_until() method
author Matthew Wild <mwild1@gmail.com>
date Tue, 29 Jun 2021 13:48:14 +0100
parent 11158:3a72cb126d6c
child 11637:19cddf92fcc2
comparison
equal deleted inserted replaced
11635:1b17b967838e 11636:11e0a0a08da3
31 assert.equal("h", b:read(1)); 31 assert.equal("h", b:read(1));
32 32
33 assert.equal("ello", b:read()); 33 assert.equal("ello", b:read());
34 assert.equal(" ", b:read()); 34 assert.equal(" ", b:read());
35 assert.equal("world", b:read()); 35 assert.equal("world", b:read());
36 end);
37 end);
38
39 describe(":read_until", function ()
40 it("works", function ()
41 local b = dbuffer.new();
42 b:write("hello\n");
43 b:write("world");
44 b:write("\n");
45 b:write("\n\n");
46 b:write("stuff");
47 b:write("more\nand more");
48
49 assert.equal(nil, b:read_until("."));
50 assert.equal(nil, b:read_until("%"));
51 assert.equal("hello\n", b:read_until("\n"));
52 assert.equal("world\n", b:read_until("\n"));
53 assert.equal("\n", b:read_until("\n"));
54 assert.equal("\n", b:read_until("\n"));
55 assert.equal("stu", b:read(3));
56 assert.equal("ffmore\n", b:read_until("\n"));
57 assert.equal(nil, b:read_until("\n"));
58 assert.equal("and more", b:read_chunk());
59 end);
60
61 it("works with multi-character sequences", function ()
62 local b = dbuffer.new();
63 b:write("hello\r\n");
64 b:write("world");
65 b:write("\r\n");
66 b:write("\r\n\r\n");
67 b:write("stuff");
68 b:write("more\r\nand more");
69
70 assert.equal(nil, b:read_until("."));
71 assert.equal(nil, b:read_until("%"));
72 assert.equal("hello\r\n", b:read_until("\r\n"));
73 assert.equal("world\r\n", b:read_until("\r\n"));
74 assert.equal("\r\n", b:read_until("\r\n"));
75 assert.equal("\r\n", b:read_until("\r\n"));
76 assert.equal("stu", b:read(3));
77 assert.equal("ffmore\r\n", b:read_until("\r\n"));
78 assert.equal(nil, b:read_until("\r\n"));
79 assert.equal("and more", b:read_chunk());
36 end); 80 end);
37 end); 81 end);
38 82
39 describe(":discard", function () 83 describe(":discard", function ()
40 local b = dbuffer.new(); 84 local b = dbuffer.new();