Add tests for buffer.c and fileio.c

This commit is contained in:
Wayne Rowcliffe
2014-07-19 22:51:04 -05:00
parent 5941ccab76
commit 7a2ea275eb
2 changed files with 299 additions and 0 deletions

216
test/unit/buffer_spec.lua Normal file
View File

@@ -0,0 +1,216 @@
local helpers = require("test.unit.helpers")
local to_cstr = helpers.to_cstr
local eq = helpers.eq
helpers.vim_init()
local buffer = helpers.cimport("./src/nvim/buffer.h")
local window = helpers.cimport("./src/nvim/window.h")
local option = helpers.cimport("./src/nvim/option.h")
--{ Initialize the options needed for interacting with buffers
window.win_alloc_first()
option.set_init_1()
--}
describe('buffer functions', function()
local buflist_new = function(file, flags)
local c_file = to_cstr(file)
return buffer.buflist_new(c_file, c_file, 1, flags)
end
local close_buffer = function(win, buf, action, abort_if_last)
return buffer.close_buffer(win, buf, action, abort_if_last)
end
local path1 = 'test_file_path'
local path2 = 'file_path_test'
local path3 = 'path_test_file'
before_each(function()
-- create the files
io.open(path1, 'w').close()
io.open(path2, 'w').close()
io.open(path3, 'w').close()
end)
after_each(function()
os.remove(path1)
os.remove(path2)
os.remove(path3)
end)
describe('buf_valid', function()
it('should view NULL as an invalid buffer', function()
eq(0, buffer.buf_valid(NULL))
end)
it('should view an open buffer as valid', function()
local buf = buflist_new(path1, buffer.BLN_LISTED)
eq(1, buffer.buf_valid(buf))
end)
it('should view a closed and hidden buffer as valid', function()
local buf = buflist_new(path1, buffer.BLN_LISTED)
close_buffer(NULL, buf, 0, 0)
eq(1, buffer.buf_valid(buf))
end)
it('should view a closed and unloaded buffer as valid', function()
local buf = buflist_new(path1, buffer.BLN_LISTED)
close_buffer(NULL, buf, buffer.DOBUF_UNLOAD, 0)
eq(1, buffer.buf_valid(buf))
end)
it('should view a closed and wiped buffer as invalid', function()
local buf = buflist_new(path1, buffer.BLN_LISTED)
close_buffer(NULL, buf, buffer.DOBUF_WIPE, 0)
eq(0, buffer.buf_valid(buf))
end)
end)
describe('buflist_findpat', function()
local ALLOW_UNLISTED = 1
local ONLY_LISTED = 0
local buflist_findpat = function(pat, allow_unlisted)
return buffer.buflist_findpat(to_cstr(pat), NULL, allow_unlisted, 0, 0)
end
it('should find exact matches', function()
local buf = buflist_new(path1, buffer.BLN_LISTED)
eq(buf.b_fnum, buflist_findpat(path1, ONLY_LISTED))
close_buffer(NULL, buf, buffer.DOBUF_WIPE, 0)
end)
it('should prefer to match the start of a file path', function()
local buf1 = buflist_new(path1, buffer.BLN_LISTED)
local buf2 = buflist_new(path2, buffer.BLN_LISTED)
local buf3 = buflist_new(path3, buffer.BLN_LISTED)
eq(buf1.b_fnum, buflist_findpat("test", ONLY_LISTED))
eq(buf2.b_fnum, buflist_findpat("file", ONLY_LISTED))
eq(buf3.b_fnum, buflist_findpat("path", ONLY_LISTED))
close_buffer(NULL, buf1, buffer.DOBUF_WIPE, 0)
close_buffer(NULL, buf2, buffer.DOBUF_WIPE, 0)
close_buffer(NULL, buf3, buffer.DOBUF_WIPE, 0)
end)
it('should prefer to match the end of a file over the middle', function()
--{ Given: Two buffers, where 'test' appears in both
-- And: 'test' appears at the end of buf3 but in the middle of buf2
local buf2 = buflist_new(path2, buffer.BLN_LISTED)
local buf3 = buflist_new(path3, buffer.BLN_LISTED)
-- Then: buf2 is the buffer that is found
eq(buf2.b_fnum, buflist_findpat("test", ONLY_LISTED))
--}
--{ When: We close buf2
close_buffer(NULL, buf2, buffer.DOBUF_WIPE, 0)
-- And: Open buf1, which has 'file' in the middle of its name
local buf1 = buflist_new(path1, buffer.BLN_LISTED)
-- Then: buf3 is found since 'file' appears at the end of the name
eq(buf3.b_fnum, buflist_findpat("file", ONLY_LISTED))
--}
close_buffer(NULL, buf1, buffer.DOBUF_WIPE, 0)
close_buffer(NULL, buf3, buffer.DOBUF_WIPE, 0)
end)
it('should match a unique fragment of a file path', function()
local buf1 = buflist_new(path1, buffer.BLN_LISTED)
local buf2 = buflist_new(path2, buffer.BLN_LISTED)
local buf3 = buflist_new(path3, buffer.BLN_LISTED)
eq(buf3.b_fnum, buflist_findpat("_test_", ONLY_LISTED))
close_buffer(NULL, buf1, buffer.DOBUF_WIPE, 0)
close_buffer(NULL, buf2, buffer.DOBUF_WIPE, 0)
close_buffer(NULL, buf3, buffer.DOBUF_WIPE, 0)
end)
it('should include / ignore unlisted buffers based on the flag.', function()
--{ Given: A buffer
local buf3 = buflist_new(path3, buffer.BLN_LISTED)
-- Then: We should find the buffer when it is given a unique pattern
eq(buf3.b_fnum, buflist_findpat("_test_", ONLY_LISTED))
--}
--{ When: We unlist the buffer
close_buffer(NULL, buf3, buffer.DOBUF_DEL, 0)
-- Then: It should not find the buffer when searching only listed buffers
eq(-1, buflist_findpat("_test_", ONLY_LISTED))
-- And: It should find the buffer when including unlisted buffers
eq(buf3.b_fnum, buflist_findpat("_test_", ALLOW_UNLISTED))
--}
--{ When: We wipe the buffer
close_buffer(NULL, buf3, buffer.DOBUF_WIPE, 0)
-- Then: It should not find the buffer at all
eq(-1, buflist_findpat("_test_", ONLY_LISTED))
eq(-1, buflist_findpat("_test_", ALLOW_UNLISTED))
--}
end)
it('should prefer listed buffers to unlisted buffers.', function()
--{ Given: Two buffers that match a pattern
local buf1 = buflist_new(path1, buffer.BLN_LISTED)
local buf2 = buflist_new(path2, buffer.BLN_LISTED)
-- Then: The first buffer is preferred when both are listed
eq(buf1.b_fnum, buflist_findpat("test", ONLY_LISTED))
--}
--{ When: The first buffer is unlisted
close_buffer(NULL, buf1, buffer.DOBUF_DEL, 0)
-- Then: The second buffer is preferred because
-- unlisted buffers are not allowed
eq(buf2.b_fnum, buflist_findpat("test", ONLY_LISTED))
--}
--{ When: We allow unlisted buffers
-- Then: The second buffer is still preferred
-- because listed buffers are preferred to unlisted
eq(buf2.b_fnum, buflist_findpat("test", ALLOW_UNLISTED))
--}
--{ When: We unlist the second buffer
close_buffer(NULL, buf2, buffer.DOBUF_DEL, 0)
-- Then: The first buffer is preferred again
-- because buf1 matches better which takes precedence
-- when both buffers have the same listing status.
eq(buf1.b_fnum, buflist_findpat("test", ALLOW_UNLISTED))
-- And: Neither buffer is returned when ignoring unlisted
eq(-1, buflist_findpat("test", ONLY_LISTED))
--}
close_buffer(NULL, buf1, buffer.DOBUF_WIPE, 0)
close_buffer(NULL, buf2, buffer.DOBUF_WIPE, 0)
end)
end)
end)

