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