summaryrefslogtreecommitdiff
path: root/src/testdir/test_fileformat.vim
blob: efc3b2252e216fedf7ce72f25754d7d6334f5645 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301

" Test behavior of fileformat after bwipeout of last buffer
func Test_fileformat_after_bw()
  bwipeout
  set fileformat&
  if &fileformat == 'dos'
    let test_fileformats = 'unix'
  elseif &fileformat == 'unix'
    let test_fileformats = 'mac'
  else  " must be mac
    let test_fileformats = 'dos'
  endif
  exec 'set fileformats='.test_fileformats
  bwipeout!
  call assert_equal(test_fileformats, &fileformat)
  set fileformats&
endfunc

func Test_fileformat_autocommand()
  let filecnt = ["", "foobar\<CR>", "eins\<CR>", "\<CR>", "zwei\<CR>", "drei", "vier", "fünf", ""]
  let ffs = &ffs
  call writefile(filecnt, 'Xfile', 'b')
  au BufReadPre Xfile set ffs=dos ff=dos
  new Xfile
  call assert_equal('dos', &l:ff)
  call assert_equal('dos', &ffs)

  " cleanup
  call delete('Xfile')
  let &ffs = ffs
  au! BufReadPre Xfile
  bw!
endfunc

" Convert the contents of a file into a literal string
func s:file2str(fname)
  let b = readfile(a:fname, 'B')
  let s = ''
  for c in b
    let s .= nr2char(c)
  endfor
  return s
endfunc

" Concatenate the contents of files 'f1' and 'f2' and create 'destfile'
func s:concat_files(f1, f2, destfile)
  let b1 = readfile(a:f1, 'B')
  let b2 = readfile(a:f2, 'B')
  let b3 = b1 + b2
  call writefile(b3, a:destfile, 'B')
endfun

