Skip to content

Commit

Permalink
Streamline acceptance tests
Browse files Browse the repository at this point in the history
* Remove outer describe
  Is now easier to read from indent and less hierarchy to remember
* use tw instead of treewalker
  Just more ergonomic
  • Loading branch information
aaronik committed Dec 18, 2024
1 parent 208c9a0 commit 15153f2
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 150 deletions.
134 changes: 66 additions & 68 deletions tests/treewalker/highlight_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,86 +2,84 @@ local util = require "treewalker.util"
local load_fixture = require "tests.load_fixture"
local stub = require 'luassert.stub'
local assert = require "luassert"
local treewalker = require 'treewalker'
local tw = require 'treewalker'
local ops = require 'treewalker.ops'

describe("Treewalker highlighting", function()
local highlight_stub = stub(ops, "highlight")
local highlight_stub = stub(ops, "highlight")

-- use with rows as they're numbered in vim lines (1-indexed)
local function assert_highlighted(srow, scol, erow, ecol, desc)
assert.same(
{ srow - 1, scol - 1, erow - 1, ecol },
highlight_stub.calls[1].refs[1],
"highlight wrong for: " .. desc
)
end
-- use with rows as they're numbered in vim lines (1-indexed)
local function assert_highlighted(srow, scol, erow, ecol, desc)
assert.same(
{ srow - 1, scol - 1, erow - 1, ecol },
highlight_stub.calls[1].refs[1],
"highlight wrong for: " .. desc
)
end

describe("regular lua file: ", function()
load_fixture("/lua.lua")
describe("Highlights in a regular lua file: ", function()
load_fixture("/lua.lua")

before_each(function()
treewalker.setup({ highlight = true })
highlight_stub = stub(ops, "highlight")
end)
before_each(function()
tw.setup({ highlight = true })
highlight_stub = stub(ops, "highlight")
end)

it("respects highlight config option", function()
treewalker.setup() -- highlight defaults to true, doesn't blow up with empty setup
vim.fn.cursor(23, 5)
treewalker.move_out()
treewalker.move_down()
treewalker.move_up()
treewalker.move_in()
assert.equal(4, #highlight_stub.calls)
it("respects highlight config option", function()
tw.setup() -- highlight defaults to true, doesn't blow up with empty setup
vim.fn.cursor(23, 5)
tw.move_out()
tw.move_down()
tw.move_up()
tw.move_in()
assert.equal(4, #highlight_stub.calls)

highlight_stub = stub(ops, "highlight")
treewalker.setup({ highlight = false })
vim.fn.cursor(23, 5)
treewalker.move_out()
treewalker.move_down()
treewalker.move_up()
treewalker.move_in()
assert.equal(0, #highlight_stub.calls)
highlight_stub = stub(ops, "highlight")
tw.setup({ highlight = false })
vim.fn.cursor(23, 5)
tw.move_out()
tw.move_down()
tw.move_up()
tw.move_in()
assert.equal(0, #highlight_stub.calls)

highlight_stub = stub(ops, "highlight")
treewalker.setup({ highlight = true })
vim.fn.cursor(23, 5)
treewalker.move_out()
treewalker.move_down()
treewalker.move_up()
treewalker.move_in()
assert.equal(4, #highlight_stub.calls)
end)
highlight_stub = stub(ops, "highlight")
tw.setup({ highlight = true })
vim.fn.cursor(23, 5)
tw.move_out()
tw.move_down()
tw.move_up()
tw.move_in()
assert.equal(4, #highlight_stub.calls)
end)

it("highlights whole functions", function()
vim.fn.cursor(10, 1)
treewalker.move_down()
assert_highlighted(21, 1, 28, 3, "is_jump_target function")
end)
it("highlights whole functions", function()
vim.fn.cursor(10, 1)
tw.move_down()
assert_highlighted(21, 1, 28, 3, "is_jump_target function")
end)

it("highlights whole lines starting with identifiers", function()
vim.fn.cursor(134, 5)
treewalker.move_up()
assert_highlighted(133, 5, 133, 33, "table.insert call")
end)
it("highlights whole lines starting with identifiers", function()
vim.fn.cursor(134, 5)
tw.move_up()
assert_highlighted(133, 5, 133, 33, "table.insert call")
end)

it("highlights whole lines starting with assignments", function()
vim.fn.cursor(133, 5)
treewalker.move_down()
assert_highlighted(134, 5, 134, 18, "child = iter()")
end)
it("highlights whole lines starting with assignments", function()
vim.fn.cursor(133, 5)
tw.move_down()
assert_highlighted(134, 5, 134, 18, "child = iter()")
end)

it("doesn't highlight the whole file", function()
vim.fn.cursor(3, 1)
treewalker.move_up()
assert_highlighted(1, 1, 1, 39, "first line")
end)
it("doesn't highlight the whole file", function()
vim.fn.cursor(3, 1)
tw.move_up()
assert_highlighted(1, 1, 1, 39, "first line")
end)

-- Note this is highly language dependent, so this test is not so powerful
it("highlights only the first item in a block", function()
vim.fn.cursor(27, 3)
treewalker.move_up()
assert_highlighted(22, 3, 26, 5, "child = iter()")
end)
-- Note this is highly language dependent, so this test is not so powerful
it("highlights only the first item in a block", function()
vim.fn.cursor(27, 3)
tw.move_up()
assert_highlighted(22, 3, 26, 5, "child = iter()")
end)
end)
161 changes: 79 additions & 82 deletions tests/treewalker/movement_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local util = require "treewalker.util"
local load_fixture = require "tests.load_fixture"
local stub = require 'luassert.stub'
local assert = require "luassert"
local treewalker = require 'treewalker'
local tw = require 'treewalker'
local ops = require 'treewalker.ops'

-- Assert the cursor is in the expected position
Expand All @@ -18,98 +18,95 @@ local function assert_cursor_at(line, column, msg)
assert.are.same({ line, column }, { current_line, current_column }, msg)
end

describe("Treewalker movement", function()
describe("regular lua file: ", function()
load_fixture("/lua.lua")
describe("Movement in a regular lua file: ", function()
load_fixture("/lua.lua")

it("moves up and down at the same pace", function()
vim.fn.cursor(1, 1) -- Reset cursor
treewalker.move_down()
assert_cursor_at(3, 1)
treewalker.move_down()
assert_cursor_at(5, 1)
treewalker.move_down()
assert_cursor_at(10, 1)
treewalker.move_down()
assert_cursor_at(21, 1)
treewalker.move_up()
assert_cursor_at(10, 1)
treewalker.move_up()
assert_cursor_at(5, 1)
treewalker.move_up()
assert_cursor_at(3, 1)
treewalker.move_up()
assert_cursor_at(1, 1)
end)

it("doesn't consider empty lines to be outer scopes", function()
vim.fn.cursor(85, 1)
treewalker.move_down()
assert_cursor_at(88, 3, "local")
it("moves up and down at the same pace", function()
vim.fn.cursor(1, 1) -- Reset cursor
tw.move_down()
assert_cursor_at(3, 1)
tw.move_down()
assert_cursor_at(5, 1)
tw.move_down()
assert_cursor_at(10, 1)
tw.move_down()
assert_cursor_at(21, 1)
tw.move_up()
assert_cursor_at(10, 1)
tw.move_up()
assert_cursor_at(5, 1)
tw.move_up()
assert_cursor_at(3, 1)
tw.move_up()
assert_cursor_at(1, 1)
end)

vim.fn.cursor(85, 1)
treewalker.move_up()
assert_cursor_at(84, 3, "end")
end)
it("doesn't consider empty lines to be outer scopes", function()
vim.fn.cursor(85, 1)
tw.move_down()
assert_cursor_at(88, 3, "local")
vim.fn.cursor(85, 1)
tw.move_up()
assert_cursor_at(84, 3, "end")
end)

it("goes into functions eagerly", function()
vim.fn.cursor(143, 1) -- In a bigger function
treewalker.move_in()
assert_cursor_at(144, 3)
treewalker.move_in()
assert_cursor_at(147, 5)
treewalker.move_in()
assert_cursor_at(149, 7)
end)
it("goes into functions eagerly", function()
vim.fn.cursor(143, 1) -- In a bigger function
tw.move_in()
assert_cursor_at(144, 3)
tw.move_in()
assert_cursor_at(147, 5)
tw.move_in()
assert_cursor_at(149, 7)
end)

it("doesn't jump into a comment", function()
vim.fn.cursor(177, 1)
treewalker.move_in()
assert_cursor_at(179, 3, "local")
end)
it("doesn't jump into a comment", function()
vim.fn.cursor(177, 1)
tw.move_in()
assert_cursor_at(179, 3, "local")
end)

it("goes out of functions", function()
vim.fn.cursor(149, 7)
treewalker.move_out()
assert_cursor_at(148, 5, "if")
treewalker.move_out()
assert_cursor_at(146, 3, "while")
treewalker.move_out()
assert_cursor_at(143, 1, "function")
end)
it("goes out of functions", function()
vim.fn.cursor(149, 7)
tw.move_out()
assert_cursor_at(148, 5, "if")
tw.move_out()
assert_cursor_at(146, 3, "while")
tw.move_out()
assert_cursor_at(143, 1, "function")
end)
end)

describe("lua spec file: ", function()
load_fixture("/lua-spec.lua")
describe("Movement in a lua spec file: ", function()
load_fixture("/lua-spec.lua")

-- go to first describe
local function go_to_describe()
vim.fn.cursor(1, 1)
for _ = 1, 6 do
treewalker.move_down()
end
assert_cursor_at(17, 1, "describe")
-- go to first describe
local function go_to_describe()
vim.fn.cursor(1, 1)
for _ = 1, 6 do
tw.move_down()
end
assert_cursor_at(17, 1, "describe")
end

-- go to first load_buf
local function go_to_load_buf()
go_to_describe()
treewalker.move_in(); treewalker.move_in()
assert_cursor_at(19, 5, "load_buf")
end
-- go to first load_buf
local function go_to_load_buf()
go_to_describe()
tw.move_in(); tw.move_in()
assert_cursor_at(19, 5, "load_buf")
end

it("moves up and down at the same pace", function()
go_to_load_buf()
treewalker.move_down(); treewalker.move_down()
assert_cursor_at(41, 5, "it")
treewalker.move_up(); treewalker.move_up()
assert_cursor_at(19, 5, "load_buf")
end)
it("moves up and down at the same pace", function()
go_to_load_buf()
tw.move_down(); tw.move_down()
assert_cursor_at(41, 5, "it")
tw.move_up(); tw.move_up()
assert_cursor_at(19, 5, "load_buf")
end)

it("down moves at least one line", function()
go_to_load_buf()
treewalker.move_down()
assert_cursor_at(21, 5, "it")
end)
it("down moves at least one line", function()
go_to_load_buf()
tw.move_down()
assert_cursor_at(21, 5, "it")
end)
end)

0 comments on commit 15153f2

Please sign in to comment.