83
test/unit/fileio_spec.lua Normal file
View File

@@ -0,0 +1,83 @@
local helpers = require("test.unit.helpers")
--{:cimport, :internalize, :eq, :neq, :ffi, :lib, :cstr, :to_cstr} = require 'test.unit.helpers'
local eq = helpers.eq
local ffi = helpers.ffi
local to_cstr = helpers.to_cstr
local fileio = helpers.cimport("./src/nvim/fileio.h")
describe('file_pat functions', function()
describe('file_pat_to_reg_pat', function()
local file_pat_to_reg_pat = function(pat)
local res = fileio.file_pat_to_reg_pat(to_cstr(pat), NULL, NULL, 0)
return ffi.string(res)
end
it('returns ^path$ regex for literal path input', function()
eq( '^path$', file_pat_to_reg_pat('path'))
end)
it('does not prepend ^ when there is a starting glob (*)', function()
eq('path$', file_pat_to_reg_pat('*path'))
end)
it('does not append $ when there is an ending glob (*)', function()
eq('^path', file_pat_to_reg_pat('path*'))
end)
it('does not include ^ or $ when surrounded by globs (*)', function()
eq('path', file_pat_to_reg_pat('*path*'))
end)
it('replaces the bash any character (?) with the regex any character (.)', function()
eq('^foo.bar$', file_pat_to_reg_pat('foo?bar'))
end)
it('replaces a glob (*) in the middle of a path with regex multiple any character (.*)',
function()
eq('^foo.*bar$', file_pat_to_reg_pat('foo*bar'))
end)
it([[unescapes \? to ?]], function()
eq('^foo?bar$', file_pat_to_reg_pat([[foo\?bar]]))
end)
it([[unescapes \% to %]], function()
eq('^foo%bar$', file_pat_to_reg_pat([[foo\%bar]]))
end)
it([[unescapes \, to ,]], function()
eq('^foo,bar$', file_pat_to_reg_pat([[foo\,bar]]))
end)
it([[unescapes '\ ' to ' ']], function()
eq('^foo bar$', file_pat_to_reg_pat([[foo\ bar]]))
end)
it([[escapes . to \.]], function()
eq([[^foo\.bar$]], file_pat_to_reg_pat('foo.bar'))
end)
it('Converts bash brace expansion {a,b} to regex options (a|b)', function()
eq([[^foo\(bar\|baz\)$]], file_pat_to_reg_pat('foo{bar,baz}'))
end)
it('Collapses multiple consecutive * into a single character', function()
eq([[^foo.*bar$]], file_pat_to_reg_pat('foo*******bar'))
eq([[foobar$]], file_pat_to_reg_pat('********foobar'))
eq([[^foobar]], file_pat_to_reg_pat('foobar********'))
end)
it('Does not escape ^', function()
eq([[^^blah$]], file_pat_to_reg_pat('^blah'))
eq([[^foo^bar$]], file_pat_to_reg_pat('foo^bar'))
end)
it('Does not escape $', function()
eq([[^blah$$]], file_pat_to_reg_pat('blah$'))
eq([[^foo$bar$]], file_pat_to_reg_pat('foo$bar'))
end)
end)
end)