From b27bc4ed26feec154d03a0d5de6fd2329162627d Mon Sep 17 00:00:00 2001 From: JuanZoran <1430359574@qq.com> Date: Fri, 31 Mar 2023 00:55:38 +0800 Subject: [PATCH] fix: add util unit test and fix util.center mistake --- lua/Trans/core/util.lua | 2 +- lua/test/setup.lua | 1 + lua/test/util_spec.lua | 75 ++++++++++++++++++++++++++-- makefile | 2 +- {script => scripts}/minimal_init.vim | 0 5 files changed, 75 insertions(+), 5 deletions(-) rename {script => scripts}/minimal_init.vim (100%) diff --git a/lua/Trans/core/util.lua b/lua/Trans/core/util.lua index 9943ac0..88702cf 100644 --- a/lua/Trans/core/util.lua +++ b/lua/Trans/core/util.lua @@ -60,6 +60,7 @@ end ---Puase coroutine for {ms} milliseconds ---@param ms integer function M.pause(ms) + assert(ms) local co = coroutine.running() vim.defer_fn(function() coroutine.resume(co) @@ -114,7 +115,6 @@ function M.center(node, win_width) end local str = node[1] - win_width = str:width() local space = math.max(0, math.floor((win_width - str:width()) / 2)) node[1] = string.rep(' ', space) .. str return node diff --git a/lua/test/setup.lua b/lua/test/setup.lua index 51fa153..e3c8864 100644 --- a/lua/test/setup.lua +++ b/lua/test/setup.lua @@ -6,6 +6,7 @@ _G.api = vim.api _G.fn = vim.fn _G.mock = require 'luassert.mock' _G.stub = require 'luassert.stub' +string.width = api.nvim_strwidth ---@param func fun(buffer: TransBuffer) ---@return fun() diff --git a/lua/test/util_spec.lua b/lua/test/util_spec.lua index 8efec98..f7238d4 100644 --- a/lua/test/util_spec.lua +++ b/lua/test/util_spec.lua @@ -1,9 +1,78 @@ +---@diagnostic disable: param-type-mismatch require 'test.setup' local util = Trans.util -describe('util.display_height', with_buffer(function(buffer) - --- TODO : +describe('util.display_height', function() it('can calculate the height of lines when window with wrap option', function() + local lines = { + '1234567890', + '1234567890', + '1234567890', + '1234567890', + '1234567890', + '1234567890', + '1234567890', + '1234567890', + '1234567890', + } + + assert.are.equal(#lines, util.display_height(lines, 10)) + assert.are.equal(#lines, util.display_height(lines, 11)) + assert.are.equal(2 * #lines, util.display_height(lines, 9)) + + -- Unicode width test + local u_lines = { + '12345678👍', -- 10 + 'あうえお', -- 8 + '𠮷野い𠮷家野家家', -- 16 + '👍👍👍お家', -- 10 + } + + assert.are.equal(4, util.display_height(u_lines, 20)) + assert.are.equal(4, util.display_height(u_lines, 16)) + assert.are.equal(5, util.display_height(u_lines, 10)) + assert.are.equal(7, util.display_height(u_lines, 8)) + assert.are.equal(9, util.display_height(u_lines, 7)) end) -end)) +end) + +describe('util.display_width', function() + it('can calculate the max width of lines', function() + local lines = { + '1234567890', + '123456789', + '12345678', + '1234567', + '123456', + '12345', + '1234', + '123', + '12', + '1', + } + + assert.are.equal(10, util.display_width(lines)) + -- Unicode width test + local u_lines = { + '12345678👍', -- 10 + 'あうえお', -- 8 + '𠮷野い𠮷家野家家', -- 16 + '👍👍👍お家', -- 10 + } + + assert.are.equal(16, util.display_width(u_lines)) + end) +end) + +describe('util.center', function() + it('will return the node if its width more than width', function() + local node = i { '1234567890' } + assert.are.same(node, util.center(node, 9)) + end) + + it('will auto padding space', function() + local node = i { '1234567890' } + assert.are.same(i { (' '):rep(2) .. '1234567890' }, util.center(node, 15)) + end) +end) diff --git a/makefile b/makefile index 2fc1b83..b3690aa 100644 --- a/makefile +++ b/makefile @@ -1,4 +1,4 @@ .PHONE: test test: - nvim --headless --noplugin -u script/minimal_init.vim -c "PlenaryBustedDirectory lua/test/ { minimal_init = './scripts/minimal_init.vim' }" -c 'qa!' + nvim --headless --noplugin -u scripts/minimal_init.vim -c "PlenaryBustedDirectory lua/test/ { minimal_init = './scripts/minimal_init.vim' }" -c 'qa!' diff --git a/script/minimal_init.vim b/scripts/minimal_init.vim similarity index 100% rename from script/minimal_init.vim rename to scripts/minimal_init.vim