Skip to content

Commit

Permalink
Merge pull request #13 from pablopunk/add-tests
Browse files Browse the repository at this point in the history
add tests
  • Loading branch information
pablopunk authored Jan 23, 2024
2 parents 4e003cf + 05ee2eb commit b0adfc1
Show file tree
Hide file tree
Showing 8 changed files with 392 additions and 0 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Tests

on: [push, pull_request]

jobs:
tests:
name: unit tests
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
rev: nightly/nvim-linux64.tar.gz
manager: sudo apt-get
# packages: -y ripgrep
- os: ubuntu-latest
rev: v0.9.0/nvim-linux64.tar.gz
manager: sudo apt-get
# packages: -y ripgrep
steps:
- uses: actions/checkout@v3
- run: date +%F > todays-date
- name: Restore from todays cache
uses: actions/cache@v3
with:
path: _neovim
key: ${{ runner.os }}-${{ matrix.rev }}-${{ hashFiles('todays-date') }}

- name: Prepare
run: |
${{ matrix.manager }} update
# ${{ matrix.manager }} install ${{ matrix.packages }}
test -d _neovim || {
mkdir -p _neovim
curl -sL "https://github.com/neovim/neovim/releases/download/${{ matrix.rev }}" | tar xzf - --strip-components=1 -C "${PWD}/_neovim"
}
mkdir -p ~/.local/share/nvim/lazy
git clone --depth 1 https://github.com/nvim-lua/plenary.nvim ~/.local/share/nvim/lazy/plenary.nvim
ln -s $(pwd) ~/.local/share/nvim/lazy
- name: Run tests
run: |
export PATH="${PWD}/_neovim/bin:${PATH}"
export VIM="${PWD}/_neovim/share/nvim/runtime"
nvim --version
make test
9 changes: 9 additions & 0 deletions .neoconf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"neodev": {
"library": {
"plugins": [
"plenary.nvim"
]
}
}
}
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
MINIMAL_INIT := ./scripts/minimal_init.vim

test:
nvim --headless --noplugin -u ${MINIMAL_INIT} \
-c "PlenaryBustedDirectory lua/ { minimal_init = '${MINIMAL_INIT}' }"

127 changes: 127 additions & 0 deletions lua/unclutter/buffer_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
local M = require "unclutter.buffer"

local CURRENT_BUFNAME = "test.lua"
local CURRENT_BUFNR = 42
local NO_FILE_BUF = 5

