From 7217360e349ff787933d91b55efdfeb435eb45cb Mon Sep 17 00:00:00 2001 From: James McCoy Date: Mon, 6 Mar 2017 10:44:07 -0500 Subject: vim-patch:7.4.2051 Problem: No proper testing of trunc_string(). Solution: Add a unittest for message.c. https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f --- test/functional/ui/inccommand_spec.lua | 18 ++++++------- test/unit/message_spec.lua | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 test/unit/message_spec.lua (limited to 'test') diff --git a/test/functional/ui/inccommand_spec.lua b/test/functional/ui/inccommand_spec.lua index 6da22b6a3a..3b31da0397 100644 --- a/test/functional/ui/inccommand_spec.lua +++ b/test/functional/ui/inccommand_spec.lua @@ -482,7 +482,7 @@ describe(":substitute, 'inccommand' preserves undo", function() {15:~ }| {15:~ }| {15:~ }| - Already...st change | + Already ...t change | ]]) else screen:expect([[ @@ -495,7 +495,7 @@ describe(":substitute, 'inccommand' preserves undo", function() {15:~ }| {15:~ }| {15:~ }| - Already...st change | + Already ...t change | ]]) end end @@ -534,7 +534,7 @@ describe(":substitute, 'inccommand' preserves undo", function() {15:~ }| {15:~ }| {15:~ }| - Already...st change | + Already ...t change | ]]) else screen:expect([[ @@ -547,7 +547,7 @@ describe(":substitute, 'inccommand' preserves undo", function() {15:~ }| {15:~ }| {15:~ }| - Already...st change | + Already ...t change | ]]) end @@ -574,7 +574,7 @@ describe(":substitute, 'inccommand' preserves undo", function() {15:~ }| {15:~ }| {15:~ }| - Already...st change | + Already ...t change | ]]) else screen:expect([[ @@ -587,7 +587,7 @@ describe(":substitute, 'inccommand' preserves undo", function() {15:~ }| {15:~ }| {15:~ }| - Already...st change | + Already ...t change | ]]) end screen:detach() @@ -616,7 +616,7 @@ describe(":substitute, 'inccommand' preserves undo", function() {15:~ }| {15:~ }| {15:~ }| - Already...st change | + Already ...t change | ]]) else screen:expect([[ @@ -629,7 +629,7 @@ describe(":substitute, 'inccommand' preserves undo", function() {15:~ }| {15:~ }| {15:~ }| - Already...st change | + Already ...t change | ]]) end @@ -653,7 +653,7 @@ describe(":substitute, 'inccommand' preserves undo", function() {15:~ }| {15:~ }| {15:~ }| - Already...st change | + Already ...t change | ]]) end screen:detach() diff --git a/test/unit/message_spec.lua b/test/unit/message_spec.lua new file mode 100644 index 0000000000..23d342c027 --- /dev/null +++ b/test/unit/message_spec.lua @@ -0,0 +1,49 @@ +local helpers = require("test.unit.helpers") + +local ffi = helpers.ffi +local eq = helpers.eq +local to_cstr = helpers.to_cstr + +local cimp = helpers.cimport('./src/nvim/message.h') + +describe('trunc_string', function() + local buffer = ffi.typeof('char_u[40]') + + local function test_inplace(s, expected) + local buf = buffer() + ffi.C.strcpy(buf, s) + cimp.trunc_string(buf, buf, 20, 40) + eq(expected, ffi.string(buf)) + end + + local function test_copy(s, expected) + local buf = buffer() + cimp.trunc_string(to_cstr(s), buf, 20, 40) + eq(expected, ffi.string(buf)) + end + + local permutations = { + { ['desc'] = 'in-place', ['func'] = test_inplace }, + { ['desc'] = 'by copy', ['func'] = test_copy }, + } + + for _,t in ipairs(permutations) do + describe('populates buf '..t.desc, function() + it('with a small string', function() + t.func('text', 'text') + end) + + it('with a medium string', function() + t.func('a short text', 'a short text') + end) + + it('with a string exactly the truncate size', function() + t.func('a text tha just fits', 'a text tha just fits') + end) + + it('with a string that must be truncated', function() + t.func('a text that nott fits', 'a text t...nott fits') + end) + end) + end +end) -- cgit From 4e3a2784ec3cb4ff79bd48f55a8edee817dec6e7 Mon Sep 17 00:00:00 2001 From: James McCoy Date: Mon, 6 Mar 2017 12:14:39 -0500 Subject: vim-patch:7.4.2068 Problem: Not all arguments of trunc_string() are tested. Memory access error when running the message tests. Solution: Add another test case. (Yegappan Lakshmanan) Make it easy to run unittests with valgrind. Fix the access error. https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31 --- test/unit/message_spec.lua | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'test') diff --git a/test/unit/message_spec.lua b/test/unit/message_spec.lua index 23d342c027..afb572347f 100644 --- a/test/unit/message_spec.lua +++ b/test/unit/message_spec.lua @@ -4,22 +4,28 @@ local ffi = helpers.ffi local eq = helpers.eq local to_cstr = helpers.to_cstr -local cimp = helpers.cimport('./src/nvim/message.h') +local cimp = helpers.cimport('./src/nvim/message.h', './src/nvim/memory.h', + './src/nvim/strings.h') describe('trunc_string', function() - local buffer = ffi.typeof('char_u[40]') - - local function test_inplace(s, expected) - local buf = buffer() + local buflen = 40 + local function test_inplace(s, expected, room) + room = room and room or 20 + local buf = cimp.xmalloc(ffi.sizeof('char_u') * buflen) ffi.C.strcpy(buf, s) - cimp.trunc_string(buf, buf, 20, 40) + cimp.trunc_string(buf, buf, room, buflen) eq(expected, ffi.string(buf)) + cimp.xfree(buf) end - local function test_copy(s, expected) - local buf = buffer() - cimp.trunc_string(to_cstr(s), buf, 20, 40) + local function test_copy(s, expected, room) + room = room and room or 20 + local buf = cimp.xmalloc(ffi.sizeof('char_u') * buflen) + local str = cimp.vim_strsave(to_cstr(s)) + cimp.trunc_string(str, buf, room, buflen) eq(expected, ffi.string(buf)) + cimp.xfree(buf) + cimp.xfree(str) end local permutations = { @@ -37,6 +43,10 @@ describe('trunc_string', function() t.func('a short text', 'a short text') end) + it('with a string of length == 1/2 room', function() + t.func('a text that fits', 'a text that fits', 34) + end) + it('with a string exactly the truncate size', function() t.func('a text tha just fits', 'a text tha just fits') end) -- cgit