neovim

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

test_fileformat.vim (8144B)


      1 " Test behavior of fileformat after bwipeout of last buffer
      2 func Test_fileformat_after_bw()
      3  bwipeout
      4  set fileformat&
      5  if &fileformat == 'dos'
      6    let test_fileformats = 'unix'
      7  elseif &fileformat == 'unix'
      8    let test_fileformats = 'mac'
      9  else  " must be mac
     10    let test_fileformats = 'dos'
     11  endif
     12  exec 'set fileformats='.test_fileformats
     13  bwipeout!
     14  call assert_equal(test_fileformats, &fileformat)
     15  set fileformats&
     16 endfunc
     17 
     18 func Test_fileformat_autocommand()
     19  let filecnt = ["", "foobar\<CR>", "eins\<CR>", "\<CR>", "zwei\<CR>", "drei", "vier", "fünf", ""]
     20  let ffs = &ffs
     21  call writefile(filecnt, 'Xfile', 'b')
     22  au BufReadPre Xfile set ffs=dos ff=dos
     23  new Xfile
     24  call assert_equal('dos', &l:ff)
     25  call assert_equal('dos', &ffs)
     26 
     27  " cleanup
     28  call delete('Xfile')
     29  let &ffs = ffs
     30  au! BufReadPre Xfile
     31  bw!
     32 endfunc
     33 
     34 func Test_fileformat_nomodifiable()
     35  new
     36  setlocal nomodifiable
     37 
     38  call assert_fails('set fileformat=latin1', 'E21:')
     39 
     40  bw
     41 endfunc
     42 
     43 " Convert the contents of a file into a literal string
     44 func s:file2str(fname)
     45  let b = readfile(a:fname, 'B')
     46  let s = ''
     47  for c in b
     48    let s .= nr2char(c)
     49  endfor
     50  return s
     51 endfunc
     52 
     53 " Concatenate the contents of files 'f1' and 'f2' and create 'destfile'
     54 func s:concat_files(f1, f2, destfile)
     55  let b1 = readfile(a:f1, 'B')
     56  let b2 = readfile(a:f2, 'B')
     57  let b3 = b1 + b2
     58  call writefile(b3, a:destfile)
     59 endfun
     60 
     61 " Test for a lot of variations of the 'fileformats' option
     62 func Test_fileformats()
     63  " create three test files, one in each format
     64  call writefile(['unix', 'unix'], 'XXUnix')
     65  call writefile(["dos\r", "dos\r"], 'XXDos')
     66  call writefile(["mac\rmac\r"], 'XXMac', 'b')
     67  " create a file with no End Of Line
     68  call writefile(["noeol"], 'XXEol', 'b')
     69  " create mixed format files
     70  call s:concat_files('XXUnix', 'XXDos', 'XXUxDs')
     71  call s:concat_files('XXUnix', 'XXMac', 'XXUxMac')
     72  call s:concat_files('XXDos', 'XXMac', 'XXDosMac')
     73  call s:concat_files('XXMac', 'XXEol', 'XXMacEol')
     74  call s:concat_files('XXUxDs', 'XXMac', 'XXUxDsMc')
     75 
     76  " The :bwipe commands below cause us to get back to the current buffer.
     77  " Avoid stray errors for various 'fileformat' values which may cause a
     78  " modeline to be misinterpreted by wiping the buffer and editing a new one.
     79  only!
     80  bwipe!
     81  enew
     82 
     83  " Test 1: try reading and writing with 'fileformats' empty
     84  set fileformats=
     85 
     86  " try with 'fileformat' set to 'unix'
     87  set fileformat=unix
     88  e! XXUnix
     89  w! Xtest
     90  call assert_equal("unix\nunix\n", s:file2str('Xtest'))
     91  e! XXDos
     92  w! Xtest
     93  call assert_equal("dos\r\ndos\r\n", s:file2str('Xtest'))
     94  e! XXMac
     95  w! Xtest
     96  call assert_equal("mac\rmac\r\n", s:file2str('Xtest'))
     97  bwipe XXUnix XXDos XXMac
     98 
     99  " try with 'fileformat' set to 'dos'
    100  set fileformat=dos
    101  e! XXUnix
    102  w! Xtest
    103  call assert_equal("unix\r\nunix\r\n", s:file2str('Xtest'))
    104  e! XXDos
    105  w! Xtest
    106  call assert_equal("dos\r\ndos\r\n", s:file2str('Xtest'))
    107  e! XXMac
    108  w! Xtest
    109  call assert_equal("mac\rmac\r\r\n", s:file2str('Xtest'))
    110  bwipe XXUnix XXDos XXMac
    111 
    112  " try with 'fileformat' set to 'mac'
    113  set fileformat=mac
    114  e! XXUnix
    115  w! Xtest
    116  call assert_equal("unix\nunix\n\r", s:file2str('Xtest'))
    117  e! XXDos
    118  w! Xtest
    119  call assert_equal("dos\r\ndos\r\n\r", s:file2str('Xtest'))
    120  e! XXMac
    121  w! Xtest
    122  call assert_equal("mac\rmac\r", s:file2str('Xtest'))
    123  bwipe XXUnix XXDos XXMac
    124 
    125  " Test 2: try reading and writing with 'fileformats' set to one format
    126 
    127  " try with 'fileformats' set to 'unix'
    128  set fileformats=unix
    129  e! XXUxDsMc
    130  w! Xtest
    131  call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r\n",
    132       \ s:file2str('Xtest'))
    133  bwipe XXUxDsMc
    134 
    135  " try with 'fileformats' set to 'dos'
    136  set fileformats=dos
    137  e! XXUxDsMc
    138  w! Xtest
    139  call assert_equal("unix\r\nunix\r\ndos\r\ndos\r\nmac\rmac\r\r\n",
    140       \ s:file2str('Xtest'))
    141  bwipe XXUxDsMc
    142 
    143  " try with 'fileformats' set to 'mac'
    144  set fileformats=mac
    145  e! XXUxDsMc
    146  w! Xtest
    147  call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r",
    148       \ s:file2str('Xtest'))
    149  bwipe XXUxDsMc
    150 
    151  " Test 3: try reading and writing with 'fileformats' set to two formats
    152 
    153  " try with 'fileformats' set to 'unix,dos'
    154  set fileformats=unix,dos
    155  e! XXUxDsMc
    156  w! Xtest
    157  call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r\n",
    158       \ s:file2str('Xtest'))
    159  bwipe XXUxDsMc
    160 
    161  e! XXUxMac
    162  w! Xtest
    163  call assert_equal("unix\nunix\nmac\rmac\r\n", s:file2str('Xtest'))
    164  bwipe XXUxMac
    165 
    166  e! XXDosMac
    167  w! Xtest
    168  call assert_equal("dos\r\ndos\r\nmac\rmac\r\r\n", s:file2str('Xtest'))
    169  bwipe XXDosMac
    170 
    171  " try with 'fileformats' set to 'unix,mac'
    172  set fileformats=unix,mac
    173  e! XXUxDs
    174  w! Xtest
    175  call assert_equal("unix\nunix\ndos\r\ndos\r\n", s:file2str('Xtest'))
    176  bwipe XXUxDs
    177 
    178  e! XXUxDsMc
    179  w! Xtest
    180  call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r\n",
    181       \ s:file2str('Xtest'))
    182  bwipe XXUxDsMc
    183 
    184  e! XXDosMac
    185  w! Xtest
    186  call assert_equal("dos\r\ndos\r\nmac\rmac\r", s:file2str('Xtest'))
    187  bwipe XXDosMac
    188 
    189  e! XXEol
    190  exe "normal ggO\<C-R>=&ffs\<CR>:\<C-R>=&ff\<CR>"
    191  w! Xtest
    192  call assert_equal("unix,mac:unix\nnoeol\n", s:file2str('Xtest'))
    193  bwipe! XXEol
    194 
    195  " try with 'fileformats' set to 'dos,mac'
    196  set fileformats=dos,mac
    197  e! XXUxDs
    198  w! Xtest
    199  call assert_equal("unix\r\nunix\r\ndos\r\ndos\r\n", s:file2str('Xtest'))
    200  bwipe XXUxDs
    201 
    202  e! XXUxMac
    203  exe "normal ggO\<C-R>=&ffs\<CR>:\<C-R>=&ff\<CR>"
    204  w! Xtest
    205  call assert_equal("dos,mac:dos\r\nunix\r\nunix\r\nmac\rmac\r\r\n",
    206       \ s:file2str('Xtest'))
    207  bwipe! XXUxMac
    208 
    209  e! XXUxDsMc
    210  w! Xtest
    211  call assert_equal("unix\r\nunix\r\ndos\r\ndos\r\nmac\rmac\r\r\n",
    212       \ s:file2str('Xtest'))
    213  bwipe XXUxDsMc
    214 
    215  e! XXMacEol
    216  exe "normal ggO\<C-R>=&ffs\<CR>:\<C-R>=&ff\<CR>"
    217  w! Xtest
    218  call assert_equal("dos,mac:mac\rmac\rmac\rnoeol\r", s:file2str('Xtest'))
    219  bwipe! XXMacEol
    220 
    221  " Test 4: try reading and writing with 'fileformats' set to three formats
    222  set fileformats=unix,dos,mac
    223  e! XXUxDsMc
    224  w! Xtest
    225  call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r\n",
    226       \ s:file2str('Xtest'))
    227  bwipe XXUxDsMc
    228 
    229  e! XXEol
    230  exe "normal ggO\<C-R>=&ffs\<CR>:\<C-R>=&ff\<CR>"
    231  w! Xtest
    232  call assert_equal("unix,dos,mac:unix\nnoeol\n", s:file2str('Xtest'))
    233  bwipe! XXEol
    234 
    235  set fileformats=mac,dos,unix
    236  e! XXUxDsMc
    237  w! Xtest
    238  call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r\n",
    239       \ s:file2str('Xtest'))
    240  bwipe XXUxDsMc
    241 
    242  e! XXEol
    243  exe "normal ggO\<C-R>=&ffs\<CR>:\<C-R>=&ff\<CR>"
    244  w! Xtest
    245  call assert_equal("mac,dos,unix:mac\rnoeol\r", s:file2str('Xtest'))
    246  bwipe! XXEol
    247 
    248  " Test 5: try with 'binary' set
    249  set fileformats=mac,unix,dos
    250  set binary
    251  e! XXUxDsMc
    252  w! Xtest
    253  call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r",
    254       \ s:file2str('Xtest'))
    255  bwipe XXUxDsMc
    256 
    257  set fileformats=mac
    258  e! XXUxDsMc
    259  w! Xtest
    260  call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r",
    261       \ s:file2str('Xtest'))
    262  bwipe XXUxDsMc
    263 
    264  set fileformats=dos
    265  e! XXUxDsMc
    266  w! Xtest
    267  call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r",
    268       \ s:file2str('Xtest'))
    269  bwipe XXUxDsMc
    270 
    271  e! XXUnix
    272  w! Xtest
    273  call assert_equal("unix\nunix\n", s:file2str('Xtest'))
    274  bwipe! XXUnix
    275 
    276  set nobinary ff& ffs&
    277 
    278  " cleanup
    279  only
    280  %bwipe!
    281  call delete('XXUnix')
    282  call delete('XXDos')
    283  call delete('XXMac')
    284  call delete('XXEol')
    285  call delete('XXUxDs')
    286  call delete('XXUxMac')
    287  call delete('XXDosMac')
    288  call delete('XXMacEol')
    289  call delete('XXUxDsMc')
    290  call delete('Xtest')
    291 endfunc
    292 
    293 " Test for changing the fileformat using ++read
    294 func Test_fileformat_plusplus_read()
    295  new
    296  call setline(1, ['one', 'two', 'three'])
    297  w ++ff=dos Xfile1
    298  enew!
    299  set ff=unix
    300  " A :read doesn't change the fileformat, but does apply to the read lines.
    301  r ++fileformat=unix Xfile1
    302  call assert_equal('unix', &fileformat)
    303  call assert_equal("three\r", getline('$'))
    304  3r ++edit Xfile1
    305  call assert_equal('dos', &fileformat)
    306  close!
    307  call delete('Xfile1')
    308  set fileformat&
    309  call assert_fails('e ++fileformat Xfile1', 'E474:')
    310  call assert_fails('e ++ff=abc Xfile1', 'E474:')
    311  call assert_fails('e ++abc1 Xfile1', 'E474:')
    312 endfunc
    313 
    314 " vim: shiftwidth=2 sts=2 expandtab