aboutsummaryrefslogtreecommitdiff
path: root/test/unit/helpers.moon
blob: 77d491a0081ee343730c137726984d9d1ae50028 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
ffi = require 'ffi'

-- load neovim shared library
testlib = os.getenv 'NVIM_TEST_LIB'
unless testlib
    testlib = './build/src/libnvim-test.so'

libnvim = ffi.load testlib

-- Luajit ffi parser doesn't understand preprocessor directives, so
-- this helper function removes common directives before passing it the to ffi.
-- It will return a pointer to the library table, emulating 'requires'
cimport = (path) ->
  header_file = io.open path, 'rb'

  if not header_file
    error "cannot find #{path}"

  header = header_file\read '*a'
  header_file.close!
  header = string.gsub header, '#include[^\n]*\n', ''
  header = string.gsub header, '#ifndef[^\n]*\n', ''
  header = string.gsub header, '#define[^\n]*\n', ''
  header = string.gsub header, '#endif[^\n]*\n', ''
  ffi.cdef header

  return libnvim

testinc = os.getenv 'TEST_INCLUDES'
unless testinc
    testinc = './build/test/includes/post'

cppimport = (path) ->
  return cimport testinc .. '/' .. path

cimport './src/types.h'

-- take a pointer to a C-allocated string and return an interned
-- version while also freeing the memory
internalize = (cdata) ->
  ffi.gc cdata, ffi.C.free
  return ffi.string cdata

cstr = ffi.typeof 'char[?]'

to_cstr = (string) ->
  cstr (string.len string) + 1, string

return {
  cimport: cimport
  cppimport: cppimport
  internalize: internalize
  eq: (expected, actual) -> assert.are.same expected, actual
  neq: (expected, actual) -> assert.are_not.same expected, actual
  ffi: ffi
  lib: libnvim
  cstr: cstr
  to_cstr: to_cstr
}