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