blob: e7d19861ef5a5e638e7c469d0a2d5ce81ab95669 [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
79 new
80
81 " Test 1: try reading and writing with 'fileformats' empty
82 set fileformats=
83
84 " try with 'fileformat' set to 'unix'
85 set fileformat=unix
86 e! XXUnix
87 w! Xtest
88 call assert_equal("unix\nunix\n", s:file2str('Xtest'))
89 e! XXDos
90 w! Xtest
91 call assert_equal("dos\r\ndos\r\n", s:file2str('Xtest'))
92 e! XXMac
93 w! Xtest
94 call assert_equal("mac\rmac\r\n", s:file2str('Xtest'))
95 bwipe XXUnix XXDos XXMac
96
97 " try with 'fileformat' set to 'dos'
98 set fileformat=dos
99 e! XXUnix
100 w! Xtest
101 call assert_equal("unix\r\nunix\r\n", s:file2str('Xtest'))
102 e! XXDos
103 w! Xtest
104 call assert_equal("dos\r\ndos\r\n", s:file2str('Xtest'))
105 e! XXMac
106 w! Xtest
107 call assert_equal("mac\rmac\r\r\n", s:file2str('Xtest'))
108 bwipe XXUnix XXDos XXMac
109
110 " try with 'fileformat' set to 'mac'
111 set fileformat=mac
112 e! XXUnix
113 w! Xtest
114 call assert_equal("unix\nunix\n\r", s:file2str('Xtest'))
115 e! XXDos
116 w! Xtest
117 call assert_equal("dos\r\ndos\r\n\r", s:file2str('Xtest'))
118 e! XXMac
119 w! Xtest
120 call assert_equal("mac\rmac\r", s:file2str('Xtest'))
121 bwipe XXUnix XXDos XXMac
122
123 " Test 2: try reading and writing with 'fileformats' set to one format
124
125 " try with 'fileformats' set to 'unix'
126 set fileformats=unix
127 e! XXUxDsMc
128 w! Xtest
129 call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r\n",
130 \ s:file2str('Xtest'))
131 bwipe XXUxDsMc
132
133 " try with 'fileformats' set to 'dos'
134 set fileformats=dos
135 e! XXUxDsMc
136 w! Xtest
137 call assert_equal("unix\r\nunix\r\ndos\r\ndos\r\nmac\rmac\r\r\n",
138 \ s:file2str('Xtest'))
139 bwipe XXUxDsMc
140
141 " try with 'fileformats' set to 'mac'
142 set fileformats=mac
143 e! XXUxDsMc
144 w! Xtest
145 call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r",
146 \ s:file2str('Xtest'))
147 bwipe XXUxDsMc
148
149 " Test 3: try reading and writing with 'fileformats' set to two formats
150
151 " try with 'fileformats' set to 'unix,dos'
152 set fileformats=unix,dos
153 e! XXUxDsMc
154 w! Xtest
155 call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r\n",
156 \ s:file2str('Xtest'))
157 bwipe XXUxDsMc
158
159 e! XXUxMac
160 w! Xtest
161 call assert_equal("unix\nunix\nmac\rmac\r\n", s:file2str('Xtest'))
162 bwipe XXUxMac
163
164 e! XXDosMac
165 w! Xtest
166 call assert_equal("dos\r\ndos\r\nmac\rmac\r\r\n", s:file2str('Xtest'))
167 bwipe XXDosMac
168
169 " try with 'fileformats' set to 'unix,mac'
170 set fileformats=unix,mac
171 e! XXUxDs
172 w! Xtest
173 call assert_equal("unix\nunix\ndos\r\ndos\r\n", s:file2str('Xtest'))
174 bwipe XXUxDs
175
176 e! XXUxDsMc
177 w! Xtest
178 call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r\n",
179 \ s:file2str('Xtest'))
180 bwipe XXUxDsMc
181
182 e! XXDosMac
183 w! Xtest
184 call assert_equal("dos\r\ndos\r\nmac\rmac\r", s:file2str('Xtest'))
185 bwipe XXDosMac
186
187 e! XXEol
188 exe "normal ggO\<C-R>=&ffs\<CR>:\<C-R>=&ff\<CR>"
189 w! Xtest
190 call assert_equal("unix,mac:unix\nnoeol\n", s:file2str('Xtest'))
191 bwipe! XXEol
192
193 " try with 'fileformats' set to 'dos,mac'
194 set fileformats=dos,mac
195 e! XXUxDs
196 w! Xtest
197 call assert_equal("unix\r\nunix\r\ndos\r\ndos\r\n", s:file2str('Xtest'))
198 bwipe XXUxDs
199
200 e! XXUxMac
201 exe "normal ggO\<C-R>=&ffs\<CR>:\<C-R>=&ff\<CR>"
202 w! Xtest
203 call assert_equal("dos,mac:dos\r\nunix\r\nunix\r\nmac\rmac\r\r\n",
204 \ s:file2str('Xtest'))
205 bwipe! XXUxMac
206
207 e! XXUxDsMc
208 w! Xtest
209 call assert_equal("unix\r\nunix\r\ndos\r\ndos\r\nmac\rmac\r\r\n",
210 \ s:file2str('Xtest'))
211 bwipe XXUxDsMc
212
213 e! XXMacEol
214 exe "normal ggO\<C-R>=&ffs\<CR>:\<C-R>=&ff\<CR>"
215 w! Xtest
216 call assert_equal("dos,mac:mac\rmac\rmac\rnoeol\r", s:file2str('Xtest'))
217 bwipe! XXMacEol
218
219 " Test 4: try reading and writing with 'fileformats' set to three formats
220 set fileformats=unix,dos,mac
221 e! XXUxDsMc
222 w! Xtest
223 call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r\n",
224 \ s:file2str('Xtest'))
225 bwipe XXUxDsMc
226
227 e! XXEol
228 exe "normal ggO\<C-R>=&ffs\<CR>:\<C-R>=&ff\<CR>"
229 w! Xtest
230 call assert_equal("unix,dos,mac:unix\nnoeol\n", s:file2str('Xtest'))
231 bwipe! XXEol
232
233 set fileformats=mac,dos,unix
234 e! XXUxDsMc
235 w! Xtest
236 call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r\n",
237 \ s:file2str('Xtest'))
238 bwipe XXUxDsMc
239
240 e! XXEol
241 exe "normal ggO\<C-R>=&ffs\<CR>:\<C-R>=&ff\<CR>"
242 w! Xtest
243 call assert_equal("mac,dos,unix:mac\rnoeol\r", s:file2str('Xtest'))
244 bwipe! XXEol
245
246 " Test 5: try with 'binary' set
247 set fileformats=mac,unix,dos
248 set binary
249 e! XXUxDsMc
250 w! Xtest
251 call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r",
252 \ s:file2str('Xtest'))
253 bwipe XXUxDsMc
254
255 set fileformats=mac
256 e! XXUxDsMc
257 w! Xtest
258 call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r",
259 \ s:file2str('Xtest'))
260 bwipe XXUxDsMc
261
262 set fileformats=dos
263 e! XXUxDsMc
264 w! Xtest
265 call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r",
266 \ s:file2str('Xtest'))
267 bwipe XXUxDsMc
268
269 e! XXUnix
270 w! Xtest
271 call assert_equal("unix\nunix\n", s:file2str('Xtest'))
272 bwipe! XXUnix
273
274 set nobinary ff& ffs&
275
276 " cleanup
277 only
278 %bwipe!
Bram Moolenaar72fcf072019-05-27 22:21:44 +0200279 call delete('XXUxDs')
280 call delete('XXUxMac')
281 call delete('XXDosMac')
282 call delete('XXMacEol')
283 call delete('XXUxDsMc')
284 call delete('Xtest')
285endfunc
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100286
287" Test for changing the fileformat using ++read
288func Test_fileformat_plusplus_read()
289 new
290 call setline(1, ['one', 'two', 'three'])
291 w ++ff=dos Xfile1
292 enew!
Bram Moolenaara36c8302020-02-16 15:08:28 +0100293 set ff=unix
Bram Moolenaar50434bd2020-02-16 14:55:22 +0100294 " A :read doesn't change the fileformat, but does apply to the read lines.
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100295 r ++fileformat=unix Xfile1
296 call assert_equal('unix', &fileformat)
Bram Moolenaar50434bd2020-02-16 14:55:22 +0100297 call assert_equal("three\r", getline('$'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100298 3r ++edit Xfile1
299 call assert_equal('dos', &fileformat)
300 close!
301 call delete('Xfile1')
302 set fileformat&
303 call assert_fails('e ++fileformat Xfile1', 'E474:')
304 call assert_fails('e ++ff=abc Xfile1', 'E474:')
305 call assert_fails('e ++abc1 Xfile1', 'E474:')
306endfunc
307
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200308" When Vim starts up with an empty buffer the first item in 'fileformats' is
309" used as the 'fileformat'.
310func Test_fileformat_on_startup()
311 let after =<< trim END
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100312 call writefile([&fileformat], 'Xonsfile', 'a')
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200313 quit
314 END
315 call RunVim(["set ffs=dos,unix,mac"], after, '')
316 call RunVim(["set ffs=mac,dos,unix"], after, '')
317 call RunVim(["set ffs=unix,mac,dos"], after, '')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100318 call assert_equal(['dos', 'mac', 'unix'], readfile('Xonsfile'))
319 call delete('Xonsfile')
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200320endfunc
321
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100322" vim: shiftwidth=2 sts=2 expandtab