summaryrefslogtreecommitdiff
path: root/src/testdir/test_modeline.vim
blob: 546c30c668a76c0085bdce422c106fbc8434735d (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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
" Tests for parsing the modeline.

func Test_modeline_invalid()
  " This was reading allocated memory in the past.
  call writefile(['vi:0', 'nothing'], 'Xmodeline')
  let modeline = &modeline
  set modeline
  call assert_fails('split Xmodeline', 'E518:')

  " Missing end colon (ignored).
  call writefile(['// vim: set ts=2'], 'Xmodeline')
  edit Xmodeline_version
  call assert_equal(8, &ts)
  bwipe!

  " Missing colon at beginning (ignored).
  call writefile(['// vim set ts=2:'], 'Xmodeline')
  edit Xmodeline_version
  call assert_equal(8, &ts)
  bwipe!

  " Missing space after vim (ignored).
  call writefile(['// vim:ts=2:'], 'Xmodeline')
  edit Xmodeline_version
  call assert_equal(8, &ts)
  bwipe!

  let &modeline = modeline
  bwipe!
  call delete('Xmodeline')
endfunc

func Test_modeline_filetype()
  call writefile(['vim: set ft=c :', 'nothing'], 'Xmodeline_filetype')
  let modeline = &modeline
  set modeline
  filetype plugin on
  split Xmodeline_filetype
  call assert_equal("c", &filetype)
  call assert_equal(1, b:did_ftplugin)
  call assert_equal("ccomplete#Complete", &ofu)

  bwipe!
  call delete('Xmodeline_filetype')
  let &modeline = modeline
  filetype plugin off
endfunc

func Test_modeline_syntax()
  call writefile(['vim: set syn=c :', 'nothing'], 'Xmodeline_syntax')
  let modeline = &modeline
  set modeline
  syntax enable
  split Xmodeline_syntax
  call assert_equal("c", &syntax)
  call assert_equal("c", b:current_syntax)

  bwipe!
  call delete('Xmodeline_syntax')
  let &modeline = modeline
  syntax off
endfunc

func Test_modeline_keymap()
  if !has('keymap')
    return
  endif
  call writefile(['vim: set keymap=greek :', 'nothing'], 'Xmodeline_keymap')
  let modeline = &modeline
  set modeline
  split Xmodeline_keymap
  call assert_equal("greek", &keymap)
  call assert_match('greek\|grk', b:keymap_name)

  bwipe!
  call delete('Xmodeline_keymap')
  let &modeline = modeline
  set keymap= iminsert=0 imsearch=-1
endfunc

func Test_modeline_version()
  let modeline = &modeline
  set modeline

  " Test with vim:{vers}: (version {vers} or later).
  call writefile(['// vim' .. v:version .. ': ts=2:'], 'Xmodeline_version')
  edit Xmodeline_version
  call assert_equal(2, &ts)
  bwipe!

  call writefile(['// vim' .. (v:version - 100) .. ': ts=2:'], 'Xmodeline_version')
  edit Xmodeline_version
  call assert_equal(2, &ts)
  bwipe!

  call writefile(['// vim' .. (v:version + 100) .. ': ts=2:'], 'Xmodeline_version')
  edit Xmodeline_version
  call assert_equal(8, &ts)
  bw!

  " Test with vim>{vers}: (version after {vers}).
  call writefile(['// vim>' .. v:version .. ': ts=2:'], 'Xmodeline_version')
  edit Xmodeline_version
  call assert_equal(8, &ts)
  bwipe!

  call writefile(['// vim>' .. (v:version - 100) .. ': ts=2:'], 'Xmodeline_version')
  edit Xmodeline_version
  call assert_equal(2, &ts)
  bwipe!

  call writefile(['// vim>' .. (v:version + 100) .. ': ts=2:'], 'Xmodeline_version')
  edit Xmodeline_version
  call assert_equal(8, &ts)
  bwipe!

  " Test with vim<{vers}: (version before {vers}).
  call writefile(['// vim<' .. v:version .. ': ts=2:'], 'Xmodeline_version')
  edit Xmodeline_version
  call assert_equal(8, &ts)
  bwipe!

  call writefile(['// vim<' .. (v:version - 100) .. ': ts=2:'], 'Xmodeline_version')
  edit Xmodeline_version
  call assert_equal(8, &ts)
  bwipe!

  call writefile(['// vim<' .. (v:version + 100) .. ': ts=2:'], 'Xmodeline_version')
  edit Xmodeline_version
  call assert_equal(2, &ts)
  bwipe!

  " Test with vim={vers}: (version {vers} only).
  call writefile(['// vim=' .. v:version .. ': ts=2:'], 'Xmodeline_version')
  edit Xmodeline_version
  call assert_equal(2, &ts)
  bwipe!

  call writefile(['// vim=' .. (v:version - 100) .. ': ts=2:'], 'Xmodeline_version')
  edit Xmodeline_version
  call assert_equal(8, &ts)
  bwipe!

  call writefile(['// vim=' .. (v:version + 100) .. ': ts=2:'], 'Xmodeline_version')
  edit Xmodeline_version
  call assert_equal(8, &ts)
  bwipe!

  let &modeline = modeline
  call delete('Xmodeline_version')
endfunc

func Test_modeline_colon()
  let modeline = &modeline
  set modeline

  call writefile(['// vim: set showbreak=\: ts=2: sw=2'], 'Xmodeline_colon')
  edit Xmodeline_colon

  " backlash colon should become colon.
  call assert_equal(':', &showbreak)

  " 'ts' should be set.
  " 'sw' should be ignored because it is after the end colon.
  call assert_equal(2, &ts)
  call assert_equal(8, &sw)

  let &modeline = modeline
  call delete('Xmodeline_colon')
endfunc

func s:modeline_fails(what, text, error)
  if !exists('+' .. a:what)
    return
  endif
  let fname = "Xmodeline_fails_" . a:what
  call writefile(['vim: set ' . a:text . ' :', 'nothing'], fname)
  let modeline = &modeline
  set modeline
  filetype plugin on
  syntax enable
  call assert_fails('split ' . fname, a:error)
  call assert_equal("", &filetype)
  call assert_equal("", &syntax)

  bwipe!
  call delete(fname)
  let &modeline = modeline
  filetype plugin off
  syntax off
endfunc

func Test_modeline_filetype_fails()
  call s:modeline_fails('filetype', 'ft=evil$CMD', 'E474:')
endfunc

func Test_modeline_syntax_fails()
  call s:modeline_fails('syntax', 'syn=evil$CMD', 'E474:')
endfunc

func Test_modeline_keymap_fails()
  call s:modeline_fails('keymap', 'keymap=evil$CMD', 'E474:')
endfunc

func Test_modeline_fails_always()
  call s:modeline_fails('backupdir', 'backupdir=Something()', 'E520:')
  call s:modeline_fails('cdpath', 'cdpath=Something()', 'E520:')
  call s:modeline_fails('charconvert', 'charconvert=Something()', 'E520:')
  call s:modeline_fails('completefunc', 'completefunc=Something()', 'E520:')
  call s:modeline_fails('cscopeprg', 'cscopeprg=Something()', 'E520:')
  call s:modeline_fails('diffexpr', 'diffexpr=Something()', 'E520:')
  call s:modeline_fails('directory', 'directory=Something()', 'E520:')
  call s:modeline_fails('equalprg', 'equalprg=Something()', 'E520:')
  call s:modeline_fails('errorfile', 'errorfile=Something()', 'E520:')
  call s:modeline_fails('exrc', 'exrc=Something()', 'E520:')
  call s:modeline_fails('formatprg', 'formatprg=Something()', 'E520:')
  call s:modeline_fails('fsync', 'fsync=Something()', 'E520:')
  call s:modeline_fails('grepprg', 'grepprg=Something()', 'E520:')
  call s:modeline_fails('helpfile', 'helpfile=Something()', 'E520:')
  call s:modeline_fails('imactivatefunc', 'imactivatefunc=Something()', 'E520:')
  call s:modeline_fails('imstatusfunc', 'imstatusfunc=Something()', 'E520:')
  call s:modeline_fails('imstyle', 'imstyle=Something()', 'E520:')
  call s:modeline_fails('keywordprg', 'keywordprg=Something()', 'E520:')
  call s:modeline_fails('langmap', 'langmap=Something()', 'E520:')
  call s:modeline_fails('luadll', 'luadll=Something()', 'E520:')
  call s:modeline_fails('makeef', 'makeef=Something()', 'E520:')
  call s:modeline_fails('makeprg', 'makeprg=Something()', 'E520:')
  call s:modeline_fails('mkspellmem', 'mkspellmem=Something()', 'E520:')
  call s:modeline_fails('mzschemedll', 'mzschemedll=Something()', 'E520:')
  call s:modeline_fails('mzschemegcdll', 'mzschemegcdll=Something()', 'E520:')
  call s:modeline_fails('modelineexpr', 'modelineexpr', 'E520:')
  call s:modeline_fails('omnifunc', 'omnifunc=Something()', 'E520:')
  call s:modeline_fails('operatorfunc', 'operatorfunc=Something()', 'E520:')
  call s:modeline_fails('perldll', 'perldll=Something()', 'E520:')
  call s:modeline_fails('printdevice', 'printdevice=Something()', 'E520:')
  call s:modeline_fails('patchexpr', 'patchexpr=Something()', 'E520:')
  call s:modeline_fails('printexpr', 'printexpr=Something()', 'E520:')
  call s:modeline_fails('pythondll', 'pythondll=Something()', 'E520:')
  call s:modeline_fails('pythonhome', 'pythonhome=Something()', 'E520:')
  call s:modeline_fails('pythonthreedll', 'pythonthreedll=Something()', 'E520:')
  call s:modeline_fails('pythonthreehome', 'pythonthreehome=Something()', 'E520:')
  call s:modeline_fails('pyxversion', 'pyxversion=Something()', 'E520:')
  call s:modeline_fails('rubydll', 'rubydll=Something()', 'E520:')
  call s:modeline_fails('runtimepath', 'runtimepath=Something()', 'E520:')
  call s:modeline_fails('secure', 'secure=Something()', 'E520:')
  call s:modeline_fails('shell', 'shell=Something()', 'E520:')
  call s:modeline_fails('shellcmdflag', 'shellcmdflag=Something()', 'E520:')
  call s:modeline_fails('shellpipe', 'shellpipe=Something()', 'E520:')
  call s:modeline_fails('shellquote', 'shellquote=Something()', 'E520:')
  call s:modeline_fails('shellredir', 'shellredir=Something()', 'E520:')
  call s:modeline_fails('shellxquote', 'shellxquote=Something()', 'E520:')
  call s:modeline_fails('spellfile', 'spellfile=Something()', 'E520:')
  call s:modeline_fails('spellsuggest', 'spellsuggest=Something()', 'E520:')
  call s:modeline_fails('tcldll', 'tcldll=Something()', 'E520:')
  call s:modeline_fails('titleold', 'titleold=Something()', 'E520:')
  call s:modeline_fails('viewdir', 'viewdir=Something()', 'E520:')
  call s:modeline_fails('viminfo', 'viminfo=Something()', 'E520:')
  call s:modeline_fails('viminfofile', 'viminfofile=Something()', 'E520:')
  call s:modeline_fails('winptydll', 'winptydll=Something()', 'E520:')
  call s:modeline_fails('undodir', 'undodir=Something()', 'E520:')
  " only check a few terminal options
  call s:modeline_fails('t_AB', 't_AB=Something()', 'E520:')
  call s:modeline_fails('t_ce', 't_ce=Something()', 'E520:')
  call s:modeline_fails('t_sr', 't_sr=Something()', 'E520:')
  call s:modeline_fails('t_8b', 't_8b=Something()', 'E520:')
endfunc

func Test_modeline_fails_modelineexpr()
  call s:modeline_fails('balloonexpr', 'balloonexpr=Something()', 'E992:')
  call s:modeline_fails('foldexpr', 'foldexpr=Something()', 'E992:')
  call s:modeline_fails('foldtext', 'foldtext=Something()', 'E992:')
  call s:modeline_fails('formatexpr', 'formatexpr=Something()', 'E992:')
  call s:modeline_fails('guitablabel', 'guitablabel=Something()', 'E992:')
  call s:modeline_fails('iconstring', 'iconstring=Something()', 'E992:')
  call s:modeline_fails('includeexpr', 'includeexpr=Something()', 'E992:')
  call s:modeline_fails('indentexpr', 'indentexpr=Something()', 'E992:')
  call s:modeline_fails('rulerformat', 'rulerformat=Something()', 'E992:')
  call s:modeline_fails('statusline', 'statusline=Something()', 'E992:')
  call s:modeline_fails('tabline', 'tabline=Something()', 'E992:')
  call s:modeline_fails('titlestring', 'titlestring=Something()', 'E992:')
endfunc

func Test_modeline_setoption_verbose()
  let modeline = &modeline
  set modeline

  let lines =<< trim END
  1 vim:ts=2
  2 two
  3 three
  4 four
  5 five
  6 six
  7 seven
  8 eight
  END
  call writefile(lines, 'Xmodeline')
  edit Xmodeline
  let info = split(execute('verbose set tabstop?'), "\n")
  call assert_match('^\s*Last set from modeline line 1$', info[-1])
  bwipe!

  let lines =<< trim END
  1 one
  2 two
  3 three
  4 vim:ts=4
  5 five
  6 six
  7 seven
  8 eight
  END
  call writefile(lines, 'Xmodeline')
  edit Xmodeline
  let info = split(execute('verbose set tabstop?'), "\n")
  call assert_match('^\s*Last set from modeline line 4$', info[-1])
  bwipe!

  let lines =<< trim END
  1 one
  2 two
  3 three
  4 four
  5 five
  6 six
  7 seven
  8 vim:ts=8
  END
  call writefile(lines, 'Xmodeline')
  edit Xmodeline
  let info = split(execute('verbose set tabstop?'), "\n")
  call assert_match('^\s*Last set from modeline line 8$', info[-1])
  bwipe!

  let &modeline = modeline
  call delete('Xmodeline')
endfunc