" Test for a lot of variations of the 'fileformats' option
func Test_fileformats()
  " create three test files, one in each format
  call writefile(['unix', 'unix'], 'XXUnix')
  call writefile(["dos\r", "dos\r"], 'XXDos')
  call writefile(["mac\rmac\r"], 'XXMac', 'b')
  " create a file with no End Of Line
  call writefile(["noeol"], 'XXEol', 'b')
  " create mixed format files
  call s:concat_files('XXUnix', 'XXDos', 'XXUxDs')
  call s:concat_files('XXUnix', 'XXMac', 'XXUxMac')
  call s:concat_files('XXDos', 'XXMac', 'XXDosMac')
  call s:concat_files('XXMac', 'XXEol', 'XXMacEol')
  call s:concat_files('XXUxDs', 'XXMac', 'XXUxDsMc')

  new

  " Test 1: try reading and writing with 'fileformats' empty
  set fileformats=

  " try with 'fileformat' set to 'unix'
  set fileformat=unix
  e! XXUnix
  w! Xtest
  call assert_equal("unix\nunix\n", s:file2str('Xtest'))
  e! XXDos
  w! Xtest
  call assert_equal("dos\r\ndos\r\n", s:file2str('Xtest'))
  e! XXMac
  w! Xtest
  call assert_equal("mac\rmac\r\n", s:file2str('Xtest'))
  bwipe XXUnix XXDos XXMac

  " try with 'fileformat' set to 'dos'
  set fileformat=dos
  e! XXUnix
  w! Xtest
  call assert_equal("unix\r\nunix\r\n", s:file2str('Xtest'))
  e! XXDos
  w! Xtest
  call assert_equal("dos\r\ndos\r\n", s:file2str('Xtest'))
  e! XXMac
  w! Xtest
  call assert_equal("mac\rmac\r\r\n", s:file2str('Xtest'))
  bwipe XXUnix XXDos XXMac

  " try with 'fileformat' set to 'mac'
  set fileformat=mac
  e! XXUnix
  w! Xtest
  call assert_equal("unix\nunix\n\r", s:file2str('Xtest'))
  e! XXDos
  w! Xtest
  call assert_equal("dos\r\ndos\r\n\r", s:file2str('Xtest'))
  e! XXMac
  w! Xtest
  call assert_equal("mac\rmac\r", s:file2str('Xtest'))
  bwipe XXUnix XXDos XXMac

  " Test 2: try reading and writing with 'fileformats' set to one format

  " try with 'fileformats' set to 'unix'
  set fileformats=unix
  e! XXUxDsMc
  w! Xtest
  call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r\n",
	      \ s:file2str('Xtest'))
  bwipe XXUxDsMc

  " try with 'fileformats' set to 'dos'
  set fileformats=dos
  e! XXUxDsMc
  w! Xtest
  call assert_equal("unix\r\nunix\r\ndos\r\ndos\r\nmac\rmac\r\r\n",
	      \ s:file2str('Xtest'))
  bwipe XXUxDsMc

  " try with 'fileformats' set to 'mac'
  set fileformats=mac
  e! XXUxDsMc
  w! Xtest
  call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r",
	      \ s:file2str('Xtest'))
  bwipe XXUxDsMc

  " Test 3: try reading and writing with 'fileformats' set to two formats

  " try with 'fileformats' set to 'unix,dos'
  set fileformats=unix,dos
  e! XXUxDsMc
  w! Xtest
  call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r\n",
	      \ s:file2str('Xtest'))
  bwipe XXUxDsMc

  e! XXUxMac
  w! Xtest
  call assert_equal("unix\nunix\nmac\rmac\r\n", s:file2str('Xtest'))
  bwipe XXUxMac

  e! XXDosMac
  w! Xtest
  call assert_equal("dos\r\ndos\r\nmac\rmac\r\r\n", s:file2str('Xtest'))
  bwipe XXDosMac

  " try with 'fileformats' set to 'unix,mac'
  set fileformats=unix,mac
  e! XXUxDs
  w! Xtest
  call assert_equal("unix\nunix\ndos\r\ndos\r\n", s:file2str('Xtest'))
  bwipe XXUxDs

  e! XXUxDsMc
  w! Xtest
  call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r\n",
	      \ s:file2str('Xtest'))
  bwipe XXUxDsMc

  e! XXDosMac
  w! Xtest
  call assert_equal("dos\r\ndos\r\nmac\rmac\r", s:file2str('Xtest'))
  bwipe XXDosMac

  e! XXEol
  exe "normal ggO\<C-R>=&ffs\<CR>:\<C-R>=&ff\<CR>"
  w! Xtest
  call assert_equal("unix,mac:unix\nnoeol\n", s:file2str('Xtest'))
  bwipe! XXEol

  " try with 'fileformats' set to 'dos,mac'
  set fileformats=dos,mac
  e! XXUxDs
  w! Xtest
  call assert_equal("unix\r\nunix\r\ndos\r\ndos\r\n", s:file2str('Xtest'))
  bwipe XXUxDs

  e! XXUxMac
  exe "normal ggO\<C-R>=&ffs\<CR>:\<C-R>=&ff\<CR>"
  w! Xtest
  call assert_equal("dos,mac:dos\r\nunix\r\nunix\r\nmac\rmac\r\r\n",
	      \ s:file2str('Xtest'))
  bwipe! XXUxMac

  e! XXUxDsMc
  w! Xtest
  call assert_equal("unix\r\nunix\r\ndos\r\ndos\r\nmac\rmac\r\r\n",
	      \ s:file2str('Xtest'))
  bwipe XXUxDsMc

  e! XXMacEol
  exe "normal ggO\<C-R>=&ffs\<CR>:\<C-R>=&ff\<CR>"
  w! Xtest
  call assert_equal("dos,mac:mac\rmac\rmac\rnoeol\r", s:file2str('Xtest'))
  bwipe! XXMacEol

  " Test 4: try reading and writing with 'fileformats' set to three formats
  set fileformats=unix,dos,mac
  e! XXUxDsMc
  w! Xtest
  call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r\n",
	      \ s:file2str('Xtest'))
  bwipe XXUxDsMc

  e! XXEol
  exe "normal ggO\<C-R>=&ffs\<CR>:\<C-R>=&ff\<CR>"
  w! Xtest
  call assert_equal("unix,dos,mac:unix\nnoeol\n", s:file2str('Xtest'))
  bwipe! XXEol

  set fileformats=mac,dos,unix
  e! XXUxDsMc
  w! Xtest
  call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r\n",
	      \ s:file2str('Xtest'))
  bwipe XXUxDsMc

  e! XXEol
  exe "normal ggO\<C-R>=&ffs\<CR>:\<C-R>=&ff\<CR>"
  w! Xtest
  call assert_equal("mac,dos,unix:mac\rnoeol\r", s:file2str('Xtest'))
  bwipe! XXEol

  " Test 5: try with 'binary' set
  set fileformats=mac,unix,dos
  set binary
  e! XXUxDsMc
  w! Xtest
  call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r",
	      \ s:file2str('Xtest'))
  bwipe XXUxDsMc

  set fileformats=mac
  e! XXUxDsMc
  w! Xtest
  call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r",
	      \ s:file2str('Xtest'))
  bwipe XXUxDsMc

  set fileformats=dos
  e! XXUxDsMc
  w! Xtest
  call assert_equal("unix\nunix\ndos\r\ndos\r\nmac\rmac\r",
	      \ s:file2str('Xtest'))
  bwipe XXUxDsMc

  e! XXUnix
  w! Xtest
  call assert_equal("unix\nunix\n", s:file2str('Xtest'))
  bwipe! XXUnix

  set nobinary ff& ffs&

  " cleanup
  only
  %bwipe!
  call delete('XXUnix')
  call delete('XXDos')
  call delete('XXMac')
  call delete('XXEol')
  call delete('XXUxDs')
  call delete('XXUxMac')
  call delete('XXDosMac')
  call delete('XXMacEol')
  call delete('XXUxDsMc')
  call delete('Xtest')
endfunc

" Test for changing the fileformat using ++read
func Test_fileformat_plusplus_read()
  new
  call setline(1, ['one', 'two', 'three'])
  w ++ff=dos Xfile1
  enew!
  set ff=unix
  " A :read doesn't change the fileformat, but does apply to the read lines.
  r ++fileformat=unix Xfile1
  call assert_equal('unix', &fileformat)
  call assert_equal("three\r", getline('$'))
  3r ++edit Xfile1
  call assert_equal('dos', &fileformat)
  close!
  call delete('Xfile1')
  set fileformat&
  call assert_fails('e ++fileformat Xfile1', 'E474:')
  call assert_fails('e ++ff=abc Xfile1', 'E474:')
  call assert_fails('e ++abc1 Xfile1', 'E474:')
endfunc

" vim: shiftwidth=2 sts=2 expandtab