describe("Buffer functions", function()
before_each(function()
-- Setting up mocks before each test
---@diagnostic disable-next-line: duplicate-set-field
vim.api.nvim_get_current_buf = function()
return CURRENT_BUFNR
end
---@diagnostic disable-next-line: duplicate-set-field
vim.fn.bufnr = function(file_path)
if file_path == CURRENT_BUFNAME then
return CURRENT_BUFNR
end
return -1
end
---@diagnostic disable-next-line: duplicate-set-field
vim.api.nvim_buf_get_name = function(buf)
if buf == CURRENT_BUFNR then
return CURRENT_BUFNAME
end
return ""
end
---@diagnostic disable-next-line: duplicate-set-field
vim.api.nvim_get_option_value = function(option, opts)
if option == "buftype" then
if opts.buf == NO_FILE_BUF then
return "nofile"
elseif opts.buf == CURRENT_BUFNR then
return ""
end
return "nofile"
end
end
---@diagnostic disable-next-line: duplicate-set-field
vim.fn.win_findbuf = function(buf)
if buf == CURRENT_BUFNR then
return { 1, 2 } -- Example buffer is open in two windows
end
return {}
end
---@diagnostic disable-next-line: duplicate-set-field
vim.fn.tabpagebuflist = function()
return { 3, CURRENT_BUFNR, 7 } -- Example buffers in current tab
end
---@diagnostic disable-next-line: duplicate-set-field
vim.api.nvim_buf_is_valid = function(buf)
return buf == CURRENT_BUFNR -- Only CURRENT_BUF is considered valid in this mock
end
---@diagnostic disable-next-line: duplicate-set-field
vim.api.nvim_buf_is_loaded = function(buf)
return buf == CURRENT_BUFNR -- Only CURRENT_BUF is considered loaded in this mock
end
vim.bo = setmetatable({}, {
__index = function(_, buf)
if buf == CURRENT_BUFNR then
return { buflisted = true }
end
return { buflisted = false }
end,
})
---@diagnostic disable-next-line: duplicate-set-field
vim.api.nvim_list_bufs = function()
return { 3, 7 } -- Example of all buffers
end
end)

it("gets the current buffer number", function()
local buf = M.current()
assert.is.equal(CURRENT_BUFNR, buf)
end)

it("gets the buffer number for a file path", function()
local buf = M.number(CURRENT_BUFNAME)
assert.is.equal(CURRENT_BUFNR, buf)
end)

it("gets the name of a buffer", function()
local name = M.name(CURRENT_BUFNR)
assert.is.equal(CURRENT_BUFNAME, name)
end)

it("gets the buffer type", function()
local buftype = M.type(NO_FILE_BUF)
assert.is.equal("nofile", buftype)
end)

it("checks if buffer is a file", function()
local isFile = M.is_file(CURRENT_BUFNR)
assert.is_true(isFile)
end)

it("gets the number of windows a buffer is open in", function()
local windows = M.windows(CURRENT_BUFNR)
assert.is.equal(2, windows)
end)

it("checks if buffer is visible in current tab", function()
local isVisible = M.is_visible(CURRENT_BUFNR)
assert.is_true(isVisible)
end)

it("checks if buffer is valid", function()
local isValid = M.is_valid(CURRENT_BUFNR)
assert.is_true(isValid)
end)

it("checks if buffer is loaded", function()
local isLoaded = M.is_loaded(CURRENT_BUFNR)
assert.is_true(isLoaded)
end)

it("checks if a buffer is listed", function()
local isListed = M.listed(CURRENT_BUFNR)
assert.is_true(isListed)
end)

it("returns the current buffer in .all()", function()
local all = M.all()
local current = M.current()
assert.is_true(vim.tbl_contains(all, current))
end)
end)
37 changes: 37 additions & 0 deletions lua/unclutter/config_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
local M = require "unclutter.config"

describe("Config module", function()
before_each(function()
-- Reset config to default values before each test
M.set {
clean_after = 3,
tabline = true,
}
end)

it("sets configuration values", function()
local newConfig = {
clean_after = 5,
tabline = false,
}
M.set(newConfig)

assert.is.equal(5, M.clean_after)
assert.is_false(M.tabline)
end)

it("handles nil options by keeping default values", function()
---@diagnostic disable-next-line: param-type-mismatch
M.set(nil)

assert.is.equal(3, M.clean_after)
assert.is_true(M.tabline)
end)

it("only updates provided fields", function()
M.set { clean_after = 7 }

assert.is.equal(7, M.clean_after)
assert.is_true(M.tabline) -- should remain unchanged
end)
end)
57 changes: 57 additions & 0 deletions lua/unclutter/tabline_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
local M = require "unclutter.tabline"

describe("Tabpage section", function()
before_each(function()
-- Mock vim.fn.tabpagenr
---@diagnostic disable-next-line: duplicate-set-field
vim.fn.tabpagenr = function(arg)
if arg == "$" then
return 3 -- total tabpages for the first test, 1 for the second
end
return 2 -- current tabpage for the first test, 1 for the second
end
end)

it("creates tabpage section for multiple tabpages", function()
M.make_tabpage_section()
assert.are.equal(" Tab 2/3 ", M.tabpage_section)
end)

it("creates no tabpage section for a single tabpage", function()
-- Change the mock for a single tabpage scenario
---@diagnostic disable-next-line: duplicate-set-field
vim.fn.tabpagenr = function(arg)
if arg == "$" then
return 1
end
return 1
end

M.make_tabpage_section()
assert.are.equal("", M.tabpage_section)
end)
end)

