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