Skip to content

Commit

Permalink
chore(format): format test files
Browse files Browse the repository at this point in the history
  • Loading branch information
tanvirtin committed Jul 4, 2024
1 parent 235ec4d commit 94b6521
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 72 deletions.
2 changes: 1 addition & 1 deletion lua/vgit/core/event.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ local event = {
CursorHold = 'CursorHold',
CursorMoved = 'CursorMoved',
InsertEnter = 'InsertEnter',
QuitPre = 'QuitPre'
QuitPre = 'QuitPre',
},
group = vim.api.nvim_create_augroup('VGitGroup', { clear = true }),
}
Expand Down
6 changes: 2 additions & 4 deletions lua/vgit/core/utils/date.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ local date = {}

function date.format(time, format)
format = format or '%d %b %Y'
local timestamp = tonumber(time)

if not timestamp or timestamp == 0 then
timestamp = os.time()
end
local timestamp = tonumber(time)
if not timestamp or timestamp == 0 then timestamp = os.time() end

return os.date(format, timestamp)
end
Expand Down
4 changes: 1 addition & 3 deletions lua/vgit/core/utils/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ function list.join(l, with)

for i = 1, #l do
result = result .. l[i]
if i ~= #l then
result = result .. with
end
if i ~= #l then result = result .. with end
end

return result
Expand Down
12 changes: 9 additions & 3 deletions tests/unit/core/Color_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ describe('Color:', function()
end)

it('should throw an error if spec is nil', function()
assert.has_error(function() Color(nil) end, 'spec is required')
assert.has_error(function()
Color(nil)
end, 'spec is required')
end)

it('should throw an error if spec.name is nil', function()
assert.has_error(function() Color({ attribute = 'fg' }) end, 'spec.name is required')
assert.has_error(function()
Color({ attribute = 'fg' })
end, 'spec.name is required')
end)

it('should throw an error if spec.attribute is nil', function()
assert.has_error(function() Color({ name = 'Normal' }) end, 'spec.attribute is required')
assert.has_error(function()
Color({ name = 'Normal' })
end, 'spec.attribute is required')
end)
end)

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/core/Watcher_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ describe('Watcher', function()

describe('watch_file', function()
it('should start watching a file', function()
watcher:watch_file(path, function()end)
watcher:watch_file(path, function() end)
assert.is_not_nil(watcher.watcher)
end)
end)

describe('watch_dir', function()
it('should start watching a directory', function()
watcher:watch_dir(path, function()end)
watcher:watch_dir(path, function() end)
assert.is_not_nil(watcher.watcher)
end)
end)
Expand Down
52 changes: 29 additions & 23 deletions tests/unit/core/Window_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('Window:', function()

local function create_test_buffer()
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, {'Line 1', 'Line 2', 'Line 3'})
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { 'Line 1', 'Line 2', 'Line 3' })
return Buffer(buf)
end

Expand All @@ -15,19 +15,17 @@ describe('Window:', function()
width = 20,
height = 10,
row = 5,
col = 5
col = 5,
}

before_each(function()
buffer = create_test_buffer()
end)

after_each(function()
if win and win.win_id and vim.api.nvim_win_is_valid(win.win_id) then
win:close()
end
if win and win.win_id and vim.api.nvim_win_is_valid(win.win_id) then win:close() end
if buffer and buffer.bufnr and vim.api.nvim_buf_is_valid(buffer.bufnr) then
vim.api.nvim_buf_delete(buffer.bufnr, {force = true})
vim.api.nvim_buf_delete(buffer.bufnr, { force = true })
end
end)

Expand All @@ -47,7 +45,9 @@ describe('Window:', function()
end)

it('should throw an error if win_id is not a number', function()
assert.has_error(function() Window('not a number') end)
assert.has_error(function()
Window('not a number')
end)
end)
end)

Expand All @@ -60,13 +60,15 @@ describe('Window:', function()
end)

it('should focus the new window if focus option is true', function()
win = Window:open(buffer, vim.tbl_extend('force', default_float_opts, {focus = true}))
win = Window:open(buffer, vim.tbl_extend('force', default_float_opts, { focus = true }))

assert.are.equal(win.win_id, vim.api.nvim_get_current_win())
end)

it('should throw an error if buffer is not provided', function()
assert.has_error(function() Window:open() end, 'buffer is required')
assert.has_error(function()
Window:open()
end, 'buffer is required')
end)
end)

Expand All @@ -80,17 +82,17 @@ describe('Window:', function()
end)

it('should return {1, 1} if get_cursor fails', function()
win = Window(-1) -- Invalid window
win = Window(-1) -- Invalid window

local cursor = win:get_cursor()
assert.are.same({1, 1}, cursor)
assert.are.same({ 1, 1 }, cursor)
end)
end)

