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