Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NMAC427 committed May 15, 2022
1 parent bdecfbd commit 7c1ac2c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 76 deletions.
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
all: test check

test:
nvim --headless --noplugin -u tests/minimal.vim +Test
install_plenary:
if [ ! -d ~/.local/share/nvim/site/pack/vendor/opt/plenary.nvim ]; \
then \
git clone --depth 1 https://github.com/nvim-lua/plenary.nvim \
~/.local/share/nvim/site/pack/vendor/opt/plenary.nvim; \
fi

test: install_plenary
nvim --headless --noplugin -u tests/minimal.vim -R \
-c "PlenaryBustedDirectory tests/ {minimal_init = 'tests/minimal.vim'}"

check:
stylua --color always --check .
36 changes: 36 additions & 0 deletions tests/heuristic_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
local gi = require("guess-indent")
local t = require("plenary.async.tests")

local test_cases = require("tests.utils").load_test_cases()

-- Check that all files indentation gets detected correctly.
t.describe("guess-indent", function()
for lang, tc in pairs(test_cases) do
t.describe("for " .. lang, function()
for _, file in ipairs(tc) do
-- Open a new buffer containing the file
vim.cmd(":edit! " .. file.path)

-- Get first line from buffer and try to extract the expectation
local line = vim.api.nvim_buf_get_lines(0, 0, 1, false)
local data = loadstring("return " .. line[1]:match("{.*}"))()
local expectation = data.expected

if data.disabled then
print("WARNING: Skipping test case for file '" .. file.path .. "'")
goto cleanup
end

-- Perfom test
t.it("should indent: " .. file.name, function()
local result = gi.guess_from_buffer()
assert.are.equal(expectation, result)
end)

-- Cleanup
::cleanup::
print(vim.cmd(":bdelete!"))
end
end)
end
end)
66 changes: 0 additions & 66 deletions tests/init_spec.lua

This file was deleted.

12 changes: 4 additions & 8 deletions tests/minimal.vim
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
if !isdirectory('plenary.nvim')
!git clone https://github.com/nvim-lua/plenary.nvim.git plenary.nvim
!git -C plenary.nvim reset --hard 1338bbe8ec6503ca1517059c52364ebf95951458
endif
packadd plenary.nvim
runtime plugin/plenary.vim

set rtp+=.

set runtimepath+=plenary.nvim,.
set noswapfile
set noundofile

runtime plugin/plenary.vim
command Test PlenaryBustedDirectory tests/ {minimal_init = 'tests/minimal.vim'}
30 changes: 30 additions & 0 deletions tests/utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
local M = {}

-- Load all test cases in the data directory
function M.load_test_cases()
local test_data_path = "./tests/data/"
local subfolders = vim.fn.readdir(test_data_path)

local test_cases = {}

for _, language in ipairs(subfolders) do
local language_path = test_data_path .. language .. "/"
if vim.fn.isdirectory(language_path) ~= 0 then
local l_test_cases = {}
local test_files = vim.fn.readdir(language_path, "v:val !~ '^\\.\\|\\~$'")

for i, file_name in ipairs(test_files) do
l_test_cases[i] = {
path = language_path .. file_name,
name = file_name,
}
end

test_cases[language] = l_test_cases
end
end

return test_cases
end

return M

0 comments on commit 7c1ac2c

Please sign in to comment.