robin/laufey

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

Justin M. KeyesMerge #41114 from epithet/default-ruler-as-expressiondab5fab948

master
20.8 KiB424 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4
5local describe, it = t.describe, t.it
6local clear = n.clear
7local command = n.command
8local exec = n.exec
9local expect_exit = n.expect_exit
10local feed = n.feed
11local fn = n.fn
12local read_file = t.read_file
13local eq = t.eq
14local write_file = t.write_file
15local is_os = t.is_os
16
17describe(':confirm command dialog', function()
18  local screen
19
20  local function start_new()
21    clear()
22    screen = Screen.new(75, 20)
23  end
24
25  -- Test for the :confirm command dialog
26  -- oldtest: Test_confirm_cmd()
27  it('works', function()
28    write_file('Xfoo', 'foo1\n')
29    write_file('Xbar', 'bar1\n')
30
31    -- Test for saving all the modified buffers
32    start_new()
33    exec([[
34      set nomore
35      new Xfoo
36      call setline(1, 'foo2')
37      new Xbar
38      call setline(1, 'bar2')
39      wincmd b
40    ]])
41    feed(':confirm qall\n')
42    screen:expect([[
43      bar2                                                                       |
44      {1:~                                                                          }|*5
45      {2:Xbar [+]                                                                   }|
46      foo2                                                                       |
47      {1:~                                                                          }|*4
48      {2:Xfoo [+]                                                                   }|
49                                                                                 |
50      {1:~                                                                          }|*2
51      {3:                                                                           }|
52      :confirm qall                                                              |
53      {6:Save changes to "Xbar"?}                                                    |
54      {6:[Y]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: }^                         |
55    ]])
56    expect_exit(n.load_adjust(1000), feed, 'A')
57
58    eq('foo2\n', read_file('Xfoo'))
59    eq('bar2\n', read_file('Xbar'))
60
61    -- Test for discarding all the changes to modified buffers
62    start_new()
63    exec([[
64      set nomore
65      new Xfoo
66      call setline(1, 'foo3')
67      new Xbar
68      call setline(1, 'bar3')
69      wincmd b
70    ]])
71    feed(':confirm qall\n')
72    screen:expect([[
73      bar3                                                                       |
74      {1:~                                                                          }|*5
75      {2:Xbar [+]                                                                   }|
76      foo3                                                                       |
77      {1:~                                                                          }|*4
78      {2:Xfoo [+]                                                                   }|
79                                                                                 |
80      {1:~                                                                          }|*2
81      {3:                                                                           }|
82      :confirm qall                                                              |
83      {6:Save changes to "Xbar"?}                                                    |
84      {6:[Y]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: }^                         |
85    ]])
86    expect_exit(n.load_adjust(1000), feed, 'D')
87
88    eq('foo2\n', read_file('Xfoo'))
89    eq('bar2\n', read_file('Xbar'))
90
91    -- Test for saving and discarding changes to some buffers
92    start_new()
93    exec([[
94      set nomore
95      new Xfoo
96      call setline(1, 'foo4')
97      new Xbar
98      call setline(1, 'bar4')
99      wincmd b
100    ]])
101    feed(':confirm qall\n')
102    screen:expect([[
103      bar4                                                                       |
104      {1:~                                                                          }|*5
105      {2:Xbar [+]                                                                   }|
106      foo4                                                                       |
107      {1:~                                                                          }|*4
108      {2:Xfoo [+]                                                                   }|
109                                                                                 |
110      {1:~                                                                          }|*2
111      {3:                                                                           }|
112      :confirm qall                                                              |
113      {6:Save changes to "Xbar"?}                                                    |
114      {6:[Y]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: }^                         |
115    ]])
116    feed('N')
117    screen:expect([[
118      bar4                                                                       |
119      {1:~                                                                          }|*5
120      {2:Xbar [+]                                                                   }|
121      foo4                                                                       |
122      {1:~                                                                          }|*4
123      {2:Xfoo [+]                                                                   }|
124                                                                                 |
125      {3:                                                                           }|
126      :confirm qall                                                              |
127      {6:Save changes to "Xbar"?}                                                    |
128                                                                                 |
129      {6:Save changes to "Xfoo"?}                                                    |
130      {6:[Y]es, (N)o, (C)ancel: }^                                                    |
131    ]])
132    expect_exit(n.load_adjust(1000), feed, 'Y')
133
134    eq('foo4\n', read_file('Xfoo'))
135    eq('bar2\n', read_file('Xbar'))
136
137    os.remove('Xfoo')
138    os.remove('Xbar')
139  end)
140
141  -- oldtest: Test_confirm_cmd_cancel()
142  it('can be cancelled', function()
143    -- Test for closing a window with a modified buffer
144    start_new()
145    screen:try_resize(75, 10)
146    exec([[
147      set nohidden nomore
148      new
149      call setline(1, 'abc')
150    ]])
151    feed(':confirm close\n')
152    screen:expect([[
153      abc                                                                        |
154      {1:~                                                                          }|*3
155      {3:[No Name] [+]                                                              }|
156                                                                                 |
157      {3:                                                                           }|
158      :confirm close                                                             |
159      {6:Save changes to "Untitled"?}                                                |
160      {6:[Y]es, (N)o, (C)ancel: }^                                                    |
161    ]])
162    feed('C')
163    screen:expect([[
164      ^abc                                                                        |
165      {1:~                                                                          }|*3
166      {3:[No Name] [+]                                                              }|
167                                                                                 |
168      {1:~                                                                          }|*2
169      {2:[No Name]                                                                  }|
170                                                                                 |
171    ]])
172    feed(':confirm close\n')
173    screen:expect([[
174      abc                                                                        |
175      {1:~                                                                          }|*3
176      {3:[No Name] [+]                                                              }|
177                                                                                 |
178      {3:                                                                           }|
179      :confirm close                                                             |
180      {6:Save changes to "Untitled"?}                                                |
181      {6:[Y]es, (N)o, (C)ancel: }^                                                    |
182    ]])
183    feed('N')
184    screen:expect([[
185      ^                                                                           |
186      {1:~                                                                          }|*8
187                                                                                 |
188    ]])
189  end)
190
191  -- oldtest: Test_confirm_q_wq()
192  it('works with :q and :wq', function()
193    write_file('Xfoo', 'foo')
194    start_new()
195    screen:try_resize(75, 8)
196    exec([[
197      set hidden nomore
198      call setline(1, 'abc')
199      edit Xfoo
200      set nofixendofline
201    ]])
202    feed(':confirm q\n')
203    screen:expect([[
204      foo                                                                        |
205      {1:~                                                                          }|*3
206      {3:                                                                           }|
207      :confirm q                                                                 |
208      {6:Save changes to "Untitled"?}                                                |
209      {6:[Y]es, (N)o, (C)ancel: }^                                                    |
210    ]])
211    feed('C')
212    screen:expect([[
213      ^abc                                                                        |
214      {1:~                                                                          }|*6
215                                                                                 |
216    ]])
217
218    command('edit Xfoo')
219    feed(':confirm wq\n')
220    screen:expect([[
221      foo                                                                        |
222      {1:~                                                                          }|*3
223      {3:                                                                           }|
224      "Xfoo" [noeol] 1L, 3B written                                              |
225      {6:Save changes to "Untitled"?}                                                |
226      {6:[Y]es, (N)o, (C)ancel: }^                                                    |
227    ]])
228    feed('C')
229    screen:expect([[
230      ^abc                                                                        |
231      {1:~                                                                          }|*6
232      "Xfoo" [noeol] 1L, 3B written                                              |
233    ]])
234
235    os.remove('Xfoo')
236  end)
237
238  -- oldtest: Test_confirm_write_ro()
239  it('works when writing a read-only file', function()
240    write_file('Xconfirm_write_ro', 'foo\n')
241    start_new()
242    screen:try_resize(75, 8)
243    exec([[
244      set ruler
245      set nobackup ff=unix cmdheight=2
246      edit Xconfirm_write_ro
247      norm Abar
248    ]])
249
250    -- Try to write with 'ro' option.
251    feed(':set ro | confirm w\n')
252    screen:expect([[
253      foobar                                                                     |
254      {1:~                                                                          }|*2
255      {3:                                                                           }|
256      :set ro | confirm w                                                        |
257      {6:'readonly' option is set for "Xconfirm_write_ro".}                          |
258      {6:Do you wish to write anyway?}                                               |
259      {6:(Y)es, [N]o: }^                                                              |
260    ]])
261    feed('N')
262    screen:expect([[
263      fooba^r                                                                     |
264      {1:~                                                                          }|*5
265                                                                                 |
266                                                               1,6            All|
267    ]])
268    eq('foo\n', read_file('Xconfirm_write_ro'))
269
270    feed(':confirm w\n')
271    screen:expect([[
272      foobar                                                                     |
273      {1:~                                                                          }|*2
274      {3:                                                                           }|
275      :confirm w                                                                 |
276      {6:'readonly' option is set for "Xconfirm_write_ro".}                          |
277      {6:Do you wish to write anyway?}                                               |
278      {6:(Y)es, [N]o: }^                                                              |
279    ]])
280    feed('Y')
281    if is_os('win') then
282      screen:expect([[
283        foobar                                                                     |
284        {1:~                                                                          }|
285        {3:                                                                           }|
286        :confirm w                                                                 |
287        {6:'readonly' option is set for "Xconfirm_write_ro".}                          |
288        {6:Do you wish to write anyway?}                                               |
289        "Xconfirm_write_ro" [unix] 1L, 7B written                                  |
290        {6:Press ENTER or type command to continue}^                                    |
291      ]])
292    else
293      screen:expect([[
294        foobar                                                                     |
295        {1:~                                                                          }|
296        {3:                                                                           }|
297        :confirm w                                                                 |
298        {6:'readonly' option is set for "Xconfirm_write_ro".}                          |
299        {6:Do you wish to write anyway?}                                               |
300        "Xconfirm_write_ro" 1L, 7B written                                         |
301        {6:Press ENTER or type command to continue}^                                    |
302      ]])
303    end
304    eq('foobar\n', read_file('Xconfirm_write_ro'))
305    feed('<CR>') -- suppress hit-enter prompt
306
307    -- Try to write with read-only file permissions.
308    fn.setfperm('Xconfirm_write_ro', 'r--r--r--')
309    feed(':set noro | silent undo | confirm w\n')
310    screen:expect([[
311      foobar                                                                     |
312      {1:~                                                                          }|
313      {3:                                                                           }|
314      :set noro | silent undo | confirm w                                        |
315      {6:File permissions of "Xconfirm_write_ro" are read-only.}                     |
316      {6:It may still be possible to write it.}                                      |
317      {6:Do you wish to try?}                                                        |
318      {6:(Y)es, [N]o: }^                                                              |
319    ]])
320    feed('Y')
321    if is_os('win') then
322      screen:expect([[
323        foobar                                                                     |
324        {3:                                                                           }|
325        :set noro | silent undo | confirm w                                        |
326        {6:File permissions of "Xconfirm_write_ro" are read-only.}                     |
327        {6:It may still be possible to write it.}                                      |
328        {6:Do you wish to try?}                                                        |
329        "Xconfirm_write_ro" [unix] 1L, 4B written                                  |
330        {6:Press ENTER or type command to continue}^                                    |
331      ]])
332    else
333      screen:expect([[
334        foobar                                                                     |
335        {3:                                                                           }|
336        :set noro | silent undo | confirm w                                        |
337        {6:File permissions of "Xconfirm_write_ro" are read-only.}                     |
338        {6:It may still be possible to write it.}                                      |
339        {6:Do you wish to try?}                                                        |
340        "Xconfirm_write_ro" 1L, 4B written                                         |
341        {6:Press ENTER or type command to continue}^                                    |
342      ]])
343    end
344    eq('foo\n', read_file('Xconfirm_write_ro'))
345    feed('<CR>') -- suppress hit-enter prompt
346
347    os.remove('Xconfirm_write_ro')
348  end)
349
350  -- oldtest: Test_confirm_write_partial_file()
351  it('works when writing a partial file', function()
352    write_file('Xwrite_partial', 'a\nb\nc\nd\n')
353    start_new()
354    screen:try_resize(75, 8)
355    exec([[
356      set ruler
357      set nobackup ff=unix cmdheight=2
358      edit Xwrite_partial
359    ]])
360
361    feed(':confirm 2,3w\n')
362    screen:expect([[
363      a                                                                          |
364      b                                                                          |
365      c                                                                          |
366      d                                                                          |
367      {3:                                                                           }|
368      :confirm 2,3w                                                              |
369      {6:Write partial file?}                                                        |
370      {6:(Y)es, [N]o: }^                                                              |
371    ]])
372    feed('N')
373    screen:expect([[
374      ^a                                                                          |
375      b                                                                          |
376      c                                                                          |
377      d                                                                          |
378      {1:~                                                                          }|*2
379                                                                                 |
380                                                               1,1            All|
381    ]])
382    eq('a\nb\nc\nd\n', read_file('Xwrite_partial'))
383    os.remove('Xwrite_partial')
384
385    feed(':confirm 2,3w\n')
386    screen:expect([[
387      a                                                                          |
388      b                                                                          |
389      c                                                                          |
390      d                                                                          |
391      {3:                                                                           }|
392      :confirm 2,3w                                                              |
393      {6:Write partial file?}                                                        |
394      {6:(Y)es, [N]o: }^                                                              |
395    ]])
396    feed('Y')
397    if is_os('win') then
398      screen:expect([[
399        a                                                                          |
400        b                                                                          |
401        c                                                                          |
402        {3:                                                                           }|
403        :confirm 2,3w                                                              |
404        {6:Write partial file?}                                                        |
405        "Xwrite_partial" [New][unix] 2L, 4B written                                |
406        {6:Press ENTER or type command to continue}^                                    |
407      ]])
408    else
409      screen:expect([[
410        a                                                                          |
411        b                                                                          |
412        c                                                                          |
413        {3:                                                                           }|
414        :confirm 2,3w                                                              |
415        {6:Write partial file?}                                                        |
416        "Xwrite_partial" [New] 2L, 4B written                                      |
417        {6:Press ENTER or type command to continue}^                                    |
418      ]])
419    end
420    eq('b\nc\n', read_file('Xwrite_partial'))
421
422    os.remove('Xwrite_partial')
423  end)
424end)