autocmd_option_spec.lua 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local clear, eq, neq, eval = n.clear, t.eq, t.neq, n.eval
  4. local api = n.api
  5. local curbuf = n.api.nvim_get_current_buf
  6. local curwin = n.api.nvim_get_current_win
  7. local exec_capture = n.exec_capture
  8. local source, command = n.source, n.command
  9. local function declare_hook_function()
  10. source([[
  11. fu! AutoCommand(match, bufnr, winnr)
  12. let l:acc = {
  13. \ 'option' : a:match,
  14. \ 'oldval' : v:option_old,
  15. \ 'oldval_l' : v:option_oldlocal,
  16. \ 'oldval_g' : v:option_oldglobal,
  17. \ 'newval' : v:option_new,
  18. \ 'scope' : v:option_type,
  19. \ 'cmd' : v:option_command,
  20. \ 'attr' : {
  21. \ 'bufnr' : a:bufnr,
  22. \ 'winnr' : a:winnr,
  23. \ }
  24. \ }
  25. call add(g:ret, l:acc)
  26. endfu
  27. ]])
  28. end
  29. local function set_hook(pattern)
  30. command(
  31. 'au OptionSet ' .. pattern .. ' :call AutoCommand(expand("<amatch>"), bufnr("%"), winnr())'
  32. )
  33. end
  34. local function init_var()
  35. command('let g:ret = []')
  36. end
  37. local function get_result()
  38. local ret = api.nvim_get_var('ret')
  39. init_var()
  40. return ret
  41. end
  42. local function expected_table(option, oldval, oldval_l, oldval_g, newval, scope, cmd, attr)
  43. return {
  44. option = option,
  45. oldval = oldval,
  46. oldval_l = oldval_l,
  47. oldval_g = oldval_g,
  48. newval = newval,
  49. scope = scope,
  50. cmd = cmd,
  51. attr = attr,
  52. }
  53. end
  54. local function expected_combination(...)
  55. local args = { ... }
  56. local ret = get_result()
  57. if not (#args == #ret) then
  58. local expecteds = {}
  59. for _, v in pairs(args) do
  60. table.insert(expecteds, expected_table(unpack(v)))
  61. end
  62. eq(expecteds, ret)
  63. return
  64. end
  65. for i, v in ipairs(args) do
  66. local attr = v[8]
  67. if not attr then
  68. -- remove attr entries
  69. ret[i].attr = nil
  70. else
  71. -- remove attr entries which are not required
  72. for k in pairs(ret[i].attr) do
  73. if not attr[k] then
  74. ret[i].attr[k] = nil
  75. end
  76. end
  77. end
  78. eq(expected_table(unpack(v)), ret[i])
  79. end
  80. end
  81. local function expected_empty()
  82. eq({}, get_result())
  83. end
  84. local function make_buffer()
  85. local old_buf = curbuf()
  86. command('botright new')
  87. local new_buf = curbuf()
  88. command('wincmd p') -- move previous window
  89. neq(old_buf, new_buf)
  90. eq(old_buf, curbuf())
  91. return new_buf
  92. end
  93. local function get_new_window_number()
  94. local old_win = curwin()
  95. command('botright new')
  96. local new_win = curwin()
  97. local new_winnr = exec_capture('echo winnr()')
  98. command('wincmd p') -- move previous window
  99. neq(old_win, new_win)
  100. eq(old_win, curwin())
  101. return new_winnr
  102. end
  103. describe('au OptionSet', function()
  104. describe('with any option (*)', function()
  105. before_each(function()
  106. clear()
  107. declare_hook_function()
  108. init_var()
  109. set_hook('*')
  110. end)
  111. it('should be called in setting number option', function()
  112. command('set nu')
  113. expected_combination({ 'number', false, false, false, true, 'global', 'set' })
  114. command('setlocal nonu')
  115. expected_combination({ 'number', true, true, '', false, 'local', 'setlocal' })
  116. command('setglobal nonu')
  117. expected_combination({ 'number', true, '', true, false, 'global', 'setglobal' })
  118. end)
  119. it('should be called in setting autoindent option', function()
  120. command('setlocal ai')
  121. expected_combination({ 'autoindent', false, false, '', true, 'local', 'setlocal' })
  122. command('setglobal ai')
  123. expected_combination({ 'autoindent', false, '', false, true, 'global', 'setglobal' })
  124. command('set noai')
  125. expected_combination({ 'autoindent', true, true, true, false, 'global', 'set' })
  126. end)
  127. it('should be called in inverting global autoindent option', function()
  128. command('set ai!')
  129. expected_combination({ 'autoindent', false, false, false, true, 'global', 'set' })
  130. end)
  131. it('should be called in being unset local autoindent option', function()
  132. command('setlocal ai')
  133. expected_combination({ 'autoindent', false, false, '', true, 'local', 'setlocal' })
  134. command('setlocal ai<')
  135. expected_combination({ 'autoindent', true, true, '', false, 'local', 'setlocal' })
  136. end)
  137. it('should be called in setting global list and number option at the same time', function()
  138. command('set list nu')
  139. expected_combination(
  140. { 'list', false, false, false, true, 'global', 'set' },
  141. { 'number', false, false, false, true, 'global', 'set' }
  142. )
  143. end)
  144. it('should not print anything, use :noa', function()
  145. command('noa set nolist nonu')
  146. expected_empty()
  147. end)
  148. it('should be called in setting local acd', function()
  149. command('setlocal acd')
  150. expected_combination({ 'autochdir', false, false, '', true, 'local', 'setlocal' })
  151. end)
  152. it('should be called in setting autoread', function()
  153. command('set noar')
  154. expected_combination({ 'autoread', true, true, true, false, 'global', 'set' })
  155. command('setlocal ar')
  156. expected_combination({ 'autoread', false, false, '', true, 'local', 'setlocal' })
  157. end)
  158. it('should be called in inverting global autoread', function()
  159. command('setglobal invar')
  160. expected_combination({ 'autoread', true, '', true, false, 'global', 'setglobal' })
  161. end)
  162. it('should be called in setting backspace option through :let', function()
  163. local oldval = eval('&backspace')
  164. command('let &bs=""')
  165. expected_combination({ 'backspace', oldval, oldval, oldval, '', 'global', 'set' })
  166. end)
  167. describe('being set by setbufvar()', function()
  168. it('should not trigger because option name is invalid', function()
  169. command('silent! call setbufvar(1, "&l:bk", 1)')
  170. expected_empty()
  171. end)
  172. it('should trigger using correct option name', function()
  173. command('call setbufvar(1, "&backup", 1)')
  174. expected_combination({ 'backup', false, false, '', true, 'local', 'setlocal' })
  175. end)
  176. it('should trigger if the current buffer is different from the targeted buffer', function()
  177. local new_buffer = make_buffer()
  178. local new_bufnr = api.nvim_buf_get_number(new_buffer)
  179. command('call setbufvar(' .. new_bufnr .. ', "&buftype", "nofile")')
  180. expected_combination({
  181. 'buftype',
  182. '',
  183. '',
  184. '',
  185. 'nofile',
  186. 'local',
  187. 'setlocal',
  188. { bufnr = new_bufnr },
  189. })
  190. end)
  191. end)
  192. it('with string global option', function()
  193. local oldval = eval('&backupext')
  194. command('set backupext=foo')
  195. expected_combination({ 'backupext', oldval, oldval, oldval, 'foo', 'global', 'set' })
  196. command('set backupext&')
  197. expected_combination({ 'backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set' })
  198. command('setglobal backupext=bar')
  199. expected_combination({ 'backupext', oldval, '', oldval, 'bar', 'global', 'setglobal' })
  200. command('noa set backupext&')
  201. -- As this is a global option this sets the global value even though :setlocal is used!
  202. command('setlocal backupext=baz')
  203. expected_combination({ 'backupext', oldval, oldval, '', 'baz', 'local', 'setlocal' })
  204. command('noa setglobal backupext=ext_global')
  205. command('noa setlocal backupext=ext_local') -- Sets the global(!) value
  206. command('set backupext=foo')
  207. expected_combination({
  208. 'backupext',
  209. 'ext_local',
  210. 'ext_local',
  211. 'ext_local',
  212. 'foo',
  213. 'global',
  214. 'set',
  215. })
  216. end)
  217. it('with string global-local (to buffer) option', function()
  218. local oldval = eval('&tags')
  219. command('set tags=tagpath')
  220. expected_combination({ 'tags', oldval, oldval, oldval, 'tagpath', 'global', 'set' })
  221. command('set tags&')
  222. expected_combination({ 'tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set' })
  223. command('setglobal tags=tagpath1')
  224. expected_combination({ 'tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal' })
  225. command('setlocal tags=tagpath2')
  226. expected_combination({ 'tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal' })
  227. -- Note: v:option_old is the old global value for global-local options.
  228. -- but the old local value for all other kinds of options.
  229. command('noa setglobal tags=tag_global')
  230. command('noa setlocal tags=tag_local')
  231. command('set tags=tagpath')
  232. expected_combination({
  233. 'tags',
  234. 'tag_global',
  235. 'tag_local',
  236. 'tag_global',
  237. 'tagpath',
  238. 'global',
  239. 'set',
  240. })
  241. -- Note: v:option_old is the old global value for global-local options.
  242. -- but the old local value for all other kinds of options.
  243. command('noa set tags=tag_global')
  244. command('noa setlocal tags=')
  245. command('set tags=tagpath')
  246. expected_combination({
  247. 'tags',
  248. 'tag_global',
  249. 'tag_global',
  250. 'tag_global',
  251. 'tagpath',
  252. 'global',
  253. 'set',
  254. })
  255. end)
  256. it('with string local (to buffer) option', function()
  257. local oldval = eval('&spelllang')
  258. command('set spelllang=elvish,klingon')
  259. expected_combination({
  260. 'spelllang',
  261. oldval,
  262. oldval,
  263. oldval,
  264. 'elvish,klingon',
  265. 'global',
  266. 'set',
  267. })
  268. command('set spelllang&')
  269. expected_combination({
  270. 'spelllang',
  271. 'elvish,klingon',
  272. 'elvish,klingon',
  273. 'elvish,klingon',
  274. oldval,
  275. 'global',
  276. 'set',
  277. })
  278. command('setglobal spelllang=elvish')
  279. expected_combination({ 'spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal' })
  280. command('noa set spelllang&')
  281. command('setlocal spelllang=klingon')
  282. expected_combination({ 'spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal' })
  283. -- Note: v:option_old is the old global value for global-local options.
  284. -- but the old local value for all other kinds of options.
  285. command('noa setglobal spelllang=spellglobal')
  286. command('noa setlocal spelllang=spelllocal')
  287. command('set spelllang=foo')
  288. expected_combination({
  289. 'spelllang',
  290. 'spelllocal',
  291. 'spelllocal',
  292. 'spellglobal',
  293. 'foo',
  294. 'global',
  295. 'set',
  296. })
  297. end)
  298. it('with string global-local (to window) option', function()
  299. local oldval = eval('&statusline')
  300. command('set statusline=foo')
  301. expected_combination({ 'statusline', oldval, oldval, '', 'foo', 'global', 'set' })
  302. -- Note: v:option_old is the old global value for global-local options.
  303. -- but the old local value for all other kinds of options.
  304. command('set statusline&')
  305. expected_combination({ 'statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set' })
  306. command('setglobal statusline=bar')
  307. expected_combination({ 'statusline', oldval, '', oldval, 'bar', 'global', 'setglobal' })
  308. command('noa set statusline&')
  309. command('setlocal statusline=baz')
  310. expected_combination({ 'statusline', oldval, oldval, '', 'baz', 'local', 'setlocal' })
  311. -- Note: v:option_old is the old global value for global-local options.
  312. -- but the old local value for all other kinds of options.
  313. command('noa setglobal statusline=bar')
  314. command('noa setlocal statusline=baz')
  315. command('set statusline=foo')
  316. expected_combination({ 'statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set' })
  317. end)
  318. it('with string local (to window) option', function()
  319. local oldval = eval('&foldignore')
  320. command('set foldignore=fo')
  321. expected_combination({ 'foldignore', oldval, oldval, oldval, 'fo', 'global', 'set' })
  322. command('set foldignore&')
  323. expected_combination({ 'foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set' })
  324. command('setglobal foldignore=bar')
  325. expected_combination({ 'foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal' })
  326. command('noa set foldignore&')
  327. command('setlocal foldignore=baz')
  328. expected_combination({ 'foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal' })
  329. command('noa setglobal foldignore=glob')
  330. command('noa setlocal foldignore=loc')
  331. command('set foldignore=fo')
  332. expected_combination({ 'foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set' })
  333. end)
  334. it('with number global option', function()
  335. command('noa setglobal cmdheight=8')
  336. command('noa setlocal cmdheight=1') -- Sets the global(!) value
  337. command('setglobal cmdheight=2')
  338. expected_combination({ 'cmdheight', 1, '', 1, 2, 'global', 'setglobal' })
  339. command('noa setglobal cmdheight=8')
  340. command('noa setlocal cmdheight=1') -- Sets the global(!) value
  341. command('setlocal cmdheight=2')
  342. expected_combination({ 'cmdheight', 1, 1, '', 2, 'local', 'setlocal' })
  343. -- Note: v:option_old is the old global value for global-local options.
  344. -- but the old local value for all other kinds of options.
  345. command('noa setglobal cmdheight=8')
  346. command('noa setlocal cmdheight=1') -- Sets the global(!) value
  347. command('set cmdheight=2')
  348. expected_combination({ 'cmdheight', 1, 1, 1, 2, 'global', 'set' })
  349. -- Note: v:option_old is the old global value for global-local options.
  350. -- but the old local value for all other kinds of options.
  351. command('noa set cmdheight=8')
  352. command('set cmdheight=2')
  353. expected_combination({ 'cmdheight', 8, 8, 8, 2, 'global', 'set' })
  354. end)
  355. it('with number global-local (to buffer) option', function()
  356. command('noa setglobal undolevels=8')
  357. command('noa setlocal undolevels=1')
  358. command('setglobal undolevels=2')
  359. expected_combination({ 'undolevels', 8, '', 8, 2, 'global', 'setglobal' })
  360. command('noa setglobal undolevels=8')
  361. command('noa setlocal undolevels=1')
  362. command('setlocal undolevels=2')
  363. expected_combination({ 'undolevels', 1, 1, '', 2, 'local', 'setlocal' })
  364. -- Note: v:option_old is the old global value for global-local options.
  365. -- but the old local value for all other kinds of options.
  366. command('noa setglobal undolevels=8')
  367. command('noa setlocal undolevels=1')
  368. command('set undolevels=2')
  369. expected_combination({ 'undolevels', 8, 1, 8, 2, 'global', 'set' })
  370. -- Note: v:option_old is the old global value for global-local options.
  371. -- but the old local value for all other kinds of options.
  372. command('noa set undolevels=8')
  373. command('set undolevels=2')
  374. expected_combination({ 'undolevels', 8, 8, 8, 2, 'global', 'set' })
  375. end)
  376. it('with number local (to buffer) option', function()
  377. command('noa setglobal wrapmargin=8')
  378. command('noa setlocal wrapmargin=1')
  379. command('setglobal wrapmargin=2')
  380. expected_combination({ 'wrapmargin', 8, '', 8, 2, 'global', 'setglobal' })
  381. command('noa setglobal wrapmargin=8')
  382. command('noa setlocal wrapmargin=1')
  383. command('setlocal wrapmargin=2')
  384. expected_combination({ 'wrapmargin', 1, 1, '', 2, 'local', 'setlocal' })
  385. command('noa setglobal wrapmargin=8')
  386. command('noa setlocal wrapmargin=1')
  387. command('set wrapmargin=2')
  388. expected_combination({ 'wrapmargin', 1, 1, 8, 2, 'global', 'set' })
  389. command('noa set wrapmargin=8')
  390. command('set wrapmargin=2')
  391. expected_combination({ 'wrapmargin', 8, 8, 8, 2, 'global', 'set' })
  392. end)
  393. it('with number global-local (to window) option', function()
  394. command('noa setglobal scrolloff=8')
  395. command('noa setlocal scrolloff=1')
  396. command('setglobal scrolloff=2')
  397. expected_combination({ 'scrolloff', 8, '', 8, 2, 'global', 'setglobal' })
  398. command('noa setglobal scrolloff=8')
  399. command('noa setlocal scrolloff=1')
  400. command('setlocal scrolloff=2')
  401. expected_combination({ 'scrolloff', 1, 1, '', 2, 'local', 'setlocal' })
  402. -- Note: v:option_old is the old global value for global-local options.
  403. -- but the old local value for all other kinds of options.
  404. command('noa setglobal scrolloff=8')
  405. command('noa setlocal scrolloff=1')
  406. command('set scrolloff=2')
  407. expected_combination({ 'scrolloff', 8, 1, 8, 2, 'global', 'set' })
  408. -- Note: v:option_old is the old global value for global-local options.
  409. -- but the old local value for all other kinds of options.
  410. command('noa set scrolloff=8')
  411. command('set scrolloff=2')
  412. expected_combination({ 'scrolloff', 8, 8, 8, 2, 'global', 'set' })
  413. end)
  414. it('with number local (to window) option', function()
  415. command('noa setglobal foldcolumn=8')
  416. command('noa setlocal foldcolumn=1')
  417. command('setglobal foldcolumn=2')
  418. expected_combination({ 'foldcolumn', '8', '', '8', '2', 'global', 'setglobal' })
  419. command('noa setglobal foldcolumn=8')
  420. command('noa setlocal foldcolumn=1')
  421. command('setlocal foldcolumn=2')
  422. expected_combination({ 'foldcolumn', '1', '1', '', '2', 'local', 'setlocal' })
  423. command('noa setglobal foldcolumn=8')
  424. command('noa setlocal foldcolumn=1')
  425. command('set foldcolumn=2')
  426. expected_combination({ 'foldcolumn', '1', '1', '8', '2', 'global', 'set' })
  427. command('noa set foldcolumn=8')
  428. command('set foldcolumn=2')
  429. expected_combination({ 'foldcolumn', '8', '8', '8', '2', 'global', 'set' })
  430. end)
  431. it('with boolean global option', function()
  432. command('noa setglobal nowrapscan')
  433. command('noa setlocal wrapscan') -- Sets the global(!) value
  434. command('setglobal nowrapscan')
  435. expected_combination({ 'wrapscan', true, '', true, false, 'global', 'setglobal' })
  436. command('noa setglobal nowrapscan')
  437. command('noa setlocal wrapscan') -- Sets the global(!) value
  438. command('setlocal nowrapscan')
  439. expected_combination({ 'wrapscan', true, true, '', false, 'local', 'setlocal' })
  440. command('noa setglobal nowrapscan')
  441. command('noa setlocal wrapscan') -- Sets the global(!) value
  442. command('set nowrapscan')
  443. expected_combination({ 'wrapscan', true, true, true, false, 'global', 'set' })
  444. command('noa set nowrapscan')
  445. command('set wrapscan')
  446. expected_combination({ 'wrapscan', false, false, false, true, 'global', 'set' })
  447. end)
  448. it('with boolean global-local (to buffer) option', function()
  449. command('noa setglobal noautoread')
  450. command('noa setlocal autoread')
  451. command('setglobal autoread')
  452. expected_combination({ 'autoread', false, '', false, true, 'global', 'setglobal' })
  453. command('noa setglobal noautoread')
  454. command('noa setlocal autoread')
  455. command('setlocal noautoread')
  456. expected_combination({ 'autoread', true, true, '', false, 'local', 'setlocal' })
  457. -- Note: v:option_old is the old global value for global-local options.
  458. -- but the old local value for all other kinds of options.
  459. command('noa setglobal noautoread')
  460. command('noa setlocal autoread')
  461. command('set autoread')
  462. expected_combination({ 'autoread', false, true, false, true, 'global', 'set' })
  463. -- Note: v:option_old is the old global value for global-local options.
  464. -- but the old local value for all other kinds of options.
  465. command('noa set noautoread')
  466. command('set autoread')
  467. expected_combination({ 'autoread', false, false, false, true, 'global', 'set' })
  468. end)
  469. it('with boolean local (to buffer) option', function()
  470. command('noa setglobal nocindent')
  471. command('noa setlocal cindent')
  472. command('setglobal cindent')
  473. expected_combination({ 'cindent', false, '', false, true, 'global', 'setglobal' })
  474. command('noa setglobal nocindent')
  475. command('noa setlocal cindent')
  476. command('setlocal nocindent')
  477. expected_combination({ 'cindent', true, true, '', false, 'local', 'setlocal' })
  478. command('noa setglobal nocindent')
  479. command('noa setlocal cindent')
  480. command('set cindent')
  481. expected_combination({ 'cindent', true, true, false, true, 'global', 'set' })
  482. command('noa set nocindent')
  483. command('set cindent')
  484. expected_combination({ 'cindent', false, false, false, true, 'global', 'set' })
  485. end)
  486. it('with boolean local (to window) option', function()
  487. command('noa setglobal nocursorcolumn')
  488. command('noa setlocal cursorcolumn')
  489. command('setglobal cursorcolumn')
  490. expected_combination({ 'cursorcolumn', false, '', false, true, 'global', 'setglobal' })
  491. command('noa setglobal nocursorcolumn')
  492. command('noa setlocal cursorcolumn')
  493. command('setlocal nocursorcolumn')
  494. expected_combination({ 'cursorcolumn', true, true, '', false, 'local', 'setlocal' })
  495. command('noa setglobal nocursorcolumn')
  496. command('noa setlocal cursorcolumn')
  497. command('set cursorcolumn')
  498. expected_combination({ 'cursorcolumn', true, true, false, true, 'global', 'set' })
  499. command('noa set nocursorcolumn')
  500. command('set cursorcolumn')
  501. expected_combination({ 'cursorcolumn', false, false, false, true, 'global', 'set' })
  502. end)
  503. end)
  504. describe('with specific option', function()
  505. before_each(function()
  506. clear()
  507. declare_hook_function()
  508. init_var()
  509. end)
  510. it('should be called iff setting readonly', function()
  511. set_hook('readonly')
  512. command('set nu')
  513. expected_empty()
  514. command('setlocal ro')
  515. expected_combination({ 'readonly', false, false, '', true, 'local', 'setlocal' })
  516. command('setglobal ro')
  517. expected_combination({ 'readonly', false, '', false, true, 'global', 'setglobal' })
  518. command('set noro')
  519. expected_combination({ 'readonly', true, true, true, false, 'global', 'set' })
  520. end)
  521. describe('being set by setbufvar()', function()
  522. it('should not trigger because option name does not match with backup', function()
  523. set_hook('backup')
  524. command('silent! call setbufvar(1, "&l:bk", 1)')
  525. expected_empty()
  526. end)
  527. it('should trigger, use correct option name backup', function()
  528. set_hook('backup')
  529. command('call setbufvar(1, "&backup", 1)')
  530. expected_combination({ 'backup', false, false, '', true, 'local', 'setlocal' })
  531. end)
  532. it('should trigger if the current buffer is different from the targeted buffer', function()
  533. set_hook('buftype')
  534. local new_buffer = make_buffer()
  535. local new_bufnr = api.nvim_buf_get_number(new_buffer)
  536. command('call setbufvar(' .. new_bufnr .. ', "&buftype", "nofile")')
  537. expected_combination({
  538. 'buftype',
  539. '',
  540. '',
  541. '',
  542. 'nofile',
  543. 'local',
  544. 'setlocal',
  545. { bufnr = new_bufnr },
  546. })
  547. end)
  548. end)
  549. describe('being set by setwinvar()', function()
  550. it('should not trigger because option name does not match with backup', function()
  551. set_hook('backup')
  552. command('silent! call setwinvar(1, "&l:bk", 1)')
  553. expected_empty()
  554. end)
  555. it('should trigger, use correct option name backup', function()
  556. set_hook('backup')
  557. command('call setwinvar(1, "&backup", 1)')
  558. expected_combination({ 'backup', false, false, '', true, 'local', 'setlocal' })
  559. end)
  560. it(
  561. 'should not trigger if the current window is different from the targeted window',
  562. function()
  563. set_hook('cursorcolumn')
  564. local new_winnr = get_new_window_number()
  565. command('call setwinvar(' .. new_winnr .. ', "&cursorcolumn", 1)')
  566. -- expected_combination({'cursorcolumn', false, true, 'local', {winnr = new_winnr}})
  567. expected_empty()
  568. end
  569. )
  570. end)
  571. describe('being set by neovim api', function()
  572. it('should trigger if a boolean option be set globally', function()
  573. set_hook('autochdir')
  574. api.nvim_set_option_value('autochdir', true, { scope = 'global' })
  575. eq(true, api.nvim_get_option_value('autochdir', { scope = 'global' }))
  576. expected_combination({ 'autochdir', false, '', false, true, 'global', 'setglobal' })
  577. end)
  578. it('should trigger if a number option be set globally', function()
  579. set_hook('cmdheight')
  580. api.nvim_set_option_value('cmdheight', 5, { scope = 'global' })
  581. eq(5, api.nvim_get_option_value('cmdheight', { scope = 'global' }))
  582. expected_combination({ 'cmdheight', 1, '', 1, 5, 'global', 'setglobal' })
  583. end)
  584. it('should trigger if a string option be set globally', function()
  585. set_hook('ambiwidth')
  586. api.nvim_set_option_value('ambiwidth', 'double', { scope = 'global' })
  587. eq('double', api.nvim_get_option_value('ambiwidth', { scope = 'global' }))
  588. expected_combination({
  589. 'ambiwidth',
  590. 'single',
  591. '',
  592. 'single',
  593. 'double',
  594. 'global',
  595. 'setglobal',
  596. })
  597. end)
  598. end)
  599. end)
  600. end)