1 local core
= require("luaevent.core")
3 describe("Buffer tests", function()
6 buffer
= core
.buffer
.new()
7 buffer2
= core
.buffer
.new()
15 local function testDataEqual(expected
, actual
, msg
)
17 assert.message("Buffer not the same: " .. msg
).are
.equal(expected
, actual
:get_data())
18 assert.message("Buffer length not the same: " .. msg
).are
.equal(#expected
, actual
:length())
19 assert.message("Buffer (from tostring) not the same: " .. msg
).are
.equal(expected
, tostring(actual
))
20 assert.message("Buffer length (from #) not zero: " .. msg
).are
.equal(#expected
, #actual
)
23 it("should be empty on startup", function()
24 testDataEqual("", buffer
, "Buffer not empty")
25 testDataEqual("", buffer2
, "Buffer2 not empty")
28 it("should support trivial simple string add", function()
30 testDataEqual("Hello", buffer
)
32 testDataEqual("HelloHello", buffer
)
35 it("should support adding multiple strings", function()
36 buffer
:add("Hello", "Hello")
37 testDataEqual("HelloHello", buffer
)
40 it("should be able to add digits", function()
42 testDataEqual("1234", buffer
)
44 testDataEqual("1234100", buffer
)
46 testDataEqual("12341001.1", buffer
)
49 it("should support addBuffer", function()
51 testDataEqual("", buffer
)
52 testDataEqual("", buffer2
)
54 testDataEqual("Hello", buffer2
)
56 testDataEqual("Hello", buffer
)
57 testDataEqual("", buffer2
)
58 assert.message("Cannot self-add buffers").has
.errors(function()
61 assert.message("Cannot self-add buffers").has
.errors(function()
64 testDataEqual("Hello", buffer
, "Failures should not affect data content")
65 testDataEqual("", buffer2
, "Failures should not affect data content")
68 it("should fail when bad values are added", function()
69 assert.message("Should not be able to add no values").has
.errors(function()
72 assert.message("Should not be able to add boolean true").has
.errors(function()
75 assert.message("Should not be able to add boolean false").has
.errors(function()
78 assert.message("Should not be able to add functions").has
.errors(function()
79 buffer
:add(function() end)
81 assert.message("Should not be able to add threads").has
.errors(function()
82 buffer
:add(coroutine
.create(function() end))
84 assert.message("Should not be able to add non-buffer userdata").has
.errors(function()
85 buffer
:add(newproxy())
87 assert.message("Should not be able to add nil values").has
.errors(function()
90 assert.message("Multiples including valid values should not affect failure results").has
.errors(function()
91 buffer
:add("Hello", 1, bufferb
, true, false, function() end, newproxy(), nil)
93 testDataEqual("", buffer
, "Buffer not empty after failing additions")
96 it("should properly support draining", function()
97 buffer
:add("123456789")
98 testDataEqual("123456789", buffer
)
99 assert.message("Cannot call drain w/ no args").has
.errors(function()
103 testDataEqual("23456789", buffer
)
105 testDataEqual("6789", buffer
)
106 assert.message("Should be able to apply draining beyond actual buffer length").has
.no
.errors(function()
109 testDataEqual("", buffer
)
110 buffer
:add("123456789")
111 testDataEqual("123456789", buffer
)
112 assert.message([[Should be able to apply negative draining to cause draining `all data`
113 (see source comments for why)]]).has
.no
.errors(function()
116 testDataEqual("", buffer
)
119 it("should have working partial reads", function()
120 buffer
:add("123456789")
121 assert.are
.equal("1234", buffer
:get_data(4))
122 assert.are
.equal("1234", buffer
:get_data(1,4))
123 assert.are
.equal("5678", buffer
:get_data(5,4))
124 assert.are
.equal("5", buffer
:get_data(5,1))
125 assert.message("Data length is capped at max obtainable").are
.equal("56789", buffer
:get_data(5,100000000))
126 assert.message("Negative sizes capture entire remaining string").are
.equal("56789", buffer
:get_data(5,-100))
127 assert.message("Negative position causes wraparound").are
.equal("9", buffer
:get_data(-1, 1))
128 assert.message("Negative wraparound does not cause length inversion").are
.equal("89", buffer
:get_data(-2,2))
134 local splitLineData
= {
137 local mixedLineData
= "1\r2\n3\r\n4\n\r5\r\r6\n\n7\r\n\r8\r\n\r9"
138 local splitMixedLineData
= {
139 "1","2","3","4","5","6","7","8", nil
141 it("should have a working readline", function()
143 testDataEqual(lineData
, buffer
)
144 for _
, data
in ipairs(splitLineData
) do
145 assert.are
.equal(data
, buffer
:readline())
147 testDataEqual("3", buffer
, "Failed readline doesn't affect buffer contents")
149 testDataEqual("", buffer
)
150 buffer
:add(mixedLineData
)
151 testDataEqual(mixedLineData
, buffer
)
152 for _
, data
in ipairs(splitMixedLineData
) do
153 assert.are
.equal(data
, buffer
:readline())
155 testDataEqual("9", buffer
)