unit_tests.vim 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804
  1. " MRU plugin unit-tests
  2. " MRU plugin settings
  3. let MRU_File='vim_mru_file'
  4. let MRU_Auto_Close=1
  5. let MRU_Max_Entries=10
  6. let MRU_buffer_name = '-RecentFiles-'
  7. " Set the $MRU_PROFILE environment variable to profile the MRU plugin
  8. let s:do_profile = 0
  9. if exists('$MRU_PROFILE')
  10. let s:do_profile = 1
  11. endif
  12. " Profile the MRU plugin
  13. if s:do_profile
  14. profile start mru_profile.txt
  15. profile! file */mru.vim
  16. endif
  17. " Tests assume that 'hidden' option is not set
  18. set nohidden
  19. source ../plugin/mru.vim
  20. " Function to log test results
  21. func! LogResult(test, result)
  22. call add(g:results, a:test . ': ' . a:result)
  23. endfunc
  24. " ==========================================================================
  25. " Test1
  26. " When the MRU list is empty, invoking the MRU command should return an error
  27. " ==========================================================================
  28. func Test_01()
  29. let test_name = 'test1'
  30. redir => msg
  31. MRU
  32. redir END
  33. if msg =~# "MRU file list is empty"
  34. call LogResult(test_name, 'pass')
  35. else
  36. call LogResult(test_name, 'FAIL')
  37. endif
  38. endfunc
  39. " ==========================================================================
  40. " Test2
  41. " Open the MRU window and check the order of files listed in the window
  42. " Open the MRU window when the window is already opened.
  43. " ==========================================================================
  44. func Test_02()
  45. let test_name = 'test2'
  46. edit file1.txt
  47. edit file2.txt
  48. edit file3.txt
  49. edit file2.txt
  50. edit file1.txt
  51. MRU
  52. MRU
  53. let l = getline(1, "$")
  54. if l[0] =~# "file1.txt" && l[1] =~# "file2.txt" && l[2] =~# "file3.txt"
  55. call LogResult(test_name, 'pass')
  56. else
  57. call LogResult(test_name, 'FAIL')
  58. endif
  59. endfunc
  60. " ==========================================================================
  61. " Test3
  62. " Select a file from the MRU window and check whether it is opened
  63. " ==========================================================================
  64. func Test_03()
  65. let test_name = 'test3'
  66. " Go to the last but one line
  67. $
  68. " Select the last file in the MRU window
  69. exe "normal \<Enter>"
  70. if fnamemodify(@%, ':p:t') !=# 'file3.txt'
  71. call LogResult(test_name, "FAIL (1)")
  72. else
  73. " Make sure the MRU window is closed
  74. if bufwinnr(g:MRU_buffer_name) == -1
  75. call LogResult(test_name, 'pass')
  76. else
  77. call LogResult(test_name, "FAIL (2)")
  78. endif
  79. endif
  80. endfunc
  81. " ==========================================================================
  82. " Test4
  83. " MRU opens a selected file in the previous/last window
  84. " ==========================================================================
  85. func Test_04()
  86. let test_name = 'test4'
  87. " Edit a file and then open a new window, open the MRU window and select the
  88. " file
  89. split file1.txt
  90. only
  91. below new
  92. MRU
  93. call search('file2.txt')
  94. exe "normal \<Enter>"
  95. if winnr() == 2
  96. call LogResult(test_name, 'pass')
  97. else
  98. call LogResult(test_name, 'FAIL')
  99. endif
  100. endfunc
  101. " ==========================================================================
  102. " Test5
  103. " MRU opens a selected file in the same window if the file is already opened
  104. " ==========================================================================
  105. func Test_05()
  106. let test_name = 'test5'
  107. edit file1.txt
  108. only
  109. below split file2.txt
  110. below split file3.txt
  111. MRU
  112. call search('file1.txt')
  113. exe "normal \<Enter>"
  114. if winnr() != 1 || fnamemodify(@%, ':p:t') !=# 'file1.txt'
  115. call LogResult(test_name, "FAIL (1)")
  116. else
  117. MRU
  118. call search('file2.txt')
  119. exe "normal \<Enter>"
  120. if winnr() != 2 || fnamemodify(@%, ':p:t') !=# 'file2.txt'
  121. call LogResult(test_name, "FAIL (2)")
  122. else
  123. MRU
  124. call search('file3.txt')
  125. exe "normal \<Enter>"
  126. if winnr() != 3 || fnamemodify(@%, ':p:t') !=# 'file3.txt'
  127. call LogResult(test_name, "FAIL (3)")
  128. else
  129. call LogResult(test_name, 'pass')
  130. endif
  131. endif
  132. endif
  133. endfunc
  134. " ==========================================================================
  135. " Test6
  136. " MRU opens a file selected with 'o' command in a new window
  137. " ==========================================================================
  138. func Test_06()
  139. let test_name = 'test6'
  140. enew | only
  141. edit file1.txt
  142. below new
  143. MRU
  144. normal o
  145. if winnr() == 3 && fnamemodify(@%, ':p:t') ==# 'file1.txt'
  146. call LogResult(test_name, 'pass')
  147. else
  148. call LogResult(test_name, 'FAIL')
  149. endif
  150. endfunc
  151. " ==========================================================================
  152. " Test7
  153. " MRU opens the selected file in a new window if the previous buffer is
  154. " modified.
  155. " ==========================================================================
  156. func Test_07()
  157. let test_name = 'test7'
  158. enew | only
  159. insert
  160. MRU plugin test
  161. .
  162. MRU
  163. call search('file3.txt')
  164. exe "normal \<Enter>"
  165. if winnr() == 1 && winnr('$') == 2 &&
  166. \ fnamemodify(@%, ':p:t') ==# 'file3.txt'
  167. call LogResult(test_name, 'pass')
  168. else
  169. call LogResult(test_name, 'FAIL')
  170. endif
  171. " Discard changes in the new buffer
  172. wincmd b
  173. enew!
  174. only
  175. endfunc
  176. " ==========================================================================
  177. " Test8
  178. " MRU opens a file selected with 'v' command in read-only mode in the current
  179. " window.
  180. " ==========================================================================
  181. func Test_08()
  182. let test_name = 'test8'
  183. enew | only
  184. MRU
  185. call search('file1.txt')
  186. normal v
  187. let r1 = &readonly
  188. MRU
  189. call search('file2.txt')
  190. exe "normal \<Enter>"
  191. let r2 = &readonly
  192. MRU
  193. call search('file1.txt')
  194. exe "normal \<Enter>"
  195. let r3 = &readonly
  196. if r1 == 1 && r2 == 0 && r3 == 1
  197. call LogResult(test_name, 'pass')
  198. else
  199. call LogResult(test_name, 'FAIL')
  200. endif
  201. endfunc
  202. " ==========================================================================
  203. " Test9
  204. " Use 'O' in the MRU window to open a file in a vertically split window
  205. " ==========================================================================
  206. func Test_09()
  207. let test_name = 'test9'
  208. enew | only
  209. edit file1.txt
  210. MRU
  211. call search('file2.txt')
  212. normal O
  213. let b1 = @%
  214. wincmd h
  215. let b2 = @%
  216. wincmd l
  217. let b3 = @%
  218. if winnr('$') == 2 && b1 ==# 'file2.txt' &&
  219. \ b2 ==# 'file1.txt' && b3 ==# 'file2.txt'
  220. call LogResult(test_name, 'pass')
  221. else
  222. call LogResult(test_name, 'FAIL')
  223. endif
  224. endfunc
  225. " ==========================================================================
  226. " Test10
  227. " Use 'p' in the MRU window to open a file in the preview window
  228. " ==========================================================================
  229. func Test_10()
  230. let test_name = 'test10'
  231. enew | only
  232. MRU
  233. call search('file3.txt')
  234. normal p
  235. wincmd P
  236. let p1 = &previewwindow
  237. let b1 = @%
  238. if winnr('$') == 2 && &previewwindow && @% =~# 'file3.txt'
  239. call LogResult(test_name, 'pass')
  240. else
  241. call LogResult(test_name, 'FAIL')
  242. endif
  243. pclose
  244. endfunc
  245. " ==========================================================================
  246. " Test11
  247. " MRU opens a file selected with 't' command in a new tab and the tab
  248. " is opened at the end
  249. " ==========================================================================
  250. func Test_11()
  251. let test_name = 'test11'
  252. enew | only
  253. edit a1.txt
  254. tabnew a2.txt
  255. tabnew a3.txt
  256. tabnew a4.txt
  257. tabfirst
  258. MRU
  259. call search('file3.txt')
  260. normal t
  261. if fnamemodify(@%, ':p:t') ==# 'file3.txt' && tabpagenr() == 5
  262. call LogResult(test_name, 'pass')
  263. else
  264. call LogResult(test_name, 'FAIL')
  265. call LogResult(test_name, "file = " . fnamemodify(@%, ':p:t'))
  266. call LogResult(test_name, "tab page = " . tabpagenr())
  267. endif
  268. tabonly
  269. endfunc
  270. " ==========================================================================
  271. " Test12
  272. " The 'q' command closes the MRU window
  273. " ==========================================================================
  274. func Test_12()
  275. let test_name = 'test12'
  276. enew | only
  277. MRU
  278. normal q
  279. if bufwinnr(g:MRU_buffer_name) == -1
  280. call LogResult(test_name, 'pass')
  281. else
  282. call LogResult(test_name, 'FAIL')
  283. endif
  284. endfunc
  285. " ==========================================================================
  286. " Test13
  287. " A selected file is opened in a new window if the previous window is a
  288. " preview window
  289. " ==========================================================================
  290. func Test_13()
  291. let test_name = 'test13'
  292. enew | only
  293. setlocal previewwindow
  294. MRU
  295. call search('file2.txt')
  296. exe "normal \<Enter>"
  297. if winnr() == 1 && winnr('$') == 2 &&
  298. \ &previewwindow == 0 &&
  299. \ fnamemodify(@%, ':p:t') ==# 'file2.txt'
  300. call LogResult(test_name, 'pass')
  301. else
  302. call LogResult(test_name, 'FAIL')
  303. endif
  304. " Close the preview window created by this test
  305. new
  306. only
  307. endfunc
  308. " ==========================================================================
  309. " Test14
  310. " A selected file is opened in a new window if the previous window contains
  311. " a special buffer (used by some other plugin)
  312. " ==========================================================================
  313. func Test_14()
  314. let test_name = 'test14'
  315. enew | only
  316. setlocal buftype=nofile
  317. MRU
  318. call search('file3.txt')
  319. exe "normal \<Enter>"
  320. if winnr() == 1 && winnr('$') == 2 &&
  321. \ &buftype == '' &&
  322. \ fnamemodify(@%, ':p:t') ==# 'file3.txt'
  323. call LogResult(test_name, 'pass')
  324. else
  325. call LogResult(test_name, 'FAIL')
  326. endif
  327. " Discard the special buffer
  328. enew
  329. endfunc
  330. " ==========================================================================
  331. " Test15
  332. " If a file selected using the 't' command is already opened in a tab,
  333. " then jump to that tab (instead of opening a new tab)
  334. " ==========================================================================
  335. func Test_15()
  336. let test_name = 'test15'
  337. enew | only
  338. " Open the test files in the middle window with empty windows at the top and
  339. " bottom
  340. edit file1.txt
  341. above new
  342. botright new
  343. tabedit file2.txt
  344. above new
  345. botright new
  346. tabedit file3.txt
  347. above new
  348. botright new
  349. tabfirst
  350. MRU
  351. call search('file3.txt')
  352. exe "normal t"
  353. if tabpagenr() != 3
  354. \ || fnamemodify(@%, ':p:t') !=# 'file3.txt'
  355. \ || winnr() != 2
  356. call LogResult(test_name, "FAIL (1)")
  357. else
  358. MRU
  359. call search('file1.txt')
  360. exe "normal t"
  361. if tabpagenr() != 1
  362. \ || fnamemodify(@%, ':p:t') !=# 'file1.txt'
  363. \ || winnr() != 2
  364. call LogResult(test_name, "FAIL (2)")
  365. else
  366. MRU
  367. call search('file2.txt')
  368. exe "normal t"
  369. if tabpagenr() != 2
  370. \ || fnamemodify(@%, ':p:t') !=# 'file2.txt'
  371. \ || winnr() != 2
  372. call LogResult(test_name, "FAIL (3)")
  373. else
  374. call LogResult(test_name, 'pass')
  375. endif
  376. endif
  377. endif
  378. " Close all the other tabs
  379. tabonly
  380. enew
  381. only
  382. endfunc
  383. " ==========================================================================
  384. " Test16
  385. " Open multiple files from the MRU window using the visual mode and by using a
  386. " count. Each file should be opened in a separate window.
  387. " ==========================================================================
  388. func Test_16()
  389. let test_name = 'test16'
  390. enew | only
  391. edit file3.txt
  392. edit file2.txt
  393. edit file1.txt
  394. enew
  395. MRU
  396. exe "normal 3\<Enter>"
  397. if winnr('$') == 3 &&
  398. \ bufwinnr('file3.txt') == 1 &&
  399. \ bufwinnr('file2.txt') == 2 &&
  400. \ bufwinnr('file1.txt') == 3
  401. let test_result = 'pass'
  402. else
  403. let test_result = 'FAIL'
  404. endif
  405. only | enew
  406. if test_result == 'pass'
  407. MRU
  408. exe "normal V2j\<Enter>"
  409. if winnr('$') == 3 &&
  410. \ bufwinnr('file1.txt') == 1 &&
  411. \ bufwinnr('file2.txt') == 2 &&
  412. \ bufwinnr('file3.txt') == 3
  413. let test_result = 'pass'
  414. else
  415. let test_result = 'FAIL'
  416. endif
  417. endif
  418. if test_result == 'pass'
  419. call LogResult(test_name, 'pass')
  420. else
  421. call LogResult(test_name, 'FAIL')
  422. endif
  423. endfunc
  424. " ==========================================================================
  425. " Test17
  426. " When the MRU list is updated, the MRU file also should updated.
  427. " ==========================================================================
  428. func Test_17()
  429. let test_name = 'test17'
  430. enew | only
  431. edit file1.txt
  432. let l = readfile(g:MRU_File)
  433. if l[1] =~# 'file1.txt'
  434. edit file2.txt
  435. let l = readfile(g:MRU_File)
  436. if l[1] =~# 'file2.txt'
  437. edit file3.txt
  438. let l = readfile(g:MRU_File)
  439. if l[1] =~# 'file3.txt'
  440. call LogResult(test_name, 'pass')
  441. else
  442. call LogResult(test_name, "FAIL (3)")
  443. endif
  444. else
  445. call LogResult(test_name, "FAIL (2)")
  446. endif
  447. else
  448. call LogResult(test_name, "FAIL (1)")
  449. endif
  450. endfunc
  451. " MRU_Test_Add_Files
  452. " Add the supplied List of files to the beginning of the MRU file
  453. func! s:MRU_Test_Add_Files(fnames)
  454. let l = readfile(g:MRU_File)
  455. call extend(l, a:fnames, 1)
  456. call writefile(l, g:MRU_File)
  457. endfunc
  458. " ==========================================================================
  459. " Test18
  460. " When the MRU file is updated by another Vim instance, the MRU plugin
  461. " should update the MRU list
  462. " ==========================================================================
  463. func Test_18()
  464. let test_name = 'test18'
  465. enew | only
  466. call s:MRU_Test_Add_Files(['/software/editors/vim',
  467. \ '/software/editors/emacs',
  468. \ '/software/editors/nano'])
  469. MRU
  470. if getline(1) ==# 'vim (/software/editors/vim)'
  471. \ && getline(2) ==# 'emacs (/software/editors/emacs)'
  472. \ && getline(3) ==# 'nano (/software/editors/nano)'
  473. call LogResult(test_name, 'pass')
  474. else
  475. call LogResult(test_name, 'FAIL')
  476. endif
  477. " Close the MRU window
  478. close
  479. endfunc
  480. " ==========================================================================
  481. " Test19
  482. " When the MRU file is updated by another Vim instance, the MRU file names
  483. " from the current instance should be merged with that list
  484. " ==========================================================================
  485. func Test_19()
  486. let test_name = 'test19'
  487. enew | only
  488. " Remove all the files from the MRU file
  489. let l = readfile(g:MRU_File)
  490. call remove(l, 1, -1)
  491. call writefile(l, g:MRU_File)
  492. edit file1.txt
  493. call s:MRU_Test_Add_Files(['/software/os/unix'])
  494. edit file2.txt
  495. call s:MRU_Test_Add_Files(['/software/os/windows'])
  496. edit file3.txt
  497. call s:MRU_Test_Add_Files(['/software/os/osx'])
  498. MRU
  499. if getline(1) ==# 'osx (/software/os/osx)'
  500. \ && getline(2) =~# 'file3.txt'
  501. \ && getline(3) ==# 'windows (/software/os/windows)'
  502. \ && getline(4) =~# 'file2.txt'
  503. \ && getline(5) ==# 'unix (/software/os/unix)'
  504. \ && getline(6) =~# 'file1.txt'
  505. call LogResult(test_name, 'pass')
  506. else
  507. call LogResult(test_name, 'FAIL')
  508. endif
  509. close
  510. endfunc
  511. " ==========================================================================
  512. " Test20
  513. " When the MRU list has more than g:MRU_Max_Entries, the list should be
  514. " trimmed. The last entries should be removed.
  515. " ==========================================================================
  516. func Test_20()
  517. let test_name = 'test20'
  518. enew | only
  519. " Create a MRU list with MRU_Max_Entries
  520. let flist = []
  521. for i in range(1, g:MRU_Max_Entries)
  522. let flist += ['/usr/share/mru_test/mru_file' . i . '.abc']
  523. endfor
  524. " Modify the MRU file to contain max entries
  525. let l = readfile(g:MRU_File)
  526. call remove(l, 1, -1)
  527. call extend(l, flist)
  528. call writefile(l, g:MRU_File)
  529. enew
  530. edit file1.txt
  531. let l = readfile(g:MRU_File)
  532. if len(l) == (g:MRU_Max_Entries + 1) &&
  533. \ l[g:MRU_Max_Entries] != '/usr/share/mru_test/mru_file9.abc'
  534. call LogResult(test_name, "FAIL (1)")
  535. else
  536. edit file2.txt
  537. let l = readfile(g:MRU_File)
  538. if len(l) == (g:MRU_Max_Entries + 1) &&
  539. \ l[g:MRU_Max_Entries] != '/usr/share/mru_test/mru_file8.abc'
  540. call LogResult(test_name, "FAIL (2)")
  541. else
  542. edit file3.txt
  543. let l = readfile(g:MRU_File)
  544. if len(l) == (g:MRU_Max_Entries + 1) &&
  545. \ l[g:MRU_Max_Entries] != '/usr/share/mru_test/mru_file7.abc'
  546. call LogResult(test_name, "FAIL (3)")
  547. else
  548. call LogResult(test_name, 'pass')
  549. endif
  550. endif
  551. endif
  552. endfunc
  553. " ==========================================================================
  554. " Test21
  555. " When an filename (already present in the MRU list) is specified to the MRU
  556. " command, it should edit the file.
  557. " ==========================================================================
  558. func Test_21()
  559. let test_name = 'test21'
  560. enew | only
  561. edit file1.txt
  562. edit file2.txt
  563. edit file3.txt
  564. enew
  565. MRU file2.txt
  566. if fnamemodify(@%, ':p:t') ==# 'file2.txt' && winnr('$') == 1
  567. call LogResult(test_name, 'pass')
  568. else
  569. call LogResult(test_name, 'FAIL')
  570. endif
  571. endfunc
  572. " ==========================================================================
  573. " Test22
  574. " When a pattern (matching multiple filenames) is specified to the MRU
  575. " command, then the MRU window should be opened with all the matching
  576. " filenames
  577. " ==========================================================================
  578. func Test_22()
  579. let test_name = 'test22'
  580. enew | only
  581. edit file1.txt
  582. edit file2.txt
  583. edit file3.txt
  584. only
  585. MRU file.*
  586. if @% != g:MRU_buffer_name
  587. call LogResult(test_name, 'FAIL')
  588. else
  589. let l = getline(1, "$")
  590. if l[0] =~# "file3.txt" && l[1] =~# "file2.txt" && l[2] =~# "file1.txt"
  591. call LogResult(test_name, 'pass')
  592. else
  593. call LogResult(test_name, 'FAIL')
  594. endif
  595. endif
  596. close
  597. endfunc
  598. " ==========================================================================
  599. " Test23
  600. " When a partial filename (matching multiple filenames) is specified to the
  601. " MRU command, then the MRU window should be opened with all the matching
  602. " filenames
  603. " ==========================================================================
  604. func Test_23()
  605. let test_name = 'test23'
  606. enew | only
  607. let g:MRU_FuzzyMatch = 0
  608. edit file1.txt
  609. edit file2.txt
  610. edit file3.txt
  611. only
  612. MRU file
  613. if @% != g:MRU_buffer_name
  614. call LogResult(test_name, 'FAIL')
  615. else
  616. let l = getline(1, "$")
  617. if l[0] =~# "file3.txt" && l[1] =~# "file2.txt" && l[2] =~# "file1.txt"
  618. call LogResult(test_name, 'pass')
  619. else
  620. call LogResult(test_name, 'FAIL')
  621. endif
  622. endif
  623. close
  624. endfunc
  625. " ==========================================================================
  626. " Test24
  627. " When a non-existing filename is specified to the MRU command, an error
  628. " message should be displayed.
  629. " ==========================================================================
  630. func Test_24()
  631. let test_name = 'test24'
  632. let g:MRU_FuzzyMatch = 0
  633. redir => msg
  634. MRU nonexistingfile.txt
  635. redir END
  636. if @% == g:MRU_buffer_name ||
  637. \ msg !~# "MRU file list doesn't contain files " .
  638. \ "matching nonexistingfile.txt"
  639. call LogResult(test_name, 'FAIL')
  640. else
  641. call LogResult(test_name, 'pass')
  642. endif
  643. endfunc
  644. " ==========================================================================
  645. " Test25
  646. " The MRU command should support filename completion. Supply a partial file
  647. " name to the MRU command and complete the filenames.
  648. " ==========================================================================
  649. func Test_25()
  650. let test_name = 'test25'
  651. enew | only
  652. edit file1.txt
  653. edit file2.txt
  654. edit file3.txt
  655. exe 'normal! :MRU file' . "\<C-A>" . "\<Home>let m='\<End>'\<CR>"
  656. let fnames = split(m)
  657. if fnames[1] =~# 'file3.txt' && fnames[2] =~# 'file2.txt' &&
  658. \ fnames[3] =~# 'file1.txt'
  659. call LogResult(test_name, 'pass')
  660. else
  661. call LogResult(test_name, 'FAIL')
  662. endif
  663. endfunc
  664. " ==========================================================================
  665. " Test26
  666. " When trying to complete filenames for the MRU command without specifying
  667. " any text should return the entire MRU list.
  668. " ==========================================================================
  669. func Test_26()
  670. let test_name = 'test26'
  671. enew | only
  672. call delete(g:MRU_File)
  673. edit file1.txt
  674. edit file2.txt
  675. edit file3.txt
  676. exe 'normal! :MRU ' . "\<C-A>" . "\<Home>let m='\<End>'\<CR>"
  677. let fnames = split(m)
  678. if fnames[1] =~# 'file3.txt' && fnames[2] =~# 'file2.txt' &&
  679. \ fnames[3] =~# 'file1.txt'
  680. call LogResult(test_name, 'pass')
  681. else
  682. call LogResult(test_name, 'FAIL')
  683. endif
  684. endfunc
  685. " ==========================================================================
  686. " Test27
  687. " When the current file/buffer has unsaved changes, MRU should open a selected
  688. " file in a new window (if the 'hidden' option is not set)
  689. " ==========================================================================
  690. func Test_27()
  691. let test_name = 'test27'
  692. enew | only
  693. edit file1.txt
  694. edit file2.txt
  695. call append(line('$'), 'Temporary changes to buffer')
  696. MRU
  697. call search('file1.txt')
  698. exe "normal \<Enter>"
  699. if winnr() == 1 && winnr('$') == 2 &&
  700. \ fnamemodify(@%, ':p:t') ==# 'file1.txt'
  701. call LogResult(test_name, 'pass')
  702. else
  703. call LogResult(test_name, 'FAIL')
  704. endif
  705. close
  706. edit!
  707. endfunc
  708. " ==========================================================================
  709. " Test28
  710. " When the current file/buffer has unsaved changes and the 'hidden' option is
  711. " set, then MRU should open a selected file in the current window
  712. " ==========================================================================
  713. func Test_28()
  714. let test_name = 'test28'
  715. enew | only
  716. edit file2.txt
  717. edit file1.txt
  718. call append(line('$'), 'Temporary changes to buffer')
  719. set hidden
  720. MRU
  721. call search('file2.txt')
  722. exe "normal \<Enter>"
  723. if winnr('$') == 1 &&
  724. \ fnamemodify(@%, ':p:t') ==# 'file2.txt'
  725. call LogResult(test_name, 'pass')
  726. else
  727. call LogResult(test_name, 'FAIL')
  728. endif
  729. edit file1.txt
  730. edit!
  731. set nohidden
  732. %bw!
  733. endfunc
  734. " ==========================================================================
  735. " Test29
  736. " Every edited file is added to the top of the MRU list. If a file is already
  737. " present in the MRU list, then it is moved to the top of the list.
  738. " ==========================================================================
  739. func Test_29()
  740. let test_name = 'test29'
  741. enew | only
  742. edit file1.txt
  743. let f1 = readfile(g:MRU_File, '', 2)
  744. edit file2.txt
  745. let f2 = readfile(g:MRU_File, '', 2)
  746. edit file3.txt
  747. let f3 = readfile(g:MRU_File, '', 2)
  748. edit file1.txt
  749. let f4 = readfile(g:MRU_File, '', 2)
  750. if f1[1] =~# 'file1.txt' && f2[1] =~# 'file2.txt' && f3[1] =~# 'file3.txt' &&
  751. \ f4[1] =~# 'file1.txt'
  752. call LogResult(test_name, 'pass')
  753. else
  754. call LogResult(test_name, 'FAIL')
  755. endif
  756. endfunc
  757. " ==========================================================================
  758. " Test30
  759. " Only file names matching the regular expression in the MRU_Include_Files
  760. " variable should be added to the MRU list.
  761. " ==========================================================================
  762. func Test_30()
  763. let test_name = 'test30'
  764. enew | only
  765. edit file1.txt
  766. let g:MRU_Include_Files='\.c'
  767. edit abc.c
  768. let f1 = readfile(g:MRU_File, '', 2)
  769. edit file1.txt
  770. let f2 = readfile(g:MRU_File, '', 2)
  771. edit def.c
  772. let f3 = readfile(g:MRU_File, '', 2)
  773. if f1[1] =~# 'abc.c' && f2[1] =~# 'abc.c' && f3[1] =~# 'def.c'
  774. call LogResult(test_name, 'pass')
  775. else
  776. call LogResult(test_name, 'FAIL')
  777. endif
  778. let g:MRU_Include_Files=''
  779. endfunc
  780. " ==========================================================================
  781. " Test31
  782. " File names matching the regular expression in the MRU_Exclude_Files
  783. " variable should not be added to the MRU list.
  784. " ==========================================================================
  785. func Test_31()
  786. let test_name = 'test31'
  787. enew | only
  788. let g:MRU_Exclude_Files='\.txt'
  789. edit abc.c
  790. let f1 = readfile(g:MRU_File, '', 2)
  791. edit file1.txt
  792. edit file2.txt
  793. edit file3.txt
  794. let f2 = readfile(g:MRU_File, '', 2)
  795. edit def.c
  796. let f3 = readfile(g:MRU_File, '', 2)
  797. let g:MRU_Exclude_Files=''
  798. edit file1.txt
  799. let f4 = readfile(g:MRU_File, '', 2)
  800. if f1[1] =~# 'abc.c' && f2[1] =~# 'abc.c' && f3[1] =~# 'def.c' &&
  801. \ f4[1] =~# 'file1.txt'
  802. call LogResult(test_name, 'pass')
  803. else
  804. call LogResult(test_name, 'FAIL')
  805. endif
  806. endfunc
  807. " ==========================================================================
  808. " Test32
  809. " If the MRU window is open, when adding a file name to the list, the MRU
  810. " window should be refreshed.
  811. " ==========================================================================
  812. func Test_32()
  813. let test_name = 'test32'
  814. enew | only
  815. MRU
  816. wincmd p
  817. edit abc.c
  818. wincmd p
  819. let s1 = getline(1)
  820. wincmd p
  821. edit file1.txt
  822. wincmd p
  823. let s2 = getline(1)
  824. close
  825. if s1 =~# 'abc.c' && s2 =~# 'file1.txt'
  826. call LogResult(test_name, 'pass')
  827. else
  828. call LogResult(test_name, 'FAIL')
  829. endif
  830. endfunc
  831. " ==========================================================================
  832. " Test33
  833. " When MRU_Use_Current_Window is set, the MRU list should be displayed in
  834. " the current window.
  835. " Selecting a file from the MRU window should replace
  836. " the MRU buffer with the selected file.
  837. " ==========================================================================
  838. func Test_33()
  839. let test_name = 'test33'
  840. enew | only
  841. edit file1.txt
  842. let g:MRU_Use_Current_Window=1
  843. MRU
  844. if winnr('$') == 1 && @% == g:MRU_buffer_name
  845. call LogResult(test_name, 'pass')
  846. else
  847. call LogResult(test_name, 'FAIL')
  848. endif
  849. let g:MRU_Use_Current_Window=0
  850. endfunc
  851. " ==========================================================================
  852. " Test34
  853. " When MRU_Use_Current_Window is set, selecting a file from the MRU window
  854. " should replace the MRU buffer with the selected file.
  855. " ==========================================================================
  856. func Test_34()
  857. let test_name = 'test34'
  858. enew | only
  859. let g:MRU_Use_Current_Window=1
  860. let w:marker=1
  861. MRU
  862. if winnr('$') == 1 && w:marker && @% == g:MRU_buffer_name
  863. call search('file2.txt')
  864. exe "normal \<Enter>"
  865. if winnr('$') == 1 && w:marker && @% == 'file2.txt'
  866. call LogResult(test_name, 'pass')
  867. else
  868. call LogResult(test_name, 'FAIL')
  869. endif
  870. else
  871. call LogResult(test_name, 'FAIL')
  872. endif
  873. unlet w:marker
  874. let g:MRU_Use_Current_Window=0
  875. endfunc
  876. " ==========================================================================
  877. " Test35
  878. " When MRU_Use_Current_Window is set, if the current buffer has unsaved
  879. " changes, then the MRU window should be opened in a split window
  880. " ==========================================================================
  881. func Test_35()
  882. let test_name = 'test35'
  883. enew | only
  884. let g:MRU_Use_Current_Window=1
  885. set modified
  886. MRU
  887. if winnr('$') == 2 && winnr() == 2 && @% == g:MRU_buffer_name
  888. call LogResult(test_name, 'pass')
  889. else
  890. call LogResult(test_name, 'FAIL')
  891. endif
  892. close
  893. set nomodified
  894. let g:MRU_Use_Current_Window=0
  895. enew | only
  896. endfunc
  897. " ==========================================================================
  898. " Test36
  899. " When MRU_Auto_Close is not set, the MRU window should not automatically
  900. " close when a file is selected. The MRU window should be kept open.
  901. " ==========================================================================
  902. func Test_36()
  903. let test_name = 'test36'
  904. enew | only
  905. let g:MRU_Auto_Close=0
  906. new
  907. MRU
  908. call search('file1.txt')
  909. exe "normal \<Enter>"
  910. 2wincmd w
  911. MRU
  912. call search('file2.txt')
  913. exe "normal \<Enter>"
  914. if winnr('$') == 3 &&
  915. \ bufwinnr('file1.txt') == 1 &&
  916. \ bufwinnr('file2.txt') == 2 &&
  917. \ bufwinnr(g:MRU_buffer_name) == 3
  918. call LogResult(test_name, 'pass')
  919. else
  920. call LogResult(test_name, 'FAIL')
  921. endif
  922. wincmd b
  923. close
  924. let g:MRU_Auto_Close=1
  925. only
  926. endfunc
  927. " ==========================================================================
  928. " Test37
  929. " When MRU_Open_File_Use_Tabs is set, a selected file should be opened in a
  930. " tab. If the file is already opened in a tab, then the focus should be moved
  931. " to that tab.
  932. " ==========================================================================
  933. func Test_37()
  934. let test_name = 'test37'
  935. enew | only
  936. let g:MRU_Open_File_Use_Tabs=1
  937. edit file1.txt
  938. MRU
  939. call search('file2.txt')
  940. exe "normal \<Enter>"
  941. MRU
  942. call search('file3.txt')
  943. exe "normal \<Enter>"
  944. MRU file1.txt
  945. let t1 = tabpagenr()
  946. MRU
  947. call search('file2.txt')
  948. exe "normal \<Enter>"
  949. let t2 = tabpagenr()
  950. MRU
  951. call search('file3.txt')
  952. exe "normal \<Enter>"
  953. let t3 = tabpagenr()
  954. tabonly | enew
  955. if t1 == 1 && t2 == 2 && t3 == 3
  956. call LogResult(test_name, 'pass')
  957. else
  958. call LogResult(test_name, 'FAIL')
  959. endif
  960. let g:MRU_Open_File_Use_Tabs=0
  961. endfunc
  962. " ==========================================================================
  963. " Test38
  964. " If the MRU_Window_Open_Always is set to 0, when the MRU command finds a
  965. " single matching file name, then it should open the MRU window. If this
  966. " variable is set to 1, then the file should be opened without opening the MRU
  967. " window.
  968. " ==========================================================================
  969. func Test_38()
  970. let test_name = 'test38'
  971. enew | only
  972. edit file3.txt
  973. enew
  974. let g:MRU_Window_Open_Always=1
  975. MRU file3.txt
  976. if winnr('$') == 2 &&
  977. \ bufwinnr(g:MRU_buffer_name) == 2
  978. let test_result = 'pass'
  979. else
  980. let test_result = 'FAIL'
  981. endif
  982. close
  983. enew | only
  984. if test_result == 'pass'
  985. let g:MRU_Window_Open_Always=0
  986. MRU file3.txt
  987. if winnr('$') == 1 &&
  988. \ bufwinnr('file3.txt') == 1
  989. let test_result = 'pass'
  990. else
  991. let test_result = 'FAIL'
  992. endif
  993. endif
  994. let g:MRU_Window_Open_Always=0
  995. if test_result == 'pass'
  996. call LogResult(test_name, 'pass')
  997. else
  998. call LogResult(test_name, 'FAIL')
  999. endif
  1000. endfunc
  1001. " ==========================================================================
  1002. " Test39
  1003. " If the current tabpage is empty, then pressing 't' in the MRU window
  1004. " should open the file in the current tabpage.
  1005. " ==========================================================================
  1006. func Test_39()
  1007. let test_name = 'test39'
  1008. enew | only | tabonly
  1009. tabnew
  1010. tabnew
  1011. tabnext 2
  1012. MRU
  1013. call search('file2.txt')
  1014. normal t
  1015. if fnamemodify(@%, ':p:t') ==# 'file2.txt' && tabpagenr() == 2
  1016. call LogResult(test_name, 'pass')
  1017. else
  1018. call LogResult(test_name, 'FAIL')
  1019. call LogResult(test_name, "file = " . fnamemodify(@%, ':p:t'))
  1020. call LogResult(test_name, "tab page = " . tabpagenr())
  1021. endif
  1022. tabonly
  1023. endfunc
  1024. " ==========================================================================
  1025. " Test40
  1026. " Pressing 'd' in the MRU window should delete the file under the cursor
  1027. " from the MRU list
  1028. " ==========================================================================
  1029. func Test_40()
  1030. let test_name = 'test40'
  1031. edit file2.txt
  1032. enew
  1033. MRU
  1034. call search('file2.txt')
  1035. normal d
  1036. close
  1037. let l = readfile(g:MRU_File)
  1038. if match(l, 'file2.txt') == -1
  1039. call LogResult(test_name, 'pass')
  1040. else
  1041. call LogResult(test_name, 'FAIL')
  1042. endif
  1043. endfunc
  1044. " ==========================================================================
  1045. " Test41
  1046. " Running the :vimgrep command should not add the files to the MRU list
  1047. " ==========================================================================
  1048. func Test_41()
  1049. let test_name = 'test41'
  1050. call writefile(['bright'], 'dummy1.txt')
  1051. call writefile(['bright'], 'dummy2.txt')
  1052. vimgrep /bright/j dummy*
  1053. let l = readfile(g:MRU_File)
  1054. if match(l, 'dummy') == -1
  1055. call LogResult(test_name, 'pass')
  1056. else
  1057. call LogResult(test_name, 'FAIL')
  1058. endif
  1059. call delete('dummy1.txt')
  1060. call delete('dummy2.txt')
  1061. endfunc
  1062. " ==========================================================================
  1063. " Test42
  1064. " Using a command modifier with the MRU command to open the MRU window
  1065. " ==========================================================================
  1066. func Test_42()
  1067. if v:version < 800
  1068. " The <mods> command modifier is supported only by Vim 8.0 and above
  1069. return
  1070. endif
  1071. let test_name = 'test42'
  1072. enew | only
  1073. topleft MRU
  1074. if winnr() == 1 && winnr('$') == 2
  1075. call LogResult(test_name, 'pass')
  1076. else
  1077. call LogResult(test_name, 'FAIL')
  1078. endif
  1079. enew | only
  1080. botright MRU
  1081. if winnr() == 2 && winnr('$') == 2
  1082. call LogResult(test_name, 'pass')
  1083. else
  1084. call LogResult(test_name, 'FAIL')
  1085. endif
  1086. enew | only
  1087. botright MRU
  1088. if winnr() == 2 && winnr('$') == 2
  1089. call LogResult(test_name, 'pass')
  1090. else
  1091. call LogResult(test_name, 'FAIL')
  1092. endif
  1093. enew | only
  1094. endfunc
  1095. " ==========================================================================
  1096. " Test43
  1097. " Opening a file using the MRU command should jump to the window containing
  1098. " the file (if it is already opened).
  1099. " ==========================================================================
  1100. func Test_43()
  1101. let test_name = 'test43'
  1102. only
  1103. edit file3.txt
  1104. below split file2.txt
  1105. below split file1.txt
  1106. wincmd t
  1107. MRU file1.txt
  1108. if winnr() != 3 || fnamemodify(@%, ':p:t') !=# 'file1.txt'
  1109. call LogResult(test_name, 'FAIL (1)')
  1110. else
  1111. MRU file2.txt
  1112. if winnr() != 2 && fnamemodify(@%, ':p:t') !=# 'file2.txt'
  1113. call LogResult(test_name, 'FAIL (2)')
  1114. else
  1115. MRU file3.txt
  1116. if winnr() != 1 && fnamemodify(@%, ':p:t') !=# 'file3.txt'
  1117. call LogResult(test_name, 'FAIL (3)')
  1118. else
  1119. call LogResult(test_name, 'pass')
  1120. endif
  1121. endif
  1122. endif
  1123. enew | only
  1124. endfunc
  1125. " ==========================================================================
  1126. " Test44
  1127. " Opening a file using the MRU command should open the file in a new window if
  1128. " the current buffer has unsaved changes.
  1129. " ==========================================================================
  1130. func Test_44()
  1131. let test_name = 'test44'
  1132. only
  1133. set modified
  1134. MRU file2.txt
  1135. if winnr('$') == 2 && winnr() == 1 &&
  1136. \ fnamemodify(@%, ':p:t') ==# 'file2.txt'
  1137. call LogResult(test_name, 'pass')
  1138. else
  1139. call LogResult(test_name, 'FAIL')
  1140. endif
  1141. close
  1142. set nomodified
  1143. endfunc
  1144. " ==========================================================================
  1145. " Test45
  1146. " Opening a file from the MRU window using 'v' should open the file in a new
  1147. " window if the current buffer has unsaved changes.
  1148. " ==========================================================================
  1149. func Test_45()
  1150. let test_name = 'test45'
  1151. only
  1152. set modified
  1153. MRU
  1154. call search('file3.txt')
  1155. normal v
  1156. if winnr('$') == 2 && winnr() == 1
  1157. \ && fnamemodify(@%, ':p:t') ==# 'file3.txt'
  1158. \ && &readonly
  1159. call LogResult(test_name, 'pass')
  1160. else
  1161. call LogResult(test_name, 'FAIL')
  1162. endif
  1163. close
  1164. set nomodified
  1165. endfunc
  1166. " ==========================================================================
  1167. " Test46
  1168. " Specify a count to the :MRU command to set the MRU window height/width
  1169. " ==========================================================================
  1170. func Test_46()
  1171. let test_name = 'test46'
  1172. only
  1173. " default height is 8
  1174. MRU
  1175. if winnr() != 2 || winheight(0) != 8
  1176. call LogResult(test_name, 'FAIL (1)')
  1177. return
  1178. endif
  1179. close
  1180. " use a specific height value
  1181. 15MRU
  1182. if winnr() != 2 || winheight(0) != 15
  1183. call LogResult(test_name, 'FAIL (2)')
  1184. return
  1185. endif
  1186. close
  1187. if v:version >= 800
  1188. " use a specific height value with a command modifier
  1189. topleft 12MRU
  1190. if winnr() != 1 || winheight(0) != 12
  1191. call LogResult(test_name, 'FAIL (3)')
  1192. return
  1193. endif
  1194. close
  1195. " check for the width (leftmost window)
  1196. vertical topleft 20MRU
  1197. if winnr() != 1 || winwidth(0) != 20
  1198. call LogResult(test_name, 'FAIL (4)')
  1199. return
  1200. endif
  1201. close
  1202. " check for the width (rightmost window)
  1203. vertical botright 25MRU
  1204. if winnr() != 2 || winwidth(0) != 25
  1205. call LogResult(test_name, 'FAIL (5)')
  1206. return
  1207. endif
  1208. close
  1209. endif
  1210. call LogResult(test_name, 'pass')
  1211. endfunc
  1212. " ==========================================================================
  1213. " Test47
  1214. " The height of the MRU window should be MRU_Window_Height
  1215. " ==========================================================================
  1216. func Test_47()
  1217. let test_name = 'test47'
  1218. only
  1219. " default height is 8
  1220. MRU
  1221. if winheight(0) != 8
  1222. call LogResult(test_name, 'FAIL (1)')
  1223. return
  1224. endif
  1225. close
  1226. let g:MRU_Window_Height = 2
  1227. MRU
  1228. if winheight(0) != 2
  1229. call LogResult(test_name, 'FAIL (2)')
  1230. return
  1231. endif
  1232. close
  1233. let g:MRU_Window_Height = 12
  1234. MRU
  1235. if winheight(0) != 12
  1236. call LogResult(test_name, 'FAIL (3)')
  1237. return
  1238. endif
  1239. close
  1240. call LogResult(test_name, 'pass')
  1241. let g:MRU_Window_Height = 8
  1242. endfunc
  1243. " ==========================================================================
  1244. " Test48
  1245. " Fuzzy search file names with MRU_FuzzyMatch set to 1.
  1246. " ==========================================================================
  1247. func Test_48()
  1248. if !exists('*matchfuzzy')
  1249. return
  1250. endif
  1251. let test_name = 'test48'
  1252. enew | only
  1253. let g:MRU_FuzzyMatch = 1
  1254. MRU F1
  1255. if fnamemodify(@%, ':p:t') ==# 'file1.txt' && winnr('$') == 1
  1256. call LogResult(test_name, 'pass')
  1257. else
  1258. call LogResult(test_name, 'FAIL (1)')
  1259. endif
  1260. let g:MRU_FuzzyMatch = 0
  1261. redir => msg
  1262. MRU F1
  1263. redir END
  1264. if msg =~# "MRU file list doesn't contain files matching F1"
  1265. call LogResult(test_name, 'pass')
  1266. else
  1267. call LogResult(test_name, 'FAIL (2)')
  1268. endif
  1269. let g:MRU_FuzzyMatch = 1
  1270. endfunc
  1271. " ==========================================================================
  1272. " Test49
  1273. " Test for creating a new file by saving an unnamed buffer.
  1274. " ==========================================================================
  1275. func Test_49()
  1276. let test_name = 'test49'
  1277. enew | only
  1278. call setline(1, 'sample file')
  1279. write sample.txt
  1280. let l = readfile(g:MRU_File)
  1281. if match(l, 'sample.txt') != -1
  1282. call LogResult(test_name, 'pass')
  1283. else
  1284. call LogResult(test_name, 'FAIL')
  1285. endif
  1286. call delete('sample.txt')
  1287. bwipe sample.txt
  1288. endfunc
  1289. " ==========================================================================
  1290. " Test50
  1291. " Test for the MruGetFiles() function
  1292. " ==========================================================================
  1293. func Test_50()
  1294. let test_name = 'test50'
  1295. enew | only
  1296. let list1 = MruGetFiles()
  1297. let list2 = readfile(g:MRU_File)
  1298. if list1 != list2[1:]
  1299. call LogResult(test_name, 'FAIL (1)')
  1300. return
  1301. endif
  1302. if MruGetFiles('x1y2z3') == []
  1303. call LogResult(test_name, 'pass')
  1304. else
  1305. call LogResult(test_name, 'FAIL (2)')
  1306. endif
  1307. endfunc
  1308. " ==========================================================================
  1309. " Test51
  1310. " Test for the :MruRefresh command
  1311. " ==========================================================================
  1312. func Test_51()
  1313. let test_name = 'test51'
  1314. enew | only
  1315. if match(MruGetFiles(), 'sample.txt') == -1
  1316. call LogResult(test_name, 'FAIL (1)')
  1317. return
  1318. endif
  1319. MruRefresh
  1320. if match(MruGetFiles(), 'sample.txt') == -1
  1321. call LogResult(test_name, 'pass')
  1322. else
  1323. call LogResult(test_name, 'FAIL (2)')
  1324. endif
  1325. endfunc
  1326. " ==========================================================================
  1327. " Test52
  1328. " Test for the re-opening a deleted buffer from the MRU list
  1329. " ==========================================================================
  1330. func Test_52()
  1331. let test_name = 'test52'
  1332. edit file1.txt
  1333. edit file2.txt
  1334. bd
  1335. " select the file from the MRU window
  1336. MRU
  1337. call search('file2.txt')
  1338. exe "normal \<Enter>"
  1339. if !&buflisted || fnamemodify(@%, ':p:t') !=# 'file2.txt'
  1340. call LogResult(test_name, 'FAIL (1)')
  1341. return
  1342. endif
  1343. " open the file directly using the command
  1344. bw file1.txt file2.txt
  1345. edit file2.txt
  1346. edit file1.txt
  1347. bd
  1348. MRU file1.txt
  1349. if !&buflisted || fnamemodify(@%, ':p:t') !=# 'file1.txt'
  1350. call LogResult(test_name, 'FAIL (2)')
  1351. return
  1352. endif
  1353. call LogResult(test_name, 'pass')
  1354. endfunc
  1355. " ==========================================================================
  1356. " Test53
  1357. " Test for using a command modifier when directly opening a file using the
  1358. " MRU command.
  1359. " ==========================================================================
  1360. func Test_53()
  1361. if v:version < 800
  1362. return
  1363. endif
  1364. let test_name = 'test53'
  1365. %bw!
  1366. topleft MRU file2.txt
  1367. if winnr('$') == 2 && winnr() == 1 && fnamemodify(@%, ':p:t') ==# 'file2.txt'
  1368. wincmd j
  1369. if winnr() != 2
  1370. call LogResult(test_name, 'FAIL (1)')
  1371. return
  1372. endif
  1373. else
  1374. call LogResult(test_name, 'FAIL (2)')
  1375. return
  1376. endif
  1377. %bw
  1378. belowright MRU file2.txt
  1379. if winnr('$') == 2 && winnr() == 2 && fnamemodify(@%, ':p:t') ==# 'file2.txt'
  1380. wincmd k
  1381. if winnr() != 1
  1382. call LogResult(test_name, 'FAIL (3)')
  1383. return
  1384. endif
  1385. else
  1386. call LogResult(test_name, 'FAIL (4)')
  1387. return
  1388. endif
  1389. %bw
  1390. vertical topleft MRU file2.txt
  1391. if winnr('$') == 2 && winnr() == 1 && fnamemodify(@%, ':p:t') ==# 'file2.txt'
  1392. wincmd l
  1393. if winnr() != 2
  1394. call LogResult(test_name, 'FAIL (5)')
  1395. return
  1396. endif
  1397. else
  1398. call LogResult(test_name, 'FAIL (6)')
  1399. return
  1400. endif
  1401. %bw
  1402. vertical belowright MRU file2.txt
  1403. if winnr('$') == 2 && winnr() == 2 && fnamemodify(@%, ':p:t') ==# 'file2.txt'
  1404. wincmd h
  1405. if winnr() != 1
  1406. call LogResult(test_name, 'FAIL (7)')
  1407. return
  1408. endif
  1409. else
  1410. call LogResult(test_name, 'FAIL (8)')
  1411. return
  1412. endif
  1413. %bw
  1414. tab MRU file2.txt
  1415. if tabpagenr() != 2 || fnamemodify(@%, ':p:t') !=# 'file2.txt'
  1416. call LogResult(test_name, 'FAIL (9)')
  1417. return
  1418. endif
  1419. %bw
  1420. call LogResult(test_name, 'pass')
  1421. endfunc
  1422. " ==========================================================================
  1423. " Test54
  1424. " Test for the :MRUToggle command.
  1425. " ==========================================================================
  1426. func Test_54()
  1427. let test_name = 'test54'
  1428. only
  1429. " open the MRU window
  1430. MRUToggle
  1431. if bufwinnr(g:MRU_buffer_name) != 2 || winnr() != 2
  1432. call LogResult(test_name, 'FAIL (1)')
  1433. return
  1434. endif
  1435. " close the MRU window
  1436. MRUToggle
  1437. if bufwinnr(g:MRU_buffer_name) != -1 || winnr() != 1
  1438. call LogResult(test_name, 'FAIL (2)')
  1439. return
  1440. endif
  1441. " close the MRU window from some other window
  1442. MRUToggle
  1443. wincmd k
  1444. MRUToggle
  1445. if bufwinnr(g:MRU_buffer_name) != -1 || winnr() != 1
  1446. call LogResult(test_name, 'FAIL (3)')
  1447. return
  1448. endif
  1449. call LogResult(test_name, 'pass')
  1450. endfunc
  1451. " ==========================================================================
  1452. " Test55
  1453. " Editing a file selected from the MRU window should set the current file to
  1454. " be the alternate file.
  1455. " ==========================================================================
  1456. func Test_55()
  1457. let test_name = 'test55'
  1458. silent! bw file1.txt file2.txt file3.txt
  1459. new
  1460. edit file1.txt
  1461. edit file2.txt
  1462. MRU
  1463. call search('file3.txt')
  1464. exe "normal \<Enter>"
  1465. if fnamemodify(@%, ':p:t') !=# 'file3.txt'
  1466. \ || fnamemodify(@#, ':p:t') !=# 'file2.txt'
  1467. call LogResult(test_name, 'FAIL')
  1468. return
  1469. endif
  1470. call LogResult(test_name, 'pass')
  1471. endfunc
  1472. " ==========================================================================
  1473. " Test56
  1474. " With MRU_Use_Current_Window set to 1, editing a file from the MRU list
  1475. " should not change the alternate file.
  1476. " ==========================================================================
  1477. func Test_56()
  1478. let test_name = 'test56'
  1479. let g:MRU_Use_Current_Window = 1
  1480. bw file1.txt file2.txt file3.txt
  1481. new
  1482. edit file3.txt
  1483. edit file1.txt
  1484. edit file2.txt
  1485. MRU
  1486. call search('file3.txt')
  1487. exe "normal \<Enter>"
  1488. if fnamemodify(@%, ':p:t') !=# 'file3.txt'
  1489. \ || fnamemodify(@#, ':p:t') !=# 'file2.txt'
  1490. call LogResult(test_name, 'FAIL (1)')
  1491. return
  1492. endif
  1493. " try viewing a file
  1494. MRU
  1495. call search('file1.txt')
  1496. normal v
  1497. if fnamemodify(@%, ':p:t') !=# 'file1.txt'
  1498. \ || fnamemodify(@#, ':p:t') !=# 'file3.txt'
  1499. \ || !&readonly
  1500. call LogResult(test_name, 'FAIL (2)')
  1501. return
  1502. endif
  1503. " try opening a wiped out buffer
  1504. bw file2.txt
  1505. MRU
  1506. call search('file2.txt')
  1507. exe "normal \<Enter>"
  1508. if fnamemodify(@%, ':p:t') !=# 'file2.txt'
  1509. \ || fnamemodify(@#, ':p:t') !=# 'file1.txt'
  1510. \ || &readonly
  1511. call LogResult(test_name, 'FAIL (3)')
  1512. return
  1513. endif
  1514. let g:MRU_Use_Current_Window = 0
  1515. bw!
  1516. call LogResult(test_name, 'pass')
  1517. endfunc
  1518. " ==========================================================================
  1519. " Test57
  1520. " When the MRU window is closed, the MRU buffer should be unloaded.
  1521. " If 'MRU_Use_Current_Window' is set, then the MRU buffer should be wiped out.
  1522. " ==========================================================================
  1523. func Test_57()
  1524. let test_name = 'test57'
  1525. MRU
  1526. let mrubnum = bufnr('')
  1527. close
  1528. if bufloaded(mrubnum)
  1529. call LogResult(test_name, 'FAIL (1)')
  1530. return
  1531. endif
  1532. let g:MRU_Use_Current_Window = 1
  1533. new
  1534. edit Xfile
  1535. MRU
  1536. let mrubnum = bufnr('')
  1537. edit #
  1538. if bufexists(mrubnum) || @% != 'Xfile'
  1539. call LogResult(test_name, 'FAIL (2)')
  1540. return
  1541. endif
  1542. let g:MRU_Use_Current_Window = 0
  1543. bw!
  1544. call LogResult(test_name, 'pass')
  1545. endfunc
  1546. " ==========================================================================
  1547. " Test58
  1548. " When the MRU window is toggled with MRU_Use_Current_Window set to 1, the
  1549. " previous buffer should be loaded.
  1550. " ==========================================================================
  1551. func Test_58()
  1552. let test_name = 'test58'
  1553. let g:MRU_Use_Current_Window = 1
  1554. new
  1555. edit Xfile
  1556. MRUToggle
  1557. if @% != g:MRU_buffer_name || winnr('$') != 2
  1558. call LogResult(test_name, 'FAIL (1)')
  1559. return
  1560. endif
  1561. MRUToggle
  1562. if @% != 'Xfile' || winnr('$') != 2
  1563. call LogResult(test_name, 'FAIL (2)')
  1564. return
  1565. endif
  1566. let g:MRU_Use_Current_Window = 0
  1567. bw!
  1568. call LogResult(test_name, 'pass')
  1569. endfunc
  1570. " ==========================================================================
  1571. " Test59
  1572. " When the MRU_Set_Alternate_File is set to 1, on plugin startup, the
  1573. " alternate file should be set to the first file in the MRU list.
  1574. " ==========================================================================
  1575. func Test_59()
  1576. if v:version < 802
  1577. return
  1578. endif
  1579. let test_name = 'test59'
  1580. call writefile([], 'Xfirstfile')
  1581. edit Xfirstfile
  1582. call writefile([
  1583. \ "let MRU_File='vim_mru_file'",
  1584. \ "let MRU_Set_Alternate_File=1",
  1585. \ "source ../plugin/mru.vim",
  1586. \ "call writefile([@#], 'Xoutput')"
  1587. \ ], 'Xscript')
  1588. silent! !vim -u NONE --noplugin i NONE -N -S Xscript -c "qa"
  1589. if !filereadable('Xoutput')
  1590. call LogResult(test_name, 'FAIL (1)')
  1591. else
  1592. let lines = readfile('Xoutput')
  1593. if len(lines) == 1 && lines[0] =~ 'Xfirstfile$'
  1594. call LogResult(test_name, 'pass')
  1595. else
  1596. call LogResult(test_name, 'FAIL (2)')
  1597. endif
  1598. endif
  1599. call delete('Xscript')
  1600. call delete('Xoutput')
  1601. call delete('Xfirstfile')
  1602. endfunc
  1603. " ==========================================================================
  1604. " Create the files used by the tests
  1605. call writefile(['MRU test file1'], 'file1.txt')
  1606. call writefile(['MRU test file2'], 'file2.txt')
  1607. call writefile(['MRU test file3'], 'file3.txt')
  1608. call writefile(['#include <stdio.h', 'int main(){}'], 'abc.c')
  1609. call writefile(['#include <stdlib.h', 'int main(){}'], 'def.c')
  1610. " Remove the results from the previous test runs
  1611. call delete('results.txt')
  1612. call delete(g:MRU_File)
  1613. let results = []
  1614. " Generate a sorted list of Test_ functions to run
  1615. redir @q
  1616. silent function /^Test_
  1617. redir END
  1618. let s:tests = split(substitute(@q, '\(function\) \(\k*()\)', '\2', 'g'))
  1619. " Run the tests
  1620. set nomore
  1621. set debug=beep
  1622. for one_test in sort(s:tests)
  1623. exe 'call ' . one_test
  1624. endfor
  1625. set more
  1626. call writefile(results, 'results.txt')
  1627. " TODO:
  1628. " Add the following tests:
  1629. " 1. When the MRU list is modified, the MRU menu should be refreshed.
  1630. " 2. Try to jump to an already open file from the MRU window and using the
  1631. " MRU command.
  1632. " Cleanup the files used by the tests
  1633. call delete('file1.txt')
  1634. call delete('file2.txt')
  1635. call delete('file3.txt')
  1636. call delete('abc.c')
  1637. call delete('def.c')
  1638. call delete(g:MRU_File)
  1639. " End of unit test execution
  1640. qall
  1641. " vim: shiftwidth=2 sts=2 expandtab