From 7c1ac2ccb6cfab3ebceb84cdca813e56e60e34c5 Mon Sep 17 00:00:00 2001 From: Nicolas Camenisch Date: Sat, 14 May 2022 23:27:30 -0400 Subject: [PATCH] Refactor tests --- Makefile | 12 ++++++-- tests/heuristic_spec.lua | 36 ++++++++++++++++++++++ tests/init_spec.lua | 66 ---------------------------------------- tests/minimal.vim | 12 +++----- tests/utils.lua | 30 ++++++++++++++++++ 5 files changed, 80 insertions(+), 76 deletions(-) create mode 100644 tests/heuristic_spec.lua delete mode 100644 tests/init_spec.lua create mode 100644 tests/utils.lua diff --git a/Makefile b/Makefile index 14f545c..b0a248b 100644 --- a/Makefile +++ b/Makefile @@ -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 . diff --git a/tests/heuristic_spec.lua b/tests/heuristic_spec.lua new file mode 100644 index 0000000..d2276e4 --- /dev/null +++ b/tests/heuristic_spec.lua @@ -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) diff --git a/tests/init_spec.lua b/tests/init_spec.lua deleted file mode 100644 index cdf9bea..0000000 --- a/tests/init_spec.lua +++ /dev/null @@ -1,66 +0,0 @@ -local gi = require("guess-indent") - --- LOAD TEST DATA -- - --- Load all test cases in the data directory -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 - ------------ --- TESTS -- ------------ - --- Correctness -describe("guess-indent", function() - for lang, tc in pairs(test_cases) do - 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 - 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) - --- Print newline. Else the the last print statement would not be visible --- in the output. -print("\n") diff --git a/tests/minimal.vim b/tests/minimal.vim index da1e24a..fdde94c 100644 --- a/tests/minimal.vim +++ b/tests/minimal.vim @@ -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'} diff --git a/tests/utils.lua b/tests/utils.lua new file mode 100644 index 0000000..222f1e6 --- /dev/null +++ b/tests/utils.lua @@ -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