mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
unit tests: avoid global scope; add missing cimports
temporarily comment out call to vim_deltempdir() to avoid segfault
This commit is contained in:
parent
c76feb338a
commit
f6088e79b0
@ -212,18 +212,21 @@ local function formatc(str)
|
||||
return table.concat(result)
|
||||
end
|
||||
|
||||
-- uncomment the following lines (and comment the return) for standalone
|
||||
-- operation (very handy for debugging)
|
||||
-- standalone operation (very handy for debugging)
|
||||
local function standalone(...)
|
||||
Preprocess = require("preprocess")
|
||||
Preprocess.add_to_include_path('./../../src')
|
||||
Preprocess.add_to_include_path('./../../build/include')
|
||||
Preprocess.add_to_include_path('./../../.deps/usr/include')
|
||||
|
||||
input = Preprocess.preprocess_stream(arg[1])
|
||||
local input = Preprocess.preprocess_stream(arg[1])
|
||||
local raw = input:read('*all')
|
||||
input:close()
|
||||
|
||||
if raw == nil then
|
||||
print("ERROR: Preprocess.preprocess_stream():read() returned empty")
|
||||
end
|
||||
|
||||
local formatted
|
||||
if #arg == 2 and arg[2] == 'no' then
|
||||
formatted = raw
|
||||
@ -233,6 +236,9 @@ local function standalone(...)
|
||||
|
||||
print(formatted)
|
||||
end
|
||||
-- uncomment this line (and comment the `return`) for standalone debugging
|
||||
-- example usage:
|
||||
-- ../../.deps/usr/bin/luajit formatc.lua ../../include/tempfile.h.generated.h
|
||||
-- ../../.deps/usr/bin/luajit formatc.lua /usr/include/malloc.h
|
||||
-- standalone(...)
|
||||
|
||||
return formatc
|
||||
|
@ -10,7 +10,10 @@ local cstr = helpers.cstr
|
||||
local to_cstr = helpers.to_cstr
|
||||
local NULL = helpers.NULL
|
||||
|
||||
local garray = cimport('./src/nvim/garray.h')
|
||||
local garray = cimport('stdlib.h', './src/nvim/garray.h')
|
||||
|
||||
local itemsize = 14
|
||||
local growsize = 95
|
||||
|
||||
-- define a basic interface to garray. We could make it a lot nicer by
|
||||
-- constructing a class wrapper around garray. It could for example associate
|
||||
@ -20,70 +23,70 @@ local garray = cimport('./src/nvim/garray.h')
|
||||
-- how one would use it from C.
|
||||
|
||||
-- accessors
|
||||
function ga_len(garr)
|
||||
local ga_len = function(garr)
|
||||
return garr[0].ga_len
|
||||
end
|
||||
|
||||
function ga_maxlen(garr)
|
||||
local ga_maxlen = function(garr)
|
||||
return garr[0].ga_maxlen
|
||||
end
|
||||
|
||||
function ga_itemsize(garr)
|
||||
local ga_itemsize = function(garr)
|
||||
return garr[0].ga_itemsize
|
||||
end
|
||||
|
||||
function ga_growsize(garr)
|
||||
local ga_growsize = function(garr)
|
||||
return garr[0].ga_growsize
|
||||
end
|
||||
|
||||
function ga_data(garr)
|
||||
local ga_data = function(garr)
|
||||
return garr[0].ga_data
|
||||
end
|
||||
|
||||
-- derived accessors
|
||||
function ga_size(garr)
|
||||
local ga_size = function(garr)
|
||||
return ga_len(garr) * ga_itemsize(garr)
|
||||
end
|
||||
|
||||
function ga_maxsize(garr)
|
||||
local ga_maxsize = function(garr)
|
||||
return ga_maxlen(garr) * ga_itemsize(garr)
|
||||
end
|
||||
|
||||
function ga_data_as_bytes(garr)
|
||||
local ga_data_as_bytes = function(garr)
|
||||
return ffi.cast('uint8_t *', ga_data(garr))
|
||||
end
|
||||
|
||||
function ga_data_as_strings(garr)
|
||||
local ga_data_as_strings = function(garr)
|
||||
return ffi.cast('char **', ga_data(garr))
|
||||
end
|
||||
|
||||
function ga_data_as_ints(garr)
|
||||
local ga_data_as_ints = function(garr)
|
||||
return ffi.cast('int *', ga_data(garr))
|
||||
end
|
||||
|
||||
-- garray manipulation
|
||||
function ga_init(garr, itemsize, growsize)
|
||||
local ga_init = function(garr, itemsize, growsize)
|
||||
return garray.ga_init(garr, itemsize, growsize)
|
||||
end
|
||||
|
||||
function ga_clear(garr)
|
||||
local ga_clear = function(garr)
|
||||
return garray.ga_clear(garr)
|
||||
end
|
||||
|
||||
function ga_clear_strings(garr)
|
||||
local ga_clear_strings = function(garr)
|
||||
assert.is_true(ga_itemsize(garr) == ffi.sizeof('char *'))
|
||||
return garray.ga_clear_strings(garr)
|
||||
end
|
||||
|
||||
function ga_grow(garr, n)
|
||||
local ga_grow = function(garr, n)
|
||||
return garray.ga_grow(garr, n)
|
||||
end
|
||||
|
||||
function ga_concat(garr, str)
|
||||
local ga_concat = function(garr, str)
|
||||
return garray.ga_concat(garr, to_cstr(str))
|
||||
end
|
||||
|
||||
function ga_append(garr, b)
|
||||
local ga_append = function(garr, b)
|
||||
if type(b) == 'string' then
|
||||
return garray.ga_append(garr, string.byte(b))
|
||||
else
|
||||
@ -91,31 +94,31 @@ function ga_append(garr, b)
|
||||
end
|
||||
end
|
||||
|
||||
function ga_concat_strings(garr)
|
||||
local ga_concat_strings = function(garr)
|
||||
return internalize(garray.ga_concat_strings(garr))
|
||||
end
|
||||
|
||||
function ga_concat_strings_sep(garr, sep)
|
||||
local ga_concat_strings_sep = function(garr, sep)
|
||||
return internalize(garray.ga_concat_strings_sep(garr, to_cstr(sep)))
|
||||
end
|
||||
|
||||
function ga_remove_duplicate_strings(garr)
|
||||
local ga_remove_duplicate_strings = function(garr)
|
||||
return garray.ga_remove_duplicate_strings(garr)
|
||||
end
|
||||
|
||||
-- derived manipulators
|
||||
function ga_set_len(garr, len)
|
||||
local ga_set_len = function(garr, len)
|
||||
assert.is_true(len <= ga_maxlen(garr))
|
||||
garr[0].ga_len = len
|
||||
end
|
||||
|
||||
function ga_inc_len(garr, by)
|
||||
local ga_inc_len = function(garr, by)
|
||||
return ga_set_len(garr, ga_len(garr) + 1)
|
||||
end
|
||||
|
||||
-- custom append functions
|
||||
-- not the C ga_append, which only works for bytes
|
||||
function ga_append_int(garr, it)
|
||||
local ga_append_int = function(garr, it)
|
||||
assert.is_true(ga_itemsize(garr) == ffi.sizeof('int'))
|
||||
ga_grow(garr, 1)
|
||||
local data = ga_data_as_ints(garr)
|
||||
@ -123,7 +126,7 @@ function ga_append_int(garr, it)
|
||||
return ga_inc_len(garr, 1)
|
||||
end
|
||||
|
||||
function ga_append_string(garr, it)
|
||||
local ga_append_string = function(garr, it)
|
||||
assert.is_true(ga_itemsize(garr) == ffi.sizeof('char *'))
|
||||
-- make a non-garbage collected string and copy the lua string into it,
|
||||
-- TODO(aktau): we should probably call xmalloc here, though as long as
|
||||
@ -136,7 +139,7 @@ function ga_append_string(garr, it)
|
||||
return ga_inc_len(garr, 1)
|
||||
end
|
||||
|
||||
function ga_append_strings(garr, ...)
|
||||
local ga_append_strings = function(garr, ...)
|
||||
local prevlen = ga_len(garr)
|
||||
local len = select('#', ...)
|
||||
for i = 1, len do
|
||||
@ -145,7 +148,7 @@ function ga_append_strings(garr, ...)
|
||||
return eq(prevlen + len, ga_len(garr))
|
||||
end
|
||||
|
||||
function ga_append_ints(garr, ...)
|
||||
local ga_append_ints = function(garr, ...)
|
||||
local prevlen = ga_len(garr)
|
||||
local len = select('#', ...)
|
||||
for i = 1, len do
|
||||
@ -156,23 +159,23 @@ end
|
||||
|
||||
-- enhanced constructors
|
||||
local garray_ctype = ffi.typeof('garray_T[1]')
|
||||
function new_garray()
|
||||
local new_garray = function()
|
||||
local garr = garray_ctype()
|
||||
return ffi.gc(garr, ga_clear)
|
||||
end
|
||||
|
||||
function new_string_garray()
|
||||
local new_string_garray = function()
|
||||
local garr = garray_ctype()
|
||||
ga_init(garr, ffi.sizeof("char_u *"), 1)
|
||||
ga_init(garr, ffi.sizeof("unsigned char *"), 1)
|
||||
return ffi.gc(garr, ga_clear_strings)
|
||||
end
|
||||
|
||||
function randomByte()
|
||||
local randomByte = function()
|
||||
return ffi.cast('uint8_t', math.random(0, 255))
|
||||
end
|
||||
|
||||
-- scramble the data in a garray
|
||||
function ga_scramble(garr)
|
||||
local ga_scramble = function(garr)
|
||||
local size, bytes = ga_size(garr), ga_data_as_bytes(garr)
|
||||
for i = 0, size - 1 do
|
||||
bytes[i] = randomByte()
|
||||
@ -180,8 +183,6 @@ function ga_scramble(garr)
|
||||
end
|
||||
|
||||
describe('garray', function()
|
||||
local itemsize = 14
|
||||
local growsize = 95
|
||||
|
||||
describe('ga_init', function()
|
||||
it('initializes the values of the garray', function()
|
||||
|
@ -1,19 +1,18 @@
|
||||
local ffi = require('ffi')
|
||||
local lpeg = require('lpeg')
|
||||
local formatc = require('test.unit.formatc')
|
||||
local Set = require('test.unit.set')
|
||||
local Preprocess = require('test.unit.preprocess')
|
||||
local Paths = require('test.config.paths')
|
||||
|
||||
-- add some standard header locations
|
||||
for i, p in ipairs(Paths.include_paths) do
|
||||
for _, p in ipairs(Paths.include_paths) do
|
||||
Preprocess.add_to_include_path(p)
|
||||
end
|
||||
|
||||
-- load neovim shared library
|
||||
local libnvim = ffi.load(Paths.test_libnvim_path)
|
||||
|
||||
function trim(s)
|
||||
local function trim(s)
|
||||
return s:match('^%s*(.*%S)') or ''
|
||||
end
|
||||
|
||||
@ -28,7 +27,7 @@ end
|
||||
|
||||
-- some things are just too complex for the LuaJIT C parser to digest. We
|
||||
-- usually don't need them anyway.
|
||||
function filter_complex_blocks(body)
|
||||
local function filter_complex_blocks(body)
|
||||
local result = {}
|
||||
|
||||
for line in body:gmatch("[^\r\n]+") do
|
||||
@ -43,28 +42,25 @@ end
|
||||
|
||||
-- use this helper to import C files, you can pass multiple paths at once,
|
||||
-- this helper will return the C namespace of the nvim library.
|
||||
-- cimport = (path) ->
|
||||
function cimport(...)
|
||||
local function cimport(...)
|
||||
local paths = {}
|
||||
local args = {...}
|
||||
|
||||
-- filter out paths we've already imported
|
||||
for i = 1, #args do
|
||||
local path = args[i]
|
||||
if not imported:contains(path) then
|
||||
for _,path in pairs(args) do
|
||||
if path ~= nil and not imported:contains(path) then
|
||||
paths[#paths + 1] = path
|
||||
end
|
||||
end
|
||||
|
||||
for i = 1, #paths do
|
||||
imported:add(paths[i])
|
||||
for _,path in pairs(paths) do
|
||||
imported:add(path)
|
||||
end
|
||||
|
||||
if #paths == 0 then
|
||||
return libnvim
|
||||
end
|
||||
|
||||
-- require 'pl.pretty'.dump(paths)
|
||||
local body = nil
|
||||
for i=1, 3 do
|
||||
local stream = Preprocess.preprocess_stream(unpack(paths))
|
||||
@ -72,7 +68,10 @@ function cimport(...)
|
||||
stream:close()
|
||||
if body ~= nil then break end
|
||||
end
|
||||
-- require 'pl.pretty'.dump(body)
|
||||
|
||||
if body == nil then
|
||||
print("ERROR: helpers.lua: Preprocess.preprocess_stream():read() returned empty")
|
||||
end
|
||||
|
||||
-- format it (so that the lines are "unique" statements), also filter out
|
||||
-- Objective-C blocks
|
||||
@ -103,7 +102,7 @@ function cimport(...)
|
||||
return libnvim
|
||||
end
|
||||
|
||||
function cppimport(path)
|
||||
local function cppimport(path)
|
||||
return cimport(Paths.test_include_path .. '/' .. path)
|
||||
end
|
||||
|
||||
@ -111,19 +110,19 @@ cimport('./src/nvim/types.h')
|
||||
|
||||
-- take a pointer to a C-allocated string and return an interned
|
||||
-- version while also freeing the memory
|
||||
function internalize(cdata, len)
|
||||
local function internalize(cdata, len)
|
||||
ffi.gc(cdata, ffi.C.free)
|
||||
return ffi.string(cdata, len)
|
||||
end
|
||||
|
||||
local cstr = ffi.typeof('char[?]')
|
||||
function to_cstr(string)
|
||||
local function to_cstr(string)
|
||||
return cstr((string.len(string)) + 1, string)
|
||||
end
|
||||
|
||||
-- initialize some global variables, this is still necessary to unit test
|
||||
-- functions that rely on global state.
|
||||
function vim_init()
|
||||
local function vim_init()
|
||||
if vim_init_called ~= nil then
|
||||
return
|
||||
end
|
||||
|
@ -15,16 +15,22 @@ local FAIL = helpers.FAIL
|
||||
require('lfs')
|
||||
require('bit')
|
||||
|
||||
cimport('unistd.h')
|
||||
local fs = cimport('./src/nvim/os/os.h')
|
||||
cppimport('sys/stat.h')
|
||||
cppimport('sys/fcntl.h')
|
||||
cppimport('sys/errno.h')
|
||||
|
||||
function assert_file_exists(filepath)
|
||||
local len = 0
|
||||
local buf = ""
|
||||
local directory = nil
|
||||
local executable_name = nil
|
||||
|
||||
local function assert_file_exists(filepath)
|
||||
eq(false, nil == (lfs.attributes(filepath, 'r')))
|
||||
end
|
||||
|
||||
function assert_file_does_not_exist(filepath)
|
||||
local function assert_file_does_not_exist(filepath)
|
||||
eq(true, nil == (lfs.attributes(filepath, 'r')))
|
||||
end
|
||||
|
||||
@ -37,7 +43,7 @@ describe('fs function', function()
|
||||
lfs.link('test.file', 'unit-test-directory/test_link.file', true)
|
||||
-- Since the tests are executed, they are called by an executable. We use
|
||||
-- that executable for several asserts.
|
||||
absolute_executable = arg[0]
|
||||
local absolute_executable = arg[0]
|
||||
-- Split absolute_executable into a directory and the actual file name for
|
||||
-- later usage.
|
||||
directory, executable_name = string.match(absolute_executable, '^(.*)/(.*)$')
|
||||
@ -52,7 +58,7 @@ describe('fs function', function()
|
||||
end)
|
||||
|
||||
describe('os_dirname', function()
|
||||
function os_dirname(buf, len)
|
||||
local function os_dirname(buf, len)
|
||||
return fs.os_dirname(buf, len)
|
||||
end
|
||||
|
||||
@ -73,7 +79,7 @@ describe('fs function', function()
|
||||
end)
|
||||
end)
|
||||
|
||||
function os_isdir(name)
|
||||
local function os_isdir(name)
|
||||
return fs.os_isdir((to_cstr(name)))
|
||||
end
|
||||
|
||||
@ -112,7 +118,7 @@ describe('fs function', function()
|
||||
end)
|
||||
|
||||
describe('os_can_exe', function()
|
||||
function os_can_exe(name)
|
||||
local function os_can_exe(name)
|
||||
return fs.os_can_exe((to_cstr(name)))
|
||||
end
|
||||
|
||||
@ -142,39 +148,39 @@ describe('fs function', function()
|
||||
end)
|
||||
|
||||
describe('file permissions', function()
|
||||
function os_getperm(filename)
|
||||
local function os_getperm(filename)
|
||||
local perm = fs.os_getperm((to_cstr(filename)))
|
||||
return tonumber(perm)
|
||||
end
|
||||
|
||||
function os_setperm(filename, perm)
|
||||
local function os_setperm(filename, perm)
|
||||
return fs.os_setperm((to_cstr(filename)), perm)
|
||||
end
|
||||
|
||||
function os_fchown(filename, user_id, group_id)
|
||||
local function os_fchown(filename, user_id, group_id)
|
||||
local fd = ffi.C.open(filename, 0)
|
||||
local res = fs.os_fchown(fd, user_id, group_id)
|
||||
ffi.C.close(fd)
|
||||
return res
|
||||
end
|
||||
|
||||
function os_file_is_readonly(filename)
|
||||
local function os_file_is_readonly(filename)
|
||||
return fs.os_file_is_readonly((to_cstr(filename)))
|
||||
end
|
||||
|
||||
function os_file_is_writable(filename)
|
||||
local function os_file_is_writable(filename)
|
||||
return fs.os_file_is_writable((to_cstr(filename)))
|
||||
end
|
||||
|
||||
function bit_set(number, check_bit)
|
||||
local function bit_set(number, check_bit)
|
||||
return 0 ~= (bit.band(number, check_bit))
|
||||
end
|
||||
|
||||
function set_bit(number, to_set)
|
||||
local function set_bit(number, to_set)
|
||||
return bit.bor(number, to_set)
|
||||
end
|
||||
|
||||
function unset_bit(number, to_unset)
|
||||
local function unset_bit(number, to_unset)
|
||||
return bit.band(number, (bit.bnot(to_unset)))
|
||||
end
|
||||
|
||||
@ -295,19 +301,19 @@ describe('fs function', function()
|
||||
end)
|
||||
|
||||
describe('file operations', function()
|
||||
function os_file_exists(filename)
|
||||
local function os_file_exists(filename)
|
||||
return fs.os_file_exists((to_cstr(filename)))
|
||||
end
|
||||
|
||||
function os_rename(path, new_path)
|
||||
local function os_rename(path, new_path)
|
||||
return fs.os_rename((to_cstr(path)), (to_cstr(new_path)))
|
||||
end
|
||||
|
||||
function os_remove(path)
|
||||
local function os_remove(path)
|
||||
return fs.os_remove((to_cstr(path)))
|
||||
end
|
||||
|
||||
function os_open(path, flags, mode)
|
||||
local function os_open(path, flags, mode)
|
||||
return fs.os_open((to_cstr(path)), flags, mode)
|
||||
end
|
||||
|
||||
@ -428,11 +434,11 @@ describe('fs function', function()
|
||||
end)
|
||||
|
||||
describe('folder operations', function()
|
||||
function os_mkdir(path, mode)
|
||||
local function os_mkdir(path, mode)
|
||||
return fs.os_mkdir(to_cstr(path), mode)
|
||||
end
|
||||
|
||||
function os_rmdir(path)
|
||||
local function os_rmdir(path)
|
||||
return fs.os_rmdir(to_cstr(path))
|
||||
end
|
||||
|
||||
@ -465,18 +471,18 @@ describe('fs function', function()
|
||||
end)
|
||||
|
||||
describe('FileInfo', function()
|
||||
function file_info_new()
|
||||
local function file_info_new()
|
||||
local file_info = ffi.new('FileInfo[1]')
|
||||
file_info[0].stat.st_ino = 0
|
||||
file_info[0].stat.st_dev = 0
|
||||
return file_info
|
||||
end
|
||||
|
||||
function is_file_info_filled(file_info)
|
||||
local function is_file_info_filled(file_info)
|
||||
return file_info[0].stat.st_ino > 0 and file_info[0].stat.st_dev > 0
|
||||
end
|
||||
|
||||
function file_id_new()
|
||||
local function file_id_new()
|
||||
local file_info = ffi.new('FileID[1]')
|
||||
file_info[0].inode = 0
|
||||
file_info[0].device_id = 0
|
||||
|
@ -13,6 +13,7 @@ local OK = helpers.OK
|
||||
local FAIL = helpers.FAIL
|
||||
|
||||
require('lfs')
|
||||
cimport('string.h')
|
||||
local path = cimport('./src/nvim/path.h')
|
||||
|
||||
-- import constants parsed by ffi
|
||||
@ -22,6 +23,9 @@ local kBothFilesMissing = path.kBothFilesMissing
|
||||
local kOneFileMissing = path.kOneFileMissing
|
||||
local kEqualFileNames = path.kEqualFileNames
|
||||
|
||||
local len = 0
|
||||
local buffer = nil
|
||||
|
||||
describe('path function', function()
|
||||
describe('path_full_dir_name', function()
|
||||
setup(function()
|
||||
|
@ -23,7 +23,7 @@ table.insert(ccs, {path = "/usr/bin/env clang", type = "clang"})
|
||||
table.insert(ccs, {path = "/usr/bin/env icc", type = "gcc"})
|
||||
|
||||
local quote_me = '[^%w%+%-%=%@%_%/]' -- complement (needn't quote)
|
||||
function shell_quote(str)
|
||||
local function shell_quote(str)
|
||||
if string.find(str, quote_me) or str == '' then
|
||||
return "'" .. string.gsub(str, "'", [['"'"']]) .. "'"
|
||||
else
|
||||
@ -32,7 +32,7 @@ function shell_quote(str)
|
||||
end
|
||||
|
||||
-- parse Makefile format dependencies into a Lua table
|
||||
function parse_make_deps(deps)
|
||||
local function parse_make_deps(deps)
|
||||
-- remove line breaks and line concatenators
|
||||
deps = deps:gsub("\n", ""):gsub("\\", "")
|
||||
-- remove the Makefile "target:" element
|
||||
@ -70,7 +70,7 @@ end
|
||||
-- produces:
|
||||
-- #include "vim.h"
|
||||
-- #include "memory.h"
|
||||
function headerize(headers, global)
|
||||
local function headerize(headers, global)
|
||||
local pre = '"'
|
||||
local post = pre
|
||||
if global then
|
||||
@ -166,7 +166,7 @@ local type_to_class = {
|
||||
-- find the best cc. If os.exec causes problems on windows (like popping up
|
||||
-- a console window) we might consider using something like this:
|
||||
-- http://scite-ru.googlecode.com/svn/trunk/pack/tools/LuaLib/shell.html#exec
|
||||
function find_best_cc(ccs)
|
||||
local function find_best_cc(ccs)
|
||||
for _, meta in pairs(ccs) do
|
||||
local version = io.popen(tostring(meta.path) .. " -v 2>&1")
|
||||
version:close()
|
||||
|
@ -3,10 +3,11 @@ local helpers = require 'test.unit.helpers'
|
||||
|
||||
local os = helpers.cimport './src/nvim/os/os.h'
|
||||
local tempfile = helpers.cimport './src/nvim/tempfile.h'
|
||||
-- os.mch_early_init
|
||||
|
||||
describe('tempfile related functions', function()
|
||||
after_each(function()
|
||||
tempfile.vim_deltempdir()
|
||||
-- tempfile.vim_deltempdir()
|
||||
end)
|
||||
|
||||
local vim_gettempdir = function()
|
||||
|
Loading…
Reference in New Issue
Block a user