1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
require("luaevent.core")
local buffer = luaevent.core.buffer
require("lunit")
lunit.import("all")
bufferTests = TestCase("bufferTests")
function bufferTests:setup()
self.buffer = buffer.new()
self.buffer2 = buffer.new()
end
function bufferTests:teardown()
self.buffer:close()
self.buffer2:close()
end
local function testDataEqual(expected, actual, msg)
msg = msg or ''
assert_equal(expected, actual:get_data(), "Buffer not the same: " .. msg)
assert_equal(#expected, actual:length(), "Buffer length not the same: " .. msg)
assert_equal(expected, tostring(actual), "Buffer (from tostring) not the same: " .. msg)
assert_equal(#expected, #actual, "Buffer length (from #) not zero: " .. msg)
end
function bufferTests:test_empty()
testDataEqual("", self.buffer, "Buffer not empty")
testDataEqual("", self.buffer2, "Buffer2 not empty")
end
function bufferTests:test_addSimpleString()
self.buffer:add("Hello")
testDataEqual("Hello", self.buffer)
self.buffer:add("Hello")
testDataEqual("HelloHello", self.buffer)
end
function bufferTests:test_addMultipleStrings()
self.buffer:add("Hello", "Hello")
testDataEqual("HelloHello", self.buffer)
end
function bufferTests:test_addDigits()
self.buffer:add(1,2,3,4)
testDataEqual("1234", self.buffer)
self.buffer:add(100)
testDataEqual("1234100", self.buffer)
self.buffer:add(1.1)
testDataEqual("12341001.1", self.buffer)
end
function bufferTests:test_addBuffer()
self.buffer:add(self.buffer2)
testDataEqual("", self.buffer)
testDataEqual("", self.buffer2)
self.buffer2:add("Hello")
testDataEqual("Hello", self.buffer2)
self.buffer:add(self.buffer2)
testDataEqual("Hello", self.buffer)
testDataEqual("", self.buffer2)
assert_error("Cannot self-add buffers", function()
self.buffer:add(self.buffer)
end)
assert_error("Cannot self-add buffers", function()
self.buffer2:add(self.buffer2)
end)
testDataEqual("Hello", self.buffer, "Failures should not affect data content")
testDataEqual("", self.buffer2, "Failures should not affect data content")
end
function bufferTests:test_addBadValues_fail()
assert_error("Should not be able to add no values", function()
self.buffer:add()
end)
assert_error("Should not be able to add boolean true", function()
self.buffer:add(true)
end)
assert_error("Should not be able to add boolean false", function()
self.buffer:add(false)
end)
assert_error("Should not be able to add functions", function()
self.buffer:add(function() end)
end)
assert_error("Should not be able to add threads", function()
self.buffer:add(coroutine.create(function() end))
end)
assert_error("Should not be able to add non-buffer userdata", function()
self.buffer:add(newproxy())
end)
assert_error("Should not be able to add nil values", function()
self.buffer:add(nil)
end)
assert_error("Multiples including valid values should not affect failure results", function()
self.buffer:add("Hello", 1, bufferb, true, false, function() end, newproxy(), nil)
end)
testDataEqual("", self.buffer, "Buffer not empty after failing additions")
end
function bufferTests:test_drain()
self.buffer:add("123456789")
testDataEqual("123456789", self.buffer)
assert_error("Cannot call drain w/ no args", function()
self.buffer:drain()
end)
self.buffer:drain(1)
testDataEqual("23456789", self.buffer)
self.buffer:drain(4)
testDataEqual("6789", self.buffer)
assert_pass("Should be able to apply draining beyond actual buffer length", function()
self.buffer:drain(5)
end)
testDataEqual("", self.buffer)
self.buffer:add("123456789")
testDataEqual("123456789", self.buffer)
assert_pass([[Should be able to apply negative draining to cause draining `all data`
(see source comments for why)]], function()
self.buffer:drain(-1)
end)
testDataEqual("", self.buffer)
end
function bufferTests:test_getPartial()
self.buffer:add("123456789")
assert_equal("1234", self.buffer:get_data(4))
assert_equal("1234", self.buffer:get_data(1,4))
assert_equal("5678", self.buffer:get_data(5,4))
assert_equal("5", self.buffer:get_data(5,1))
assert_equal("56789", self.buffer:get_data(5,100000000), "Data length is capped at max obtainable")
assert_equal("56789", self.buffer:get_data(5,-100), "Negative sizes capture entire remaining string")
assert_equal("9", self.buffer:get_data(-1, 1, "Negative position causes wraparound"))
assert_equal("89", self.buffer:get_data(-2,2, "Negative wraparound does not cause length inversion"))
end
local lineData = [[1
2
3]]
local splitLineData = {
"1","2",nil
}
local mixedLineData = "1\r2\n3\r\n4\n\r5\r\r6\n\n7\r\n\r8\r\n\r9"
local splitMixedLineData = {
"1","2","3","4","5","","6","","7","","8","", nil
}
function bufferTests:test_readline()
self.buffer:add(lineData)
testDataEqual(lineData, self.buffer)
for _, data in ipairs(splitLineData) do
assert_equal(data, self.buffer:readline())
end
testDataEqual("3", self.buffer, "Failed readline doesn't affect buffer contents")
self.buffer:drain(-1)
testDataEqual("", self.buffer)
self.buffer:add(mixedLineData)
testDataEqual(mixedLineData, self.buffer)
for _, data in ipairs(splitMixedLineData) do
assert_equal(data, self.buffer:readline())
end
testDataEqual("9", self.buffer)
end
lunit.run()
|