-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(textobj): add tests for textobject
Add unit tests for the textobject module in csvview. These tests cover various scenarios including selecting fields with and without delimiters, handling comments, and empty lines.
- Loading branch information
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
---@diagnostic disable: await-in-sync | ||
local config = require("csvview.config") | ||
local csvview = require("csvview") | ||
local textobject = require("csvview.textobject") | ||
|
||
---@type CsvView.Options | ||
local opts = { parser = { comments = { "#" } } } | ||
|
||
local lines = { | ||
"Index,ID,Name,Email,Birthday", | ||
"1,XUMMW7737A,Jane Davis,jane.williams@example.org,1964-03-22", | ||
"# this is a comment", | ||
"", | ||
} | ||
|
||
---@class CsvView.TextObjectCase | ||
---@field name string | ||
---@field cursor { row: integer, col: integer } 1-based row and 0-based column | ||
---@field opts { include_delimiter: boolean } | ||
---@field expected string | ||
|
||
---@type CsvView.TextObjectCase[] | ||
local cases = { | ||
{ | ||
name = "selects the current field without delimiter", | ||
cursor = { row = 2, col = string.find(lines[2], "M") }, ---@diagnostic disable-line | ||
opts = { include_delimiter = false }, | ||
expected = "XUMMW7737A", | ||
}, | ||
{ | ||
name = "selects the current field with delimiter. The cursor is first column", | ||
cursor = { row = 2, col = 0 }, | ||
opts = { include_delimiter = true }, | ||
expected = "1,", | ||
}, | ||
{ | ||
name = "selects the current field with delimiter. The cursor is last column", | ||
cursor = { row = 2, col = string.find(lines[2], "4") }, ---@diagnostic disable-line | ||
opts = { include_delimiter = true }, | ||
expected = ",1964-03-22", | ||
}, | ||
{ | ||
name = "select nothing if the cursor is on a comment line", | ||
cursor = { row = 3, col = 0 }, | ||
opts = { include_delimiter = false }, | ||
expected = "", | ||
}, | ||
{ | ||
name = "select nothing if the cursor is on an empty line", | ||
cursor = { row = 4, col = 0 }, | ||
opts = { include_delimiter = false }, | ||
expected = "", | ||
}, | ||
} | ||
|
||
describe("csvview.textobject", function() | ||
before_each(function() | ||
config.setup() | ||
csvview.setup() | ||
end) | ||
|
||
for _, case in ipairs(cases) do | ||
it(case.name, function() | ||
local bufnr = vim.api.nvim_create_buf(false, true) | ||
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) | ||
vim.api.nvim_win_set_buf(0, bufnr) | ||
|
||
csvview.enable(bufnr, opts) | ||
|
||
-- Move cursor to the specified field | ||
vim.api.nvim_win_set_cursor(0, { case.cursor.row, case.cursor.col }) | ||
textobject.field(bufnr, case.opts) | ||
|
||
-- Clear the register | ||
vim.fn.setreg("0", "") | ||
|
||
-- Copy the selected text | ||
vim.cmd("normal! y") | ||
local selected = vim.fn.getreg("0") | ||
assert.are.same(case.expected, selected) | ||
end) | ||
end | ||
end) |