diff options
-rw-r--r-- | src/event_buffer.c | 2 | ||||
-rw-r--r-- | test/event_buffer-tests.lua | 117 |
2 files changed, 119 insertions, 0 deletions
diff --git a/src/event_buffer.c b/src/event_buffer.c index 4d65273..110a41c 100644 --- a/src/event_buffer.c +++ b/src/event_buffer.c @@ -68,6 +68,7 @@ static int event_buffer_gc(lua_State* L) { progressively adds items to the buffer if arg[*] is string, treat as a string:format call if arg[*] is a buffer, perform event_add_buffer + expects at least 1 other argument returns number of bytes added */ static int event_buffer_add(lua_State* L) { @@ -76,6 +77,7 @@ static int event_buffer_add(lua_State* L) { int oldLength = EVBUFFER_LENGTH(buffer); int last = lua_gettop(L); int i; + if(last == 1) luaL_error(L, "Not enough arguments to add: expects at least 1 additional operand"); for(i = 2; i <= last; i++) { if(!lua_isstring(L, i) && !is_event_buffer(L, i)) luaL_argerror(L, i, "Argument is not a string or buffer object"); diff --git a/test/event_buffer-tests.lua b/test/event_buffer-tests.lua new file mode 100644 index 0000000..02c25fc --- /dev/null +++ b/test/event_buffer-tests.lua @@ -0,0 +1,117 @@ +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) +end + +lunit.run()
\ No newline at end of file |