mirror of
				https://github.com/neovim/neovim.git
				synced 2025-10-26 12:27:24 +00:00 
			
		
		
		
	unittests: Use more adequate names for some functions
This commit is contained in:
		| @@ -38,13 +38,14 @@ local function only_separate(func) | ||||
|     return func(...) | ||||
|   end | ||||
| end | ||||
| local deferred_calls_init = {} | ||||
| local deferred_calls_mod = nil | ||||
| local function deferred_call(func, ret) | ||||
| local child_calls_init = {} | ||||
| local child_calls_mod = nil | ||||
| local child_calls_mod_once = nil | ||||
| local function child_call(func, ret) | ||||
|   return function(...) | ||||
|     local deferred_calls = deferred_calls_mod or deferred_calls_init | ||||
|     local child_calls = child_calls_mod or child_calls_init | ||||
|     if child_pid ~= 0 then | ||||
|       deferred_calls[#deferred_calls + 1] = {func=func, args={...}} | ||||
|       child_calls[#child_calls + 1] = {func=func, args={...}} | ||||
|       return ret | ||||
|     else | ||||
|       return func(...) | ||||
| @@ -52,15 +53,27 @@ local function deferred_call(func, ret) | ||||
|   end | ||||
| end | ||||
|  | ||||
| local separate_cleanups_mod = nil | ||||
| local function separate_cleanup(func) | ||||
|   return function(...) | ||||
|     local separate_cleanups = separate_cleanups_mod | ||||
|     if child_pid ~= 0 then | ||||
|       separate_cleanups[#separate_cleanups + 1] = {args={...}} | ||||
|     else | ||||
|       func(...) | ||||
|     end | ||||
| -- Run some code at the start of the child process, before running the test | ||||
| -- itself. Is supposed to be run in `before_each`. | ||||
| local function child_call_once(func, ...) | ||||
|   if child_pid ~= 0 then | ||||
|     child_calls_mod_once[#child_calls_mod_once + 1] = { | ||||
|       func=func, args={...}} | ||||
|   else | ||||
|     func(...) | ||||
|   end | ||||
| end | ||||
|  | ||||
| local child_cleanups_mod_once = nil | ||||
|  | ||||
| -- Run some code at the end of the child process, before exiting. Is supposed to | ||||
| -- be run in `before_each` because `after_each` is run after child has exited. | ||||
| local function child_cleanup_once(func, ...) | ||||
|   local child_cleanups = child_cleanups_mod_once | ||||
|   if child_pid ~= 0 then | ||||
|     child_cleanups[#child_cleanups + 1] = {func=func, args={...}} | ||||
|   else | ||||
|     func(...) | ||||
|   end | ||||
| end | ||||
|  | ||||
| @@ -70,7 +83,7 @@ local lib = setmetatable({}, { | ||||
|   __index = only_separate(function(_, idx) | ||||
|     return libnvim[idx] | ||||
|   end), | ||||
|   __newindex = deferred_call(function(_, idx, val) | ||||
|   __newindex = child_call(function(_, idx, val) | ||||
|     libnvim[idx] = val | ||||
|   end), | ||||
| }) | ||||
| @@ -78,24 +91,31 @@ local lib = setmetatable({}, { | ||||
| local init = only_separate(function() | ||||
|   -- load neovim shared library | ||||
|   libnvim = ffi.load(Paths.test_libnvim_path) | ||||
|   for _, c in ipairs(deferred_calls_init) do | ||||
|   for _, c in ipairs(child_calls_init) do | ||||
|     c.func(unpack(c.args)) | ||||
|   end | ||||
|   libnvim.time_init() | ||||
|   libnvim.early_init() | ||||
|   libnvim.event_init() | ||||
|   if deferred_calls_mod then | ||||
|     for _, c in ipairs(deferred_calls_mod) do | ||||
|   if child_calls_mod then | ||||
|     for _, c in ipairs(child_calls_mod) do | ||||
|       c.func(unpack(c.args)) | ||||
|     end | ||||
|   end | ||||
|   if child_calls_mod_once then | ||||
|     for _, c in ipairs(child_calls_mod_once) do | ||||
|       c.func(unpack(c.args)) | ||||
|     end | ||||
|     child_calls_mod_once = nil | ||||
|   end | ||||
| end) | ||||
|  | ||||
| local deinit = only_separate(function() | ||||
|   if separate_cleanups_mod then | ||||
|     for _, c in ipairs(separate_cleanups_mod) do | ||||
|   if child_cleanups_mod_once then | ||||
|     for _, c in ipairs(child_cleanups_mod_once) do | ||||
|       c.func(unpack(c.args)) | ||||
|     end | ||||
|     child_cleanups_mod_once = nil | ||||
|   end | ||||
| end) | ||||
|  | ||||
| @@ -188,7 +208,7 @@ local cimport_immediate = function(...) | ||||
|   end | ||||
| end | ||||
|  | ||||
| cimportstr = deferred_call(function(preprocess_cache, path) | ||||
| cimportstr = child_call(function(preprocess_cache, path) | ||||
|   if imported:contains(path) then | ||||
|     return lib | ||||
|   end | ||||
| @@ -247,7 +267,7 @@ local function alloc_log_new() | ||||
|       end | ||||
|     end | ||||
|   end | ||||
|   log.save_original_functions = deferred_call(log.save_original_functions) | ||||
|   log.save_original_functions = child_call(log.save_original_functions) | ||||
|   function log:set_mocks() | ||||
|     for _, k in ipairs(allocator_functions) do | ||||
|       do | ||||
| @@ -274,7 +294,7 @@ local function alloc_log_new() | ||||
|       end | ||||
|     end | ||||
|   end | ||||
|   log.set_mocks = deferred_call(log.set_mocks) | ||||
|   log.set_mocks = child_call(log.set_mocks) | ||||
|   function log:clear() | ||||
|     self.log = {} | ||||
|   end | ||||
| @@ -457,8 +477,9 @@ if os.getenv('NVIM_TEST_PRINT_SYSCALLS') == '1' then | ||||
| end | ||||
|  | ||||
| local function gen_itp(it) | ||||
|   deferred_calls_mod = {} | ||||
|   separate_cleanups_mod = {} | ||||
|   child_calls_mod = {} | ||||
|   child_calls_mod_once = {} | ||||
|   child_cleanups_mod_once = {} | ||||
|   preprocess_cache_mod = map(function(v) return v end, preprocess_cache_init) | ||||
|   previous_defines_mod = previous_defines_init | ||||
|   local function just_fail(_) | ||||
| @@ -551,8 +572,8 @@ local module = { | ||||
|   alloc_log_new = alloc_log_new, | ||||
|   gen_itp = gen_itp, | ||||
|   only_separate = only_separate, | ||||
|   deferred_call = deferred_call, | ||||
|   separate_cleanup = separate_cleanup, | ||||
|   child_call_once = child_call_once, | ||||
|   child_cleanup_once = child_cleanup_once, | ||||
| } | ||||
| return function(after_each) | ||||
|   if after_each then | ||||
|   | ||||
| @@ -1,7 +1,7 @@ | ||||
| local helpers = require("test.unit.helpers")(after_each) | ||||
| local itp = helpers.gen_itp(it) | ||||
|  | ||||
| local deferred_call = helpers.deferred_call | ||||
| local child_call_once = helpers.child_call_once | ||||
| local cimport = helpers.cimport | ||||
| local ffi = helpers.ffi | ||||
| local eq = helpers.eq | ||||
| @@ -23,21 +23,23 @@ describe("multiqueue (multi-level event-queue)", function() | ||||
|     multiqueue.multiqueue_free(q) | ||||
|   end | ||||
|  | ||||
|   before_each(deferred_call(function() | ||||
|     parent = multiqueue.multiqueue_new_parent(ffi.NULL, ffi.NULL) | ||||
|     child1 = multiqueue.multiqueue_new_child(parent) | ||||
|     child2 = multiqueue.multiqueue_new_child(parent) | ||||
|     child3 = multiqueue.multiqueue_new_child(parent) | ||||
|     put(child1, 'c1i1') | ||||
|     put(child1, 'c1i2') | ||||
|     put(child2, 'c2i1') | ||||
|     put(child1, 'c1i3') | ||||
|     put(child2, 'c2i2') | ||||
|     put(child2, 'c2i3') | ||||
|     put(child2, 'c2i4') | ||||
|     put(child3, 'c3i1') | ||||
|     put(child3, 'c3i2') | ||||
|   end)) | ||||
|   before_each(function() | ||||
|     child_call_once(function() | ||||
|       parent = multiqueue.multiqueue_new_parent(ffi.NULL, ffi.NULL) | ||||
|       child1 = multiqueue.multiqueue_new_child(parent) | ||||
|       child2 = multiqueue.multiqueue_new_child(parent) | ||||
|       child3 = multiqueue.multiqueue_new_child(parent) | ||||
|       put(child1, 'c1i1') | ||||
|       put(child1, 'c1i2') | ||||
|       put(child2, 'c2i1') | ||||
|       put(child1, 'c1i3') | ||||
|       put(child2, 'c2i2') | ||||
|       put(child2, 'c2i3') | ||||
|       put(child2, 'c2i4') | ||||
|       put(child3, 'c3i1') | ||||
|       put(child3, 'c3i2') | ||||
|     end) | ||||
|   end) | ||||
|  | ||||
|   itp('keeps count of added events', function() | ||||
|     eq(3, multiqueue.multiqueue_size(child1)) | ||||
|   | ||||
| @@ -5,7 +5,7 @@ local eq = helpers.eq | ||||
| local ffi = helpers.ffi | ||||
| local cstr = helpers.cstr | ||||
| local to_cstr = helpers.to_cstr | ||||
| local deferred_call = helpers.deferred_call | ||||
| local child_call_once = helpers.child_call_once | ||||
|  | ||||
| local rbuffer = helpers.cimport("./test/unit/fixtures/rbuffer.h") | ||||
|  | ||||
| @@ -32,11 +32,13 @@ describe('rbuffer functions', function() | ||||
|     return ffi.string(rbuffer.rbuffer_get(rbuf, idx), 1) | ||||
|   end | ||||
|  | ||||
|   before_each(deferred_call(function() | ||||
|     rbuf = ffi.gc(rbuffer.rbuffer_new(capacity), rbuffer.rbuffer_free) | ||||
|     -- fill the internal buffer with the character '0' to simplify inspecting | ||||
|     ffi.C.memset(rbuf.start_ptr, string.byte('0'), capacity) | ||||
|   end)) | ||||
|   before_each(function() | ||||
|     child_call_once(function() | ||||
|       rbuf = ffi.gc(rbuffer.rbuffer_new(capacity), rbuffer.rbuffer_free) | ||||
|       -- fill the internal buffer with the character '0' to simplify inspecting | ||||
|       ffi.C.memset(rbuf.start_ptr, string.byte('0'), capacity) | ||||
|     end) | ||||
|   end) | ||||
|  | ||||
|   describe('RBUFFER_UNTIL_FULL', function() | ||||
|     local chunks | ||||
|   | ||||
| @@ -5,17 +5,18 @@ local itp = helpers.gen_itp(it) | ||||
| local eq = helpers.eq | ||||
| local neq = helpers.neq | ||||
| local cimport = helpers.cimport | ||||
| local deferred_call = helpers.deferred_call | ||||
| local separate_cleanup = helpers.separate_cleanup | ||||
| local child_call_once = helpers.child_call_once | ||||
| local child_cleanup_once = helpers.child_cleanup_once | ||||
|  | ||||
| local lib = cimport('./src/nvim/os/os.h', './src/nvim/fileio.h') | ||||
|  | ||||
| describe('tempfile related functions', function() | ||||
|   before_each(deferred_call(function() | ||||
|     lib.vim_deltempdir() | ||||
|   end)) | ||||
|   separate_cleanup(function() | ||||
|     lib.vim_deltempdir() | ||||
|   before_each(function() | ||||
|     local function vim_deltempdir() | ||||
|       lib.vim_deltempdir() | ||||
|     end | ||||
|     child_call_once(vim_deltempdir) | ||||
|     child_cleanup_once(vim_deltempdir) | ||||
|   end) | ||||
|  | ||||
|   local vim_gettempdir = function() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 ZyX
					ZyX