robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
4.3 KiB167 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each = t.describe, t.it, t.before_each
5local clear = n.clear
6local eq = t.eq
7local exec = n.exec
8local api = n.api
9local dedent = t.dedent
10local Screen = require('test.functional.ui.screen')
11
12---@param reverse_tree {[integer]:integer}
13local function generate_undo_tree_from_rev(reverse_tree)
14  for k, v in ipairs(reverse_tree) do
15    exec('undo ' .. v)
16    api.nvim_buf_set_lines(0, 0, -1, true, { tostring(k) })
17  end
18end
19---@param buf integer
20---@return string
21local function buf_get_lines_and_extmark(buf)
22  local lines = api.nvim_buf_get_lines(buf, 0, -1, true)
23  local ns = api.nvim_create_namespace('nvim.undotree')
24  local extmarks = api.nvim_buf_get_extmarks(buf, ns, 0, -1, { details = true })
25  for i = #extmarks, 1, -1 do
26    local extmark = extmarks[i]
27    ---@type nil,integer,nil,vim.api.keyset.extmark_details
28    local _, row, _, opts = unpack(extmark)
29    local virt_lines = assert(opts.virt_lines)
30    for _, v in ipairs(virt_lines) do
31      local virt_line = v[1][1]
32      table.insert(lines, row + 2, virt_line)
33    end
34  end
35  return table.concat(lines, '\n')
36end
37
38local function strip_time(text)
39  return text:gsub('%s-%(.-%)', '')
40end
41
42describe(':Undotree', function()
43  before_each(function()
44    clear({
45      args = {
46        '--clean',
47        -- autoread plugin causes flaky tests
48        '--cmd',
49        'let g:loaded_autoread=1',
50      },
51    })
52    exec 'packadd nvim.undotree'
53  end)
54
55  it('works', function()
56    api.nvim_set_current_line('foo')
57    exec 'Undotree'
58    local buf = api.nvim_get_current_buf()
59    local win = api.nvim_get_current_win()
60    eq(
61      dedent [[
62    *    0
63    *    1]],
64      strip_time(buf_get_lines_and_extmark(buf))
65    )
66    eq(2, api.nvim_win_get_cursor(win)[1])
67    exec 'wincmd w'
68
69    -- Doing changes moves cursor in undotree
70    exec 'undo'
71    eq(1, api.nvim_win_get_cursor(win)[1])
72    api.nvim_set_current_line('bar')
73    eq(3, api.nvim_win_get_cursor(win)[1])
74
75    eq(
76      dedent [[
77    *    0
78    |\
79    | *    1
80    *    2]],
81      strip_time(buf_get_lines_and_extmark(buf))
82    )
83
84    -- Moving the cursor in undotree changes the buffer
85    eq('bar', api.nvim_get_current_line())
86    exec 'wincmd w'
87    exec '2'
88    exec 'wincmd w'
89    eq('foo', api.nvim_get_current_line())
90  end)
91
92  it('sync scroll pos when undo', function()
93    local screen = Screen.new(30, 10)
94    for _ = 1, n.api.nvim_get_option_value('lines', {}) do
95      api.nvim_set_current_line('foo')
96    end
97    exec 'Undotree'
98    exec 'wincmd w'
99    exec 'silent undo'
100    screen:expect([[
101      *    3   │^foo                 |
102      *    4   │{1:~                   }|
103      *    5   │{1:~                   }|
104      *    6   │{1:~                   }|
105      *    7   │{1:~                   }|
106      *    8   │{1:~                   }|
107      {21:*    9   }│{1:~                   }|
108      *    10  │{1:~                   }|
109      {2:<or  [-]  }{3:[No Name] [+]       }|
110                                    |
111    ]])
112  end)
113
114  describe('branch+remove is correctly graphed', function()
115    it('when branching left', function()
116      generate_undo_tree_from_rev({ 0, 1, 2, 3, 1, 3, 4, 3, 2, 0 })
117      exec 'Undotree'
118      eq(
119        dedent([[
120        *    0
121        |\
122        | *    1
123        | |\
124        | | *    2
125        | | |\
126        | | | *    3
127        | | | |\
128        | | | | *    4
129        | * | | |    5
130        |  / /| |]] --[[This is the line being tested, e.g. remove&branch left]] .. '\n' .. [[
131        | | | * |    6
132        | | |  /
133        | | | *    7
134        | | *    8
135        | *    9
136        *    10]]),
137        strip_time(buf_get_lines_and_extmark(0))
138      )
139    end)
140
141    it('when branching right', function()
142      generate_undo_tree_from_rev({ 0, 1, 2, 3, 3, 1, 4, 2, 1, 0 })
143      exec 'Undotree'
144      eq(
145        dedent([[
146        *    0
147        |\
148        | *    1
149        | |\
150        | | *    2
151        | | |\
152        | | | *    3
153        | | | |\
154        | | | | *    4
155        | | | * |    5
156        | |\ \  |]] --[[This is the line being tested, e.g. remove&branch right]] .. '\n' .. [[
157        | | * | |    6
158        | |  / /
159        | | | *    7
160        | | *    8
161        | *    9
162        *    10]]),
163        strip_time(buf_get_lines_and_extmark(0))
164      )
165    end)
166  end)
167end)