feat: add plenary tests (#420)

* feat: add tests

* fix(hash): use h * 33 + c for djb2

* fix: unload all lua module in before_each
This commit is contained in:
Null Chilly 2023-02-27 23:51:09 +07:00 committed by GitHub
parent 87516f4326
commit bad9c23f12
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 102 additions and 1 deletions

32
.github/workflows/tests.yml vendored Normal file
View file

@ -0,0 +1,32 @@
---
name: Tests
on:
pull_request: ~
push:
paths-ignore:
- "*.md"
branches:
- main
jobs:
build:
name: Run tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Neovim
shell: bash
run: |
wget -q https://github.com/neovim/neovim/releases/download/nightly/nvim-linux64.deb -O /tmp/nvim.deb
sudo dpkg -i /tmp/nvim.deb
- name: Fetch dependencies
run: |
git clone --depth 1 https://github.com/nvim-lua/plenary.nvim ~/.local/share/nvim/site/pack/vendor/start/plenary.nvim
git clone --depth 1 https://github.com/kyazdani42/nvim-web-devicons ~/.local/share/nvim/site/pack/vendor/start/nvim-web-devicons
ln -s $(pwd) ~/.local/share/nvim/site/pack/vendor/start
- name: Run tests
run: |
nvim --version
[ ! -d tests ] && exit 0
nvim --headless -u tests/init.lua -c "PlenaryBustedDirectory tests/ {minimal_init = 'tests/init.lua', sequential = true}"

View file

@ -4,7 +4,7 @@ local B = bit or bit32 or require "catppuccin.lib.vim.bit"
local hash_str = function(str) -- djb2, https://stackoverflow.com/questions/7666509/hash-function-for-string
local hash = 5381
for i = 1, #str do
hash = B.lshift(hash, 5) + string.byte(str, i)
hash = B.lshift(hash, 5) + hash + string.byte(str, i)
end
return hash
end

20
tests/compile_spec.lua Normal file
View file

@ -0,0 +1,20 @@
describe("compile", function()
before_each(function()
for name, _ in pairs(package.loaded) do
if name:match "^catppuccin" and name ~= "catppuccin" then package.loaded[name] = nil end
end
end)
it("without setup", function()
assert.equals(pcall(function() require("catppuccin").compile() end), true)
end)
it("user 1", function()
require("catppuccin").setup {
custom_highlight = function(C)
return {
SpellBad = { cterm = { underdashed = true } },
}
end,
}
assert.equals(pcall(function() require("catppuccin").compile() end), true)
end)
end)

5
tests/load_spec.lua Normal file
View file

@ -0,0 +1,5 @@
describe("set colorscheme", function()
it("without calling setup", function()
assert.equals(pcall(function() vim.cmd.colorscheme "catppuccin" end), true)
end)
end)

3
tests/minimal_init.vim Normal file
View file

@ -0,0 +1,3 @@
set rtp+=.
runtime! plugin/plenary.vim

41
tests/setup_spec.lua Normal file
View file

@ -0,0 +1,41 @@
describe("setup hash", function()
local hash = require("catppuccin.lib.hashing").hash
it("typo", function() assert.are_not.equals(hash { custom_highlight = {} }, hash { ustom_highlight = {} }) end)
it(
"when table order is shuffled",
function()
assert.equals(
hash {
custom_highlight = {
Search = { fg = "#F5C2E7", bg = "#45475A", style = { "bold" } },
IncSearch = { fg = "#45475A", bg = "#F5C2E7" },
},
},
hash {
custom_highlight = {
Search = { style = { "bold" }, bg = "#45475A", fg = "#F5C2E7" },
IncSearch = { bg = "#F5C2E7", fg = "#45475A" },
},
}
)
end
)
it(
"when toggle true/false",
function()
assert.are_not.equals({
integrations = {
navic = true,
noice = true,
fidget = true,
},
}, {
integrations = {
navic = true,
noice = false,
fidget = false,
},
})
end
)
end)