unit_tests.vim 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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(bufname('%'), ':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(bufname('%'), ':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(bufname('%'), ':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(bufname('%'), ':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(bufname('%'), ':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(bufname('%'), ':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 veritcally 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 = bufname('%')
  211. wincmd h
  212. let b2 = bufname('%')
  213. wincmd l
  214. let b3 = bufname('%')
  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 = bufname('%')
  235. if winnr('$') == 2 && &previewwindow && bufname('%') =~# '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. tabnew
  251. tabnew
  252. tabnew
  253. tabfirst
  254. MRU
  255. call search('file3.txt')
  256. normal t
  257. if fnamemodify(bufname('%'), ':p:t') ==# 'file3.txt' && tabpagenr() == 5
  258. call LogResult(test_name, 'pass')
  259. else
  260. call LogResult(test_name, 'FAIL')
  261. call LogResult(test_name, "file = " . fnamemodify(bufname('%'), ':p:t'))
  262. call LogResult(test_name, "tab page = " . tabpagenr())
  263. endif
  264. tabonly
  265. endfunc
  266. " ==========================================================================
  267. " Test12
  268. " The 'q' command closes the MRU window
  269. " ==========================================================================
  270. func Test_12()
  271. let test_name = 'test12'
  272. enew | only
  273. MRU
  274. normal q
  275. if bufwinnr(g:MRU_buffer_name) == -1
  276. call LogResult(test_name, 'pass')
  277. else
  278. call LogResult(test_name, 'FAIL')
  279. endif
  280. endfunc
  281. " ==========================================================================
  282. " Test13
  283. " A selected file is opened in a new window if the previous window is a
  284. " preview window
  285. " ==========================================================================
  286. func Test_13()
  287. let test_name = 'test13'
  288. enew | only
  289. setlocal previewwindow
  290. MRU
  291. call search('file2.txt')
  292. exe "normal \<Enter>"
  293. if winnr() == 1 && winnr('$') == 2 &&
  294. \ &previewwindow == 0 &&
  295. \ fnamemodify(bufname('%'), ':p:t') ==# 'file2.txt'
  296. call LogResult(test_name, 'pass')
  297. else
  298. call LogResult(test_name, 'FAIL')
  299. endif
  300. " Close the preview window created by this test
  301. new
  302. only
  303. endfunc
  304. " ==========================================================================
  305. " Test14
  306. " A selected file is opened in a new window if the previous window contains
  307. " a special buffer (used by some other plugin)
  308. " ==========================================================================
  309. func Test_14()
  310. let test_name = 'test14'
  311. enew | only
  312. setlocal buftype=nofile
  313. MRU
  314. call search('file3.txt')
  315. exe "normal \<Enter>"
  316. if winnr() == 1 && winnr('$') == 2 &&
  317. \ &buftype == '' &&
  318. \ fnamemodify(bufname('%'), ':p:t') ==# 'file3.txt'
  319. call LogResult(test_name, 'pass')
  320. else
  321. call LogResult(test_name, 'FAIL')
  322. endif
  323. " Discard the special buffer
  324. enew
  325. endfunc
  326. " ==========================================================================
  327. " Test15
  328. " If a file selected using the 't' command is already opened in a tab,
  329. " then jump to that tab (instead of opening a new tab)
  330. " ==========================================================================
  331. func Test_15()
  332. let test_name = 'test15'
  333. enew | only
  334. " Open the test files in the middle window with empty windows at the top and
  335. " bottom
  336. edit file1.txt
  337. above new
  338. botright new
  339. tabedit file2.txt
  340. above new
  341. botright new
  342. tabedit file3.txt
  343. above new
  344. botright new
  345. tabfirst
  346. MRU
  347. call search('file3.txt')
  348. exe "normal t"
  349. if tabpagenr() != 3
  350. \ || fnamemodify(bufname('%'), ':p:t') !=# 'file3.txt'
  351. \ || winnr() != 2
  352. call LogResult(test_name, "FAIL (1)")
  353. else
  354. MRU
  355. call search('file1.txt')
  356. exe "normal t"
  357. if tabpagenr() != 1
  358. \ || fnamemodify(bufname('%'), ':p:t') !=# 'file1.txt'
  359. \ || winnr() != 2
  360. call LogResult(test_name, "FAIL (2)")
  361. else
  362. MRU
  363. call search('file2.txt')
  364. exe "normal t"
  365. if tabpagenr() != 2
  366. \ || fnamemodify(bufname('%'), ':p:t') !=# 'file2.txt'
  367. \ || winnr() != 2
  368. call LogResult(test_name, "FAIL (3)")
  369. else
  370. call LogResult(test_name, 'pass')
  371. endif
  372. endif
  373. endif
  374. " Close all the other tabs
  375. tabonly
  376. enew
  377. only
  378. endfunc
  379. " ==========================================================================
  380. " Test16
  381. " Open multiple files from the MRU window using the visual mode and by using a
  382. " count. Each file should be opened in a separate window.
  383. " ==========================================================================
  384. func Test_16()
  385. let test_name = 'test16'
  386. enew | only
  387. edit file3.txt
  388. edit file2.txt
  389. edit file1.txt
  390. enew
  391. MRU
  392. exe "normal 3\<Enter>"
  393. if winnr('$') == 3 &&
  394. \ bufwinnr('file3.txt') == 1 &&
  395. \ bufwinnr('file2.txt') == 2 &&
  396. \ bufwinnr('file1.txt') == 3
  397. let test_result = 'pass'
  398. else
  399. let test_result = 'FAIL'
  400. endif
  401. only | enew
  402. if test_result == 'pass'
  403. MRU
  404. exe "normal V2j\<Enter>"
  405. if winnr('$') == 3 &&
  406. \ bufwinnr('file1.txt') == 1 &&
  407. \ bufwinnr('file2.txt') == 2 &&
  408. \ bufwinnr('file3.txt') == 3
  409. let test_result = 'pass'
  410. else
  411. let test_result = 'FAIL'
  412. endif
  413. endif
  414. if test_result == 'pass'
  415. call LogResult(test_name, 'pass')
  416. else
  417. call LogResult(test_name, 'FAIL')
  418. endif
  419. endfunc
  420. " ==========================================================================
  421. " Test17
  422. " When the MRU list is updated, the MRU file also should updated.
  423. " ==========================================================================
  424. func Test_17()
  425. let test_name = 'test17'
  426. enew | only
  427. edit file1.txt
  428. let l = readfile(g:MRU_File)
  429. if l[1] =~# 'file1.txt'
  430. edit file2.txt
  431. let l = readfile(g:MRU_File)
  432. if l[1] =~# 'file2.txt'
  433. edit file3.txt
  434. let l = readfile(g:MRU_File)
  435. if l[1] =~# 'file3.txt'
  436. call LogResult(test_name, 'pass')
  437. else
  438. call LogResult(test_name, "FAIL (3)")
  439. endif
  440. else
  441. call LogResult(test_name, "FAIL (2)")
  442. endif
  443. else
  444. call LogResult(test_name, "FAIL (1)")
  445. endif
  446. endfunc
  447. " MRU_Test_Add_Files
  448. " Add the supplied List of files to the beginning of the MRU file
  449. func! s:MRU_Test_Add_Files(fnames)
  450. let l = readfile(g:MRU_File)
  451. call extend(l, a:fnames, 1)
  452. call writefile(l, g:MRU_File)
  453. endfunc
  454. " ==========================================================================
  455. " Test18
  456. " When the MRU file is updated by another Vim instance, the MRU plugin
  457. " should update the MRU list
  458. " ==========================================================================
  459. func Test_18()
  460. let test_name = 'test18'
  461. enew | only
  462. call s:MRU_Test_Add_Files(['/software/editors/vim',
  463. \ '/software/editors/emacs',
  464. \ '/software/editors/nano'])
  465. MRU
  466. if getline(1) ==# 'vim (/software/editors/vim)'
  467. \ && getline(2) ==# 'emacs (/software/editors/emacs)'
  468. \ && getline(3) ==# 'nano (/software/editors/nano)'
  469. call LogResult(test_name, 'pass')
  470. else
  471. call LogResult(test_name, 'FAIL')
  472. endif
  473. " Close the MRU window
  474. close
  475. endfunc
  476. " ==========================================================================
  477. " Test19
  478. " When the MRU file is updated by another Vim instance, the MRU file names
  479. " from the current instance should be merged with that list
  480. " ==========================================================================
  481. func Test_19()
  482. let test_name = 'test19'
  483. enew | only
  484. " Remove all the files from the MRU file
  485. let l = readfile(g:MRU_File)
  486. call remove(l, 1, -1)
  487. call writefile(l, g:MRU_File)
  488. edit file1.txt
  489. call s:MRU_Test_Add_Files(['/software/os/unix'])
  490. edit file2.txt
  491. call s:MRU_Test_Add_Files(['/software/os/windows'])
  492. edit file3.txt
  493. call s:MRU_Test_Add_Files(['/software/os/osx'])
  494. MRU
  495. if getline(1) ==# 'osx (/software/os/osx)'
  496. \ && getline(2) =~# 'file3.txt'
  497. \ && getline(3) ==# 'windows (/software/os/windows)'
  498. \ && getline(4) =~# 'file2.txt'
  499. \ && getline(5) ==# 'unix (/software/os/unix)'
  500. \ && getline(6) =~# 'file1.txt'
  501. call LogResult(test_name, 'pass')
  502. else
  503. call LogResult(test_name, 'FAIL')
  504. endif
  505. close
  506. endfunc
  507. " ==========================================================================
  508. " Test20
  509. " When the MRU list has more than g:MRU_Max_Entries, the list should be
  510. " trimmed. The last entries should be removed.
  511. " ==========================================================================
  512. func Test_20()
  513. let test_name = 'test20'
  514. enew | only
  515. " Create a MRU list with MRU_Max_Entries
  516. let flist = []
  517. for i in range(1, g:MRU_Max_Entries)
  518. let flist += ['/usr/share/mru_test/mru_file' . i . '.abc']
  519. endfor
  520. " Modify the MRU file to contain max entries
  521. let l = readfile(g:MRU_File)
  522. call remove(l, 1, -1)
  523. call extend(l, flist)
  524. call writefile(l, g:MRU_File)
  525. enew
  526. edit file1.txt
  527. let l = readfile(g:MRU_File)
  528. if len(l) == (g:MRU_Max_Entries + 1) &&
  529. \ l[g:MRU_Max_Entries] != '/usr/share/mru_test/mru_file9.abc'
  530. call LogResult(test_name, "FAIL (1)")
  531. else
  532. edit file2.txt
  533. let l = readfile(g:MRU_File)
  534. if len(l) == (g:MRU_Max_Entries + 1) &&
  535. \ l[g:MRU_Max_Entries] != '/usr/share/mru_test/mru_file8.abc'
  536. call LogResult(test_name, "FAIL (2)")
  537. else
  538. edit file3.txt
  539. let l = readfile(g:MRU_File)
  540. if len(l) == (g:MRU_Max_Entries + 1) &&
  541. \ l[g:MRU_Max_Entries] != '/usr/share/mru_test/mru_file7.abc'
  542. call LogResult(test_name, "FAIL (3)")
  543. else
  544. call LogResult(test_name, 'pass')
  545. endif
  546. endif
  547. endif
  548. endfunc
  549. " ==========================================================================
  550. " Test21
  551. " When an filename (already present in the MRU list) is specified to the MRU
  552. " command, it should edit the file.
  553. " ==========================================================================
  554. func Test_21()
  555. let test_name = 'test21'
  556. enew | only
  557. edit file1.txt
  558. edit file2.txt
  559. edit file3.txt
  560. enew
  561. MRU file2.txt
  562. if fnamemodify(bufname('%'), ':p:t') ==# 'file2.txt' && winnr('$') == 1
  563. call LogResult(test_name, 'pass')
  564. else
  565. call LogResult(test_name, 'FAIL')
  566. endif
  567. endfunc
  568. " ==========================================================================
  569. " Test22
  570. " When a pattern (matching multiple filenames) is specified to the MRU
  571. " command, then the MRU window should be opened with all the matching
  572. " filenames
  573. " ==========================================================================
  574. func Test_22()
  575. let test_name = 'test22'
  576. enew | only
  577. edit file1.txt
  578. edit file2.txt
  579. edit file3.txt
  580. only
  581. MRU file.*
  582. if bufname('%') != g:MRU_buffer_name
  583. call LogResult(test_name, 'FAIL')
  584. else
  585. let l = getline(1, "$")
  586. if l[0] =~# "file3.txt" && l[1] =~# "file2.txt" && l[2] =~# "file1.txt"
  587. call LogResult(test_name, 'pass')
  588. else
  589. call LogResult(test_name, 'FAIL')
  590. endif
  591. endif
  592. close
  593. endfunc
  594. " ==========================================================================
  595. " Test23
  596. " When a partial filename (matching multiple filenames) is specified to the
  597. " MRU command, then the MRU window should be opened with all the matching
  598. " filenames
  599. " ==========================================================================
  600. func Test_23()
  601. let test_name = 'test23'
  602. enew | only
  603. edit file1.txt
  604. edit file2.txt
  605. edit file3.txt
  606. only
  607. MRU file
  608. if bufname('%') != g:MRU_buffer_name
  609. call LogResult(test_name, 'FAIL')
  610. else
  611. let l = getline(1, "$")
  612. if l[0] =~# "file3.txt" && l[1] =~# "file2.txt" && l[2] =~# "file1.txt"
  613. call LogResult(test_name, 'pass')
  614. else
  615. call LogResult(test_name, 'FAIL')
  616. endif
  617. endif
  618. close
  619. endfunc
  620. " ==========================================================================
  621. " Test24
  622. " When a non-existing filename is specified to the MRU command, an error
  623. " message should be displayed.
  624. " ==========================================================================
  625. func Test_24()
  626. let test_name = 'test24'
  627. redir => msg
  628. MRU nonexistingfile.txt
  629. redir END
  630. if bufname('%') == g:MRU_buffer_name ||
  631. \ msg !~# "MRU file list doesn't contain files " .
  632. \ "matching nonexistingfile.txt"
  633. call LogResult(test_name, 'FAIL')
  634. else
  635. call LogResult(test_name, 'pass')
  636. endif
  637. endfunc
  638. " ==========================================================================
  639. " Test25
  640. " The MRU command should support filename completion. Supply a partial file
  641. " name to the MRU command and complete the filenames.
  642. " ==========================================================================
  643. func Test_25()
  644. let test_name = 'test25'
  645. enew | only
  646. edit file1.txt
  647. edit file2.txt
  648. edit file3.txt
  649. exe 'normal! :MRU file' . "\<C-A>" . "\<Home>let m='\<End>'\<CR>"
  650. let fnames = split(m)
  651. if fnames[1] =~# 'file3.txt' && fnames[2] =~# 'file2.txt' &&
  652. \ fnames[3] =~# 'file1.txt'
  653. call LogResult(test_name, 'pass')
  654. else
  655. call LogResult(test_name, 'FAIL')
  656. endif
  657. endfunc
  658. " ==========================================================================
  659. " Test26
  660. " When trying to complete filenames for the MRU command without specifying
  661. " any text should return the the entire MRU list.
  662. " ==========================================================================
  663. func Test_26()
  664. let test_name = 'test26'
  665. enew | only
  666. call delete(g:MRU_File)
  667. edit file1.txt
  668. edit file2.txt
  669. edit file3.txt
  670. exe 'normal! :MRU ' . "\<C-A>" . "\<Home>let m='\<End>'\<CR>"
  671. let fnames = split(m)
  672. if fnames[1] =~# 'file3.txt' && fnames[2] =~# 'file2.txt' &&
  673. \ fnames[3] =~# 'file1.txt'
  674. call LogResult(test_name, 'pass')
  675. else
  676. call LogResult(test_name, 'FAIL')
  677. endif
  678. endfunc
  679. " ==========================================================================
  680. " Test27
  681. " When the current file/buffer has unsaved changes, MRU should open a selected
  682. " file in a new window (if the 'hidden' option is not set)
  683. " ==========================================================================
  684. func Test_27()
  685. let test_name = 'test27'
  686. enew | only
  687. edit file1.txt
  688. edit file2.txt
  689. call append(line('$'), 'Temporary changes to buffer')
  690. MRU
  691. call search('file1.txt')
  692. exe "normal \<Enter>"
  693. if winnr() == 1 && winnr('$') == 2 &&
  694. \ fnamemodify(bufname('%'), ':p:t') ==# 'file1.txt'
  695. call LogResult(test_name, 'pass')
  696. else
  697. call LogResult(test_name, 'FAIL')
  698. endif
  699. close
  700. edit!
  701. endfunc
  702. " ==========================================================================
  703. " Test28
  704. " When the current file/buffer has unsaved changes and the 'hidden' option is
  705. " set, then MRU should open a selected file in the current window
  706. " ==========================================================================
  707. func Test_28()
  708. let test_name = 'test28'
  709. enew | only
  710. edit file2.txt
  711. edit file1.txt
  712. call append(line('$'), 'Temporary changes to buffer')
  713. set hidden
  714. MRU
  715. call search('file2.txt')
  716. exe "normal \<Enter>"
  717. if winnr('$') == 1 &&
  718. \ fnamemodify(bufname('%'), ':p:t') ==# 'file2.txt'
  719. call LogResult(test_name, 'pass')
  720. else
  721. call LogResult(test_name, 'FAIL')
  722. endif
  723. edit file1.txt
  724. edit!
  725. set nohidden
  726. %bw!
  727. endfunc
  728. " ==========================================================================
  729. " Test29
  730. " Every edited file is added to the top of the MRU list. If a file is already
  731. " present in the MRU list, then it is moved to the top of the list.
  732. " ==========================================================================
  733. func Test_29()
  734. let test_name = 'test29'
  735. enew | only
  736. edit file1.txt
  737. let f1 = readfile(g:MRU_File, '', 2)
  738. edit file2.txt
  739. let f2 = readfile(g:MRU_File, '', 2)
  740. edit file3.txt
  741. let f3 = readfile(g:MRU_File, '', 2)
  742. edit file1.txt
  743. let f4 = readfile(g:MRU_File, '', 2)
  744. if f1[1] =~# 'file1.txt' && f2[1] =~# 'file2.txt' && f3[1] =~# 'file3.txt' &&
  745. \ f4[1] =~# 'file1.txt'
  746. call LogResult(test_name, 'pass')
  747. else
  748. call LogResult(test_name, 'FAIL')
  749. endif
  750. endfunc
  751. " ==========================================================================
  752. " Test30
  753. " Only file names matching the regular expression in the MRU_Include_Files
  754. " variable should be added to the MRU list.
  755. " ==========================================================================
  756. func Test_30()
  757. let test_name = 'test30'
  758. enew | only
  759. edit file1.txt
  760. let g:MRU_Include_Files='\.c'
  761. edit abc.c
  762. let f1 = readfile(g:MRU_File, '', 2)
  763. edit file1.txt
  764. let f2 = readfile(g:MRU_File, '', 2)
  765. edit def.c
  766. let f3 = readfile(g:MRU_File, '', 2)
  767. if f1[1] =~# 'abc.c' && f2[1] =~# 'abc.c' && f3[1] =~# 'def.c'
  768. call LogResult(test_name, 'pass')
  769. else
  770. call LogResult(test_name, 'FAIL')
  771. endif
  772. let g:MRU_Include_Files=''
  773. endfunc
  774. " ==========================================================================
  775. " Test31
  776. " File names matching the regular expression in the MRU_Exclude_Files
  777. " variable should not be added to the MRU list.
  778. " ==========================================================================
  779. func Test_31()
  780. let test_name = 'test31'
  781. enew | only
  782. let g:MRU_Exclude_Files='\.txt'
  783. edit abc.c
  784. let f1 = readfile(g:MRU_File, '', 2)
  785. edit file1.txt
  786. edit file2.txt
  787. edit file3.txt
  788. let f2 = readfile(g:MRU_File, '', 2)
  789. edit def.c
  790. let f3 = readfile(g:MRU_File, '', 2)
  791. let g:MRU_Exclude_Files=''
  792. edit file1.txt
  793. let f4 = readfile(g:MRU_File, '', 2)
  794. if f1[1] =~# 'abc.c' && f2[1] =~# 'abc.c' && f3[1] =~# 'def.c' &&
  795. \ f4[1] =~# 'file1.txt'
  796. call LogResult(test_name, 'pass')
  797. else
  798. call LogResult(test_name, 'FAIL')
  799. endif
  800. endfunc
  801. " ==========================================================================
  802. " Test32
  803. " If the MRU window is open, when adding a file name to the list, the MRU
  804. " window should be refreshed.
  805. " ==========================================================================
  806. func Test_32()
  807. let test_name = 'test32'
  808. enew | only
  809. MRU
  810. wincmd p
  811. edit abc.c
  812. wincmd p
  813. let s1 = getline(1)
  814. wincmd p
  815. edit file1.txt
  816. wincmd p
  817. let s2 = getline(1)
  818. close
  819. if s1 =~# 'abc.c' && s2 =~# 'file1.txt'
  820. call LogResult(test_name, 'pass')
  821. else
  822. call LogResult(test_name, 'FAIL')
  823. endif
  824. endfunc
  825. " ==========================================================================
  826. " Test33
  827. " When MRU_Use_Current_Window is set, the MRU list should be displayed in
  828. " the current window.
  829. " Selecting a file from the MRU window should replace
  830. " the MRU buffer with the selected file.
  831. " ==========================================================================
  832. func Test_33()
  833. let test_name = 'test33'
  834. enew | only
  835. edit file1.txt
  836. let g:MRU_Use_Current_Window=1
  837. MRU
  838. if winnr('$') == 1 && bufname('%') == g:MRU_buffer_name
  839. call LogResult(test_name, 'pass')
  840. else
  841. call LogResult(test_name, 'FAIL')
  842. endif
  843. let g:MRU_Use_Current_Window=0
  844. endfunc
  845. " ==========================================================================
  846. " Test34
  847. " When MRU_Use_Current_Window is set, selecting a file from the MRU window
  848. " should replace the MRU buffer with the selected file.
  849. " ==========================================================================
  850. func Test_34()
  851. let test_name = 'test34'
  852. enew | only
  853. let g:MRU_Use_Current_Window=1
  854. let w:marker=1
  855. MRU
  856. if winnr('$') == 1 && w:marker && bufname('%') == g:MRU_buffer_name
  857. call search('file2.txt')
  858. exe "normal \<Enter>"
  859. if winnr('$') == 1 && w:marker && bufname('%') == 'file2.txt'
  860. call LogResult(test_name, 'pass')
  861. else
  862. call LogResult(test_name, 'FAIL')
  863. endif
  864. else
  865. call LogResult(test_name, 'FAIL')
  866. endif
  867. unlet w:marker
  868. let g:MRU_Use_Current_Window=0
  869. endfunc
  870. " ==========================================================================
  871. " Test35
  872. " When MRU_Auto_Close is not set, the MRU window should not automatically
  873. " close when a file is selected. The MRU window should be kept open.
  874. " ==========================================================================
  875. func Test_35()
  876. let test_name = 'test35'
  877. enew | only
  878. let g:MRU_Auto_Close=0
  879. new
  880. MRU
  881. call search('file1.txt')
  882. exe "normal \<Enter>"
  883. 2wincmd w
  884. MRU
  885. call search('file2.txt')
  886. exe "normal \<Enter>"
  887. if winnr('$') == 3 &&
  888. \ bufwinnr('file1.txt') == 1 &&
  889. \ bufwinnr('file2.txt') == 2 &&
  890. \ bufwinnr(g:MRU_buffer_name) == 3
  891. call LogResult(test_name, 'pass')
  892. else
  893. call LogResult(test_name, 'FAIL')
  894. endif
  895. wincmd b
  896. close
  897. let g:MRU_Auto_Close=1
  898. only
  899. endfunc
  900. " ==========================================================================
  901. " Test36
  902. " When MRU_Open_File_Use_Tabs is set, a selected file should be opened in a
  903. " tab. If the file is already opened in a tab, then the focus should be moved
  904. " to that tab.
  905. " ==========================================================================
  906. func Test_36()
  907. let test_name = 'test36'
  908. enew | only
  909. let g:MRU_Open_File_Use_Tabs=1
  910. edit file1.txt
  911. MRU
  912. call search('file2.txt')
  913. exe "normal \<Enter>"
  914. MRU
  915. call search('file3.txt')
  916. exe "normal \<Enter>"
  917. MRU file1.txt
  918. let t1 = tabpagenr()
  919. MRU
  920. call search('file2.txt')
  921. exe "normal \<Enter>"
  922. let t2 = tabpagenr()
  923. MRU
  924. call search('file3.txt')
  925. exe "normal \<Enter>"
  926. let t3 = tabpagenr()
  927. tabonly | enew
  928. if t1 == 1 && t2 == 2 && t3 == 3
  929. call LogResult(test_name, 'pass')
  930. else
  931. call LogResult(test_name, 'FAIL')
  932. endif
  933. let g:MRU_Open_File_Use_Tabs=0
  934. endfunc
  935. " ==========================================================================
  936. " Test37
  937. " If the MRU_Window_Open_Always is set to 0, when the MRU command finds a
  938. " single matching file name, then it should open the MRU window. If this
  939. " variable is set to 1, then the file should be opened without opening the MRU
  940. " window.
  941. " ==========================================================================
  942. func Test_37()
  943. let test_name = 'test37'
  944. enew | only
  945. edit file3.txt
  946. enew
  947. let g:MRU_Window_Open_Always=1
  948. MRU file3.txt
  949. if winnr('$') == 2 &&
  950. \ bufwinnr(g:MRU_buffer_name) == 2
  951. let test_result = 'pass'
  952. else
  953. let test_result = 'FAIL'
  954. endif
  955. close
  956. enew | only
  957. if test_result == 'pass'
  958. let g:MRU_Window_Open_Always=0
  959. MRU file3.txt
  960. if winnr('$') == 1 &&
  961. \ bufwinnr('file3.txt') == 1
  962. let test_result = 'pass'
  963. else
  964. let test_result = 'FAIL'
  965. endif
  966. endif
  967. let g:MRU_Window_Open_Always=0
  968. if test_result == 'pass'
  969. call LogResult(test_name, 'pass')
  970. else
  971. call LogResult(test_name, 'FAIL')
  972. endif
  973. endfunc
  974. " ==========================================================================
  975. " Create the files used by the tests
  976. call writefile(['MRU test file1'], 'file1.txt')
  977. call writefile(['MRU test file2'], 'file2.txt')
  978. call writefile(['MRU test file3'], 'file3.txt')
  979. call writefile(['#include <stdio.h', 'int main(){}'], 'abc.c')
  980. call writefile(['#include <stdlib.h', 'int main(){}'], 'def.c')
  981. " Remove the results from the previous test runs
  982. call delete('results.txt')
  983. call delete(g:MRU_File)
  984. " Generate a sorted list of Test_ functions to run
  985. redir @q
  986. silent function /^Test_
  987. redir END
  988. let s:tests = split(substitute(@q, '\(function\) \(\k*()\)', '\2', 'g'))
  989. " Run the tests
  990. for one_test in sort(s:tests)
  991. exe 'call ' . one_test
  992. endfor
  993. " TODO:
  994. " 1. When the MRU list is modified, the MRU menu should be refreshed.
  995. " 2. Lock and Unlock the MRU list.
  996. " 3. Try to jump to an already open file from the MRU window and using the
  997. " MRU command.
  998. " 4. Open an existing file but not present in the MRU list using the MRU command
  999. " 5. Split open a file in readonly mode.
  1000. " Cleanup the files used by the tests
  1001. call delete('file1.txt')
  1002. call delete('file2.txt')
  1003. call delete('file3.txt')
  1004. call delete('abc.c')
  1005. call delete('def.c')
  1006. call delete(g:MRU_File)
  1007. " End of unit test execution
  1008. qall
  1009. " vim: shiftwidth=2 sts=2 expandtab