Skip to content

Commit

Permalink
refactor: view_spec.lua for better readability
Browse files Browse the repository at this point in the history
Refactored the view_spec.lua file to improve readability and maintainability.
Consolidated test cases into a table and used a loop to run them, reducing
redundant code. Updated comments for clarity.
  • Loading branch information
hat0uma committed Oct 20, 2024
1 parent a7906a9 commit 25d963b
Showing 1 changed file with 56 additions and 69 deletions.
125 changes: 56 additions & 69 deletions tests/view_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ local csvview = require("csvview")
local metrics = require("csvview.metrics")
local view = require("csvview.view")

--- get lines with extmarks
--- Get lines extmarks applied
---@param bufnr integer
---@param ns integer
---@return string[]
local function get_lines_with_extmarks(bufnr, ns)
-- get lines and extmarks
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, true)
local extmarks = vim.api.nvim_buf_get_extmarks(bufnr, ns, 0, -1, { details = true })
local col_offset = {} --- @type integer[]
Expand Down Expand Up @@ -42,87 +43,73 @@ describe("view", function()
local ns = vim.api.nvim_get_namespaces()["csv_extmark"]
describe("CsvView:render", function()
describe("should align correctly even if it contains multibyte characters", function()
it("display_mode = 'highlight'", function()
local bufnr = vim.api.nvim_create_buf(false, true)
local opts = config.get({
view = {
min_column_width = 5,
spacing = 1,
display_mode = "highlight",
-- define test cases
--- @type table<string, {opts: CsvViewOptions, lines: string[], expected: string[]}>
local cases = {
["display_mode = 'highlight'"] = {
opts = { view = { display_mode = "highlight", spacing = 1, min_column_width = 5 } },
lines = {
"column1(number),column2(emoji),column3(string)",
"111,πŸ˜€,abcde",
"222222222222,πŸ˜’πŸ˜’πŸ˜’πŸ˜’,fgh",
"333333333333333333,😎b😎b😎b😎b😎b😎b,ijk",
},
})
local lines = {
"column1(number),column2(emoji),column3(string)",
"111,πŸ˜€,abcde",
"222222222222,πŸ˜’πŸ˜’πŸ˜’πŸ˜’,fgh",
"333333333333333333,😎b😎b😎b😎b😎b😎b,ijk",
}
local expected = {
"column1(number) ,column2(emoji) ,column3(string) ",
" 111,πŸ˜€ ,abcde ",
" 222222222222,πŸ˜’πŸ˜’πŸ˜’πŸ˜’ ,fgh ",
" 333333333333333333,😎b😎b😎b😎b😎b😎b ,ijk ",
}
expected = {
"column1(number) ,column2(emoji) ,column3(string) ",
" 111,πŸ˜€ ,abcde ",
" 222222222222,πŸ˜’πŸ˜’πŸ˜’πŸ˜’ ,fgh ",
" 333333333333333333,😎b😎b😎b😎b😎b😎b ,ijk ",
},
},
["display_mode = 'border'"] = {
opts = { view = { display_mode = "border", spacing = 1, min_column_width = 5 } },
lines = {
"column1(number),column2(emoji),column3(string)",
"111,πŸ˜€,abcde",
"222222222222,πŸ˜’πŸ˜’πŸ˜’πŸ˜’,fgh",
"333333333333333333,😎b😎b😎b😎b😎b😎b,ijk",
},
expected = {
"column1(number) β”‚column2(emoji) β”‚column3(string) ",
" 111β”‚πŸ˜€ β”‚abcde ",
" 222222222222β”‚πŸ˜’πŸ˜’πŸ˜’πŸ˜’ β”‚fgh ",
" 333333333333333333β”‚πŸ˜Žb😎b😎b😎b😎b😎b β”‚ijk ",
},
},
}

vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
local co = coroutine.running()
metrics.compute_csv_metrics(bufnr, opts, function(fields, column_max_widths)
local v = view.CsvView:new(bufnr, fields, column_max_widths, opts)
-- run test cases.
for name, c in pairs(cases) do
it(name, function() ---@async
-- create buffer and set lines
local bufnr = vim.api.nvim_create_buf(false, true)
local opts = config.get(c.opts)
local lines = c.lines
local expected = c.expected
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)

-- test
v:render(1, vim.api.nvim_buf_line_count(bufnr))
local actual = get_lines_with_extmarks(bufnr, ns)
for i, line in ipairs(actual) do
assert.are.same(expected[i], line)
end
vim.schedule(function()
coroutine.resume(co)
-- compute metrics
local co = coroutine.running()
metrics.compute_csv_metrics(bufnr, opts, function(fields, column_max_widths)
vim.schedule(function()
coroutine.resume(co, fields, column_max_widths)
end)
end)
end)

coroutine.yield()
end)

it("display_mode = 'border'", function()
local bufnr = vim.api.nvim_create_buf(false, true)
local opts = config.get({
view = {
min_column_width = 5,
spacing = 1,
display_mode = "border",
},
})
local lines = {
"column1(number),column2(emoji),column3(string)",
"111,πŸ˜€,abcde",
"222222222222,πŸ˜’πŸ˜’πŸ˜’πŸ˜’,fgh",
"333333333333333333,😎b😎b😎b😎b😎b😎b,ijk",
}
local expected = {
"column1(number) β”‚column2(emoji) β”‚column3(string) ",
" 111β”‚πŸ˜€ β”‚abcde ",
" 222222222222β”‚πŸ˜’πŸ˜’πŸ˜’πŸ˜’ β”‚fgh ",
" 333333333333333333β”‚πŸ˜Žb😎b😎b😎b😎b😎b β”‚ijk ",
}
-- wait for the completion of the metrics computation
local fields, column_max_widths = coroutine.yield()

vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
local co = coroutine.running()
metrics.compute_csv_metrics(bufnr, opts, function(fields, column_max_widths)
-- create view and render
local v = view.CsvView:new(bufnr, fields, column_max_widths, opts)

-- test
v:render(1, vim.api.nvim_buf_line_count(bufnr))

-- check the result
local actual = get_lines_with_extmarks(bufnr, ns)
for i, line in ipairs(actual) do
assert.are.same(expected[i], line)
end
vim.schedule(function()
coroutine.resume(co)
end)
end)

coroutine.yield()
end)
end
end)
end)
end)

0 comments on commit 25d963b

Please sign in to comment.