unit_tests.vim 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  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 following variable to 1, to profile the MRU plugin
  8. let s:do_profile=0
  9. " Profile the MRU plugin
  10. if s:do_profile
  11. profile start mru_profile.txt
  12. profile! file */mru.vim
  13. endif
  14. source ../plugin/mru.vim
  15. " Function to log test results
  16. func! LogResult(test, result)
  17. redir >> results.txt
  18. silent echon "\r" . a:test . ': ' . a:result . "\n"
  19. redir END
  20. endfunc
  21. " ==========================================================================
  22. " Test1
  23. " When the MRU list is empty, invoking the MRU command should return an error
  24. " ==========================================================================
  25. func Test_01()
  26. let test_name = 'test1'
  27. redir => msg
  28. MRU
  29. redir END
  30. if msg =~# "MRU file list is empty"
  31. call LogResult(test_name, 'pass')
  32. else
  33. call LogResult(test_name, 'FAIL')
  34. endif
  35. endfunc
  36. " ==========================================================================
  37. " Test2
  38. " Open the MRU window and check the order of files listed in the window
  39. " Open the MRU window when the window is already opened.
  40. " ==========================================================================
  41. func Test_02()
  42. let test_name = 'test2'
  43. edit file1.txt
  44. edit file2.txt
  45. edit file3.txt
  46. edit file2.txt
  47. edit file1.txt
  48. MRU
  49. MRU
  50. let l = getline(1, "$")
  51. if l[0] =~# "file1.txt" && l[1] =~# "file2.txt" && l[2] =~# "file3.txt"
  52. call LogResult(test_name, 'pass')
  53. else
  54. call LogResult(test_name, 'FAIL')
  55. endif
  56. endfunc
  57. " ==========================================================================
  58. " Test3
  59. " Select a file from the MRU window and check whether it is opened
  60. " ==========================================================================
  61. func Test_03()
  62. let test_name = 'test3'
  63. " Go to the last but one line
  64. $
  65. " Select the last file in the MRU window
  66. exe "normal \<Enter>"
  67. if fnamemodify(@%, ':p:t') !=# 'file3.txt'
  68. call LogResult(test_name, "FAIL (1)")
  69. else
  70. " Make sure the MRU window is closed
  71. if bufwinnr(g:MRU_buffer_name) == -1
  72. call LogResult(test_name, 'pass')
  73. else
  74. call LogResult(test_name, "FAIL (2)")
  75. endif
  76. endif
  77. endfunc
  78. " ==========================================================================
  79. " Test4
  80. " MRU opens a selected file in the previous/last window
  81. " ==========================================================================
  82. func Test_04()
  83. let test_name = 'test4'
  84. " Edit a file and then open a new window, open the MRU window and select the
  85. " file
  86. split file1.txt
  87. only
  88. below new
  89. MRU
  90. call search('file2.txt')
  91. exe "normal \<Enter>"
  92. if winnr() == 2
  93. call LogResult(test_name, 'pass')
  94. else
  95. call LogResult(test_name, 'FAIL')
  96. endif
  97. endfunc
  98. " ==========================================================================
  99. " Test5
  100. " MRU opens a selected file in the same window if the file is already opened
  101. " ==========================================================================
  102. func Test_05()
  103. let test_name = 'test5'
  104. edit file1.txt
  105. only
  106. below split file2.txt
  107. below split file3.txt
  108. MRU
  109. call search('file1.txt')
  110. exe "normal \<Enter>"
  111. if winnr() != 1 || fnamemodify(@%, ':p:t') !=# 'file1.txt'
  112. call LogResult(test_name, "FAIL (1)")
  113. else
  114. MRU
  115. call search('file2.txt')
  116. exe "normal \<Enter>"
  117. if winnr() != 2 || fnamemodify(@%, ':p:t') !=# 'file2.txt'
  118. call LogResult(test_name, "FAIL (2)")
  119. else
  120. MRU
  121. call search('file3.txt')
  122. exe "normal \<Enter>"
  123. if winnr() != 3 || fnamemodify(@%, ':p:t') !=# 'file3.txt'
  124. call LogResult(test_name, "FAIL (3)")
  125. else
  126. call LogResult(test_name, 'pass')
  127. endif
  128. endif
  129. endif
  130. endfunc
  131. " ==========================================================================
  132. " Test6
  133. " MRU opens a file selected with 'o' command in a new window
  134. " ==========================================================================
  135. func Test_06()
  136. let test_name = 'test6'
  137. enew | only
  138. edit file1.txt
  139. below new
  140. MRU
  141. normal o
  142. if winnr() == 3 && fnamemodify(@%, ':p:t') ==# 'file1.txt'
  143. call LogResult(test_name, 'pass')
  144. else
  145. call LogResult(test_name, 'FAIL')
  146. endif
  147. endfunc
  148. " ==========================================================================
  149. " Test7
  150. " MRU opens the selected file in a new window if the previous buffer is
  151. " modified.
  152. " ==========================================================================
  153. func Test_07()
  154. let test_name = 'test7'
  155. enew | only
  156. insert
  157. MRU plugin test
  158. .
  159. MRU
  160. call search('file3.txt')
  161. exe "normal \<Enter>"
  162. if winnr() == 1 && winnr('$') == 2 &&
  163. \ fnamemodify(@%, ':p:t') ==# 'file3.txt'
  164. call LogResult(test_name, 'pass')
  165. else
  166. call LogResult(test_name, 'FAIL')
  167. endif
  168. " Discard changes in the new buffer
  169. wincmd b
  170. enew!
  171. only
  172. endfunc
  173. " ==========================================================================
  174. " Test8
  175. " MRU opens a file selected with 'v' command in read-only mode in the current
  176. " window.
  177. " ==========================================================================
  178. func Test_08()
  179. let test_name = 'test8'
  180. enew | only
  181. MRU
  182. call search('file1.txt')
  183. normal v
  184. let r1 = &readonly
  185. MRU
  186. call search('file2.txt')
  187. exe "normal \<Enter>"
  188. let r2 = &readonly
  189. MRU
  190. call search('file1.txt')
  191. exe "normal \<Enter>"
  192. let r3 = &readonly
  193. if r1 == 1 && r2 == 0 && r3 == 0
  194. call LogResult(test_name, 'pass')
  195. else
  196. call LogResult(test_name, 'FAIL')
  197. endif
  198. endfunc
  199. " ==========================================================================
  200. " Test9
  201. " Use 'O' in the MRU window to open a file in a vertically split window
  202. " ==========================================================================
  203. func Test_09()
  204. let test_name = 'test9'
  205. enew | only
  206. edit file1.txt
  207. MRU
  208. call search('file2.txt')
  209. normal O
  210. let b1 = @%
  211. wincmd h
  212. let b2 = @%
  213. wincmd l
  214. let b3 = @%
  215. if winnr('$') == 2 && b1 ==# 'file2.txt' &&
  216. \ b2 ==# 'file1.txt' && b3 ==# 'file2.txt'
  217. call LogResult(test_name, 'pass')
  218. else
  219. call LogResult(test_name, 'FAIL')
  220. endif
  221. endfunc
  222. " ==========================================================================
  223. " Test10
  224. " Use 'p' in the MRU window to open a file in the preview window
  225. " ==========================================================================
  226. func Test_10()
  227. let test_name = 'test10'
  228. enew | only
  229. MRU
  230. call search('file3.txt')
  231. normal p
  232. wincmd P
  233. let p1 = &previewwindow
  234. let b1 = @%
  235. if winnr('$') == 2 && &previewwindow && @% =~# 'file3.txt'
  236. call LogResult(test_name, 'pass')
  237. else
  238. call LogResult(test_name, 'FAIL')
  239. endif
  240. pclose
  241. endfunc
  242. " ==========================================================================
  243. " Test11
  244. " MRU opens a file selected with 't' command in a new tab and the tab
  245. " is opened at the end
  246. " ==========================================================================
  247. func Test_11()
  248. let test_name = 'test11'
  249. enew | only
  250. edit a1.txt
  251. tabnew a2.txt
  252. tabnew a3.txt
  253. tabnew a4.txt
  254. tabfirst
  255. MRU
  256. call search('file3.txt')
  257. normal t
  258. if fnamemodify(@%, ':p:t') ==# 'file3.txt' && tabpagenr() == 5
  259. call LogResult(test_name, 'pass')
  260. else
  261. call LogResult(test_name, 'FAIL')
  262. call LogResult(test_name, "file = " . fnamemodify(@%, ':p:t'))
  263. call LogResult(test_name, "tab page = " . tabpagenr())
  264. endif
  265. tabonly
  266. endfunc
  267. " ==========================================================================
  268. " Test12
  269. " The 'q' command closes the MRU window
  270. " ==========================================================================
  271. func Test_12()
  272. let test_name = 'test12'
  273. enew | only
  274. MRU
  275. normal q
  276. if bufwinnr(g:MRU_buffer_name) == -1
  277. call LogResult(test_name, 'pass')
  278. else
  279. call LogResult(test_name, 'FAIL')
  280. endif
  281. endfunc
  282. " ==========================================================================
  283. " Test13
  284. " A selected file is opened in a new window if the previous window is a
  285. " preview window
  286. " ==========================================================================
  287. func Test_13()
  288. let test_name = 'test13'
  289. enew | only
  290. setlocal previewwindow
  291. MRU
  292. call search('file2.txt')
  293. exe "normal \<Enter>"
  294. if winnr() == 1 && winnr('$') == 2 &&
  295. \ &previewwindow == 0 &&
  296. \ fnamemodify(@%, ':p:t') ==# 'file2.txt'
  297. call LogResult(test_name, 'pass')
  298. else
  299. call LogResult(test_name, 'FAIL')
  300. endif
  301. " Close the preview window created by this test
  302. new
  303. only
  304. endfunc
  305. " ==========================================================================
  306. " Test14
  307. " A selected file is opened in a new window if the previous window contains
  308. " a special buffer (used by some other plugin)
  309. " ==========================================================================
  310. func Test_14()
  311. let test_name = 'test14'
  312. enew | only
  313. setlocal buftype=nofile
  314. MRU
  315. call search('file3.txt')
  316. exe "normal \<Enter>"
  317. if winnr() == 1 && winnr('$') == 2 &&
  318. \ &buftype == '' &&
  319. \ fnamemodify(@%, ':p:t') ==# 'file3.txt'
  320. call LogResult(test_name, 'pass')
  321. else
  322. call LogResult(test_name, 'FAIL')
  323. endif
  324. " Discard the special buffer
  325. enew
  326. endfunc
  327. " ==========================================================================
  328. " Test15
  329. " If a file selected using the 't' command is already opened in a tab,
  330. " then jump to that tab (instead of opening a new tab)
  331. " ==========================================================================
  332. func Test_15()
  333. let test_name = 'test15'
  334. enew | only
  335. " Open the test files in the middle window with empty windows at the top and
  336. " bottom
  337. edit file1.txt
  338. above new
  339. botright new
  340. tabedit file2.txt
  341. above new
  342. botright new
  343. tabedit file3.txt
  344. above new
  345. botright new
  346. tabfirst
  347. MRU
  348. call search('file3.txt')
  349. exe "normal t"
  350. if tabpagenr() != 3
  351. \ || fnamemodify(@%, ':p:t') !=# 'file3.txt'
  352. \ || winnr() != 2
  353. call LogResult(test_name, "FAIL (1)")
  354. else
  355. MRU
  356. call search('file1.txt')
  357. exe "normal t"
  358. if tabpagenr() != 1
  359. \ || fnamemodify(@%, ':p:t') !=# 'file1.txt'
  360. \ || winnr() != 2
  361. call LogResult(test_name, "FAIL (2)")
  362. else
  363. MRU
  364. call search('file2.txt')
  365. exe "normal t"
  366. if tabpagenr() != 2
  367. \ || fnamemodify(@%, ':p:t') !=# 'file2.txt'
  368. \ || winnr() != 2
  369. call LogResult(test_name, "FAIL (3)")
  370. else
  371. call LogResult(test_name, 'pass')
  372. endif
  373. endif
  374. endif
  375. " Close all the other tabs
  376. tabonly
  377. enew
  378. only
  379. endfunc
  380. " ==========================================================================
  381. " Test16
  382. " Open multiple files from the MRU window using the visual mode and by using a
  383. " count. Each file should be opened in a separate window.
  384. " ==========================================================================
  385. func Test_16()
  386. let test_name = 'test16'
  387. enew | only
  388. edit file3.txt
  389. edit file2.txt
  390. edit file1.txt
  391. enew
  392. MRU
  393. exe "normal 3\<Enter>"
  394. if winnr('$') == 3 &&
  395. \ bufwinnr('file3.txt') == 1 &&
  396. \ bufwinnr('file2.txt') == 2 &&
  397. \ bufwinnr('file1.txt') == 3
  398. let test_result = 'pass'
  399. else
  400. let test_result = 'FAIL'
  401. endif
  402. only | enew
  403. if test_result == 'pass'
  404. MRU
  405. exe "normal V2j\<Enter>"
  406. if winnr('$') == 3 &&
  407. \ bufwinnr('file1.txt') == 1 &&
  408. \ bufwinnr('file2.txt') == 2 &&
  409. \ bufwinnr('file3.txt') == 3
  410. let test_result = 'pass'
  411. else
  412. let test_result = 'FAIL'
  413. endif
  414. endif
  415. if test_result == 'pass'
  416. call LogResult(test_name, 'pass')
  417. else
  418. call LogResult(test_name, 'FAIL')
  419. endif
  420. endfunc
  421. " ==========================================================================
  422. " Test17
  423. " When the MRU list is updated, the MRU file also should updated.
  424. " ==========================================================================
  425. func Test_17()
  426. let test_name = 'test17'
  427. enew | only
  428. edit file1.txt
  429. let l = readfile(g:MRU_File)
  430. if l[1] =~# 'file1.txt'
  431. edit file2.txt
  432. let l = readfile(g:MRU_File)
  433. if l[1] =~# 'file2.txt'
  434. edit file3.txt
  435. let l = readfile(g:MRU_File)
  436. if l[1] =~# 'file3.txt'
  437. call LogResult(test_name, 'pass')
  438. else
  439. call LogResult(test_name, "FAIL (3)")
  440. endif
  441. else
  442. call LogResult(test_name, "FAIL (2)")
  443. endif
  444. else
  445. call LogResult(test_name, "FAIL (1)")
  446. endif
  447. endfunc
  448. " MRU_Test_Add_Files
  449. " Add the supplied List of files to the beginning of the MRU file
  450. func! s:MRU_Test_Add_Files(fnames)
  451. let l = readfile(g:MRU_File)
  452. call extend(l, a:fnames, 1)
  453. call writefile(l, g:MRU_File)
  454. endfunc
  455. " ==========================================================================
  456. " Test18
  457. " When the MRU file is updated by another Vim instance, the MRU plugin
  458. " should update the MRU list
  459. " ==========================================================================
  460. func Test_18()
  461. let test_name = 'test18'
  462. enew | only
  463. call s:MRU_Test_Add_Files(['/software/editors/vim',
  464. \ '/software/editors/emacs',
  465. \ '/software/editors/nano'])
  466. MRU
  467. if getline(1) ==# 'vim (/software/editors/vim)'
  468. \ && getline(2) ==# 'emacs (/software/editors/emacs)'
  469. \ && getline(3) ==# 'nano (/software/editors/nano)'
  470. call LogResult(test_name, 'pass')
  471. else
  472. call LogResult(test_name, 'FAIL')
  473. endif
  474. " Close the MRU window
  475. close
  476. endfunc
  477. " ==========================================================================
  478. " Test19
  479. " When the MRU file is updated by another Vim instance, the MRU file names
  480. " from the current instance should be merged with that list
  481. " ==========================================================================
  482. func Test_19()
  483. let test_name = 'test19'
  484. enew | only
  485. " Remove all the files from the MRU file
  486. let l = readfile(g:MRU_File)
  487. call remove(l, 1, -1)
  488. call writefile(l, g:MRU_File)
  489. edit file1.txt
  490. call s:MRU_Test_Add_Files(['/software/os/unix'])
  491. edit file2.txt
  492. call s:MRU_Test_Add_Files(['/software/os/windows'])
  493. edit file3.txt
  494. call s:MRU_Test_Add_Files(['/software/os/osx'])
  495. MRU
  496. if getline(1) ==# 'osx (/software/os/osx)'
  497. \ && getline(2) =~# 'file3.txt'
  498. \ && getline(3) ==# 'windows (/software/os/windows)'
  499. \ && getline(4) =~# 'file2.txt'
  500. \ && getline(5) ==# 'unix (/software/os/unix)'
  501. \ && getline(6) =~# 'file1.txt'
  502. call LogResult(test_name, 'pass')
  503. else
  504. call LogResult(test_name, 'FAIL')
  505. endif
  506. close
  507. endfunc
  508. " ==========================================================================
  509. " Test20
  510. " When the MRU list has more than g:MRU_Max_Entries, the list should be
  511. " trimmed. The last entries should be removed.
  512. " ==========================================================================
  513. func Test_20()
  514. let test_name = 'test20'
  515. enew | only
  516. " Create a MRU list with MRU_Max_Entries
  517. let flist = []
  518. for i in range(1, g:MRU_Max_Entries)
  519. let flist += ['/usr/share/mru_test/mru_file' . i . '.abc']
  520. endfor
  521. " Modify the MRU file to contain max entries
  522. let l = readfile(g:MRU_File)
  523. call remove(l, 1, -1)
  524. call extend(l, flist)
  525. call writefile(l, g:MRU_File)
  526. enew
  527. edit file1.txt
  528. let l = readfile(g:MRU_File)
  529. if len(l) == (g:MRU_Max_Entries + 1) &&
  530. \ l[g:MRU_Max_Entries] != '/usr/share/mru_test/mru_file9.abc'
  531. call LogResult(test_name, "FAIL (1)")
  532. else
  533. edit file2.txt
  534. let l = readfile(g:MRU_File)
  535. if len(l) == (g:MRU_Max_Entries + 1) &&
  536. \ l[g:MRU_Max_Entries] != '/usr/share/mru_test/mru_file8.abc'
  537. call LogResult(test_name, "FAIL (2)")
  538. else
  539. edit file3.txt
  540. let l = readfile(g:MRU_File)
  541. if len(l) == (g:MRU_Max_Entries + 1) &&
  542. \ l[g:MRU_Max_Entries] != '/usr/share/mru_test/mru_file7.abc'
  543. call LogResult(test_name, "FAIL (3)")
  544. else
  545. call LogResult(test_name, 'pass')
  546. endif
  547. endif
  548. endif
  549. endfunc
  550. " ==========================================================================
  551. " Test21
  552. " When an filename (already present in the MRU list) is specified to the MRU
  553. " command, it should edit the file.
  554. " ==========================================================================
  555. func Test_21()
  556. let test_name = 'test21'
  557. enew | only
  558. edit file1.txt
  559. edit file2.txt
  560. edit file3.txt
  561. enew
  562. MRU file2.txt
  563. if fnamemodify(@%, ':p:t') ==# 'file2.txt' && winnr('$') == 1
  564. call LogResult(test_name, 'pass')
  565. else
  566. call LogResult(test_name, 'FAIL')
  567. endif
  568. endfunc
  569. " ==========================================================================
  570. " Test22
  571. " When a pattern (matching multiple filenames) is specified to the MRU
  572. " command, then the MRU window should be opened with all the matching
  573. " filenames
  574. " ==========================================================================
  575. func Test_22()
  576. let test_name = 'test22'
  577. enew | only
  578. edit file1.txt
  579. edit file2.txt
  580. edit file3.txt
  581. only
  582. MRU file.*
  583. if @% != g:MRU_buffer_name
  584. call LogResult(test_name, 'FAIL')
  585. else
  586. let l = getline(1, "$")
  587. if l[0] =~# "file3.txt" && l[1] =~# "file2.txt" && l[2] =~# "file1.txt"
  588. call LogResult(test_name, 'pass')
  589. else
  590. call LogResult(test_name, 'FAIL')
  591. endif
  592. endif
  593. close
  594. endfunc
  595. " ==========================================================================
  596. " Test23
  597. " When a partial filename (matching multiple filenames) is specified to the
  598. " MRU command, then the MRU window should be opened with all the matching
  599. " filenames
  600. " ==========================================================================
  601. func Test_23()
  602. let test_name = 'test23'
  603. enew | only
  604. edit file1.txt
  605. edit file2.txt
  606. edit file3.txt
  607. only
  608. MRU file
  609. if @% != g:MRU_buffer_name
  610. call LogResult(test_name, 'FAIL')
  611. else
  612. let l = getline(1, "$")
  613. if l[0] =~# "file3.txt" && l[1] =~# "file2.txt" && l[2] =~# "file1.txt"
  614. call LogResult(test_name, 'pass')
  615. else
  616. call LogResult(test_name, 'FAIL')
  617. endif
  618. endif
  619. close
  620. endfunc
  621. " ==========================================================================
  622. " Test24
  623. " When a non-existing filename is specified to the MRU command, an error
  624. " message should be displayed.
  625. " ==========================================================================
  626. func Test_24()
  627. let test_name = 'test24'
  628. redir => msg
  629. MRU nonexistingfile.txt
  630. redir END
  631. if @% == g:MRU_buffer_name ||
  632. \ msg !~# "MRU file list doesn't contain files " .
  633. \ "matching nonexistingfile.txt"
  634. call LogResult(test_name, 'FAIL')
  635. else
  636. call LogResult(test_name, 'pass')
  637. endif
  638. endfunc
  639. " ==========================================================================
  640. " Test25
  641. " The MRU command should support filename completion. Supply a partial file
  642. " name to the MRU command and complete the filenames.
  643. " ==========================================================================
  644. func Test_25()
  645. let test_name = 'test25'
  646. enew | only
  647. edit file1.txt
  648. edit file2.txt
  649. edit file3.txt
  650. exe 'normal! :MRU file' . "\<C-A>" . "\<Home>let m='\<End>'\<CR>"
  651. let fnames = split(m)
  652. if fnames[1] =~# 'file3.txt' && fnames[2] =~# 'file2.txt' &&
  653. \ fnames[3] =~# 'file1.txt'
  654. call LogResult(test_name, 'pass')
  655. else
  656. call LogResult(test_name, 'FAIL')
  657. endif
  658. endfunc
  659. " ==========================================================================
  660. " Test26
  661. " When trying to complete filenames for the MRU command without specifying
  662. " any text should return the entire MRU list.
  663. " ==========================================================================
  664. func Test_26()
  665. let test_name = 'test26'
  666. enew | only
  667. call delete(g:MRU_File)
  668. edit file1.txt
  669. edit file2.txt
  670. edit file3.txt
  671. exe 'normal! :MRU ' . "\<C-A>" . "\<Home>let m='\<End>'\<CR>"
  672. let fnames = split(m)
  673. if fnames[1] =~# 'file3.txt' && fnames[2] =~# 'file2.txt' &&
  674. \ fnames[3] =~# 'file1.txt'
  675. call LogResult(test_name, 'pass')
  676. else
  677. call LogResult(test_name, 'FAIL')
  678. endif
  679. endfunc
  680. " ==========================================================================
  681. " Test27
  682. " When the current file/buffer has unsaved changes, MRU should open a selected
  683. " file in a new window (if the 'hidden' option is not set)
  684. " ==========================================================================
  685. func Test_27()
  686. let test_name = 'test27'
  687. enew | only
  688. edit file1.txt
  689. edit file2.txt
  690. call append(line('$'), 'Temporary changes to buffer')
  691. MRU
  692. call search('file1.txt')
  693. exe "normal \<Enter>"
  694. if winnr() == 1 && winnr('$') == 2 &&
  695. \ fnamemodify(@%, ':p:t') ==# 'file1.txt'
  696. call LogResult(test_name, 'pass')
  697. else
  698. call LogResult(test_name, 'FAIL')
  699. endif
  700. close
  701. edit!
  702. endfunc
  703. " ==========================================================================
  704. " Test28
  705. " When the current file/buffer has unsaved changes and the 'hidden' option is
  706. " set, then MRU should open a selected file in the current window
  707. " ==========================================================================
  708. func Test_28()
  709. let test_name = 'test28'
  710. enew | only
  711. edit file2.txt
  712. edit file1.txt
  713. call append(line('$'), 'Temporary changes to buffer')
  714. set hidden
  715. MRU
  716. call search('file2.txt')
  717. exe "normal \<Enter>"
  718. if winnr('$') == 1 &&
  719. \ fnamemodify(@%, ':p:t') ==# 'file2.txt'
  720. call LogResult(test_name, 'pass')
  721. else
  722. call LogResult(test_name, 'FAIL')
  723. endif
  724. edit file1.txt
  725. edit!
  726. set nohidden
  727. %bw!
  728. endfunc
  729. " ==========================================================================
  730. " Test29
  731. " Every edited file is added to the top of the MRU list. If a file is already
  732. " present in the MRU list, then it is moved to the top of the list.
  733. " ==========================================================================
  734. func Test_29()
  735. let test_name = 'test29'
  736. enew | only
  737. edit file1.txt
  738. let f1 = readfile(g:MRU_File, '', 2)
  739. edit file2.txt
  740. let f2 = readfile(g:MRU_File, '', 2)
  741. edit file3.txt
  742. let f3 = readfile(g:MRU_File, '', 2)
  743. edit file1.txt
  744. let f4 = readfile(g:MRU_File, '', 2)
  745. if f1[1] =~# 'file1.txt' && f2[1] =~# 'file2.txt' && f3[1] =~# 'file3.txt' &&
  746. \ f4[1] =~# 'file1.txt'
  747. call LogResult(test_name, 'pass')
  748. else
  749. call LogResult(test_name, 'FAIL')
  750. endif
  751. endfunc
  752. " ==========================================================================
  753. " Test30
  754. " Only file names matching the regular expression in the MRU_Include_Files
  755. " variable should be added to the MRU list.
  756. " ==========================================================================
  757. func Test_30()
  758. let test_name = 'test30'
  759. enew | only
  760. edit file1.txt
  761. let g:MRU_Include_Files='\.c'
  762. edit abc.c
  763. let f1 = readfile(g:MRU_File, '', 2)
  764. edit file1.txt
  765. let f2 = readfile(g:MRU_File, '', 2)
  766. edit def.c
  767. let f3 = readfile(g:MRU_File, '', 2)
  768. if f1[1] =~# 'abc.c' && f2[1] =~# 'abc.c' && f3[1] =~# 'def.c'
  769. call LogResult(test_name, 'pass')
  770. else
  771. call LogResult(test_name, 'FAIL')
  772. endif
  773. let g:MRU_Include_Files=''
  774. endfunc
  775. " ==========================================================================
  776. " Test31
  777. " File names matching the regular expression in the MRU_Exclude_Files
  778. " variable should not be added to the MRU list.
  779. " ==========================================================================
  780. func Test_31()
  781. let test_name = 'test31'
  782. enew | only
  783. let g:MRU_Exclude_Files='\.txt'
  784. edit abc.c
  785. let f1 = readfile(g:MRU_File, '', 2)
  786. edit file1.txt
  787. edit file2.txt
  788. edit file3.txt
  789. let f2 = readfile(g:MRU_File, '', 2)
  790. edit def.c
  791. let f3 = readfile(g:MRU_File, '', 2)
  792. let g:MRU_Exclude_Files=''
  793. edit file1.txt
  794. let f4 = readfile(g:MRU_File, '', 2)
  795. if f1[1] =~# 'abc.c' && f2[1] =~# 'abc.c' && f3[1] =~# 'def.c' &&
  796. \ f4[1] =~# 'file1.txt'
  797. call LogResult(test_name, 'pass')
  798. else
  799. call LogResult(test_name, 'FAIL')
  800. endif
  801. endfunc
  802. " ==========================================================================
  803. " Test32
  804. " If the MRU window is open, when adding a file name to the list, the MRU
  805. " window should be refreshed.
  806. " ==========================================================================
  807. func Test_32()
  808. let test_name = 'test32'
  809. enew | only
  810. MRU
  811. wincmd p
  812. edit abc.c
  813. wincmd p
  814. let s1 = getline(1)
  815. wincmd p
  816. edit file1.txt
  817. wincmd p
  818. let s2 = getline(1)
  819. close
  820. if s1 =~# 'abc.c' && s2 =~# 'file1.txt'
  821. call LogResult(test_name, 'pass')
  822. else
  823. call LogResult(test_name, 'FAIL')
  824. endif
  825. endfunc
  826. " ==========================================================================
  827. " Test33
  828. " When MRU_Use_Current_Window is set, the MRU list should be displayed in
  829. " the current window.
  830. " Selecting a file from the MRU window should replace
  831. " the MRU buffer with the selected file.
  832. " ==========================================================================
  833. func Test_33()
  834. let test_name = 'test33'
  835. enew | only
  836. edit file1.txt
  837. let g:MRU_Use_Current_Window=1
  838. MRU
  839. if winnr('$') == 1 && @% == g:MRU_buffer_name
  840. call LogResult(test_name, 'pass')
  841. else
  842. call LogResult(test_name, 'FAIL')
  843. endif
  844. let g:MRU_Use_Current_Window=0
  845. endfunc
  846. " ==========================================================================
  847. " Test34
  848. " When MRU_Use_Current_Window is set, selecting a file from the MRU window
  849. " should replace the MRU buffer with the selected file.
  850. " ==========================================================================
  851. func Test_34()
  852. let test_name = 'test34'
  853. enew | only
  854. let g:MRU_Use_Current_Window=1
  855. let w:marker=1
  856. MRU
  857. if winnr('$') == 1 && w:marker && @% == g:MRU_buffer_name
  858. call search('file2.txt')
  859. exe "normal \<Enter>"
  860. if winnr('$') == 1 && w:marker && @% == 'file2.txt'
  861. call LogResult(test_name, 'pass')
  862. else
  863. call LogResult(test_name, 'FAIL')
  864. endif
  865. else
  866. call LogResult(test_name, 'FAIL')
  867. endif
  868. unlet w:marker
  869. let g:MRU_Use_Current_Window=0
  870. endfunc
  871. " ==========================================================================
  872. " Test35
  873. " When MRU_Use_Current_Window is set, if the current buffer has unsaved
  874. " changes, then the MRU window should be opened in a split window
  875. " ==========================================================================
  876. func Test_35()
  877. let test_name = 'test35'
  878. enew | only
  879. let g:MRU_Use_Current_Window=1
  880. set modified
  881. MRU
  882. if winnr('$') == 2 && winnr() == 2 && @% == g:MRU_buffer_name
  883. call LogResult(test_name, 'pass')
  884. else
  885. call LogResult(test_name, 'FAIL')
  886. endif
  887. close
  888. set nomodified
  889. let g:MRU_Use_Current_Window=0
  890. enew | only
  891. endfunc
  892. " ==========================================================================
  893. " Test36
  894. " When MRU_Auto_Close is not set, the MRU window should not automatically
  895. " close when a file is selected. The MRU window should be kept open.
  896. " ==========================================================================
  897. func Test_36()
  898. let test_name = 'test36'
  899. enew | only
  900. let g:MRU_Auto_Close=0
  901. new
  902. MRU
  903. call search('file1.txt')
  904. exe "normal \<Enter>"
  905. 2wincmd w
  906. MRU
  907. call search('file2.txt')
  908. exe "normal \<Enter>"
  909. if winnr('$') == 3 &&
  910. \ bufwinnr('file1.txt') == 1 &&
  911. \ bufwinnr('file2.txt') == 2 &&
  912. \ bufwinnr(g:MRU_buffer_name) == 3
  913. call LogResult(test_name, 'pass')
  914. else
  915. call LogResult(test_name, 'FAIL')
  916. endif
  917. wincmd b
  918. close
  919. let g:MRU_Auto_Close=1
  920. only
  921. endfunc
  922. " ==========================================================================
  923. " Test37
  924. " When MRU_Open_File_Use_Tabs is set, a selected file should be opened in a
  925. " tab. If the file is already opened in a tab, then the focus should be moved
  926. " to that tab.
  927. " ==========================================================================
  928. func Test_37()
  929. let test_name = 'test37'
  930. enew | only
  931. let g:MRU_Open_File_Use_Tabs=1
  932. edit file1.txt
  933. MRU
  934. call search('file2.txt')
  935. exe "normal \<Enter>"
  936. MRU
  937. call search('file3.txt')
  938. exe "normal \<Enter>"
  939. MRU file1.txt
  940. let t1 = tabpagenr()
  941. MRU
  942. call search('file2.txt')
  943. exe "normal \<Enter>"
  944. let t2 = tabpagenr()
  945. MRU
  946. call search('file3.txt')
  947. exe "normal \<Enter>"
  948. let t3 = tabpagenr()
  949. tabonly | enew
  950. if t1 == 1 && t2 == 2 && t3 == 3
  951. call LogResult(test_name, 'pass')
  952. else
  953. call LogResult(test_name, 'FAIL')
  954. endif
  955. let g:MRU_Open_File_Use_Tabs=0
  956. endfunc
  957. " ==========================================================================
  958. " Test38
  959. " If the MRU_Window_Open_Always is set to 0, when the MRU command finds a
  960. " single matching file name, then it should open the MRU window. If this
  961. " variable is set to 1, then the file should be opened without opening the MRU
  962. " window.
  963. " ==========================================================================
  964. func Test_38()
  965. let test_name = 'test38'
  966. enew | only
  967. edit file3.txt
  968. enew
  969. let g:MRU_Window_Open_Always=1
  970. MRU file3.txt
  971. if winnr('$') == 2 &&
  972. \ bufwinnr(g:MRU_buffer_name) == 2
  973. let test_result = 'pass'
  974. else
  975. let test_result = 'FAIL'
  976. endif
  977. close
  978. enew | only
  979. if test_result == 'pass'
  980. let g:MRU_Window_Open_Always=0
  981. MRU file3.txt
  982. if winnr('$') == 1 &&
  983. \ bufwinnr('file3.txt') == 1
  984. let test_result = 'pass'
  985. else
  986. let test_result = 'FAIL'
  987. endif
  988. endif
  989. let g:MRU_Window_Open_Always=0
  990. if test_result == 'pass'
  991. call LogResult(test_name, 'pass')
  992. else
  993. call LogResult(test_name, 'FAIL')
  994. endif
  995. endfunc
  996. " ==========================================================================
  997. " Test39
  998. " If the current tabpage is empty, then pressing 't' in the MRU window
  999. " should open the file in the current tabpage.
  1000. " ==========================================================================
  1001. func Test_39()
  1002. let test_name = 'test39'
  1003. enew | only | tabonly
  1004. tabnew
  1005. tabnew
  1006. tabnext 2
  1007. MRU
  1008. call search('file2.txt')
  1009. normal t
  1010. if fnamemodify(@%, ':p:t') ==# 'file2.txt' && tabpagenr() == 2
  1011. call LogResult(test_name, 'pass')
  1012. else
  1013. call LogResult(test_name, 'FAIL')
  1014. call LogResult(test_name, "file = " . fnamemodify(@%, ':p:t'))
  1015. call LogResult(test_name, "tab page = " . tabpagenr())
  1016. endif
  1017. tabonly
  1018. endfunc
  1019. " ==========================================================================
  1020. " Test40
  1021. " Pressing 'd' in the MRU window should delete the file under the cursor
  1022. " from the MRU list
  1023. " ==========================================================================
  1024. func Test_40()
  1025. let test_name = 'test40'
  1026. edit file2.txt
  1027. MRU
  1028. call search('file2.txt')
  1029. normal d
  1030. close
  1031. let l = readfile(g:MRU_File)
  1032. if match(l, 'file2.txt') == -1
  1033. call LogResult(test_name, 'pass')
  1034. else
  1035. call LogResult(test_name, 'FAIL')
  1036. endif
  1037. endfunc
  1038. " ==========================================================================
  1039. " Test41
  1040. " Running the :vimgrep command should not add the files to the MRU list
  1041. " ==========================================================================
  1042. func Test_41()
  1043. let test_name = 'test41'
  1044. call writefile(['bright'], 'dummy1.txt')
  1045. call writefile(['bright'], 'dummy2.txt')
  1046. vimgrep /bright/j dummy*
  1047. let l = readfile(g:MRU_File)
  1048. if match(l, 'dummy') == -1
  1049. call LogResult(test_name, 'pass')
  1050. else
  1051. call LogResult(test_name, 'FAIL')
  1052. endif
  1053. call delete('dummy1.txt')
  1054. call delete('dummy2.txt')
  1055. endfunc
  1056. " ==========================================================================
  1057. " Test42
  1058. " Using a command modifier with the MRU command to open the MRU window
  1059. " ==========================================================================
  1060. func Test_42()
  1061. if v:version < 800
  1062. " The <mods> command modifier is supported only by Vim 8.0 and above
  1063. return
  1064. endif
  1065. let test_name = 'test42'
  1066. enew | only
  1067. topleft MRU
  1068. if winnr() == 1 && winnr('$') == 2
  1069. call LogResult(test_name, 'pass')
  1070. else
  1071. call LogResult(test_name, 'FAIL')
  1072. endif
  1073. enew | only
  1074. botright MRU
  1075. if winnr() == 2 && winnr('$') == 2
  1076. call LogResult(test_name, 'pass')
  1077. else
  1078. call LogResult(test_name, 'FAIL')
  1079. endif
  1080. enew | only
  1081. botright MRU
  1082. if winnr() == 2 && winnr('$') == 2
  1083. call LogResult(test_name, 'pass')
  1084. else
  1085. call LogResult(test_name, 'FAIL')
  1086. endif
  1087. enew | only
  1088. endfunc
  1089. " ==========================================================================
  1090. " Test43
  1091. " Opening a file using the MRU command should jump to the window containing
  1092. " the file (if it is already opened).
  1093. " ==========================================================================
  1094. func Test_43()
  1095. let test_name = 'test43'
  1096. only
  1097. edit file3.txt
  1098. below split file2.txt
  1099. below split file1.txt
  1100. wincmd t
  1101. MRU file1.txt
  1102. if winnr() != 3 || fnamemodify(@%, ':p:t') !=# 'file1.txt'
  1103. call LogResult(test_name, 'FAIL (1)')
  1104. else
  1105. MRU file2.txt
  1106. if winnr() != 2 && fnamemodify(@%, ':p:t') !=# 'file2.txt'
  1107. call LogResult(test_name, 'FAIL (2)')
  1108. else
  1109. MRU file3.txt
  1110. if winnr() != 1 && fnamemodify(@%, ':p:t') !=# 'file3.txt'
  1111. call LogResult(test_name, 'FAIL (3)')
  1112. else
  1113. call LogResult(test_name, 'pass')
  1114. endif
  1115. endif
  1116. endif
  1117. enew | only
  1118. endfunc
  1119. " ==========================================================================
  1120. " Test44
  1121. " Opening a file using the MRU command should open the file in a new window if
  1122. " the current buffer has unsaved changes.
  1123. " ==========================================================================
  1124. func Test_44()
  1125. let test_name = 'test44'
  1126. only
  1127. set modified
  1128. MRU file2.txt
  1129. if winnr('$') == 2 && winnr() == 1 &&
  1130. \ fnamemodify(@%, ':p:t') ==# 'file2.txt'
  1131. call LogResult(test_name, 'pass')
  1132. else
  1133. call LogResult(test_name, 'FAIL')
  1134. endif
  1135. close
  1136. set nomodified
  1137. endfunc
  1138. " ==========================================================================
  1139. " Test45
  1140. " Opening a file from the MRU window using 'v' should open the file in a new
  1141. " window if the current buffer has unsaved changes.
  1142. " ==========================================================================
  1143. func Test_45()
  1144. let test_name = 'test45'
  1145. only
  1146. set modified
  1147. MRU
  1148. call search('file3.txt')
  1149. normal v
  1150. if winnr('$') == 2 && winnr() == 1
  1151. \ && fnamemodify(@%, ':p:t') ==# 'file3.txt'
  1152. \ && &readonly
  1153. call LogResult(test_name, 'pass')
  1154. else
  1155. call LogResult(test_name, 'FAIL')
  1156. endif
  1157. close
  1158. set nomodified
  1159. endfunc
  1160. " ==========================================================================
  1161. " Test46
  1162. " Specify a count to the :MRU command to set the MRU window height/width
  1163. " ==========================================================================
  1164. func Test_46()
  1165. let test_name = 'test46'
  1166. only
  1167. " default height is 8
  1168. MRU
  1169. if winnr() != 2 || winheight(0) != 8
  1170. call LogResult(test_name, 'FAIL (1)')
  1171. return
  1172. endif
  1173. close
  1174. " use a specific height value
  1175. 15MRU
  1176. if winnr() != 2 || winheight(0) != 15
  1177. call LogResult(test_name, 'FAIL (2)')
  1178. return
  1179. endif
  1180. close
  1181. if v:version >= 800
  1182. " use a specific height value with a command modifier
  1183. topleft 12MRU
  1184. if winnr() != 1 || winheight(0) != 12
  1185. call LogResult(test_name, 'FAIL (3)')
  1186. return
  1187. endif
  1188. close
  1189. " check for the width (leftmost window)
  1190. vertical topleft 20MRU
  1191. if winnr() != 1 || winwidth(0) != 20
  1192. call LogResult(test_name, 'FAIL (4)')
  1193. return
  1194. endif
  1195. close
  1196. " check for the width (rightmost window)
  1197. vertical botright 25MRU
  1198. if winnr() != 2 || winwidth(0) != 25
  1199. call LogResult(test_name, 'FAIL (5)')
  1200. return
  1201. endif
  1202. close
  1203. endif
  1204. call LogResult(test_name, 'pass')
  1205. endfunc
  1206. " ==========================================================================
  1207. " Create the files used by the tests
  1208. call writefile(['MRU test file1'], 'file1.txt')
  1209. call writefile(['MRU test file2'], 'file2.txt')
  1210. call writefile(['MRU test file3'], 'file3.txt')
  1211. call writefile(['#include <stdio.h', 'int main(){}'], 'abc.c')
  1212. call writefile(['#include <stdlib.h', 'int main(){}'], 'def.c')
  1213. " Remove the results from the previous test runs
  1214. call delete('results.txt')
  1215. call delete(g:MRU_File)
  1216. " Generate a sorted list of Test_ functions to run
  1217. redir @q
  1218. silent function /^Test_
  1219. redir END
  1220. let s:tests = split(substitute(@q, '\(function\) \(\k*()\)', '\2', 'g'))
  1221. " Run the tests
  1222. set nomore
  1223. set debug=beep
  1224. for one_test in sort(s:tests)
  1225. exe 'call ' . one_test
  1226. endfor
  1227. set more
  1228. " TODO:
  1229. " 1. When the MRU list is modified, the MRU menu should be refreshed.
  1230. " 2. Try to jump to an already open file from the MRU window and using the
  1231. " MRU command.
  1232. " 3. Open an existing file but not present in the MRU list using the MRU command
  1233. " Cleanup the files used by the tests
  1234. call delete('file1.txt')
  1235. call delete('file2.txt')
  1236. call delete('file3.txt')
  1237. call delete('abc.c')
  1238. call delete('def.c')
  1239. call delete(g:MRU_File)
  1240. " End of unit test execution
  1241. qall
  1242. " vim: shiftwidth=2 sts=2 expandtab