test_modeline.vim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. " Tests for parsing the modeline.
  2. source check.vim
  3. func Test_modeline_invalid()
  4. " This was reading allocated memory in the past.
  5. call writefile(['vi:0', 'nothing'], 'Xmodeline')
  6. let modeline = &modeline
  7. set modeline
  8. call assert_fails('split Xmodeline', 'E518:')
  9. " Missing end colon (ignored).
  10. call writefile(['// vim: set ts=2'], 'Xmodeline')
  11. edit Xmodeline_version
  12. call assert_equal(8, &ts)
  13. bwipe!
  14. " Missing colon at beginning (ignored).
  15. call writefile(['// vim set ts=2:'], 'Xmodeline')
  16. edit Xmodeline_version
  17. call assert_equal(8, &ts)
  18. bwipe!
  19. " Missing space after vim (ignored).
  20. call writefile(['// vim:ts=2:'], 'Xmodeline')
  21. edit Xmodeline_version
  22. call assert_equal(8, &ts)
  23. bwipe!
  24. let &modeline = modeline
  25. bwipe!
  26. call delete('Xmodeline')
  27. endfunc
  28. func Test_modeline_filetype()
  29. call writefile(['vim: set ft=c :', 'nothing'], 'Xmodeline_filetype')
  30. let modeline = &modeline
  31. set modeline
  32. filetype plugin on
  33. split Xmodeline_filetype
  34. call assert_equal("c", &filetype)
  35. call assert_equal(1, b:did_ftplugin)
  36. call assert_equal("ccomplete#Complete", &ofu)
  37. bwipe!
  38. call delete('Xmodeline_filetype')
  39. let &modeline = modeline
  40. filetype plugin off
  41. endfunc
  42. func Test_modeline_syntax()
  43. call writefile(['vim: set syn=c :', 'nothing'], 'Xmodeline_syntax')
  44. let modeline = &modeline
  45. set modeline
  46. syntax enable
  47. split Xmodeline_syntax
  48. call assert_equal("c", &syntax)
  49. call assert_equal("c", b:current_syntax)
  50. bwipe!
  51. call delete('Xmodeline_syntax')
  52. let &modeline = modeline
  53. syntax off
  54. endfunc
  55. func Test_modeline_keymap()
  56. if !has('keymap')
  57. return
  58. endif
  59. call writefile(['vim: set keymap=greek :', 'nothing'], 'Xmodeline_keymap')
  60. let modeline = &modeline
  61. set modeline
  62. split Xmodeline_keymap
  63. call assert_equal("greek", &keymap)
  64. call assert_match('greek\|grk', b:keymap_name)
  65. bwipe!
  66. call delete('Xmodeline_keymap')
  67. let &modeline = modeline
  68. set keymap= iminsert=0 imsearch=-1
  69. endfunc
  70. func Test_modeline_version()
  71. let modeline = &modeline
  72. set modeline
  73. " Test with vim:{vers}: (version {vers} or later).
  74. call writefile(['// vim' .. v:version .. ': ts=2:'], 'Xmodeline_version')
  75. edit Xmodeline_version
  76. call assert_equal(2, &ts)
  77. bwipe!
  78. call writefile(['// vim' .. (v:version - 100) .. ': ts=2:'], 'Xmodeline_version')
  79. edit Xmodeline_version
  80. call assert_equal(2, &ts)
  81. bwipe!
  82. call writefile(['// vim' .. (v:version + 100) .. ': ts=2:'], 'Xmodeline_version')
  83. edit Xmodeline_version
  84. call assert_equal(8, &ts)
  85. bw!
  86. " Test with vim>{vers}: (version after {vers}).
  87. call writefile(['// vim>' .. v:version .. ': ts=2:'], 'Xmodeline_version')
  88. edit Xmodeline_version
  89. call assert_equal(8, &ts)
  90. bwipe!
  91. call writefile(['// vim>' .. (v:version - 100) .. ': ts=2:'], 'Xmodeline_version')
  92. edit Xmodeline_version
  93. call assert_equal(2, &ts)
  94. bwipe!
  95. call writefile(['// vim>' .. (v:version + 100) .. ': ts=2:'], 'Xmodeline_version')
  96. edit Xmodeline_version
  97. call assert_equal(8, &ts)
  98. bwipe!
  99. " Test with vim<{vers}: (version before {vers}).
  100. call writefile(['// vim<' .. v:version .. ': ts=2:'], 'Xmodeline_version')
  101. edit Xmodeline_version
  102. call assert_equal(8, &ts)
  103. bwipe!
  104. call writefile(['// vim<' .. (v:version - 100) .. ': ts=2:'], 'Xmodeline_version')
  105. edit Xmodeline_version
  106. call assert_equal(8, &ts)
  107. bwipe!
  108. call writefile(['// vim<' .. (v:version + 100) .. ': ts=2:'], 'Xmodeline_version')
  109. edit Xmodeline_version
  110. call assert_equal(2, &ts)
  111. bwipe!
  112. " Test with vim={vers}: (version {vers} only).
  113. call writefile(['// vim=' .. v:version .. ': ts=2:'], 'Xmodeline_version')
  114. edit Xmodeline_version
  115. call assert_equal(2, &ts)
  116. bwipe!
  117. call writefile(['// vim=' .. (v:version - 100) .. ': ts=2:'], 'Xmodeline_version')
  118. edit Xmodeline_version
  119. call assert_equal(8, &ts)
  120. bwipe!
  121. call writefile(['// vim=' .. (v:version + 100) .. ': ts=2:'], 'Xmodeline_version')
  122. edit Xmodeline_version
  123. call assert_equal(8, &ts)
  124. bwipe!
  125. let &modeline = modeline
  126. call delete('Xmodeline_version')
  127. endfunc
  128. func Test_modeline_colon()
  129. let modeline = &modeline
  130. set modeline
  131. call writefile(['// vim: set showbreak=\: ts=2: sw=2'], 'Xmodeline_colon')
  132. edit Xmodeline_colon
  133. " backlash colon should become colon.
  134. call assert_equal(':', &showbreak)
  135. " 'ts' should be set.
  136. " 'sw' should be ignored because it is after the end colon.
  137. call assert_equal(2, &ts)
  138. call assert_equal(8, &sw)
  139. let &modeline = modeline
  140. call delete('Xmodeline_colon')
  141. endfunc
  142. func s:modeline_fails(what, text, error)
  143. " Don't use CheckOption(), it would skip the whole test
  144. " just for a single un-supported option
  145. if !exists('+' .. a:what)
  146. return
  147. endif
  148. let fname = "Xmodeline_fails_" . a:what
  149. call writefile(['vim: set ' . a:text . ' :', 'nothing'], fname)
  150. let modeline = &modeline
  151. set modeline
  152. filetype plugin on
  153. syntax enable
  154. call assert_fails('split ' . fname, a:error)
  155. call assert_equal("", &filetype)
  156. call assert_equal("", &syntax)
  157. bwipe!
  158. call delete(fname)
  159. let &modeline = modeline
  160. filetype plugin off
  161. syntax off
  162. endfunc
  163. func Test_modeline_filetype_fails()
  164. call s:modeline_fails('filetype', 'ft=evil$CMD', 'E474:')
  165. endfunc
  166. func Test_modeline_syntax_fails()
  167. call s:modeline_fails('syntax', 'syn=evil$CMD', 'E474:')
  168. endfunc
  169. func Test_modeline_keymap_fails()
  170. call s:modeline_fails('keymap', 'keymap=evil$CMD', 'E474:')
  171. endfunc
  172. func Test_modeline_fails_always()
  173. call s:modeline_fails('backupdir', 'backupdir=Something()', 'E520:')
  174. call s:modeline_fails('cdpath', 'cdpath=Something()', 'E520:')
  175. call s:modeline_fails('charconvert', 'charconvert=Something()', 'E520:')
  176. call s:modeline_fails('completefunc', 'completefunc=Something()', 'E520:')
  177. call s:modeline_fails('cscopeprg', 'cscopeprg=Something()', 'E520:')
  178. call s:modeline_fails('diffexpr', 'diffexpr=Something()', 'E520:')
  179. call s:modeline_fails('directory', 'directory=Something()', 'E520:')
  180. call s:modeline_fails('equalprg', 'equalprg=Something()', 'E520:')
  181. call s:modeline_fails('errorfile', 'errorfile=Something()', 'E520:')
  182. call s:modeline_fails('exrc', 'exrc=Something()', 'E520:')
  183. call s:modeline_fails('findfunc', 'findfunc=Something', 'E520:')
  184. call s:modeline_fails('formatprg', 'formatprg=Something()', 'E520:')
  185. call s:modeline_fails('fsync', 'fsync=Something()', 'E520:')
  186. call s:modeline_fails('grepprg', 'grepprg=Something()', 'E520:')
  187. call s:modeline_fails('helpfile', 'helpfile=Something()', 'E520:')
  188. call s:modeline_fails('imactivatefunc', 'imactivatefunc=Something()', 'E520:')
  189. call s:modeline_fails('imstatusfunc', 'imstatusfunc=Something()', 'E520:')
  190. call s:modeline_fails('imstyle', 'imstyle=Something()', 'E520:')
  191. call s:modeline_fails('keywordprg', 'keywordprg=Something()', 'E520:')
  192. call s:modeline_fails('langmap', 'langmap=Something()', 'E520:')
  193. call s:modeline_fails('luadll', 'luadll=Something()', 'E520:')
  194. call s:modeline_fails('makeef', 'makeef=Something()', 'E520:')
  195. call s:modeline_fails('makeprg', 'makeprg=Something()', 'E520:')
  196. call s:modeline_fails('mkspellmem', 'mkspellmem=Something()', 'E520:')
  197. call s:modeline_fails('mzschemedll', 'mzschemedll=Something()', 'E520:')
  198. call s:modeline_fails('mzschemegcdll', 'mzschemegcdll=Something()', 'E520:')
  199. call s:modeline_fails('modelineexpr', 'modelineexpr', 'E520:')
  200. call s:modeline_fails('omnifunc', 'omnifunc=Something()', 'E520:')
  201. call s:modeline_fails('operatorfunc', 'operatorfunc=Something()', 'E520:')
  202. call s:modeline_fails('perldll', 'perldll=Something()', 'E520:')
  203. call s:modeline_fails('printdevice', 'printdevice=Something()', 'E520:')
  204. call s:modeline_fails('patchexpr', 'patchexpr=Something()', 'E520:')
  205. call s:modeline_fails('printexpr', 'printexpr=Something()', 'E520:')
  206. call s:modeline_fails('pythondll', 'pythondll=Something()', 'E520:')
  207. call s:modeline_fails('pythonhome', 'pythonhome=Something()', 'E520:')
  208. call s:modeline_fails('pythonthreedll', 'pythonthreedll=Something()', 'E520:')
  209. call s:modeline_fails('pythonthreehome', 'pythonthreehome=Something()', 'E520:')
  210. call s:modeline_fails('pyxversion', 'pyxversion=Something()', 'E520:')
  211. call s:modeline_fails('rubydll', 'rubydll=Something()', 'E520:')
  212. call s:modeline_fails('runtimepath', 'runtimepath=Something()', 'E520:')
  213. call s:modeline_fails('secure', 'secure=Something()', 'E520:')
  214. call s:modeline_fails('shell', 'shell=Something()', 'E520:')
  215. call s:modeline_fails('shellcmdflag', 'shellcmdflag=Something()', 'E520:')
  216. call s:modeline_fails('shellpipe', 'shellpipe=Something()', 'E520:')
  217. call s:modeline_fails('shellquote', 'shellquote=Something()', 'E520:')
  218. call s:modeline_fails('shellredir', 'shellredir=Something()', 'E520:')
  219. call s:modeline_fails('shellxquote', 'shellxquote=Something()', 'E520:')
  220. call s:modeline_fails('spellfile', 'spellfile=Something()', 'E520:')
  221. call s:modeline_fails('spellsuggest', 'spellsuggest=Something()', 'E520:')
  222. call s:modeline_fails('tcldll', 'tcldll=Something()', 'E520:')
  223. call s:modeline_fails('titleold', 'titleold=Something()', 'E520:')
  224. call s:modeline_fails('viewdir', 'viewdir=Something()', 'E520:')
  225. call s:modeline_fails('viminfo', 'viminfo=Something()', 'E520:')
  226. call s:modeline_fails('viminfofile', 'viminfofile=Something()', 'E520:')
  227. call s:modeline_fails('winptydll', 'winptydll=Something()', 'E520:')
  228. call s:modeline_fails('undodir', 'undodir=Something()', 'E520:')
  229. " only check a few terminal options
  230. " Skip these since nvim doesn't support termcodes as options
  231. "call s:modeline_fails('t_AB', 't_AB=Something()', 'E520:')
  232. "call s:modeline_fails('t_ce', 't_ce=Something()', 'E520:')
  233. "call s:modeline_fails('t_sr', 't_sr=Something()', 'E520:')
  234. "call s:modeline_fails('t_8b', 't_8b=Something()', 'E520:')
  235. endfunc
  236. func Test_modeline_fails_modelineexpr()
  237. call s:modeline_fails('balloonexpr', 'balloonexpr=Something()', 'E992:')
  238. call s:modeline_fails('foldexpr', 'foldexpr=Something()', 'E992:')
  239. call s:modeline_fails('foldtext', 'foldtext=Something()', 'E992:')
  240. call s:modeline_fails('formatexpr', 'formatexpr=Something()', 'E992:')
  241. call s:modeline_fails('guitablabel', 'guitablabel=Something()', 'E992:')
  242. call s:modeline_fails('iconstring', 'iconstring=Something()', 'E992:')
  243. call s:modeline_fails('includeexpr', 'includeexpr=Something()', 'E992:')
  244. call s:modeline_fails('indentexpr', 'indentexpr=Something()', 'E992:')
  245. call s:modeline_fails('rulerformat', 'rulerformat=Something()', 'E992:')
  246. call s:modeline_fails('statusline', 'statusline=Something()', 'E992:')
  247. call s:modeline_fails('tabline', 'tabline=Something()', 'E992:')
  248. call s:modeline_fails('titlestring', 'titlestring=Something()', 'E992:')
  249. endfunc
  250. func Test_modeline_setoption_verbose()
  251. let modeline = &modeline
  252. set modeline
  253. let lines =<< trim END
  254. 1 vim:ts=2
  255. 2 two
  256. 3 three
  257. 4 four
  258. 5 five
  259. 6 six
  260. 7 seven
  261. 8 eight
  262. END
  263. call writefile(lines, 'Xmodeline')
  264. edit Xmodeline
  265. let info = split(execute('verbose set tabstop?'), "\n")
  266. call assert_match('^\s*Last set from modeline line 1$', info[-1])
  267. bwipe!
  268. let lines =<< trim END
  269. 1 one
  270. 2 two
  271. 3 three
  272. 4 vim:ts=4
  273. 5 five
  274. 6 six
  275. 7 seven
  276. 8 eight
  277. END
  278. call writefile(lines, 'Xmodeline')
  279. edit Xmodeline
  280. let info = split(execute('verbose set tabstop?'), "\n")
  281. call assert_match('^\s*Last set from modeline line 4$', info[-1])
  282. bwipe!
  283. let lines =<< trim END
  284. 1 one
  285. 2 two
  286. 3 three
  287. 4 four
  288. 5 five
  289. 6 six
  290. 7 seven
  291. 8 vim:ts=8
  292. END
  293. call writefile(lines, 'Xmodeline')
  294. edit Xmodeline
  295. let info = split(execute('verbose set tabstop?'), "\n")
  296. call assert_match('^\s*Last set from modeline line 8$', info[-1])
  297. bwipe!
  298. let &modeline = modeline
  299. call delete('Xmodeline')
  300. endfunc
  301. " Test for the 'modeline' default value in compatible and non-compatible modes
  302. " for root and non-root accounts
  303. func Test_modeline_default()
  304. " set compatible
  305. " call assert_false(&modeline)
  306. set nocompatible
  307. call assert_equal(IsRoot() ? 0 : 1, &modeline)
  308. " set compatible&vi
  309. " call assert_false(&modeline)
  310. set compatible&vim
  311. call assert_equal(IsRoot() ? 0 : 1, &modeline)
  312. set compatible& modeline&
  313. endfunc
  314. " Some options cannot be set from the modeline when 'diff' option is set
  315. func Test_modeline_diff_buffer()
  316. call writefile(['vim: diff foldmethod=marker wrap'], 'Xfile')
  317. set foldmethod& nowrap
  318. new Xfile
  319. call assert_equal('manual', &foldmethod)
  320. call assert_false(&wrap)
  321. set wrap&
  322. call delete('Xfile')
  323. bw
  324. endfunc
  325. func Test_modeline_disable()
  326. set modeline
  327. call writefile(['vim: sw=2', 'vim: nomodeline', 'vim: sw=3'], 'Xmodeline_disable')
  328. edit Xmodeline_disable
  329. call assert_equal(2, &sw)
  330. call delete('Xmodeline_disable')
  331. endfunc
  332. " vim: shiftwidth=2 sts=2 expandtab