Skip to content

Commit

Permalink
tests: better testing (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
tris203 authored May 21, 2024
1 parent 672a00f commit b667b09
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 125 deletions.
2 changes: 1 addition & 1 deletion lua/precognition/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ M.char_classes = {
---@param big_word boolean
---@return integer
function M.char_class(char, big_word)
big_word = big_word or false
assert(type(big_word) == "boolean", "big_word must be a boolean")
local cc = M.char_classes
local byte = string.byte(char)

Expand Down
256 changes: 138 additions & 118 deletions tests/precognition/horizontal_motions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,40 @@ local eq = assert.are.same

describe("boundaries", function()
it("finds the next word boundary", function()
eq(5, hm.next_word_boundary("abc efg", 1, 7, false))
eq(5, hm.next_word_boundary("abc efg", 2, 7, false))
eq(5, hm.next_word_boundary("abc efg", 3, 7, false))
eq(5, hm.next_word_boundary("abc efg", 4, 7, false))
eq(0, hm.next_word_boundary("abc efg", 5, 7, false))
eq(0, hm.next_word_boundary("abc efg", 6, 7, false))
eq(0, hm.next_word_boundary("abc efg", 7, 7, false))
local str = "abc efg"
eq(5, hm.next_word_boundary(str, 1, #str, false))
eq(5, hm.next_word_boundary(str, 2, #str, false))
eq(5, hm.next_word_boundary(str, 3, #str, false))
eq(5, hm.next_word_boundary(str, 4, #str, false))
eq(0, hm.next_word_boundary(str, 5, #str, false))
eq(0, hm.next_word_boundary(str, 6, #str, false))
eq(0, hm.next_word_boundary(str, 7, #str, false))

eq(9, hm.next_word_boundary("slighly more complex test", 1, 22, false))
eq(9, hm.next_word_boundary("slighly more complex test", 2, 22, false))
eq(14, hm.next_word_boundary("slighly more complex test", 10, 22, false))
eq(14, hm.next_word_boundary("slighly more complex test", 13, 22, false))
eq(22, hm.next_word_boundary("slighly more complex test", 15, 22, false))
eq(22, hm.next_word_boundary("slighly more complex test", 21, 22, false))
str = "slighly more complex test"
eq(9, hm.next_word_boundary(str, 1, #str, false))
eq(9, hm.next_word_boundary(str, 2, #str, false))
eq(14, hm.next_word_boundary(str, 10, #str, false))
eq(14, hm.next_word_boundary(str, 13, #str, false))
eq(22, hm.next_word_boundary(str, 15, #str, false))
eq(22, hm.next_word_boundary(str, 21, #str, false))

eq(5, hm.next_word_boundary(" myFunction(example, stuff)", 1, 30, false))
eq(5, hm.next_word_boundary(" myFunction(example, stuff)", 2, 30, false))
eq(5, hm.next_word_boundary(" myFunction(example, stuff)", 3, 30, false))
eq(15, hm.next_word_boundary(" myFunction(example, stuff)", 5, 30, false))
eq(16, hm.next_word_boundary(" myFunction(example, stuff)", 15, 30, false))
eq(23, hm.next_word_boundary(" myFunction(example, stuff)", 16, 30, false))
eq(25, hm.next_word_boundary(" myFunction(example, stuff)", 23, 30, false))
eq(25, hm.next_word_boundary(" myFunction(example, stuff)", 24, 30, false))
eq(30, hm.next_word_boundary(" myFunction(example, stuff)", 25, 30, false))
eq(0, hm.next_word_boundary(" myFunction(example, stuff)", 30, 30, false))
str = " myFunction(example, stuff)"
eq(5, hm.next_word_boundary(str, 1, #str, false))
eq(5, hm.next_word_boundary(str, 2, #str, false))
eq(5, hm.next_word_boundary(str, 3, #str, false))
eq(15, hm.next_word_boundary(str, 5, #str, false))
eq(16, hm.next_word_boundary(str, 15, #str, false))
eq(23, hm.next_word_boundary(str, 16, #str, false))
eq(25, hm.next_word_boundary(str, 23, #str, false))
eq(25, hm.next_word_boundary(str, 24, #str, false))
eq(30, hm.next_word_boundary(str, 25, #str, false))
eq(0, hm.next_word_boundary(str, 30, #str, false))
end)

it("finds next big word boundary", function()
eq(12, hm.next_word_boundary("a big.word string", 3, 17, true))
eq(12, hm.next_word_boundary("a big.word string", 4, 17, true))
local str = "a big.word string"
eq(12, hm.next_word_boundary(str, 3, #str, true))
eq(12, hm.next_word_boundary(str, 4, #str, true))
end)

it("can walk string with w", function()
Expand Down Expand Up @@ -62,45 +66,49 @@ describe("boundaries", function()

describe("previous word boundary", function()
it("finds the previous word boundary", function()
eq(0, hm.prev_word_boundary("abc efg", 1, 7, false))
eq(1, hm.prev_word_boundary("abc efg", 2, 7, false))
eq(1, hm.prev_word_boundary("abc efg", 3, 7, false))
eq(1, hm.prev_word_boundary("abc efg", 4, 7, false))
eq(1, hm.prev_word_boundary("abc efg", 5, 7, false))
eq(5, hm.prev_word_boundary("abc efg", 6, 7, false))
eq(5, hm.prev_word_boundary("abc efg", 7, 7, false))
local str = "abc efg"
eq(0, hm.prev_word_boundary(str, 1, #str, false))
eq(1, hm.prev_word_boundary(str, 2, #str, false))
eq(1, hm.prev_word_boundary(str, 3, #str, false))
eq(1, hm.prev_word_boundary(str, 4, #str, false))
eq(1, hm.prev_word_boundary(str, 5, #str, false))
eq(5, hm.prev_word_boundary(str, 6, #str, false))
eq(5, hm.prev_word_boundary(str, 7, #str, false))

eq(9, hm.prev_word_boundary("slighly more complex test", 10, 22, false))
eq(9, hm.prev_word_boundary("slighly more complex test", 11, 22, false))
eq(14, hm.prev_word_boundary("slighly more complex test", 15, 22, false))
eq(14, hm.prev_word_boundary("slighly more complex test", 16, 22, false))
eq(22, hm.prev_word_boundary("slighly more complex test", 23, 22, false))
eq(22, hm.prev_word_boundary("slighly more complex test", 24, 22, false))
eq(22, hm.prev_word_boundary("slighly more complex test", 25, 22, false))
eq(0, hm.prev_word_boundary("slighly more complex test", 1, 22, false))
str = "slighly more complex test"
eq(9, hm.prev_word_boundary(str, 10, #str, false))
eq(9, hm.prev_word_boundary(str, 11, #str, false))
eq(14, hm.prev_word_boundary(str, 15, #str, false))
eq(14, hm.prev_word_boundary(str, 16, #str, false))
eq(22, hm.prev_word_boundary(str, 23, #str, false))
eq(22, hm.prev_word_boundary(str, 24, #str, false))
eq(22, hm.prev_word_boundary(str, 25, #str, false))
eq(0, hm.prev_word_boundary(str, 1, #str, false))

eq(0, hm.prev_word_boundary(" myFunction(example, stuff)", 1, 30, false))
eq(0, hm.prev_word_boundary(" myFunction(example, stuff)", 2, 30, false))
eq(0, hm.prev_word_boundary(" myFunction(example, stuff)", 3, 30, false))
eq(0, hm.prev_word_boundary(" myFunction(example, stuff)", 4, 30, false))
eq(0, hm.prev_word_boundary(" myFunction(example, stuff)", 5, 30, false))
eq(5, hm.prev_word_boundary(" myFunction(example, stuff)", 6, 30, false))
eq(5, hm.prev_word_boundary(" myFunction(example, stuff)", 15, 30, false))
eq(15, hm.prev_word_boundary(" myFunction(example, stuff)", 16, 30, false))
eq(16, hm.prev_word_boundary(" myFunction(example, stuff)", 17, 30, false))
eq(16, hm.prev_word_boundary(" myFunction(example, stuff)", 18, 30, false))
eq(16, hm.prev_word_boundary(" myFunction(example, stuff)", 19, 30, false))
eq(23, hm.prev_word_boundary(" myFunction(example, stuff)", 25, 30, false))
eq(25, hm.prev_word_boundary(" myFunction(example, stuff)", 26, 30, false))
eq(25, hm.prev_word_boundary(" myFunction(example, stuff)", 27, 30, false))
eq(25, hm.prev_word_boundary(" myFunction(example, stuff)", 28, 30, false))
eq(25, hm.prev_word_boundary(" myFunction(example, stuff)", 29, 30, false))
eq(25, hm.prev_word_boundary(" myFunction(example, stuff)", 30, 30, false))
str = " myFunction(example, stuff)"
eq(0, hm.prev_word_boundary(str, 1, #str, false))
eq(0, hm.prev_word_boundary(str, 2, #str, false))
eq(0, hm.prev_word_boundary(str, 3, #str, false))
eq(0, hm.prev_word_boundary(str, 4, #str, false))
eq(0, hm.prev_word_boundary(str, 5, #str, false))
eq(5, hm.prev_word_boundary(str, 6, #str, false))
eq(5, hm.prev_word_boundary(str, 15, #str, false))
eq(15, hm.prev_word_boundary(str, 16, #str, false))
eq(16, hm.prev_word_boundary(str, 17, #str, false))
eq(16, hm.prev_word_boundary(str, 18, #str, false))
eq(16, hm.prev_word_boundary(str, 19, #str, false))
eq(23, hm.prev_word_boundary(str, 25, #str, false))
eq(25, hm.prev_word_boundary(str, 26, #str, false))
eq(25, hm.prev_word_boundary(str, 27, #str, false))
eq(25, hm.prev_word_boundary(str, 28, #str, false))
eq(25, hm.prev_word_boundary(str, 29, #str, false))
eq(25, hm.prev_word_boundary(str, 30, #str, false))
end)

it("finds previous big word boundary", function()
eq(3, hm.prev_word_boundary("a big.word string", 10, 17, true))
eq(3, hm.prev_word_boundary("a big.word string", 10, 17, true))
local str = "a big.word string"
eq(3, hm.prev_word_boundary(str, 10, #str, true))
eq(3, hm.prev_word_boundary(str, 10, #str, true))
end)

it("can walk string with b", function()
Expand All @@ -127,33 +135,37 @@ describe("boundaries", function()

describe("end of current word", function()
it("finds the end of words", function()
eq(3, hm.end_of_word("abc efg", 1, 7, false))
eq(3, hm.end_of_word("abc efg", 2, 7, false))
eq(7, hm.end_of_word("abc efg", 3, 7, false))
local str = "abc efg"
eq(3, hm.end_of_word(str, 1, #str, false))
eq(3, hm.end_of_word(str, 2, #str, false))
eq(7, hm.end_of_word(str, 3, #str, false))

eq(7, hm.end_of_word("slighly more complex test", 1, 25, false))
eq(7, hm.end_of_word("slighly more complex test", 2, 25, false))
eq(12, hm.end_of_word("slighly more complex test", 10, 25, false))
eq(20, hm.end_of_word("slighly more complex test", 13, 25, false))
eq(20, hm.end_of_word("slighly more complex test", 15, 25, false))
eq(25, hm.end_of_word("slighly more complex test", 21, 25, false))
str = "slighly more complex test"
eq(7, hm.end_of_word(str, 1, #str, false))
eq(7, hm.end_of_word(str, 2, #str, false))
eq(12, hm.end_of_word(str, 10, #str, false))
eq(20, hm.end_of_word(str, 13, #str, false))
eq(20, hm.end_of_word(str, 15, #str, false))
eq(25, hm.end_of_word(str, 21, #str, false))

eq(14, hm.end_of_word(" myFunction(example, stuff)", 1, 30, false))
eq(14, hm.end_of_word(" myFunction(example, stuff)", 2, 30, false))
eq(14, hm.end_of_word(" myFunction(example, stuff)", 3, 30, false))
eq(14, hm.end_of_word(" myFunction(example, stuff)", 5, 30, false))
eq(15, hm.end_of_word(" myFunction(example, stuff)", 14, 30, false))
eq(22, hm.end_of_word(" myFunction(example, stuff)", 15, 30, false))
eq(22, hm.end_of_word(" myFunction(example, stuff)", 16, 30, false))
eq(29, hm.end_of_word(" myFunction(example, stuff)", 23, 30, false))
eq(29, hm.end_of_word(" myFunction(example, stuff)", 24, 30, false))
eq(29, hm.end_of_word(" myFunction(example, stuff)", 25, 30, false))
eq(30, hm.end_of_word(" myFunction(example, stuff)", 29, 30, false))
eq(0, hm.end_of_word(" myFunction(example, stuff)", 30, 30, false))
str = " myFunction(example, stuff)"
eq(14, hm.end_of_word(str, 1, #str, false))
eq(14, hm.end_of_word(str, 2, #str, false))
eq(14, hm.end_of_word(str, 3, #str, false))
eq(14, hm.end_of_word(str, 5, #str, false))
eq(15, hm.end_of_word(str, 14, #str, false))
eq(22, hm.end_of_word(str, 15, #str, false))
eq(22, hm.end_of_word(str, 16, #str, false))
eq(29, hm.end_of_word(str, 23, #str, false))
eq(29, hm.end_of_word(str, 24, #str, false))
eq(29, hm.end_of_word(str, 25, #str, false))
eq(30, hm.end_of_word(str, 29, #str, false))
eq(0, hm.end_of_word(str, 30, #str, false))
end)

it("finds the end of the current big word", function()
eq(10, hm.end_of_word("a big.word string", 3, 17, true))
local str = "a big.word string"
eq(10, hm.end_of_word(str, 3, #str, true))
end)
end)
end)
Expand Down Expand Up @@ -182,66 +194,74 @@ end)

describe("matching brackets", function()
it("if cursor is over a bracket it can find the pair", function()
eq(9, hm.matching_bracket("abc (efg)", 5, 9))
eq(0, hm.matching_bracket("abc (efg)", 6, 9))
eq(0, hm.matching_bracket("abc (efg)", 7, 9))
eq(0, hm.matching_bracket("abc (efg)", 8, 9))
eq(5, hm.matching_bracket("abc (efg)", 9, 9))
local str = "abc (efg)"
eq(9, hm.matching_bracket(str, 5, #str))
eq(0, hm.matching_bracket(str, 6, #str))
eq(0, hm.matching_bracket(str, 7, #str))
eq(0, hm.matching_bracket(str, 8, #str))
eq(5, hm.matching_bracket(str, 9, #str))
end)

it("if cursor is over a square bracket it can find the pair", function()
eq(9, hm.matching_bracket("abc [efg]", 5, 9))
eq(0, hm.matching_bracket("abc [efg]", 6, 9))
eq(0, hm.matching_bracket("abc [efg]", 7, 9))
eq(0, hm.matching_bracket("abc [efg]", 8, 9))
eq(5, hm.matching_bracket("abc [efg]", 9, 9))
local str = "abc [efg]"
eq(9, hm.matching_bracket(str, 5, #str))
eq(0, hm.matching_bracket(str, 6, #str))
eq(0, hm.matching_bracket(str, 7, #str))
eq(0, hm.matching_bracket(str, 8, #str))
eq(5, hm.matching_bracket(str, 9, #str))
end)

it("if cursor is over a curly bracket it can find the pair", function()
eq(9, hm.matching_bracket("abc {efg}", 5, 9))
eq(0, hm.matching_bracket("abc {efg}", 6, 9))
eq(0, hm.matching_bracket("abc {efg}", 7, 9))
eq(0, hm.matching_bracket("abc {efg}", 8, 9))
eq(5, hm.matching_bracket("abc {efg}", 9, 9))
local str = "abc {efg}"
eq(9, hm.matching_bracket(str, 5, #str))
eq(0, hm.matching_bracket(str, 6, #str))
eq(0, hm.matching_bracket(str, 7, #str))
eq(0, hm.matching_bracket(str, 8, #str))
eq(5, hm.matching_bracket(str, 9, #str))
end)

it("nested brackets find the correct pair", function()
eq(19, hm.matching_bracket("abc (efg [hij] klm)", 5, 19))
eq(0, hm.matching_bracket("abc (efg [hij] klm)", 6, 19))
eq(14, hm.matching_bracket("abc (efg [hij] klm)", 10, 19))
eq(10, hm.matching_bracket("abc (efg [hij] klm)", 14, 19))
eq(0, hm.matching_bracket("abc (efg [hij] klm)", 15, 19))
eq(5, hm.matching_bracket("abc (efg [hij] klm)", 19, 19))
local str = "abc (efg [hij] klm)"
eq(19, hm.matching_bracket(str, 5, #str))
eq(0, hm.matching_bracket(str, 6, #str))
eq(14, hm.matching_bracket(str, 10, #str))
eq(10, hm.matching_bracket(str, 14, #str))
eq(0, hm.matching_bracket(str, 15, #str))
eq(5, hm.matching_bracket(str, 19, #str))
end)

it("nested brackets of the same type find the correct pair", function()
eq(19, hm.matching_bracket("abc (efg (hij) klm)", 5, 19))
eq(0, hm.matching_bracket("abc (efg (hij) klm)", 6, 19))
eq(14, hm.matching_bracket("abc (efg (hij) klm)", 10, 19))
eq(10, hm.matching_bracket("abc (efg (hij) klm)", 14, 19))
eq(0, hm.matching_bracket("abc (efg (hij) klm)", 15, 19))
eq(5, hm.matching_bracket("abc (efg (hij) klm)", 19, 19))
local str = "abc (efg (hij) klm)"
eq(19, hm.matching_bracket(str, 5, #str))
eq(0, hm.matching_bracket(str, 6, #str))
eq(14, hm.matching_bracket(str, 10, #str))
eq(10, hm.matching_bracket(str, 14, #str))
eq(0, hm.matching_bracket(str, 15, #str))
eq(5, hm.matching_bracket(str, 19, #str))
end)

it("if cursor is over an unclosed bracket it returns 0", function()
eq(0, hm.matching_bracket("abc (efg", 5, 8))
eq(0, hm.matching_bracket("abc [efg", 5, 8))
eq(0, hm.matching_bracket("abc {efg", 5, 8))
local str = "abc (efg"
eq(0, hm.matching_bracket(str, 5, #str))
eq(0, hm.matching_bracket(str, 5, #str))
eq(0, hm.matching_bracket(str, 5, #str))
end)
end)

describe("matching comments", function()
it("if cursor is over a comment it can find the pair", function()
eq(11, hm.matching_comment("abc /*efg*/", 5, 11))
eq(11, hm.matching_comment("abc /*efg*/", 6, 11))
eq(0, hm.matching_comment("abc /*efg*/", 7, 11))
eq(5, hm.matching_comment("abc /*efg*/", 10, 11))
eq(5, hm.matching_comment("abc /*efg*/", 11, 11))
local str = "abc /*efg*/"
eq(11, hm.matching_comment(str, 5, #str))
eq(11, hm.matching_comment(str, 6, #str))
eq(0, hm.matching_comment(str, 7, #str))
eq(5, hm.matching_comment(str, 10, #str))
eq(5, hm.matching_comment(str, 11, #str))
end)

it("if cursor is over an unclosed comment it returns 0", function()
eq(0, hm.matching_comment("abc /*efg", 5, 9))
eq(0, hm.matching_comment("abc /*efg", 6, 9))
local str = "abc /*efg"
eq(0, hm.matching_comment(str, 5, #str))
eq(0, hm.matching_comment(str, 6, #str))
end)
end)

Expand Down
Loading

0 comments on commit b667b09

Please sign in to comment.