blob: 7d919c24a9d475e6efcf42752ac6f68eaae35398 [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
Bram Moolenaar70e67252022-09-27 19:34:35 +010025 call writefile(filecnt, 'Xffafile', 'bD')
Bram Moolenaarb18b4962022-09-02 21:55:50 +010026 au BufReadPre Xffafile set ffs=dos ff=dos
27 new Xffafile
Bram Moolenaar1695f992017-01-24 13:18:43 +010028 call assert_equal('dos', &l:ff)
29 call assert_equal('dos', &ffs)
30
31 " cleanup
Bram Moolenaar1695f992017-01-24 13:18:43 +010032 let &ffs = ffs
Bram Moolenaarb18b4962022-09-02 21:55:50 +010033 au! BufReadPre Xffafile
Bram Moolenaar1695f992017-01-24 13:18:43 +010034 bw!
Bram Moolenaar7a2699e2017-01-23 21:31:09 +010035endfunc
Bram Moolenaar72fcf072019-05-27 22:21:44 +020036
Bram Moolenaar6fd367a2021-03-13 13:14:04 +010037func Test_fileformat_nomodifiable()
38 new
39 setlocal nomodifiable
40
41 call assert_fails('set fileformat=latin1', 'E21:')
42
43 bw
44endfunc
45
Bram Moolenaar72fcf072019-05-27 22:21:44 +020046" Convert the contents of a file into a literal string
47func s:file2str(fname)
48 let b = readfile(a:fname, 'B')
49 let s = ''
50 for c in b
51 let s .= nr2char(c)
52 endfor
53 return s
54endfunc
55
56" Concatenate the contents of files 'f1' and 'f2' and create 'destfile'
57func s:concat_files(f1, f2, destfile)
58 let b1 = readfile(a:f1, 'B')
59 let b2 = readfile(a:f2, 'B')
60 let b3 = b1 + b2
61 call writefile(b3, a:destfile, 'B')
62endfun
63
64" Test for a lot of variations of the 'fileformats' option
65func Test_fileformats()
66 " create three test files, one in each format
Bram Moolenaar70e67252022-09-27 19:34:35 +010067 call writefile(['unix', 'unix'], 'XXUnix', 'D')
68 call writefile(["dos\r", "dos\r"], 'XXDos', 'D')
69 call writefile(["mac\rmac\r"], 'XXMac', 'bD')
Bram Moolenaar72fcf072019-05-27 22:21:44 +020070 " create a file with no End Of Line
Bram Moolenaar70e67252022-09-27 19:34:35 +010071 call writefile(["noeol"], 'XXEol', 'bD')
Bram Moolenaar72fcf072019-05-27 22:21:44 +020072 " create mixed format files
73 call s:concat_files('XXUnix', 'XXDos', 'XXUxDs')
74 call s:concat_files('XXUnix', 'XXMac', 'XXUxMac')
75 call s:concat_files('XXDos', 'XXMac', 'XXDosMac')
76 call s:concat_files('XXMac', 'XXEol', 'XXMacEol')
77 call s:concat_files('XXUxDs', 'XXMac', 'XXUxDsMc')
78
Bram Moolenaar88456cd2022-11-18 22:14:09 +000079 " The :bwipe commands below cause us to get back to the current buffer.
80 " Avoid stray errors for various 'fileformat' values which may cause a
81 " modeline to be misinterpreted by wiping the buffer and editing a new one.
82 only!
83 bwipe!
84 enew
Bram Moolenaar72fcf072019-05-27 22:21:44 +020085
86 " Test 1: try reading and writing with 'fileformats' empty
87 set fileformats=
88
89 " try with 'fileformat' set to 'unix'
90 set fileformat=unix
91 e! XXUnix
92 w! Xtest
93 call assert_equal("unix\nunix\n", s:file2str('Xtest'))
94 e! XXDos
95 w! Xtest
96 call assert_equal("dos\r\ndos\r\n", s:file2str('Xtest'))
97 e! XXMac
98 w! Xtest
99 call assert_equal("mac\rmac\r\n", s:file2str('Xtest'))
100 bwipe XXUnix XXDos XXMac
101
102 " try with 'fileformat' set to 'dos'
103 set fileformat=dos
104 e! XXUnix
105 w! Xtest
106 call assert_equal("unix\r\nunix\r\n", s:file2str('Xtest'))
107 e! XXDos
108 w! Xtest
109 call assert_equal("dos\r\ndos\r\n", s:file2str('Xtest'))
110 e! XXMac
111 w! Xtest
112 call assert_equal("mac\rmac\r\r\n", s:file2str('Xtest'))
113 bwipe XXUnix XXDos XXMac
114
115 " try with 'fileformat' set to 'mac'
116 set fileformat=mac
117 e! XXUnix
118 w! Xtest
119 call assert_equal("unix\nunix\n\r", s:file2str('Xtest'))
120 e! XXDos
121 w! Xtest
122 call assert_equal("dos\r\ndos\r\n\r", s:file2str('Xtest'))
123 e! XXMac
124 w! Xtest
125 call assert_equal("mac\rmac\r", s:file2str('Xtest'))
126 bwipe XXUnix XXDos XXMac
127
128 " Test 2: try reading and writing with 'fileformats' set to one format
129
130 " try with 'fileformats' set to 'unix'
131 set fileformats=unix
132 e! XXUxDsMc
133 w! Xtest
134 call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r\n",
135 \ s:file2str('Xtest'))
136 bwipe XXUxDsMc
137
138 " try with 'fileformats' set to 'dos'
139 set fileformats=dos
140 e! XXUxDsMc
141 w! Xtest
142 call assert_equal("unix\r\nunix\r\ndos\r\ndos\r\nmac\rmac\r\r\n",
143 \ s:file2str('Xtest'))
144 bwipe XXUxDsMc
145
146 " try with 'fileformats' set to 'mac'
147 set fileformats=mac
148 e! XXUxDsMc
149 w! Xtest
150 call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r",
151 \ s:file2str('Xtest'))
152 bwipe XXUxDsMc
153
154 " Test 3: try reading and writing with 'fileformats' set to two formats
155
156 " try with 'fileformats' set to 'unix,dos'
157 set fileformats=unix,dos
158 e! XXUxDsMc
159 w! Xtest
160 call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r\n",
161 \ s:file2str('Xtest'))
162 bwipe XXUxDsMc
163
164 e! XXUxMac
165 w! Xtest
166 call assert_equal("unix\nunix\nmac\rmac\r\n", s:file2str('Xtest'))
167 bwipe XXUxMac
168
169 e! XXDosMac
170 w! Xtest
171 call assert_equal("dos\r\ndos\r\nmac\rmac\r\r\n", s:file2str('Xtest'))
172 bwipe XXDosMac
173
174 " try with 'fileformats' set to 'unix,mac'
175 set fileformats=unix,mac
176 e! XXUxDs
177 w! Xtest
178 call assert_equal("unix\nunix\ndos\r\ndos\r\n", s:file2str('Xtest'))
179 bwipe XXUxDs
180
181 e! XXUxDsMc
182 w! Xtest
183 call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r\n",
184 \ s:file2str('Xtest'))
185 bwipe XXUxDsMc
186
187 e! XXDosMac
188 w! Xtest
189 call assert_equal("dos\r\ndos\r\nmac\rmac\r", s:file2str('Xtest'))
190 bwipe XXDosMac
191
192 e! XXEol
193 exe "normal ggO\<C-R>=&ffs\<CR>:\<C-R>=&ff\<CR>"
194 w! Xtest
195 call assert_equal("unix,mac:unix\nnoeol\n", s:file2str('Xtest'))
196 bwipe! XXEol
197
198 " try with 'fileformats' set to 'dos,mac'
199 set fileformats=dos,mac
200 e! XXUxDs
201 w! Xtest
202 call assert_equal("unix\r\nunix\r\ndos\r\ndos\r\n", s:file2str('Xtest'))
203 bwipe XXUxDs
204
205 e! XXUxMac
206 exe "normal ggO\<C-R>=&ffs\<CR>:\<C-R>=&ff\<CR>"
207 w! Xtest
208 call assert_equal("dos,mac:dos\r\nunix\r\nunix\r\nmac\rmac\r\r\n",
209 \ s:file2str('Xtest'))
210 bwipe! XXUxMac
211
212 e! XXUxDsMc
213 w! Xtest
214 call assert_equal("unix\r\nunix\r\ndos\r\ndos\r\nmac\rmac\r\r\n",
215 \ s:file2str('Xtest'))
216 bwipe XXUxDsMc
217
218 e! XXMacEol
219 exe "normal ggO\<C-R>=&ffs\<CR>:\<C-R>=&ff\<CR>"
220 w! Xtest
221 call assert_equal("dos,mac:mac\rmac\rmac\rnoeol\r", s:file2str('Xtest'))
222 bwipe! XXMacEol
223
224 " Test 4: try reading and writing with 'fileformats' set to three formats
225 set fileformats=unix,dos,mac
226 e! XXUxDsMc
227 w! Xtest
228 call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r\n",
229 \ s:file2str('Xtest'))
230 bwipe XXUxDsMc
231
232 e! XXEol
233 exe "normal ggO\<C-R>=&ffs\<CR>:\<C-R>=&ff\<CR>"
234 w! Xtest
235 call assert_equal("unix,dos,mac:unix\nnoeol\n", s:file2str('Xtest'))
236 bwipe! XXEol
237
238 set fileformats=mac,dos,unix
239 e! XXUxDsMc
240 w! Xtest
241 call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r\n",
242 \ s:file2str('Xtest'))
243 bwipe XXUxDsMc
244
245 e! XXEol
246 exe "normal ggO\<C-R>=&ffs\<CR>:\<C-R>=&ff\<CR>"
247 w! Xtest
248 call assert_equal("mac,dos,unix:mac\rnoeol\r", s:file2str('Xtest'))
249 bwipe! XXEol
250
251 " Test 5: try with 'binary' set
252 set fileformats=mac,unix,dos
253 set binary
254 e! XXUxDsMc
255 w! Xtest
256 call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r",
257 \ s:file2str('Xtest'))
258 bwipe XXUxDsMc
259
260 set fileformats=mac
261 e! XXUxDsMc
262 w! Xtest
263 call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r",
264 \ s:file2str('Xtest'))
265 bwipe XXUxDsMc
266
267 set fileformats=dos
268 e! XXUxDsMc
269 w! Xtest
270 call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r",
271 \ s:file2str('Xtest'))
272 bwipe XXUxDsMc
273
274 e! XXUnix
275 w! Xtest
276 call assert_equal("unix\nunix\n", s:file2str('Xtest'))
277 bwipe! XXUnix
278
279 set nobinary ff& ffs&
280
281 " cleanup
282 only
283 %bwipe!
Bram Moolenaar72fcf072019-05-27 22:21:44 +0200284 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
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100317 call writefile([&fileformat], 'Xonsfile', 'a')
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200318 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, '')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100323 call assert_equal(['dos', 'mac', 'unix'], readfile('Xonsfile'))
324 call delete('Xonsfile')
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200325endfunc
326
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100327" vim: shiftwidth=2 sts=2 expandtab