headers: updates Lua 5.2 workaround to allow 5.3 version
[luaevent.git] / test / event_buffer-tests.lua
blobc561c345b08e22a0a673effab8bebeb529b5f467
1 local core = require("luaevent.core")
3 describe("Buffer tests", function()
4 local buffer, buffer2
5 before_each(function()
6 buffer = core.buffer.new()
7 buffer2 = core.buffer.new()
8 end)
10 after_each(function()
11 buffer:close()
12 buffer2:close()
13 end)
15 local function testDataEqual(expected, actual, msg)
16 msg = msg or ''
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)
21 end
23 it("should be empty on startup", function()
24 testDataEqual("", buffer, "Buffer not empty")
25 testDataEqual("", buffer2, "Buffer2 not empty")
26 end)
28 it("should support trivial simple string add", function()
29 buffer:add("Hello")
30 testDataEqual("Hello", buffer)
31 buffer:add("Hello")
32 testDataEqual("HelloHello", buffer)
33 end)
35 it("should support adding multiple strings", function()
36 buffer:add("Hello", "Hello")
37 testDataEqual("HelloHello", buffer)
38 end)
40 it("should be able to add digits", function()
41 buffer:add(1,2,3,4)
42 testDataEqual("1234", buffer)
43 buffer:add(100)
44 testDataEqual("1234100", buffer)
45 buffer:add(1.1)
46 testDataEqual("12341001.1", buffer)
47 end)
49 it("should support addBuffer", function()
50 buffer:add(buffer2)
51 testDataEqual("", buffer)
52 testDataEqual("", buffer2)
53 buffer2:add("Hello")
54 testDataEqual("Hello", buffer2)
55 buffer:add(buffer2)
56 testDataEqual("Hello", buffer)
57 testDataEqual("", buffer2)
58 assert.message("Cannot self-add buffers").has.errors(function()
59 buffer:add(buffer)
60 end)
61 assert.message("Cannot self-add buffers").has.errors(function()
62 buffer2:add(buffer2)
63 end)
64 testDataEqual("Hello", buffer, "Failures should not affect data content")
65 testDataEqual("", buffer2, "Failures should not affect data content")
66 end)
68 it("should fail when bad values are added", function()
69 assert.message("Should not be able to add no values").has.errors(function()
70 buffer:add()
71 end)
72 assert.message("Should not be able to add boolean true").has.errors(function()
73 buffer:add(true)
74 end)
75 assert.message("Should not be able to add boolean false").has.errors(function()
76 buffer:add(false)
77 end)
78 assert.message("Should not be able to add functions").has.errors(function()
79 buffer:add(function() end)
80 end)
81 assert.message("Should not be able to add threads").has.errors(function()
82 buffer:add(coroutine.create(function() end))
83 end)
84 assert.message("Should not be able to add non-buffer userdata").has.errors(function()
85 buffer:add(newproxy())
86 end)
87 assert.message("Should not be able to add nil values").has.errors(function()
88 buffer:add(nil)
89 end)
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)
92 end)
93 testDataEqual("", buffer, "Buffer not empty after failing additions")
94 end)
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()
100 buffer:drain()
101 end)
102 buffer:drain(1)
103 testDataEqual("23456789", buffer)
104 buffer:drain(4)
105 testDataEqual("6789", buffer)
106 assert.message("Should be able to apply draining beyond actual buffer length").has.no.errors(function()
107 buffer:drain(5)
108 end)
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()
114 buffer:drain(-1)
115 end)
116 testDataEqual("", buffer)
117 end)
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))
129 end)
131 local lineData = [[1
134 local splitLineData = {
135 "1","2",nil
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()
142 buffer:add(lineData)
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")
148 buffer:drain(-1)
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)
156 end)
157 end)