describe("Buffer management", function()
before_each(function()
M.buffers_to_keep = {}
end)

it("keeps a buffer", function()
M.keep_buffer(1)
assert.is_true(M.is_buffer_kept(1))
end)

it("removes a buffer", function()
M.keep_buffer(1)
M.remove_buffer(1)
assert.is_false(M.is_buffer_kept(1))
end)

it("toggles a buffer", function()
M.toggle_buffer(1)
assert.is_true(M.is_buffer_kept(1))
M.toggle_buffer(1)
assert.is_false(M.is_buffer_kept(1))
end)
end)
103 changes: 103 additions & 0 deletions lua/unclutter/unclutter_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
local mock = require "luassert.mock"
local M = require "unclutter.unclutter"
local config = require "unclutter.config"

local autocmds, buffer, tabline

describe("Unclutter", function()
before_each(function()
M.enabled = false
M.buf_just_left = nil

autocmds = mock(require "unclutter.autocmds", true)
buffer = mock(require "unclutter.buffer", true)
tabline = mock(require "unclutter.tabline", true)

config.set {
clean_after = 3,
tabline = true,
}
end)

after_each(function()
mock.clear(autocmds)
mock.clear(buffer)
mock.clear(tabline)
end)

describe("enable", function()
it("enables the plugin with tabline enabled", function()
M.enable { tabline = true }
assert.is_true(M.enabled)
assert.stub(tabline.enable).was_called()
assert.stub(tabline.disable).was_not_called()
end)

it("enables the plugin with tabline disabled", function()
M.enable { tabline = false }
assert.is_true(M.enabled)
assert.stub(tabline.enable).was_not_called()
assert.stub(tabline.disable).was_called()
end)

it("enables the plugin with default settings when no options are provided", function()
M.enable {}
assert.is_true(M.enabled)
assert.are.equal(3, config.clean_after) -- Default value
assert.are.equal(true, config.tabline) -- Default value
end)

it("partially updates settings when partial options are provided", function()
M.enable { tabline = false } -- Only providing tabline option
assert.is_true(M.enabled)
assert.are.equal(3, config.clean_after) -- Default value
assert.are.equal(false, config.tabline) -- Updated value
end)
end)

describe("disable", function()
it("disables the plugin", function()
M.enabled = true
M.disable()
assert.is_false(M.enabled)
assert.stub(autocmds.remove_augroup).was_called()
assert.stub(tabline.disable).was_called()
end)
end)

describe("buffer_should_be_hidden_on_leave", function()
it("checks if buffer should be hidden on leave", function()
buffer.current.returns(2)
buffer.is_file.returns(true)
buffer.is_visible.returns(false)
buffer.windows.returns(0)
tabline.is_buffer_kept.returns(false)

local result = M.buffer_should_be_hidden_on_leave(1)
assert.is_true(result)
end)
it("does not hide the buffer if it is kept", function()
buffer.current.returns(2)
buffer.is_file.returns(true)
buffer.is_visible.returns(false)
buffer.windows.returns(0)
tabline.is_buffer_kept.returns(true) -- Buffer is kept

local result = M.buffer_should_be_hidden_on_leave(1)
assert.is_false(result)
end)
end)

describe("keep_all_buffers", function()
it("keeps all buffers", function()
buffer.all.returns { 1, 2, 3 }
M.keep_all_buffers()
assert.stub(tabline.keep_buffer).was_called(3)
end)
it("does nothing when there are no buffers", function()
buffer.all.returns {}
M.keep_all_buffers()
assert.stub(tabline.keep_buffer).was_not_called()
end)
end)
end)
6 changes: 6 additions & 0 deletions scripts/minimal_init.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let plenary_path = stdpath('data') . '/lazy/plenary.nvim/'
execute 'set rtp+=' . plenary_path

runtime! plugin/plenary.vim
runtime! plugin/unclutter.lua

0 comments on commit b0adfc1

Please sign in to comment.