unit_tests.vim 44 KB

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