diff options
Diffstat (limited to 'test/unit/rbuffer_spec.lua')
-rw-r--r-- | test/unit/rbuffer_spec.lua | 100 |
1 files changed, 34 insertions, 66 deletions
diff --git a/test/unit/rbuffer_spec.lua b/test/unit/rbuffer_spec.lua index 89136410d3..e9104dd5c4 100644 --- a/test/unit/rbuffer_spec.lua +++ b/test/unit/rbuffer_spec.lua @@ -1,9 +1,11 @@ -local helpers = require("test.unit.helpers") +local helpers = require("test.unit.helpers")(after_each) +local itp = helpers.gen_itp(it) -local ffi = helpers.ffi -local eq = helpers.eq -local cstr = helpers.cstr +local eq = helpers.eq +local ffi = helpers.ffi +local cstr = helpers.cstr local to_cstr = helpers.to_cstr +local child_call_once = helpers.child_call_once local rbuffer = helpers.cimport("./test/unit/fixtures/rbuffer.h") @@ -31,9 +33,11 @@ describe('rbuffer functions', function() end before_each(function() - rbuf = ffi.gc(rbuffer.rbuffer_new(capacity), rbuffer.rbuffer_free) - -- fill the internal buffer with the character '0' to simplify inspecting - ffi.C.memset(rbuf.start_ptr, string.byte('0'), capacity) + child_call_once(function() + rbuf = ffi.gc(rbuffer.rbuffer_new(capacity), rbuffer.rbuffer_free) + -- fill the internal buffer with the character '0' to simplify inspecting + ffi.C.memset(rbuf.start_ptr, string.byte('0'), capacity) + end) end) describe('RBUFFER_UNTIL_FULL', function() @@ -50,66 +54,51 @@ describe('rbuffer functions', function() end) describe('with empty buffer in one contiguous chunk', function() - it('is called once with the empty chunk', function() + itp('is called once with the empty chunk', function() collect_write_chunks() eq({'0000000000000000'}, chunks) end) end) describe('with partially empty buffer in one contiguous chunk', function() - before_each(function() + itp('is called once with the empty chunk', function() write('string') - end) - - it('is called once with the empty chunk', function() collect_write_chunks() eq({'0000000000'}, chunks) end) end) describe('with filled buffer in one contiguous chunk', function() - before_each(function() + itp('is not called', function() write('abcdefghijklmnopq') - end) - - it('is not called', function() collect_write_chunks() eq({}, chunks) end) end) describe('with buffer partially empty in two contiguous chunks', function() - before_each(function() + itp('is called twice with each filled chunk', function() write('1234567890') read(8) - end) - - it('is called twice with each filled chunk', function() collect_write_chunks() eq({'000000', '12345678'}, chunks) end) end) describe('with buffer empty in two contiguous chunks', function() - before_each(function() + itp('is called twice with each filled chunk', function() write('12345678') read(8) - end) - - it('is called twice with each filled chunk', function() collect_write_chunks() eq({'00000000', '12345678'}, chunks) end) end) describe('with buffer filled in two contiguous chunks', function() - before_each(function() + itp('is not called', function() write('12345678') read(8) write('abcdefghijklmnopq') - end) - - it('is not called', function() collect_write_chunks() eq({}, chunks) end) @@ -130,55 +119,43 @@ describe('rbuffer functions', function() end) describe('with empty buffer', function() - it('is not called', function() + itp('is not called', function() collect_read_chunks() eq({}, chunks) end) end) describe('with partially filled buffer in one contiguous chunk', function() - before_each(function() + itp('is called once with the filled chunk', function() write('string') - end) - - it('is called once with the filled chunk', function() collect_read_chunks() eq({'string'}, chunks) end) end) describe('with filled buffer in one contiguous chunk', function() - before_each(function() + itp('is called once with the filled chunk', function() write('abcdefghijklmnopq') - end) - - it('is called once with the filled chunk', function() collect_read_chunks() eq({'abcdefghijklmnop'}, chunks) end) end) describe('with buffer partially filled in two contiguous chunks', function() - before_each(function() + itp('is called twice with each filled chunk', function() write('1234567890') read(10) write('long string') - end) - - it('is called twice with each filled chunk', function() collect_read_chunks() eq({'long s', 'tring'}, chunks) end) end) describe('with buffer filled in two contiguous chunks', function() - before_each(function() + itp('is called twice with each filled chunk', function() write('12345678') read(8) write('abcdefghijklmnopq') - end) - - it('is called twice with each filled chunk', function() collect_read_chunks() eq({'abcdefgh', 'ijklmnop'}, chunks) end) @@ -198,20 +175,17 @@ describe('rbuffer functions', function() end) describe('with empty buffer', function() - it('is not called', function() + itp('is not called', function() collect_chars() eq({}, chars) end) end) describe('with buffer filled in two contiguous chunks', function() - before_each(function() + itp('collects each character and index', function() write('1234567890') read(10) write('long string') - end) - - it('collects each character and index', function() collect_chars() eq({{'l', 0}, {'o', 1}, {'n', 2}, {'g', 3}, {' ', 4}, {'s', 5}, {'t', 6}, {'r', 7}, {'i', 8}, {'n', 9}, {'g', 10}}, chars) @@ -232,20 +206,17 @@ describe('rbuffer functions', function() end) describe('with empty buffer', function() - it('is not called', function() + itp('is not called', function() collect_chars() eq({}, chars) end) end) describe('with buffer filled in two contiguous chunks', function() - before_each(function() + itp('collects each character and index', function() write('1234567890') read(10) write('long string') - end) - - it('collects each character and index', function() collect_chars() eq({{'g', 10}, {'n', 9}, {'i', 8}, {'r', 7}, {'t', 6}, {'s', 5}, {' ', 4}, {'g', 3}, {'n', 2}, {'o', 1}, {'l', 0}}, chars) @@ -264,13 +235,10 @@ describe('rbuffer functions', function() end describe('with buffer filled in two contiguous chunks', function() - before_each(function() + itp('compares the common longest sequence', function() write('1234567890') read(10) write('long string') - end) - - it('compares the common longest sequence', function() eq(0, cmp('long string')) eq(0, cmp('long strin')) eq(-1, cmp('long striM')) @@ -282,31 +250,31 @@ describe('rbuffer functions', function() end) describe('with empty buffer', function() - it('returns 0 since no characters are compared', function() + itp('returns 0 since no characters are compared', function() eq(0, cmp('')) end) end) end) describe('rbuffer_write', function() - it('fills the internal buffer and returns the write count', function() + itp('fills the internal buffer and returns the write count', function() eq(12, write('short string')) eq('short string0000', inspect()) end) - it('wont write beyond capacity', function() + itp('wont write beyond capacity', function() eq(16, write('very very long string')) eq('very very long s', inspect()) end) end) describe('rbuffer_read', function() - it('reads what was previously written', function() + itp('reads what was previously written', function() write('to read') eq('to read', read(20)) end) - it('reads nothing if the buffer is empty', function() + itp('reads nothing if the buffer is empty', function() eq('', read(20)) write('empty') eq('empty', read(20)) @@ -315,7 +283,7 @@ describe('rbuffer functions', function() end) describe('rbuffer_get', function() - it('fetch the pointer at offset, wrapping if required', function() + itp('fetch the pointer at offset, wrapping if required', function() write('1234567890') read(10) write('long string') @@ -334,7 +302,7 @@ describe('rbuffer functions', function() end) describe('wrapping behavior', function() - it('writing/reading wraps across the end of the internal buffer', function() + itp('writing/reading wraps across the end of the internal buffer', function() write('1234567890') eq('1234', read(4)) eq('5678', read(4)) |