Skip to content

Commit

Permalink
feat(tests): dts testing
Browse files Browse the repository at this point in the history
feat: add DTS workflow and update DTS tests

- Add GitHub Actions workflow for DTS testing
- Update Makefile to include DTS target
- Modify DTS script to run multiple simulations
- Fix cursor position calculation in DTS utility

test: increase test count to 1 million

Increase the number of test iterations from 100k to 1M to improve test
coverage and reliability.

feat(tests): final fixes and notes around stage 1 dts
  • Loading branch information
tris203 committed Dec 19, 2024
1 parent 58b1f39 commit 6a924e9
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 4 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/dts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: DTS

on:
pull_request:
branches:
- "main"

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
nvim-versions: ['nightly']
os: [ubuntu-latest]
fail-fast: false
name: DTS Tests
steps:
- name: checkout
uses: actions/checkout@v4

- uses: rhysd/action-setup-vim@v1
with:
neovim: true
version: ${{ matrix.nvim-versions }}

- name: run dts
run: make dts
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
TESTS_INIT=tests/minimal.lua
TESTS_DIR=tests/
DTS_SCRIPT=tests/precognition/dts.lua
SEED_START=0
NUM_TESTS=1000000

.PHONY: test

Expand All @@ -9,3 +12,10 @@ test:
--noplugin \
-u ${TESTS_INIT} \
-c "PlenaryBustedDirectory ${TESTS_DIR} { minimal_init = '${TESTS_INIT}' }" \

dts:
@nvim \
--headless \
--noplugin \
-u ${TESTS_INIT} \
-l ${DTS_SCRIPT} ${SEED_START} ${NUM_TESTS} \
2 changes: 2 additions & 0 deletions lua/precognition/horizontal_motions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ function M.end_of_word(str, cursorcol, linelen, big_word, recursive)
if
(c_class == cc.punctuation and next_char_class ~= cc.punctuation)
or (next_char_class == cc.punctuation and c_class ~= cc.punctuation)
or (c_class == cc.emoji and next_char_class ~= cc.emoji)
or (next_char_class == cc.emoji and c_class ~= cc.emoji)
then
offset = offset + 1
char = vim.fn.strcharpart(str, offset - 1, 1)
Expand Down
3 changes: 3 additions & 0 deletions lua/precognition/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,9 @@ local state = {
ns = function()
return ns
end,
default_hint_config = function()
return defaultHintConfig
end,
is_visible = function()
return visible
end,
Expand Down
17 changes: 13 additions & 4 deletions tests/precognition/dts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function M.test(seed)
W = hm.next_word_boundary(cur_line, cursorcol, line_len, true),
E = hm.end_of_word(cur_line, cursorcol, line_len, true),
B = hm.prev_word_boundary(cur_line, cursorcol, line_len, true),
-- TODO: fix some edge cases around pairs and we can enable this
-- MatchingPair = hm.matching_pair(cur_line, cursorcol, line_len)(cur_line, cursorcol, line_len),
Dollar = hm.line_end(cur_line, cursorcol, line_len),
Zero = 1,
Expand All @@ -42,18 +43,26 @@ function M.test(seed)
for loc, col in pairs(virtual_line_marks) do
local key = precognition.default_hint_config[loc].text
vim.api.nvim_set_current_buf(temp_buf)
-- vim.api.nvim_win_set_cursor(0, { 1, cursorcol - 1 })
vim.fn.setcursorcharpos(1, cursorcol)
local cur_before = vim.fn.getcursorcharpos(0)
vim.api.nvim_feedkeys(key, "ntx", true)
local cur = vim.fn.getcursorcharpos(0)
local actual_col = cur[3]
-- eq(cursorcol, 1)
local cur_after = vim.fn.getcursorcharpos(0)
local actual_col = cur_after[3]
if col ~= 0 then
if col ~= actual_col then
vim.print(string.format("[SEED: %d]%s", seed, cur_line))
vim.print(
string.format("with cursor at %s, motion %s, expected %s, got %s", cursorcol, key, col, actual_col)
)
vim.print(
string.format(
"before: %s, input %s, after: %s",
vim.inspect(cur_before),
key,
vim.inspect(cur_after)
)
)
vim.print(vim.inspect(virtual_line_marks))
os.exit(1)
end
end
Expand Down
11 changes: 11 additions & 0 deletions tests/precognition/horizontal_motions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,17 @@ describe("edge case", function()
eq(3, hm.prev_word_boundary(str, 5, len, false))
end)

it("multibyte end_of_word", function()
local str = "# 💭👀precognition.nvim"
local len = vim.fn.strcharlen(str)
eq(4, hm.end_of_word(str, 1, len, false))
eq(16, hm.end_of_word(str, 4, len, false))

str = "🌞7😘aa"
len = vim.fn.strcharlen(str)
eq(3, hm.end_of_word(str, 2, len, false))
end)

it("quoted strings", function()
local str = 'this = "that"'
eq(8, hm.end_of_word(str, 6, #str, false))
Expand Down
52 changes: 52 additions & 0 deletions tests/precognition/utils/dts.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
local M = {}

local ranges = {
{ 32, 126 }, -- Basic Latin (ASCII)
--TODO: Add other character ranges but this opens a load of multibyte edge cases
-- { 160, 591 }, -- Latin-1 Supplement and Latin Extended-A
-- { 880, 2047 }, -- Greek, Cyrillic, Armenian, Hebrew
-- { 8192, 8303 }, -- General Punctuation
-- { 9728, 9983 }, -- Miscellaneous Symbols
-- { 12352, 12447 }, -- Hiragana
-- { 19904, 19967 }, -- Mahjong Tiles
-- { 0x1F300, 0x1F6FF }, -- Emoji
}

---@class dts.Random
---@field cursor_col number
---@field line string

---Generate a random line with Unicode characters.
---@param seed number
---@return dts.Random
function M.generate_random_line(seed)
math.randomseed(seed) -- Set the seed for reproducibility

-- Randomize the line length (e.g., between 20 and 100 characters)
local line_length = math.random(20, 100)

-- Function to generate a random printable Unicode character
local function random_unicode_char()
-- Randomly pick a range
local range = ranges[math.random(1, #ranges)]
-- Generate a random codepoint within the selected range
local codepoint = math.random(range[1], range[2])
return vim.fn.nr2char(codepoint) -- Convert codepoint to UTF-8 character
end

-- Generate the random line with Unicode characters
local line = ""
for _ = 1, line_length do
line = line .. random_unicode_char()
end

-- Choose a random cursor position within the line
local cursor_col = math.random(1, vim.fn.strcharlen(line)) -- Ensure valid cursor position
---@type dts.Random
return {
cursor_col = cursor_col,
line = line,
}
end

return M

0 comments on commit 6a924e9

Please sign in to comment.