robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
5.8 KiB269 linesraw
1-- Tests for undo tree and :earlier and :later.
2local t = require('test.testutil')
3local n = require('test.functional.testnvim')()
4
5local describe, it, before_each, teardown = t.describe, t.it, t.before_each, t.teardown
6local feed_command = n.feed_command
7local write_file = t.write_file
8local command = n.command
9local source = n.source
10local expect = n.expect
11local clear = n.clear
12local feed = n.feed
13local eval = n.eval
14local eq = t.eq
15
16local function expect_empty_buffer()
17  return expect('')
18end
19local function expect_line(line)
20  return eq(line, eval('getline(".")'))
21end
22
23describe('undo tree:', function()
24  before_each(clear)
25  local fname = 'Xtest_functional_legacy_undotree'
26  teardown(function()
27    os.remove(fname .. '.source')
28  end)
29
30  describe(':earlier and :later', function()
31    before_each(function()
32      os.remove(fname)
33    end)
34    teardown(function()
35      os.remove(fname)
36    end)
37
38    it('time specifications, g- g+', function()
39      -- We write the test text to a file in order to prevent nvim to record
40      -- the inserting of the text into the undo history.
41      write_file(fname, '\n123456789\n')
42
43      -- `:earlier` and `:later` are (obviously) time-sensitive, so this test
44      -- sometimes fails if the system is under load. It is wrapped in a local
45      -- function to allow multiple attempts.
46      local function test_earlier_later()
47        clear()
48        feed_command('e ' .. fname)
49        -- Assert that no undo history is present.
50        eq({}, eval('undotree().entries'))
51        -- Delete three characters and undo.
52        feed('Gxxx')
53        expect_line('456789')
54        feed('g-')
55        expect_line('3456789')
56        feed('g-')
57        expect_line('23456789')
58        feed('g-')
59        expect_line('123456789')
60        feed('g-')
61        expect_line('123456789')
62
63        -- Delete three other characters and go back in time step by step.
64        feed('$xxx')
65        expect_line('123456')
66        command('sleep 1')
67        feed('g-')
68        expect_line('1234567')
69        feed('g-')
70        expect_line('12345678')
71        feed('g-')
72        expect_line('456789')
73        feed('g-')
74        expect_line('3456789')
75        feed('g-')
76        expect_line('23456789')
77        feed('g-')
78        expect_line('123456789')
79        feed('g-')
80        expect_line('123456789')
81        feed('g-')
82        expect_line('123456789')
83        feed('10g+')
84        expect_line('123456')
85
86        -- Delay for two seconds and go some seconds forward and backward.
87        command('sleep 2')
88        feed('Aa<esc>')
89        feed('Ab<esc>')
90        feed('Ac<esc>')
91        expect_line('123456abc')
92        feed_command('earlier 1s')
93        expect_line('123456')
94        feed_command('earlier 3s')
95        expect_line('123456789')
96        feed_command('later 1s')
97        expect_line('123456')
98        feed_command('later 1h')
99        expect_line('123456abc')
100      end
101
102      t.retry(2, nil, test_earlier_later)
103    end)
104
105    it('file-write specifications', function()
106      feed('ione one one<esc>')
107      feed_command('w ' .. fname)
108      feed('otwo<esc>')
109      feed('otwo<esc>')
110      feed_command('w')
111      feed('othree<esc>')
112      feed_command('earlier 1f')
113      expect([[
114        one one one
115        two
116        two]])
117      feed_command('earlier 1f')
118      expect('one one one')
119      feed_command('earlier 1f')
120      expect_empty_buffer()
121      feed_command('later 1f')
122      expect('one one one')
123      feed_command('later 1f')
124      expect([[
125        one one one
126        two
127        two]])
128      feed_command('later 1f')
129      expect([[
130        one one one
131        two
132        two
133        three]])
134    end)
135  end)
136
137  it('scripts produce one undo-block for all changes by default', function()
138    source([[
139      normal Aaaaa
140      normal obbbb
141      normal occcc
142    ]])
143    expect([[
144      aaaa
145      bbbb
146      cccc]])
147    feed('u')
148    expect_empty_buffer()
149  end)
150
151  it("setting 'undolevel' can break undo-blocks (inside scripts)", function()
152    -- :source is required (because interactive changes are _not_ grouped,
153    -- even with :undojoin).
154    source([[
155      normal Aaaaa
156      set ul=100
157      normal obbbb
158      set ul=100
159      normal occcc
160    ]])
161    expect([[
162      aaaa
163      bbbb
164      cccc]])
165    feed('u')
166    expect([[
167      aaaa
168      bbbb]])
169    feed('u')
170    expect('aaaa')
171    feed('u')
172    expect_empty_buffer()
173  end)
174
175  it(':undojoin can join undo-blocks inside scripts', function()
176    feed('Goaaaa<esc>')
177    feed('obbbb<esc>u')
178    expect_line('aaaa')
179    source([[
180      normal obbbb
181      set ul=100
182      undojoin
183      normal occcc
184    ]])
185    feed('u')
186    expect_line('aaaa')
187  end)
188
189  it('undo an expression-register', function()
190    local normal_commands = 'o1\027a2\018=string(123)\n\027'
191    write_file(fname .. '.source', normal_commands)
192
193    feed('oa<esc>')
194    feed('ob<esc>')
195    feed([[o1<esc>a2<C-R>=setline('.','1234')<cr><esc>]])
196    expect([[
197
198      a
199      b
200      12034]])
201    feed('uu')
202    expect([[
203
204      a
205      b
206      1]])
207    feed('oc<esc>')
208    feed([[o1<esc>a2<C-R>=setline('.','1234')<cr><esc>]])
209    expect([[
210
211      a
212      b
213      1
214      c
215      12034]])
216    feed('u')
217    expect([[
218
219      a
220      b
221      1
222      c
223      12]])
224    feed('od<esc>')
225    feed_command('so! ' .. fname .. '.source')
226    expect([[
227
228      a
229      b
230      1
231      c
232      12
233      d
234      12123]])
235    feed('u')
236    expect([[
237
238      a
239      b
240      1
241      c
242      12
243      d]])
244
245    -- The above behaviour was tested in the legacy Vim test because the
246    -- legacy tests were executed with ':so!'.  The behavior differs for
247    -- interactive use (even in Vim; see ":help :undojoin"):
248    feed(normal_commands)
249    expect([[
250
251      a
252      b
253      1
254      c
255      12
256      d
257      12123]])
258    feed('u')
259    expect([[
260
261      a
262      b
263      1
264      c
265      12
266      d
267      1]])
268  end)
269end)