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