From b667b09c4a0f83f4d543ccc6e5256b2c035a9f62 Mon Sep 17 00:00:00 2001 From: Tristan Knight Date: Tue, 21 May 2024 22:18:02 +0100 Subject: [PATCH] tests: better testing (#24) --- lua/precognition/utils.lua | 2 +- .../precognition/horizontal_motions_spec.lua | 256 ++++++++++-------- tests/precognition/virtline_spec.lua | 17 +- 3 files changed, 150 insertions(+), 125 deletions(-) diff --git a/lua/precognition/utils.lua b/lua/precognition/utils.lua index ab13953..04ac447 100644 --- a/lua/precognition/utils.lua +++ b/lua/precognition/utils.lua @@ -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) diff --git a/tests/precognition/horizontal_motions_spec.lua b/tests/precognition/horizontal_motions_spec.lua index fc47ef9..5500d5d 100644 --- a/tests/precognition/horizontal_motions_spec.lua +++ b/tests/precognition/horizontal_motions_spec.lua @@ -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() @@ -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() @@ -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) @@ -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) diff --git a/tests/precognition/virtline_spec.lua b/tests/precognition/virtline_spec.lua index ce816d6..a925e68 100644 --- a/tests/precognition/virtline_spec.lua +++ b/tests/precognition/virtline_spec.lua @@ -42,6 +42,7 @@ describe("Build Virtual Line", function() it("can build a complex virtual line", function() ---@type Precognition.VirtLine + ---@diagnostic disable-next-line: missing-fields local marks = { Caret = 1, e = 6, @@ -78,9 +79,9 @@ describe("Build Virtual Line", function() local line_len = vim.fn.strcharlen(cur_line) local virt_line = precognition.build_virt_line({ - w = hm.next_word_boundary(cur_line, cursorcol, line_len), - e = hm.end_of_word(cur_line, cursorcol, line_len), - b = hm.prev_word_boundary(cur_line, cursorcol, line_len), + w = hm.next_word_boundary(cur_line, cursorcol, line_len, false), + e = hm.end_of_word(cur_line, cursorcol, line_len, false), + b = hm.prev_word_boundary(cur_line, cursorcol, line_len, false), Caret = hm.line_start_non_whitespace(cur_line, cursorcol, line_len), Dollar = hm.line_end(cur_line, cursorcol, line_len), }, line_len) @@ -98,9 +99,9 @@ describe("Build Virtual Line", function() local line_len = vim.fn.strcharlen(cur_line) local virt_line = precognition.build_virt_line({ - w = hm.next_word_boundary(cur_line, cursorcol, line_len), - e = hm.end_of_word(cur_line, cursorcol, line_len), - b = hm.prev_word_boundary(cur_line, cursorcol, line_len), + w = hm.next_word_boundary(cur_line, cursorcol, line_len, false), + e = hm.end_of_word(cur_line, cursorcol, line_len, false), + b = hm.prev_word_boundary(cur_line, cursorcol, line_len, false), Caret = hm.line_start_non_whitespace(cur_line, cursorcol, line_len), Dollar = hm.line_end(cur_line, cursorcol, line_len), }, line_len) @@ -113,6 +114,7 @@ end) describe("Priority", function() it("0 priority item is not added", function() precognition.setup({ + ---@diagnostic disable-next-line: missing-fields hints = { Caret = { prio = 0, @@ -138,6 +140,7 @@ describe("Priority", function() it("a higher priority mark in the same space takes priority", function() precognition.setup({ + ---@diagnostic disable-next-line: missing-fields hints = { Caret = { prio = 0, @@ -163,6 +166,7 @@ describe("Priority", function() it("a higher priority mark in the same space takes priority", function() precognition.setup({ + ---@diagnostic disable-next-line: missing-fields hints = { Caret = { prio = 1, @@ -185,6 +189,7 @@ describe("Priority", function() eq(1, #virtual_line[1][1]) precognition.setup({ + ---@diagnostic disable-next-line: missing-fields hints = { Caret = { prio = 100,