unit_tests.vim 35 KB

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