# HG changeset patch # User Thomas Harning Jr # Date 1189221800 14400 # Node ID 139601546bd9eca2db4082a4f9608cbb914882bd # Parent 4f81725a1e5fd17ab74a042ba4de1c71b8f8a5e0 Learned event_buffer tests and added failure for 'add' if no args passed. lunit-based unit tests for event_buffer have been added. They are currently not complete but are in the works to help manage corner cases/etc. These should also get behaviors well-defined. diff -r 4f81725a1e5f -r 139601546bd9 src/event_buffer.c --- a/src/event_buffer.c Fri Sep 07 22:19:31 2007 -0400 +++ b/src/event_buffer.c Fri Sep 07 23:23:20 2007 -0400 @@ -68,6 +68,7 @@ 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 @@ 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 -r 4f81725a1e5f -r 139601546bd9 test/event_buffer-tests.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/event_buffer-tests.lua Fri Sep 07 23:23:20 2007 -0400 @@ -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