diff options
Diffstat (limited to 'test/unit')
-rw-r--r-- | test/unit/fixtures/queue.c | 16 | ||||
-rw-r--r-- | test/unit/fixtures/queue.h | 4 | ||||
-rw-r--r-- | test/unit/os/fs_spec.lua | 70 | ||||
-rw-r--r-- | test/unit/queue_spec.lua | 123 |
4 files changed, 187 insertions, 26 deletions
diff --git a/test/unit/fixtures/queue.c b/test/unit/fixtures/queue.c new file mode 100644 index 0000000000..bbb6274b21 --- /dev/null +++ b/test/unit/fixtures/queue.c @@ -0,0 +1,16 @@ +#include <string.h> +#include <stdlib.h> +#include "nvim/event/queue.h" +#include "queue.h" + + +void ut_queue_put(Queue *queue, const char *str) +{ + queue_put(queue, NULL, 1, str); +} + +const char *ut_queue_get(Queue *queue) +{ + Event event = queue_get(queue); + return event.argv[0]; +} diff --git a/test/unit/fixtures/queue.h b/test/unit/fixtures/queue.h new file mode 100644 index 0000000000..ae949c9f29 --- /dev/null +++ b/test/unit/fixtures/queue.h @@ -0,0 +1,4 @@ +#include "nvim/event/queue.h" + +void ut_queue_put(Queue *queue, const char *str); +const char *ut_queue_get(Queue *queue); diff --git a/test/unit/os/fs_spec.lua b/test/unit/os/fs_spec.lua index 20aca9109e..28e831229f 100644 --- a/test/unit/os/fs_spec.lua +++ b/test/unit/os/fs_spec.lua @@ -32,6 +32,14 @@ local directory = nil local absolute_executable = nil local executable_name = nil +local function set_bit(number, to_set) + return bit.bor(number, to_set) +end + +local function unset_bit(number, to_unset) + return bit.band(number, (bit.bnot(to_unset))) +end + local function assert_file_exists(filepath) neq(nil, lfs.attributes(filepath)) end @@ -40,11 +48,24 @@ local function assert_file_does_not_exist(filepath) eq(nil, lfs.attributes(filepath)) end +local function os_setperm(filename, perm) + return fs.os_setperm((to_cstr(filename)), perm) +end + +local function os_getperm(filename) + local perm = fs.os_getperm((to_cstr(filename))) + return tonumber(perm) +end + describe('fs function', function() + local orig_test_file_perm setup(function() lfs.mkdir('unit-test-directory'); + io.open('unit-test-directory/test.file', 'w').close() + orig_test_file_perm = os_getperm('unit-test-directory/test.file') + io.open('unit-test-directory/test_2.file', 'w').close() lfs.link('test.file', 'unit-test-directory/test_link.file', true) -- Since the tests are executed, they are called by an executable. We use @@ -188,6 +209,10 @@ describe('fs function', function() end) describe('file permissions', function() + before_each(function() + os_setperm('unit-test-directory/test.file', orig_test_file_perm) + end) + local function os_getperm(filename) local perm = fs.os_getperm((to_cstr(filename))) return tonumber(perm) @@ -204,8 +229,8 @@ describe('fs function', function() return res end - local function os_file_is_readonly(filename) - return fs.os_file_is_readonly((to_cstr(filename))) + local function os_file_is_readable(filename) + return fs.os_file_is_readable((to_cstr(filename))) end local function os_file_is_writable(filename) @@ -216,14 +241,6 @@ describe('fs function', function() return 0 ~= (bit.band(number, check_bit)) end - local function set_bit(number, to_set) - return bit.bor(number, to_set) - end - - local function unset_bit(number, to_unset) - return bit.band(number, (bit.bnot(to_unset))) - end - describe('os_getperm', function() it('returns -1 when the given file does not exist', function() eq(-1, (os_getperm('non-existing-file'))) @@ -270,7 +287,7 @@ describe('fs function', function() -- Some systems may not have `id` utility. if (os.execute('id -G > /dev/null 2>&1') ~= 0) then - pending('skipped (missing `id` utility)') + pending('skipped (missing `id` utility)', function() end) else it('owner of a file may change the group of the file to any group of which that owner is a member', function() local file_gid = lfs.attributes(filename, 'gid') @@ -293,10 +310,8 @@ describe('fs function', function() end) end - -- On Windows `os_fchown` always returns 0 - -- because `uv_fs_chown` is no-op on this platform. if (ffi.os == 'Windows' or ffi.C.geteuid() == 0) then - pending('skipped (os_fchown is no-op on Windows)') + pending('skipped (uv_fs_chown is no-op on Windows)', function() end) else it('returns nonzero if process has not enough permissions', function() -- chown to root @@ -305,33 +320,36 @@ describe('fs function', function() end end) - describe('os_file_is_readonly', function() - it('returns true if the file is readonly', function() + + describe('os_file_is_readable', function() + it('returns false if the file is not readable', function() local perm = os_getperm('unit-test-directory/test.file') - local perm_orig = perm - perm = unset_bit(perm, ffi.C.kS_IWUSR) - perm = unset_bit(perm, ffi.C.kS_IWGRP) - perm = unset_bit(perm, ffi.C.kS_IWOTH) + perm = unset_bit(perm, ffi.C.kS_IRUSR) + perm = unset_bit(perm, ffi.C.kS_IRGRP) + perm = unset_bit(perm, ffi.C.kS_IROTH) eq(OK, (os_setperm('unit-test-directory/test.file', perm))) - eq(true, os_file_is_readonly('unit-test-directory/test.file')) - eq(OK, (os_setperm('unit-test-directory/test.file', perm_orig))) + eq(false, os_file_is_readable('unit-test-directory/test.file')) + end) + + it('returns false if the file does not exist', function() + eq(false, os_file_is_readable( + 'unit-test-directory/what_are_you_smoking.gif')) end) - it('returns false if the file is writable', function() - eq(false, os_file_is_readonly('unit-test-directory/test.file')) + it('returns true if the file is readable', function() + eq(true, os_file_is_readable( + 'unit-test-directory/test.file')) end) end) describe('os_file_is_writable', function() it('returns 0 if the file is readonly', function() local perm = os_getperm('unit-test-directory/test.file') - local perm_orig = perm perm = unset_bit(perm, ffi.C.kS_IWUSR) perm = unset_bit(perm, ffi.C.kS_IWGRP) perm = unset_bit(perm, ffi.C.kS_IWOTH) eq(OK, (os_setperm('unit-test-directory/test.file', perm))) eq(0, os_file_is_writable('unit-test-directory/test.file')) - eq(OK, (os_setperm('unit-test-directory/test.file', perm_orig))) end) it('returns 1 if the file is writable', function() diff --git a/test/unit/queue_spec.lua b/test/unit/queue_spec.lua new file mode 100644 index 0000000000..9326c1cad6 --- /dev/null +++ b/test/unit/queue_spec.lua @@ -0,0 +1,123 @@ +local helpers = require("test.unit.helpers") + +local ffi = helpers.ffi +local eq = helpers.eq + +local queue = helpers.cimport("./test/unit/fixtures/queue.h") + +describe('queue', function() + local parent, child1, child2, child3 + + local function put(q, str) + queue.ut_queue_put(q, str) + end + + local function get(q) + return ffi.string(queue.ut_queue_get(q)) + end + + local function free(q) + queue.queue_free(q) + end + + before_each(function() + parent = queue.queue_new_parent(ffi.NULL, ffi.NULL) + child1 = queue.queue_new_child(parent) + child2 = queue.queue_new_child(parent) + child3 = queue.queue_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) + + it('removing from parent removes from child', function() + eq('c1i1', get(parent)) + eq('c1i2', get(parent)) + eq('c2i1', get(parent)) + eq('c1i3', get(parent)) + eq('c2i2', get(parent)) + eq('c2i3', get(parent)) + eq('c2i4', get(parent)) + end) + + it('removing from child removes from parent', function() + eq('c2i1', get(child2)) + eq('c2i2', get(child2)) + eq('c1i1', get(child1)) + eq('c1i2', get(parent)) + eq('c1i3', get(parent)) + eq('c2i3', get(parent)) + eq('c2i4', get(parent)) + end) + + it('removing from child at the beginning of parent', function() + eq('c1i1', get(child1)) + eq('c1i2', get(child1)) + eq('c2i1', get(parent)) + end) + + it('removing from parent after get from parent and put to child', function() + eq('c1i1', get(parent)) + eq('c1i2', get(parent)) + eq('c2i1', get(parent)) + eq('c1i3', get(parent)) + eq('c2i2', get(parent)) + eq('c2i3', get(parent)) + eq('c2i4', get(parent)) + eq('c3i1', get(parent)) + put(child1, 'c1i11') + put(child1, 'c1i22') + eq('c3i2', get(parent)) + eq('c1i11', get(parent)) + eq('c1i22', get(parent)) + end) + + it('removing from parent after get and put to child', function() + eq('c1i1', get(child1)) + eq('c1i2', get(child1)) + eq('c2i1', get(child2)) + eq('c1i3', get(child1)) + eq('c2i2', get(child2)) + eq('c2i3', get(child2)) + eq('c2i4', get(child2)) + eq('c3i1', get(child3)) + eq('c3i2', get(parent)) + put(child1, 'c1i11') + put(child2, 'c2i11') + put(child1, 'c1i12') + eq('c2i11', get(child2)) + eq('c1i11', get(parent)) + eq('c1i12', get(parent)) + end) + + it('put after removing from child at the end of parent', function() + eq('c3i1', get(child3)) + eq('c3i2', get(child3)) + put(child1, 'c1i11') + put(child2, 'c2i11') + eq('c1i1', get(parent)) + eq('c1i2', get(parent)) + eq('c2i1', get(parent)) + eq('c1i3', get(parent)) + eq('c2i2', get(parent)) + eq('c2i3', get(parent)) + eq('c2i4', get(parent)) + eq('c1i11', get(parent)) + eq('c2i11', get(parent)) + end) + + it('removes from parent queue when child is freed', function() + free(child2) + eq('c1i1', get(parent)) + eq('c1i2', get(parent)) + eq('c1i3', get(parent)) + eq('c3i1', get(child3)) + eq('c3i2', get(child3)) + end) +end) |