vimsupport_test.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. # Copyright (C) 2015 YouCompleteMe contributors
  2. #
  3. # This file is part of YouCompleteMe.
  4. #
  5. # YouCompleteMe is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # YouCompleteMe is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  17. from ycm.test_utils import MockVimModule, MockVimCommand
  18. MockVimModule()
  19. from ycm import vimsupport
  20. from nose.tools import eq_
  21. from hamcrest import assert_that, calling, raises, none
  22. from mock import MagicMock, call, patch
  23. import os
  24. def ReplaceChunk_SingleLine_Repl_1_test():
  25. # Replace with longer range
  26. # 12345678901234567
  27. result_buffer = [ "This is a string" ]
  28. start, end = _BuildLocations( 1, 1, 1, 5 )
  29. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  30. end,
  31. 'How long',
  32. 0,
  33. 0,
  34. result_buffer )
  35. eq_( [ "How long is a string" ], result_buffer )
  36. eq_( line_offset, 0 )
  37. eq_( char_offset, 4 )
  38. # and replace again, using delta
  39. start, end = _BuildLocations( 1, 10, 1, 11 )
  40. ( new_line_offset, new_char_offset ) = vimsupport.ReplaceChunk(
  41. start,
  42. end,
  43. ' piece of ',
  44. line_offset,
  45. char_offset,
  46. result_buffer )
  47. line_offset += new_line_offset
  48. char_offset += new_char_offset
  49. eq_( [ 'How long is a piece of string' ], result_buffer )
  50. eq_( new_line_offset, 0 )
  51. eq_( new_char_offset, 9 )
  52. eq_( line_offset, 0 )
  53. eq_( char_offset, 13 )
  54. # and once more, for luck
  55. start, end = _BuildLocations( 1, 11, 1, 17 )
  56. ( new_line_offset, new_char_offset ) = vimsupport.ReplaceChunk(
  57. start,
  58. end,
  59. 'pie',
  60. line_offset,
  61. char_offset,
  62. result_buffer )
  63. line_offset += new_line_offset
  64. char_offset += new_char_offset
  65. eq_( ['How long is a piece of pie' ], result_buffer )
  66. eq_( new_line_offset, 0 )
  67. eq_( new_char_offset, -3 )
  68. eq_( line_offset, 0 )
  69. eq_( char_offset, 10 )
  70. def ReplaceChunk_SingleLine_Repl_2_test():
  71. # Replace with shorter range
  72. # 12345678901234567
  73. result_buffer = [ "This is a string" ]
  74. start, end = _BuildLocations( 1, 11, 1, 17 )
  75. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  76. end,
  77. 'test',
  78. 0,
  79. 0,
  80. result_buffer )
  81. eq_( [ "This is a test" ], result_buffer )
  82. eq_( line_offset, 0 )
  83. eq_( char_offset, -2 )
  84. def ReplaceChunk_SingleLine_Repl_3_test():
  85. # Replace with equal range
  86. # 12345678901234567
  87. result_buffer = [ "This is a string" ]
  88. start, end = _BuildLocations( 1, 6, 1, 8 )
  89. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  90. end,
  91. 'be',
  92. 0,
  93. 0,
  94. result_buffer )
  95. eq_( [ "This be a string" ], result_buffer )
  96. eq_( line_offset, 0 )
  97. eq_( char_offset, 0 )
  98. def ReplaceChunk_SingleLine_Add_1_test():
  99. # Insert at start
  100. result_buffer = [ "is a string" ]
  101. start, end = _BuildLocations( 1, 1, 1, 1 )
  102. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  103. end,
  104. 'This ',
  105. 0,
  106. 0,
  107. result_buffer )
  108. eq_( [ "This is a string" ], result_buffer )
  109. eq_( line_offset, 0 )
  110. eq_( char_offset, 5 )
  111. def ReplaceChunk_SingleLine_Add_2_test():
  112. # Insert at end
  113. result_buffer = [ "This is a " ]
  114. start, end = _BuildLocations( 1, 11, 1, 11 )
  115. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  116. end,
  117. 'string',
  118. 0,
  119. 0,
  120. result_buffer )
  121. eq_( [ "This is a string" ], result_buffer )
  122. eq_( line_offset, 0 )
  123. eq_( char_offset, 6 )
  124. def ReplaceChunk_SingleLine_Add_3_test():
  125. # Insert in the middle
  126. result_buffer = [ "This is a string" ]
  127. start, end = _BuildLocations( 1, 8, 1, 8 )
  128. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  129. end,
  130. ' not',
  131. 0,
  132. 0,
  133. result_buffer )
  134. eq_( [ "This is not a string" ], result_buffer )
  135. eq_( line_offset, 0 )
  136. eq_( char_offset, 4 )
  137. def ReplaceChunk_SingleLine_Del_1_test():
  138. # Delete from start
  139. result_buffer = [ "This is a string" ]
  140. start, end = _BuildLocations( 1, 1, 1, 6 )
  141. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  142. end,
  143. '',
  144. 0,
  145. 0,
  146. result_buffer )
  147. eq_( [ "is a string" ], result_buffer )
  148. eq_( line_offset, 0 )
  149. eq_( char_offset, -5 )
  150. def ReplaceChunk_SingleLine_Del_2_test():
  151. # Delete from end
  152. result_buffer = [ "This is a string" ]
  153. start, end = _BuildLocations( 1, 10, 1, 18 )
  154. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  155. end,
  156. '',
  157. 0,
  158. 0,
  159. result_buffer )
  160. eq_( [ "This is a" ], result_buffer )
  161. eq_( line_offset, 0 )
  162. eq_( char_offset, -8 )
  163. def ReplaceChunk_SingleLine_Del_3_test():
  164. # Delete from middle
  165. result_buffer = [ "This is not a string" ]
  166. start, end = _BuildLocations( 1, 9, 1, 13 )
  167. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  168. end,
  169. '',
  170. 0,
  171. 0,
  172. result_buffer )
  173. eq_( [ "This is a string" ], result_buffer )
  174. eq_( line_offset, 0 )
  175. eq_( char_offset, -4 )
  176. def ReplaceChunk_RemoveSingleLine_test():
  177. result_buffer = [ "aAa", "aBa", "aCa" ]
  178. start, end = _BuildLocations( 2, 1, 3, 1 )
  179. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, '',
  180. 0, 0, result_buffer )
  181. expected_buffer = [ "aAa", "aCa" ]
  182. eq_( expected_buffer, result_buffer )
  183. eq_( line_offset, -1 )
  184. eq_( char_offset, 0 )
  185. def ReplaceChunk_SingleToMultipleLines_test():
  186. result_buffer = [ "aAa",
  187. "aBa",
  188. "aCa" ]
  189. start, end = _BuildLocations( 2, 2, 2, 2 )
  190. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'Eb\nbF',
  191. 0, 0, result_buffer )
  192. expected_buffer = [ "aAa",
  193. "aEb",
  194. "bFBa",
  195. "aCa" ]
  196. eq_( expected_buffer, result_buffer )
  197. eq_( line_offset, 1 )
  198. eq_( char_offset, 1 )
  199. # now make another change to the "2nd" line
  200. start, end = _BuildLocations( 2, 3, 2, 4 )
  201. ( new_line_offset, new_char_offset ) = vimsupport.ReplaceChunk(
  202. start,
  203. end,
  204. 'cccc',
  205. line_offset,
  206. char_offset,
  207. result_buffer )
  208. line_offset += new_line_offset
  209. char_offset += new_char_offset
  210. eq_( [ "aAa", "aEb", "bFBcccc", "aCa" ], result_buffer )
  211. eq_( line_offset, 1 )
  212. eq_( char_offset, 4 )
  213. def ReplaceChunk_SingleToMultipleLines2_test():
  214. result_buffer = [ "aAa", "aBa", "aCa" ]
  215. start, end = _BuildLocations( 2, 2, 2, 2 )
  216. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  217. end,
  218. 'Eb\nbFb\nG',
  219. 0,
  220. 0,
  221. result_buffer )
  222. expected_buffer = [ "aAa", "aEb", "bFb", "GBa", "aCa" ]
  223. eq_( expected_buffer, result_buffer )
  224. eq_( line_offset, 2 )
  225. eq_( char_offset, 0 )
  226. def ReplaceChunk_SingleToMultipleLines3_test():
  227. result_buffer = [ "aAa", "aBa", "aCa" ]
  228. start, end = _BuildLocations( 2, 2, 2, 2 )
  229. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  230. end,
  231. 'Eb\nbFb\nbGb',
  232. 0,
  233. 0,
  234. result_buffer )
  235. expected_buffer = [ "aAa", "aEb", "bFb", "bGbBa", "aCa" ]
  236. eq_( expected_buffer, result_buffer )
  237. eq_( line_offset, 2 )
  238. eq_( char_offset, 2 )
  239. def ReplaceChunk_SingleToMultipleLinesReplace_test():
  240. result_buffer = [ "aAa", "aBa", "aCa" ]
  241. start, end = _BuildLocations( 1, 2, 1, 4 )
  242. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  243. end,
  244. 'Eb\nbFb\nbGb',
  245. 0,
  246. 0,
  247. result_buffer )
  248. expected_buffer = [ "aEb", "bFb", "bGb", "aBa", "aCa" ]
  249. eq_( expected_buffer, result_buffer )
  250. eq_( line_offset, 2 )
  251. eq_( char_offset, 0 )
  252. def ReplaceChunk_SingleToMultipleLinesReplace_2_test():
  253. result_buffer = [ "aAa",
  254. "aBa",
  255. "aCa" ]
  256. start, end = _BuildLocations( 1, 2, 1, 4 )
  257. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  258. end,
  259. 'Eb\nbFb\nbGb',
  260. 0,
  261. 0,
  262. result_buffer )
  263. expected_buffer = [ "aEb",
  264. "bFb",
  265. "bGb",
  266. "aBa",
  267. "aCa" ]
  268. eq_( expected_buffer, result_buffer )
  269. eq_( line_offset, 2 )
  270. eq_( char_offset, 0 )
  271. # now do a subsequent change (insert at end of line "1")
  272. start, end = _BuildLocations( 1, 4, 1, 4 )
  273. ( new_line_offset, new_char_offset ) = vimsupport.ReplaceChunk(
  274. start,
  275. end,
  276. 'cccc',
  277. line_offset,
  278. char_offset,
  279. result_buffer )
  280. line_offset += new_line_offset
  281. char_offset += new_char_offset
  282. eq_( [ "aEb",
  283. "bFb",
  284. "bGbcccc",
  285. "aBa",
  286. "aCa" ], result_buffer )
  287. eq_( line_offset, 2 )
  288. eq_( char_offset, 4 )
  289. def ReplaceChunk_MultipleLinesToSingleLine_test():
  290. result_buffer = [ "aAa", "aBa", "aCaaaa" ]
  291. start, end = _BuildLocations( 2, 2, 3, 2 )
  292. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'E',
  293. 0, 0, result_buffer )
  294. expected_buffer = [ "aAa", "aECaaaa" ]
  295. eq_( expected_buffer, result_buffer )
  296. eq_( line_offset, -1 )
  297. eq_( char_offset, 1 )
  298. # make another modification applying offsets
  299. start, end = _BuildLocations( 3, 3, 3, 4 )
  300. ( new_line_offset, new_char_offset ) = vimsupport.ReplaceChunk(
  301. start,
  302. end,
  303. 'cccc',
  304. line_offset,
  305. char_offset,
  306. result_buffer )
  307. line_offset += new_line_offset
  308. char_offset += new_char_offset
  309. eq_( [ "aAa", "aECccccaaa" ], result_buffer )
  310. eq_( line_offset, -1 )
  311. eq_( char_offset, 4 )
  312. # and another, for luck
  313. start, end = _BuildLocations( 3, 4, 3, 5 )
  314. ( new_line_offset, new_char_offset ) = vimsupport.ReplaceChunk(
  315. start,
  316. end,
  317. 'dd\ndd',
  318. line_offset,
  319. char_offset,
  320. result_buffer )
  321. line_offset += new_line_offset
  322. char_offset += new_char_offset
  323. eq_( [ "aAa", "aECccccdd", "ddaa" ], result_buffer )
  324. eq_( line_offset, 0 )
  325. eq_( char_offset, -2 )
  326. def ReplaceChunk_MultipleLinesToSameMultipleLines_test():
  327. result_buffer = [ "aAa", "aBa", "aCa", "aDe" ]
  328. start, end = _BuildLocations( 2, 2, 3, 2 )
  329. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'Eb\nbF',
  330. 0, 0, result_buffer )
  331. expected_buffer = [ "aAa", "aEb", "bFCa", "aDe" ]
  332. eq_( expected_buffer, result_buffer )
  333. eq_( line_offset, 0 )
  334. eq_( char_offset, 1 )
  335. def ReplaceChunk_MultipleLinesToMoreMultipleLines_test():
  336. result_buffer = [ "aAa", "aBa", "aCa", "aDe" ]
  337. start, end = _BuildLocations( 2, 2, 3, 2 )
  338. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  339. end,
  340. 'Eb\nbFb\nbG',
  341. 0,
  342. 0,
  343. result_buffer )
  344. expected_buffer = [ "aAa", "aEb", "bFb", "bGCa", "aDe" ]
  345. eq_( expected_buffer, result_buffer )
  346. eq_( line_offset, 1 )
  347. eq_( char_offset, 1 )
  348. def ReplaceChunk_MultipleLinesToLessMultipleLines_test():
  349. result_buffer = [ "aAa", "aBa", "aCa", "aDe" ]
  350. start, end = _BuildLocations( 1, 2, 3, 2 )
  351. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'Eb\nbF',
  352. 0, 0, result_buffer )
  353. expected_buffer = [ "aEb", "bFCa", "aDe" ]
  354. eq_( expected_buffer, result_buffer )
  355. eq_( line_offset, -1 )
  356. eq_( char_offset, 1 )
  357. def ReplaceChunk_MultipleLinesToEvenLessMultipleLines_test():
  358. result_buffer = [ "aAa", "aBa", "aCa", "aDe" ]
  359. start, end = _BuildLocations( 1, 2, 4, 2 )
  360. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'Eb\nbF',
  361. 0, 0, result_buffer )
  362. expected_buffer = [ "aEb", "bFDe" ]
  363. eq_( expected_buffer, result_buffer )
  364. eq_( line_offset, -2 )
  365. eq_( char_offset, 1 )
  366. def ReplaceChunk_SpanBufferEdge_test():
  367. result_buffer = [ "aAa", "aBa", "aCa" ]
  368. start, end = _BuildLocations( 1, 1, 1, 3 )
  369. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'bDb',
  370. 0, 0, result_buffer )
  371. expected_buffer = [ "bDba", "aBa", "aCa" ]
  372. eq_( expected_buffer, result_buffer )
  373. eq_( line_offset, 0 )
  374. eq_( char_offset, 1 )
  375. def ReplaceChunk_DeleteTextInLine_test():
  376. result_buffer = [ "aAa", "aBa", "aCa" ]
  377. start, end = _BuildLocations( 2, 2, 2, 3 )
  378. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, '',
  379. 0, 0, result_buffer )
  380. expected_buffer = [ "aAa", "aa", "aCa" ]
  381. eq_( expected_buffer, result_buffer )
  382. eq_( line_offset, 0 )
  383. eq_( char_offset, -1 )
  384. def ReplaceChunk_AddTextInLine_test():
  385. result_buffer = [ "aAa", "aBa", "aCa" ]
  386. start, end = _BuildLocations( 2, 2, 2, 2 )
  387. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'bDb',
  388. 0, 0, result_buffer )
  389. expected_buffer = [ "aAa", "abDbBa", "aCa" ]
  390. eq_( expected_buffer, result_buffer )
  391. eq_( line_offset, 0 )
  392. eq_( char_offset, 3 )
  393. def ReplaceChunk_ReplaceTextInLine_test():
  394. result_buffer = [ "aAa", "aBa", "aCa" ]
  395. start, end = _BuildLocations( 2, 2, 2, 3 )
  396. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'bDb',
  397. 0, 0, result_buffer )
  398. expected_buffer = [ "aAa", "abDba", "aCa" ]
  399. eq_( expected_buffer, result_buffer )
  400. eq_( line_offset, 0 )
  401. eq_( char_offset, 2 )
  402. def ReplaceChunk_SingleLineOffsetWorks_test():
  403. result_buffer = [ "aAa", "aBa", "aCa" ]
  404. start, end = _BuildLocations( 1, 1, 1, 2 )
  405. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'bDb',
  406. 1, 1, result_buffer )
  407. expected_buffer = [ "aAa", "abDba", "aCa" ]
  408. eq_( expected_buffer, result_buffer )
  409. eq_( line_offset, 0 )
  410. eq_( char_offset, 2 )
  411. def ReplaceChunk_SingleLineToMultipleLinesOffsetWorks_test():
  412. result_buffer = [ "aAa", "aBa", "aCa" ]
  413. start, end = _BuildLocations( 1, 1, 1, 2 )
  414. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'Db\nE',
  415. 1, 1, result_buffer )
  416. expected_buffer = [ "aAa", "aDb", "Ea", "aCa" ]
  417. eq_( expected_buffer, result_buffer )
  418. eq_( line_offset, 1 )
  419. eq_( char_offset, -1 )
  420. def ReplaceChunk_MultipleLinesToSingleLineOffsetWorks_test():
  421. result_buffer = [ "aAa", "aBa", "aCa" ]
  422. start, end = _BuildLocations( 1, 1, 2, 2 )
  423. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'bDb',
  424. 1, 1, result_buffer )
  425. expected_buffer = [ "aAa", "abDbCa" ]
  426. eq_( expected_buffer, result_buffer )
  427. eq_( line_offset, -1 )
  428. eq_( char_offset, 3 )
  429. def ReplaceChunk_MultipleLineOffsetWorks_test():
  430. result_buffer = [ "aAa", "aBa", "aCa" ]
  431. start, end = _BuildLocations( 3, 1, 4, 3 )
  432. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  433. end,
  434. 'bDb\nbEb\nbFb',
  435. -1,
  436. 1,
  437. result_buffer )
  438. expected_buffer = [ "aAa", "abDb", "bEb", "bFba" ]
  439. eq_( expected_buffer, result_buffer )
  440. eq_( line_offset, 1 )
  441. eq_( char_offset, 1 )
  442. def _BuildLocations( start_line, start_column, end_line, end_column ):
  443. return {
  444. 'line_num' : start_line,
  445. 'column_num': start_column,
  446. }, {
  447. 'line_num' : end_line,
  448. 'column_num': end_column,
  449. }
  450. def ReplaceChunksList_SortedChunks_test():
  451. chunks = [
  452. _BuildChunk( 1, 4, 1, 4, '('),
  453. _BuildChunk( 1, 11, 1, 11, ')' )
  454. ]
  455. result_buffer = [ "CT<10 >> 2> ct" ]
  456. vimsupport.ReplaceChunksList( chunks, result_buffer )
  457. expected_buffer = [ "CT<(10 >> 2)> ct" ]
  458. eq_( expected_buffer, result_buffer )
  459. def ReplaceChunksList_UnsortedChunks_test():
  460. chunks = [
  461. _BuildChunk( 1, 11, 1, 11, ')'),
  462. _BuildChunk( 1, 4, 1, 4, '(' )
  463. ]
  464. result_buffer = [ "CT<10 >> 2> ct" ]
  465. vimsupport.ReplaceChunksList( chunks, result_buffer )
  466. expected_buffer = [ "CT<(10 >> 2)> ct" ]
  467. eq_( expected_buffer, result_buffer )
  468. def _BuildChunk( start_line, start_column, end_line, end_column,
  469. replacement_text ):
  470. return {
  471. 'range': {
  472. 'start': {
  473. 'line_num': start_line,
  474. 'column_num': start_column,
  475. },
  476. 'end': {
  477. 'line_num': end_line,
  478. 'column_num': end_column,
  479. },
  480. },
  481. 'replacement_text': replacement_text
  482. }
  483. @patch( 'vim.command' )
  484. @patch( 'vim.current' )
  485. def WriteToPreviewWindow_test( vim_current, vim_command ):
  486. vim_current.window.options.__getitem__ = MagicMock( return_value = True )
  487. vimsupport.WriteToPreviewWindow( "test" )
  488. vim_command.assert_has_calls( [
  489. call( 'silent! pclose!' ),
  490. call( 'silent! pedit! _TEMP_FILE_' ),
  491. call( 'silent! wincmd P' ),
  492. call( 'silent! wincmd p' ) ] )
  493. vim_current.buffer.__setitem__.assert_called_with(
  494. slice( None, None, None ), [ 'test' ] )
  495. vim_current.buffer.options.__setitem__.assert_has_calls( [
  496. call( 'buftype', 'nofile' ),
  497. call( 'swapfile', False ),
  498. call( 'modifiable', False ),
  499. call( 'modified', False ),
  500. call( 'readonly', True ),
  501. ], any_order = True )
  502. @patch( 'vim.current' )
  503. def WriteToPreviewWindow_MultiLine_test( vim_current ):
  504. vim_current.window.options.__getitem__ = MagicMock( return_value = True )
  505. vimsupport.WriteToPreviewWindow( "test\ntest2" )
  506. vim_current.buffer.__setitem__.assert_called_with(
  507. slice( None, None, None ), [ 'test', 'test2' ] )
  508. @patch( 'vim.command' )
  509. @patch( 'vim.current' )
  510. def WriteToPreviewWindow_JumpFail_test( vim_current, vim_command ):
  511. vim_current.window.options.__getitem__ = MagicMock( return_value = False )
  512. vimsupport.WriteToPreviewWindow( "test" )
  513. vim_command.assert_has_calls( [
  514. call( 'silent! pclose!' ),
  515. call( 'silent! pedit! _TEMP_FILE_' ),
  516. call( 'silent! wincmd P' ),
  517. call( "echom 'test'" ),
  518. ] )
  519. vim_current.buffer.__setitem__.assert_not_called()
  520. vim_current.buffer.options.__setitem__.assert_not_called()
  521. @patch( 'vim.command' )
  522. @patch( 'vim.current' )
  523. def WriteToPreviewWindow_JumpFail_MultiLine_test( vim_current, vim_command ):
  524. vim_current.window.options.__getitem__ = MagicMock( return_value = False )
  525. vimsupport.WriteToPreviewWindow( "test\ntest2" )
  526. vim_command.assert_has_calls( [
  527. call( 'silent! pclose!' ),
  528. call( 'silent! pedit! _TEMP_FILE_' ),
  529. call( 'silent! wincmd P' ),
  530. call( "echom 'test'" ),
  531. call( "echom 'test2'" ),
  532. ] )
  533. vim_current.buffer.__setitem__.assert_not_called()
  534. vim_current.buffer.options.__setitem__.assert_not_called()
  535. def CheckFilename_test():
  536. assert_that(
  537. calling( vimsupport.CheckFilename ).with_args( None ),
  538. raises( RuntimeError, "'None' is not a valid filename" )
  539. )
  540. assert_that(
  541. calling( vimsupport.CheckFilename ).with_args( 'nonexistent_file' ),
  542. raises( RuntimeError,
  543. "filename 'nonexistent_file' cannot be opened. "
  544. "\[Errno 2\] No such file or directory: 'nonexistent_file'" )
  545. )
  546. assert_that( vimsupport.CheckFilename( __file__ ), none() )
  547. def BufferIsVisibleForFilename_test():
  548. buffers = [
  549. {
  550. 'number': 1,
  551. 'filename': os.path.realpath( 'visible_filename' ),
  552. 'window': 1
  553. },
  554. {
  555. 'number': 2,
  556. 'filename': os.path.realpath( 'hidden_filename' ),
  557. }
  558. ]
  559. with patch( 'vim.buffers', buffers ):
  560. eq_( vimsupport.BufferIsVisibleForFilename( 'visible_filename' ), True )
  561. eq_( vimsupport.BufferIsVisibleForFilename( 'hidden_filename' ), False )
  562. eq_( vimsupport.BufferIsVisibleForFilename( 'another_filename' ), False )
  563. @patch( 'vim.command', side_effect = MockVimCommand )
  564. def CloseBuffersForFilename_test( vim_command ):
  565. buffers = [
  566. {
  567. 'number': 2,
  568. 'filename': os.path.realpath( 'some_filename' ),
  569. },
  570. {
  571. 'number': 5,
  572. 'filename': os.path.realpath( 'some_filename' ),
  573. },
  574. {
  575. 'number': 1,
  576. 'filename': os.path.realpath( 'another_filename' )
  577. }
  578. ]
  579. with patch( 'vim.buffers', buffers ):
  580. vimsupport.CloseBuffersForFilename( 'some_filename' )
  581. vim_command.assert_has_calls( [
  582. call( 'silent! bwipeout! 2' ),
  583. call( 'silent! bwipeout! 5' )
  584. ], any_order = True )
  585. @patch( 'vim.command' )
  586. @patch( 'vim.current' )
  587. def OpenFilename_test( vim_current, vim_command ):
  588. # Options used to open a logfile
  589. options = {
  590. 'size': vimsupport.GetIntValue( '&previewheight' ),
  591. 'fix': True,
  592. 'watch': True,
  593. 'position': 'end'
  594. }
  595. vimsupport.OpenFilename( __file__, options )
  596. vim_command.assert_has_calls( [
  597. call( 'silent! 12split {0}'.format( __file__ ) ),
  598. call( "exec "
  599. "'au BufEnter <buffer> :silent! checktime {0}'".format( __file__ ) ),
  600. call( 'silent! normal G zz' ),
  601. call( 'silent! wincmd p' )
  602. ] )
  603. vim_current.buffer.options.__setitem__.assert_has_calls( [
  604. call( 'autoread', True ),
  605. ] )
  606. vim_current.window.options.__setitem__.assert_has_calls( [
  607. call( 'winfixheight', True )
  608. ] )