blob: 6ba6e49ada716fab9c785463a36d0df6dd0a96a5 [file] [log] [blame]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001" Test for 'fileformat'
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02002
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +02003source shared.vim
4
Bram Moolenaar72fcf072019-05-27 22:21:44 +02005" Test behavior of fileformat after bwipeout of last buffer
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006func 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&
20endfunc
Bram Moolenaar7a2699e2017-01-23 21:31:09 +010021
22func Test_fileformat_autocommand()
Bram Moolenaar2aa5f692017-01-24 15:46:48 +010023 let filecnt = ["", "foobar\<CR>", "eins\<CR>", "\<CR>", "zwei\<CR>", "drei", "vier", "fΓΌnf", ""]
Bram Moolenaar1695f992017-01-24 13:18:43 +010024 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 Moolenaar7a2699e2017-01-23 21:31:09 +010036endfunc
Bram Moolenaar72fcf072019-05-27 22:21:44 +020037
Bram Moolenaar6fd367a2021-03-13 13:14:04 +010038func Test_fileformat_nomodifiable()
39 new
40 setlocal nomodifiable
41
42 call assert_fails('set fileformat=latin1', 'E21:')
43
44 bw
45endfunc
46
Bram Moolenaar72fcf072019-05-27 22:21:44 +020047" Convert the contents of a file into a literal string
48func 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
55endfunc
56
57" Concatenate the contents of files 'f1' and 'f2' and create 'destfile'
58func 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')
63endfun
64
65" Test for a lot of variations of the 'fileformats' option
66func 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')
290endfunc
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100291
292" Test for changing the fileformat using ++read
293func Test_fileformat_plusplus_read()
294 new
295 call setline(1, ['one', 'two', 'three'])
296 w ++ff=dos Xfile1
297 enew!
Bram Moolenaara36c8302020-02-16 15:08:28 +0100298 set ff=unix
Bram Moolenaar50434bd2020-02-16 14:55:22 +0100299 " A :read doesn't change the fileformat, but does apply to the read lines.
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100300 r ++fileformat=unix Xfile1
301 call assert_equal('unix', &fileformat)
Bram Moolenaar50434bd2020-02-16 14:55:22 +0100302 call assert_equal("three\r", getline('$'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100303 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:')
311endfunc
312
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200313" When Vim starts up with an empty buffer the first item in 'fileformats' is
314" used as the 'fileformat'.
315func 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')
325endfunc
326
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100327" vim: shiftwidth=2 sts=2 expandtab