Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#9): added tests for setup #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.PHONY: test test-nvim test-all

test:
@printf "\nRunning vusted tests\n"
@vusted ./tests

test-nvim:
@printf "\nRunning plenary tests\n"
@nvim --headless --noplugin -u tests/minimal_init.lua -c "PlenaryBustedDirectory tests/freeze {minimal_init = 'tests/minimal_init.lua'}"

test-all: test test-nvim
14 changes: 7 additions & 7 deletions lua/freeze/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local default_output = "freeze.png"

local freeze = {
opts = {
dir = ".",
dir = vim.env.PWD,
output = default_output,
theme = "default",
config = "base",
Expand Down Expand Up @@ -63,13 +63,15 @@ end
---
--- This function will take your lines and the found Vim filetype and pass it
--- to `freeze --language <vim filetype> --lines <start_line>,<end_line> <file>`
--- @param start_line number the starting line to pass to freeze
--- @param end_line number the ending line to pass to freeze
--- @param start_line number|nil the starting line to pass to freeze
--- @param end_line number|nil the ending line to pass to freeze
function freeze.freeze(start_line, end_line)
if vim.fn.executable("freeze") ~= 1 then
vim.notify("`freeze` not found!", vim.log.levels.WARN, { title = "Freeze" })
return
end
start_line = start_line or 1
end_line = end_line or vim.api.nvim_buf_line_count(0)

local language = vim.api.nvim_buf_get_option(0, "filetype")
local file = vim.api.nvim_buf_get_name(0)
Expand Down Expand Up @@ -149,14 +151,12 @@ end
--- Sets up :Freeze for freezing a selection and :FreezeLine
--- to freeze a single line.
function freeze.setup(plugin_opts)
for k, v in pairs(plugin_opts) do
freeze.opts[k] = v
end
freeze.opts = vim.tbl_extend("force", {}, freeze.opts, plugin_opts or {})
vim.api.nvim_create_user_command("Freeze", function(opts)
if opts.count > 0 then
freeze.freeze(opts.line1, opts.line2)
else
freeze.freeze(1, vim.api.nvim_buf_line_count(0))
freeze.freeze()
end
end, { range = true })
vim.api.nvim_create_user_command("FreezeLine", function(_)
Expand Down
163 changes: 163 additions & 0 deletions tests/freeze/setup_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
local loop = vim.loop
local api = vim.api
local buf = nil
local win = nil
local delay = 5000

local function create_buffer()
local width = vim.o.columns
local height = vim.o.lines
local height_ratio = 0.7
local width_ratio = 0.7
local win_height = math.ceil(height * height_ratio)
local win_width = math.ceil(width * width_ratio)
local row = math.ceil((height - win_height) / 2 - 1)
local col = math.ceil((width - win_width) / 2)
local win_opts = {
style = "minimal",
relative = "editor",
width = win_width,
height = win_height,
row = row,
col = col,
border = "none",
}
vim.loop.chdir(vim.env.PWD .. "/tests/mocks")
buf = api.nvim_create_buf(false, true)
win = api.nvim_open_win(buf, true, win_opts)

api.nvim_win_set_option(win, "winblend", 0)
api.nvim_buf_set_option(buf, "bufhidden", "wipe")
api.nvim_buf_set_option(buf, "filetype", "typescript")
api.nvim_buf_set_name(buf, "testing.ts")

return buf
end

local function get_image_path()
return vim.loop.fs_stat(vim.env.PWD .. "/freeze.png")
end

describe("[freeze.nvim tests]", function()
local default_opts = {
dir = vim.env.PWD,
output = "freeze.png",
theme = "default",
config = "base",
open = false,
}
local freeze = require("freeze")
local same = assert.are.same
before_each(function()
freeze.setup(default_opts)
end)
describe("sets", function()
it("autocommands", function()
vim.cmd("runtime lua/freeze.lua")
freeze.setup()
local user_commands = api.nvim_get_commands({})
assert.are.not_same(nil, user_commands.Freeze)
assert.are.not_same(nil, user_commands.FreezeLine)
end)

it("up with default config", function()
local expected = {
dir = vim.env.PWD,
output = "freeze.png",
theme = "default",
config = "base",
open = false,
}
local actual = require("freeze")
freeze.setup()
same(expected, actual.opts)
end)

it("up with custom config", function()
local opts = {
dir = "/tmp/freeze_images",
theme = "rose-pine-moon",
}
freeze.setup(opts)
local actual = freeze.opts
local expected = vim.tbl_deep_extend("force", {}, actual, opts or {})
same(expected.dir, actual.dir)
same("/tmp/freeze_images", actual.dir)
same(expected.theme, expected.theme)
same("rose-pine-moon", actual.theme)
same(expected, actual)
end)
end)

describe("freezes", function()
-- Change directory to original and close opened windows
before_each(function()
loop.chdir(vim.env.PWD)
if win ~= nil and buf ~= nil then
api.nvim_win_close(win, true)
win = nil
end
os.remove(vim.env.PWD .. "/freeze.png")
end)

-- Change directory to original and remove created image
after_each(function()
loop.chdir(vim.env.PWD)
os.remove(vim.env.PWD .. "/freeze.png")
end)

it("an entire file", function()
local buffer = create_buffer()

freeze.setup()
api.nvim_buf_call(buffer, freeze.freeze)

-- waits `delay` time and if founds the image it stops
if vim.wait(delay, function()
return get_image_path() ~= nil
end) then
local actual = get_image_path()
assert.are.not_same(nil, actual)
end
end)

it("a range of a file", function()
local buffer = create_buffer()

freeze.setup()
api.nvim_buf_call(buffer, function()
freeze.freeze(1, 2)
end)

if vim.wait(delay, function()
return get_image_path() ~= nil
end) then
local actual = get_image_path()
assert.are.not_same(nil, actual)
end
end)

it("a line of a file", function()
local buffer = create_buffer()

freeze.setup()
api.nvim_buf_call(buffer, function()
if win ~= nil then
api.nvim_win_set_cursor(win, { 1, 2 })
local row_col = api.nvim_win_get_cursor(win)
local line = row_col[2]
freeze.freeze(line, line)
else
api.nvim_err_writeln("window is nil")
end
end)

if vim.wait(delay, function()
return get_image_path() ~= nil
end) then
local actual = get_image_path()
assert.are.not_same(nil, actual)
end
end)
end)
end)
11 changes: 11 additions & 0 deletions tests/minimal_init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local plenary_dir = os.getenv("PLENARY_DIR") or "/tmp/plenary.nvim"
local is_not_a_directory = vim.fn.isdirectory(plenary_dir) == 0
if is_not_a_directory then
vim.fn.system({ "git", "clone", "https://github.com/nvim-lua/plenary.nvim", plenary_dir })
end

vim.opt.rtp:append(".")
vim.opt.rtp:append(plenary_dir)

vim.cmd("runtime plugin/plenary.vim")
require("plenary.busted")
7 changes: 7 additions & 0 deletions tests/mocks/testing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const foo = "bar";

function foobar() {
console.log(`foo${foo}`);
}

foobar();