Apply small refactor to unit tests

Redefine macro constants as enums in the ffi and import those in their
respective test modules.
This commit is contained in:
Thiago de Arruda 2014-03-04 11:26:37 -03:00
parent 0677e0ee9c
commit 34538a82f3
3 changed files with 44 additions and 31 deletions

View File

@ -3,24 +3,27 @@ ffi = require 'ffi'
-- load neovim shared library -- load neovim shared library
libnvim = ffi.load './build/src/libnvim-test.so' libnvim = ffi.load './build/src/libnvim-test.so'
-- Luajit ffi parser only understands function signatures. -- Luajit ffi parser doesn't understand preprocessor directives, so
-- This helper function normalizes headers, passes to ffi and returns the -- this helper function removes common directives before passing it the to ffi.
-- library pointer -- It will return a pointer to the library table, emulating 'requires'
cimport = (path) -> cimport = (path) ->
-- Can't parse some of vim types, perhaps need to define those before header_file = io.open path, 'rb'
-- automatically importing to ffi
-- header_file = io.open path, 'rb' if not header_file
-- header = header_file\read '*a' error "cannot find #{path}"
-- header_file.close!
-- header = string.gsub header, '#include[^\n]*\n', '' header = header_file\read '*a'
-- header = string.gsub header, '#ifndef[^\n]*\n', '' header_file.close!
-- header = string.gsub header, '#define[^\n]*\n', '' header = string.gsub header, '#include[^\n]*\n', ''
-- header = string.gsub header, '#endif[^\n]*\n', '' header = string.gsub header, '#ifndef[^\n]*\n', ''
-- ffi.cdef header header = string.gsub header, '#define[^\n]*\n', ''
header = string.gsub header, '#endif[^\n]*\n', ''
ffi.cdef header
return libnvim return libnvim
cimport './src/types.h'
-- take a pointer to a C-allocated string and return an interned -- take a pointer to a C-allocated string and return an interned
-- version while also freeing the memory -- version while also freeing the memory
internalize = (cdata) -> internalize = (cdata) ->
@ -32,4 +35,6 @@ return {
internalize: internalize internalize: internalize
eq: (expected, actual) -> assert.are.same expected, actual eq: (expected, actual) -> assert.are.same expected, actual
ffi: ffi ffi: ffi
lib: libnvim
cstr: ffi.typeof 'char[?]'
} }

View File

@ -1,13 +1,21 @@
{:cimport, :internalize, :eq, :ffi} = require 'test.unit.helpers' {:cimport, :internalize, :eq, :ffi, :lib, :cstr} = require 'test.unit.helpers'
misc1 = cimport './src/misc1.h' --misc1 = cimport './src/misc1.h'
cstr = ffi.typeof 'char[?]'
-- TODO extract constants from vim.h -- remove these statements once 'cimport' is working properly for misc1.h
misc1 = lib
ffi.cdef [[
enum FPC {
FPC_SAME = 1, FPC_DIFF = 2, FPC_NOTX = 4, FPC_DIFFX = 6, FPC_SAMEX = 7
};
int fullpathcmp(char_u *s1, char_u *s2, int checkname);
]]
-- import constants parsed by ffi
{:FPC_SAME, :FPC_DIFF, :FPC_NOTX, :FPC_DIFFX, :FPC_SAMEX} = lib
describe 'misc1 function', -> describe 'misc1 function', ->
describe 'fullpathcmp', -> describe 'fullpathcmp', ->
ffi.cdef 'int fullpathcmp(char *s1, char *s2, int checkname);'
fullpathcmp = (s1, s2, cn) -> fullpathcmp = (s1, s2, cn) ->
s1 = cstr (string.len s1) + 1, s1 s1 = cstr (string.len s1) + 1, s1
@ -17,12 +25,6 @@ describe 'misc1 function', ->
f1 = 'f1.o' f1 = 'f1.o'
f2 = 'f2.o' f2 = 'f2.o'
FPC_SAME = 1
FPC_DIFF = 2
FPC_NOTX = 4
FPC_DIFFX = 6
FPC_SAMEX = 7
before_each -> before_each ->
-- create the three files that will be used in this spec -- create the three files that will be used in this spec
(io.open f1, 'w').close! (io.open f1, 'w').close!

View File

@ -1,16 +1,22 @@
{:cimport, :internalize, :eq, :ffi} = require 'test.unit.helpers' {:cimport, :internalize, :eq, :ffi, :lib, :cstr} = require 'test.unit.helpers'
require 'lfs' require 'lfs'
fs = cimport './src/fs.h' -- fs = cimport './src/os/os.h'
cstr = ffi.typeof 'char[?]' -- remove these statements once 'cimport' is working properly for misc1.h
fs = lib
ffi.cdef [[
enum OKFAIL {
OK = 1, FAIL = 0
};
int mch_dirname(char_u *buf, int len);
]]
-- import constants parsed by ffi
{:OK, :FAIL} = lib
describe 'fs function', -> describe 'fs function', ->
export OK = 1
export FAIL = 0
describe 'mch_dirname', -> describe 'mch_dirname', ->
ffi.cdef 'int mch_dirname(char *buf, int len);'
mch_dirname = (buf, len) -> mch_dirname = (buf, len) ->
fs.mch_dirname buf, len fs.mch_dirname buf, len