unit_tests.vim 42 KB

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