unittests: Try using syscall library instead (ffi-based)

This commit is contained in:
ZyX
2016-11-01 09:50:49 +03:00
parent b92d6aaf0d
commit 9f29a76cab

View File

@@ -4,7 +4,6 @@ local Set = require('test.unit.set')
local Preprocess = require('test.unit.preprocess') local Preprocess = require('test.unit.preprocess')
local Paths = require('test.config.paths') local Paths = require('test.config.paths')
local global_helpers = require('test.helpers') local global_helpers = require('test.helpers')
local posix = require('posix')
local assert = require('luassert') local assert = require('luassert')
local say = require('say') local say = require('say')
@@ -56,6 +55,10 @@ end
local previous_defines = '' local previous_defines = ''
local cdef = ffi.cdef
local cimportstr
-- use this helper to import C files, you can pass multiple paths at once, -- 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. -- this helper will return the C namespace of the nvim library.
local function cimport(...) local function cimport(...)
@@ -80,6 +83,10 @@ local function cimport(...)
local body local body
body, previous_defines = Preprocess.preprocess(previous_defines, unpack(paths)) body, previous_defines = Preprocess.preprocess(previous_defines, unpack(paths))
return cimportstr(body)
end
cimportstr = function(body)
-- format it (so that the lines are "unique" statements), also filter out -- format it (so that the lines are "unique" statements), also filter out
-- Objective-C blocks -- Objective-C blocks
if os.getenv('NVIM_TEST_PRINT_I') == '1' then if os.getenv('NVIM_TEST_PRINT_I') == '1' then
@@ -89,6 +96,8 @@ local function cimport(...)
print(lnum, line) print(lnum, line)
end end
end end
body = body:gsub('//.*', '')
body = body:gsub('/%*.*%*/', '')
body = formatc(body) body = formatc(body)
body = filter_complex_blocks(body) body = filter_complex_blocks(body)
@@ -125,11 +134,15 @@ local function cimport(...)
print(lnum, line) print(lnum, line)
end end
end end
ffi.cdef(table.concat(new_lines, "\n")) cdef(table.concat(new_lines, "\n"))
return libnvim return libnvim
end end
ffi.cdef = cimportstr
local syscall = require('syscall')
ffi.cdef = cdef
local function cppimport(path) local function cppimport(path)
return cimport(Paths.test_include_path .. '/' .. path) return cimport(Paths.test_include_path .. '/' .. path)
end end
@@ -219,6 +232,39 @@ do
main.event_init() main.event_init()
end end
local sc
if posix ~= nil then
sc = {
fork = posix.fork,
pipe = posix.pipe,
read = posix.read,
write = posix.write,
close = posix.close,
wait = posix.wait,
exit = posix._exit,
}
else
sc = {
fork = syscall.fork,
pipe = function()
local ret = {syscall.pipe()}
return ret[3], ret[4]
end,
read = function(rd, len)
return rd:read(nil, len)
end,
write = function(wr, s)
return wr:write(s)
end,
close = function(p)
return p:close()
end,
wait = syscall.wait,
exit = syscall.exit,
}
end
local function gen_itp(it) local function gen_itp(it)
local function just_fail(_) local function just_fail(_)
return false return false
@@ -230,36 +276,36 @@ local function gen_itp(it)
'assertion.just_fail.negative') 'assertion.just_fail.negative')
local function itp(name, func) local function itp(name, func)
it(name, function() it(name, function()
local rd, wr = posix.pipe() local rd, wr = sc.pipe()
local pid = posix.fork() local pid = sc.fork()
if pid == 0 then if pid == 0 then
posix.close(rd) sc.close(rd)
collectgarbage('stop') collectgarbage('stop')
local err, emsg = pcall(func) local err, emsg = pcall(func)
collectgarbage('restart') collectgarbage('restart')
emsg = tostring(emsg) emsg = tostring(emsg)
if not err then if not err then
posix.write(wr, ('-\n%05u\n%s'):format(#emsg, emsg)) sc.write(wr, ('-\n%05u\n%s'):format(#emsg, emsg))
posix.close(wr) sc.close(wr)
posix._exit(1) sc.exit(1)
else else
posix.write(wr, '+\n') sc.write(wr, '+\n')
posix.close(wr) sc.close(wr)
posix._exit(0) sc.exit(0)
end end
else else
posix.close(wr) sc.close(wr)
posix.wait(pid) sc.wait(pid)
local res = posix.read(rd, 2) local res = sc.read(rd, 2)
eq(2, #res) eq(2, #res)
if res == '+\n' then if res == '+\n' then
return return
end end
eq('-\n', res) eq('-\n', res)
local len_s = posix.read(rd, 5) local len_s = sc.read(rd, 5)
local len = tonumber(len_s) local len = tonumber(len_s)
neq(0, len) neq(0, len)
local err = posix.read(rd, len + 1) local err = sc.read(rd, len + 1)
assert.just_fail(err) assert.just_fail(err)
end end
end) end)