neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

excmd_spec.lua (21240B)


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