1 require("luaevent.core")
2 local buffer
= luaevent
.core
.buffer
8 bufferTests
= TestCase("bufferTests")
10 function bufferTests
:setup()
11 self
.buffer
= buffer
.new()
12 self
.buffer2
= buffer
.new()
15 function bufferTests
:teardown()
20 local function testDataEqual(expected
, actual
, msg
)
22 assert_equal(expected
, actual
:get_data(), "Buffer not the same: " .. msg
)
23 assert_equal(#expected
, actual
:length(), "Buffer length not the same: " .. msg
)
24 assert_equal(expected
, tostring(actual
), "Buffer (from tostring) not the same: " .. msg
)
25 assert_equal(#expected
, #actual
, "Buffer length (from #) not zero: " .. msg
)
28 function bufferTests
:test_empty()
29 testDataEqual("", self
.buffer
, "Buffer not empty")
30 testDataEqual("", self
.buffer2
, "Buffer2 not empty")
33 function bufferTests
:test_addSimpleString()
34 self
.buffer
:add("Hello")
35 testDataEqual("Hello", self
.buffer
)
36 self
.buffer
:add("Hello")
37 testDataEqual("HelloHello", self
.buffer
)
40 function bufferTests
:test_addMultipleStrings()
41 self
.buffer
:add("Hello", "Hello")
42 testDataEqual("HelloHello", self
.buffer
)
45 function bufferTests
:test_addDigits()
46 self
.buffer
:add(1,2,3,4)
47 testDataEqual("1234", self
.buffer
)
49 testDataEqual("1234100", self
.buffer
)
51 testDataEqual("12341001.1", self
.buffer
)
54 function bufferTests
:test_addBuffer()
55 self
.buffer
:add(self
.buffer2
)
56 testDataEqual("", self
.buffer
)
57 testDataEqual("", self
.buffer2
)
58 self
.buffer2
:add("Hello")
59 testDataEqual("Hello", self
.buffer2
)
60 self
.buffer
:add(self
.buffer2
)
61 testDataEqual("Hello", self
.buffer
)
62 testDataEqual("", self
.buffer2
)
63 assert_error("Cannot self-add buffers", function()
64 self
.buffer
:add(self
.buffer
)
66 assert_error("Cannot self-add buffers", function()
67 self
.buffer2
:add(self
.buffer2
)
69 testDataEqual("Hello", self
.buffer
, "Failures should not affect data content")
70 testDataEqual("", self
.buffer2
, "Failures should not affect data content")
73 function bufferTests
:test_addBadValues_fail()
74 assert_error("Should not be able to add no values", function()
77 assert_error("Should not be able to add boolean true", function()
80 assert_error("Should not be able to add boolean false", function()
81 self
.buffer
:add(false)
83 assert_error("Should not be able to add functions", function()
84 self
.buffer
:add(function() end)
86 assert_error("Should not be able to add threads", function()
87 self
.buffer
:add(coroutine
.create(function() end))
89 assert_error("Should not be able to add non-buffer userdata", function()
90 self
.buffer
:add(newproxy())
92 assert_error("Should not be able to add nil values", function()
95 assert_error("Multiples including valid values should not affect failure results", function()
96 self
.buffer
:add("Hello", 1, bufferb
, true, false, function() end, newproxy(), nil)
98 testDataEqual("", self
.buffer
, "Buffer not empty after failing additions")
101 function bufferTests
:test_drain()
102 self
.buffer
:add("123456789")
103 testDataEqual("123456789", self
.buffer
)
104 assert_error("Cannot call drain w/ no args", function()
108 testDataEqual("23456789", self
.buffer
)
110 testDataEqual("6789", self
.buffer
)
111 assert_pass("Should be able to apply draining beyond actual buffer length", function()
114 testDataEqual("", self
.buffer
)