describe('get_lnum', function()
it('should return the current line number', function()
win = Window:open(buffer, default_float_opts)
win:set_cursor({2, 0})
win:set_cursor({ 2, 0 })

assert.are.equal(2, win:get_lnum())
end)
Expand Down Expand Up @@ -129,20 +131,20 @@ describe('Window:', function()
describe('set_cursor', function()
it('should set cursor position', function()
win = Window:open(buffer, default_float_opts)
win:set_cursor({2, 0})
win:set_cursor({ 2, 0 })
local cursor = win:get_cursor()

assert.are.same({2, 0}, cursor)
assert.are.same({ 2, 0 }, cursor)
end)
end)

describe('set_lnum', function()
it('should set the line number while maintaining column position', function()
win = Window:open(buffer, default_float_opts)
win:set_cursor({1, 2})
win:set_cursor({ 1, 2 })
win:set_lnum(2)

assert.are.same({2, 2}, win:get_cursor())
assert.are.same({ 2, 2 }, win:get_cursor())
end)
end)

Expand Down Expand Up @@ -176,7 +178,7 @@ describe('Window:', function()
describe('set_win_plot and get_win_plot', function()
it('should set and get window configuration', function()
win = Window:open(buffer, default_float_opts)
local config = {relative = 'editor', row = 6, col = 11, width = 31, height = 16}
local config = { relative = 'editor', row = 6, col = 11, width = 31, height = 16 }
win:set_win_plot(config)
local new_config = win:get_win_plot()

Expand All @@ -190,7 +192,7 @@ describe('Window:', function()
describe('assign_options', function()
it('should set multiple window options', function()
win = Window:open(buffer, default_float_opts)
win:assign_options({wrap = false, number = true})
win:assign_options({ wrap = false, number = true })

assert.is_false(vim.api.nvim_win_get_option(win.win_id, 'wrap'))
assert.is_true(vim.api.nvim_win_get_option(win.win_id, 'number'))
Expand Down Expand Up @@ -223,21 +225,21 @@ describe('Window:', function()

describe('is_focused', function()
it('should return true if window is focused', function()
win = Window:open(buffer, vim.tbl_extend('force', default_float_opts, {focus = true}))
win = Window:open(buffer, vim.tbl_extend('force', default_float_opts, { focus = true }))

assert.is_true(win:is_focused())
end)

it('should return false if window is not focused', function()
win = Window:open(buffer, vim.tbl_extend('force', default_float_opts, {focus = false}))
win = Window:open(buffer, vim.tbl_extend('force', default_float_opts, { focus = false }))

assert.is_false(win:is_focused())
end)
end)

describe('focus', function()
it('should focus the window', function()
win = Window:open(buffer, vim.tbl_extend('force', default_float_opts, {focus = false}))
win = Window:open(buffer, vim.tbl_extend('force', default_float_opts, { focus = false }))
win:focus()

assert.is_true(win:is_focused())
Expand Down Expand Up @@ -266,14 +268,18 @@ describe('Window:', function()
win = Window:open(buffer, default_float_opts)
win:position_cursor()

assert.has_no.errors(function() win:get_cursor() end)
assert.has_no.errors(function()
win:get_cursor()
end)
end)

it('should position cursor at the top when specified', function()
win = Window:open(buffer, default_float_opts)
win:position_cursor('top')

assert.has_no.errors(function() win:get_cursor() end)
assert.has_no.errors(function()
win:get_cursor()
end)
end)
end)

Expand Down
12 changes: 9 additions & 3 deletions tests/unit/core/icons_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ describe('icons', function()
describe('get', function()
it('should return icon and color when nvim-web-devicons is loaded', function()
package.loaded['nvim-web-devicons'] = {
has_loaded = function() return true end,
get_icon = function(fname, ext) return '', 'blue' end,
has_loaded = function()
return true
end,
get_icon = function(fname, ext)
return '', 'blue'
end,
}
local icon, color = icons.get('icons_spec.lua', 'lua')
assert.equals('', icon)
Expand All @@ -18,7 +22,9 @@ describe('icons', function()

it('should return nil and empty string when nvim-web-devicons is not loaded', function()
package.loaded['nvim-web-devicons'] = {
has_loaded = function() return false end,
has_loaded = function()
return false
end,
}

local icon, color = icons.get('test.txt', 'txt')
Expand Down
11 changes: 4 additions & 7 deletions tests/unit/core/keymap_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,12 @@ describe('keymap', function()
})

for index in ipairs(expected) do
assert.stub(vim.api.nvim_set_keymap).was.called_with(
expected[index][1],
expected[index][2],
expected[index][3],
{
assert
.stub(vim.api.nvim_set_keymap).was
.called_with(expected[index][1], expected[index][2], expected[index][3], {
noremap = true,
silent = true,
}
)
})
end
end)
end)
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/core/utils_date_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local eq = assert.are.same
describe('utils.date:', function()
describe('date.format', function()
it('should format the date using the default format', function()
local time = 1609477200 -- January 1, 2021
local time = 1609477200 -- January 1, 2021
local formatted_date = utils.date.format(time)
assert.are.equal(formatted_date, '01 Jan 2021')
end)
Expand All @@ -20,18 +20,18 @@ describe('utils.date:', function()
it('should handle invalid time input gracefully', function()
local invalid_time = 'invalid'
local formatted_date = utils.date.format(invalid_time)
local expected_date = os.date('%d %b %Y', os.time()) -- Use the current date
local expected_date = os.date('%d %b %Y', os.time()) -- Use the current date
assert.are.equal(formatted_date, expected_date)
end)

it('should handle nil time input gracefully', function()
local formatted_date = utils.date.format(nil)
local expected_date = os.date('%d %b %Y', os.time()) -- Use the current date
local expected_date = os.date('%d %b %Y', os.time()) -- Use the current date
assert.are.equal(formatted_date, expected_date)
end)

it('should handle nil format input gracefully', function()
local time = 1609477200 -- January 1, 2021
local time = 1609477200 -- January 1, 2021
local formatted_date = utils.date.format(time, nil)
assert.are.equal(formatted_date, '01 Jan 2021')
end)
Expand Down
Loading

0 comments on commit 94b6521

Please sign in to comment.