vimsupport_test.py 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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 ExtendedMock, 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. import json
  25. def ReplaceChunk_SingleLine_Repl_1_test():
  26. # Replace with longer range
  27. # 12345678901234567
  28. result_buffer = [ "This is a string" ]
  29. start, end = _BuildLocations( 1, 1, 1, 5 )
  30. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  31. end,
  32. 'How long',
  33. 0,
  34. 0,
  35. result_buffer )
  36. eq_( [ "How long is a string" ], result_buffer )
  37. eq_( line_offset, 0 )
  38. eq_( char_offset, 4 )
  39. # and replace again, using delta
  40. start, end = _BuildLocations( 1, 10, 1, 11 )
  41. ( new_line_offset, new_char_offset ) = vimsupport.ReplaceChunk(
  42. start,
  43. end,
  44. ' piece of ',
  45. line_offset,
  46. char_offset,
  47. result_buffer )
  48. line_offset += new_line_offset
  49. char_offset += new_char_offset
  50. eq_( [ 'How long is a piece of string' ], result_buffer )
  51. eq_( new_line_offset, 0 )
  52. eq_( new_char_offset, 9 )
  53. eq_( line_offset, 0 )
  54. eq_( char_offset, 13 )
  55. # and once more, for luck
  56. start, end = _BuildLocations( 1, 11, 1, 17 )
  57. ( new_line_offset, new_char_offset ) = vimsupport.ReplaceChunk(
  58. start,
  59. end,
  60. 'pie',
  61. line_offset,
  62. char_offset,
  63. result_buffer )
  64. line_offset += new_line_offset
  65. char_offset += new_char_offset
  66. eq_( ['How long is a piece of pie' ], result_buffer )
  67. eq_( new_line_offset, 0 )
  68. eq_( new_char_offset, -3 )
  69. eq_( line_offset, 0 )
  70. eq_( char_offset, 10 )
  71. def ReplaceChunk_SingleLine_Repl_2_test():
  72. # Replace with shorter range
  73. # 12345678901234567
  74. result_buffer = [ "This is a string" ]
  75. start, end = _BuildLocations( 1, 11, 1, 17 )
  76. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  77. end,
  78. 'test',
  79. 0,
  80. 0,
  81. result_buffer )
  82. eq_( [ "This is a test" ], result_buffer )
  83. eq_( line_offset, 0 )
  84. eq_( char_offset, -2 )
  85. def ReplaceChunk_SingleLine_Repl_3_test():
  86. # Replace with equal range
  87. # 12345678901234567
  88. result_buffer = [ "This is a string" ]
  89. start, end = _BuildLocations( 1, 6, 1, 8 )
  90. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  91. end,
  92. 'be',
  93. 0,
  94. 0,
  95. result_buffer )
  96. eq_( [ "This be a string" ], result_buffer )
  97. eq_( line_offset, 0 )
  98. eq_( char_offset, 0 )
  99. def ReplaceChunk_SingleLine_Add_1_test():
  100. # Insert at start
  101. result_buffer = [ "is a string" ]
  102. start, end = _BuildLocations( 1, 1, 1, 1 )
  103. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  104. end,
  105. 'This ',
  106. 0,
  107. 0,
  108. result_buffer )
  109. eq_( [ "This is a string" ], result_buffer )
  110. eq_( line_offset, 0 )
  111. eq_( char_offset, 5 )
  112. def ReplaceChunk_SingleLine_Add_2_test():
  113. # Insert at end
  114. result_buffer = [ "This is a " ]
  115. start, end = _BuildLocations( 1, 11, 1, 11 )
  116. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  117. end,
  118. 'string',
  119. 0,
  120. 0,
  121. result_buffer )
  122. eq_( [ "This is a string" ], result_buffer )
  123. eq_( line_offset, 0 )
  124. eq_( char_offset, 6 )
  125. def ReplaceChunk_SingleLine_Add_3_test():
  126. # Insert in the middle
  127. result_buffer = [ "This is a string" ]
  128. start, end = _BuildLocations( 1, 8, 1, 8 )
  129. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  130. end,
  131. ' not',
  132. 0,
  133. 0,
  134. result_buffer )
  135. eq_( [ "This is not a string" ], result_buffer )
  136. eq_( line_offset, 0 )
  137. eq_( char_offset, 4 )
  138. def ReplaceChunk_SingleLine_Del_1_test():
  139. # Delete from start
  140. result_buffer = [ "This is a string" ]
  141. start, end = _BuildLocations( 1, 1, 1, 6 )
  142. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  143. end,
  144. '',
  145. 0,
  146. 0,
  147. result_buffer )
  148. eq_( [ "is a string" ], result_buffer )
  149. eq_( line_offset, 0 )
  150. eq_( char_offset, -5 )
  151. def ReplaceChunk_SingleLine_Del_2_test():
  152. # Delete from end
  153. result_buffer = [ "This is a string" ]
  154. start, end = _BuildLocations( 1, 10, 1, 18 )
  155. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  156. end,
  157. '',
  158. 0,
  159. 0,
  160. result_buffer )
  161. eq_( [ "This is a" ], result_buffer )
  162. eq_( line_offset, 0 )
  163. eq_( char_offset, -8 )
  164. def ReplaceChunk_SingleLine_Del_3_test():
  165. # Delete from middle
  166. result_buffer = [ "This is not a string" ]
  167. start, end = _BuildLocations( 1, 9, 1, 13 )
  168. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  169. end,
  170. '',
  171. 0,
  172. 0,
  173. result_buffer )
  174. eq_( [ "This is a string" ], result_buffer )
  175. eq_( line_offset, 0 )
  176. eq_( char_offset, -4 )
  177. def ReplaceChunk_RemoveSingleLine_test():
  178. result_buffer = [ "aAa", "aBa", "aCa" ]
  179. start, end = _BuildLocations( 2, 1, 3, 1 )
  180. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, '',
  181. 0, 0, result_buffer )
  182. expected_buffer = [ "aAa", "aCa" ]
  183. eq_( expected_buffer, result_buffer )
  184. eq_( line_offset, -1 )
  185. eq_( char_offset, 0 )
  186. def ReplaceChunk_SingleToMultipleLines_test():
  187. result_buffer = [ "aAa",
  188. "aBa",
  189. "aCa" ]
  190. start, end = _BuildLocations( 2, 2, 2, 2 )
  191. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'Eb\nbF',
  192. 0, 0, result_buffer )
  193. expected_buffer = [ "aAa",
  194. "aEb",
  195. "bFBa",
  196. "aCa" ]
  197. eq_( expected_buffer, result_buffer )
  198. eq_( line_offset, 1 )
  199. eq_( char_offset, 1 )
  200. # now make another change to the "2nd" line
  201. start, end = _BuildLocations( 2, 3, 2, 4 )
  202. ( new_line_offset, new_char_offset ) = vimsupport.ReplaceChunk(
  203. start,
  204. end,
  205. 'cccc',
  206. line_offset,
  207. char_offset,
  208. result_buffer )
  209. line_offset += new_line_offset
  210. char_offset += new_char_offset
  211. eq_( [ "aAa", "aEb", "bFBcccc", "aCa" ], result_buffer )
  212. eq_( line_offset, 1 )
  213. eq_( char_offset, 4 )
  214. def ReplaceChunk_SingleToMultipleLines2_test():
  215. result_buffer = [ "aAa", "aBa", "aCa" ]
  216. start, end = _BuildLocations( 2, 2, 2, 2 )
  217. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  218. end,
  219. 'Eb\nbFb\nG',
  220. 0,
  221. 0,
  222. result_buffer )
  223. expected_buffer = [ "aAa", "aEb", "bFb", "GBa", "aCa" ]
  224. eq_( expected_buffer, result_buffer )
  225. eq_( line_offset, 2 )
  226. eq_( char_offset, 0 )
  227. def ReplaceChunk_SingleToMultipleLines3_test():
  228. result_buffer = [ "aAa", "aBa", "aCa" ]
  229. start, end = _BuildLocations( 2, 2, 2, 2 )
  230. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  231. end,
  232. 'Eb\nbFb\nbGb',
  233. 0,
  234. 0,
  235. result_buffer )
  236. expected_buffer = [ "aAa", "aEb", "bFb", "bGbBa", "aCa" ]
  237. eq_( expected_buffer, result_buffer )
  238. eq_( line_offset, 2 )
  239. eq_( char_offset, 2 )
  240. def ReplaceChunk_SingleToMultipleLinesReplace_test():
  241. result_buffer = [ "aAa", "aBa", "aCa" ]
  242. start, end = _BuildLocations( 1, 2, 1, 4 )
  243. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  244. end,
  245. 'Eb\nbFb\nbGb',
  246. 0,
  247. 0,
  248. result_buffer )
  249. expected_buffer = [ "aEb", "bFb", "bGb", "aBa", "aCa" ]
  250. eq_( expected_buffer, result_buffer )
  251. eq_( line_offset, 2 )
  252. eq_( char_offset, 0 )
  253. def ReplaceChunk_SingleToMultipleLinesReplace_2_test():
  254. result_buffer = [ "aAa",
  255. "aBa",
  256. "aCa" ]
  257. start, end = _BuildLocations( 1, 2, 1, 4 )
  258. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  259. end,
  260. 'Eb\nbFb\nbGb',
  261. 0,
  262. 0,
  263. result_buffer )
  264. expected_buffer = [ "aEb",
  265. "bFb",
  266. "bGb",
  267. "aBa",
  268. "aCa" ]
  269. eq_( expected_buffer, result_buffer )
  270. eq_( line_offset, 2 )
  271. eq_( char_offset, 0 )
  272. # now do a subsequent change (insert at end of line "1")
  273. start, end = _BuildLocations( 1, 4, 1, 4 )
  274. ( new_line_offset, new_char_offset ) = vimsupport.ReplaceChunk(
  275. start,
  276. end,
  277. 'cccc',
  278. line_offset,
  279. char_offset,
  280. result_buffer )
  281. line_offset += new_line_offset
  282. char_offset += new_char_offset
  283. eq_( [ "aEb",
  284. "bFb",
  285. "bGbcccc",
  286. "aBa",
  287. "aCa" ], result_buffer )
  288. eq_( line_offset, 2 )
  289. eq_( char_offset, 4 )
  290. def ReplaceChunk_MultipleLinesToSingleLine_test():
  291. result_buffer = [ "aAa", "aBa", "aCaaaa" ]
  292. start, end = _BuildLocations( 2, 2, 3, 2 )
  293. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'E',
  294. 0, 0, result_buffer )
  295. expected_buffer = [ "aAa", "aECaaaa" ]
  296. eq_( expected_buffer, result_buffer )
  297. eq_( line_offset, -1 )
  298. eq_( char_offset, 1 )
  299. # make another modification applying offsets
  300. start, end = _BuildLocations( 3, 3, 3, 4 )
  301. ( new_line_offset, new_char_offset ) = vimsupport.ReplaceChunk(
  302. start,
  303. end,
  304. 'cccc',
  305. line_offset,
  306. char_offset,
  307. result_buffer )
  308. line_offset += new_line_offset
  309. char_offset += new_char_offset
  310. eq_( [ "aAa", "aECccccaaa" ], result_buffer )
  311. eq_( line_offset, -1 )
  312. eq_( char_offset, 4 )
  313. # and another, for luck
  314. start, end = _BuildLocations( 3, 4, 3, 5 )
  315. ( new_line_offset, new_char_offset ) = vimsupport.ReplaceChunk(
  316. start,
  317. end,
  318. 'dd\ndd',
  319. line_offset,
  320. char_offset,
  321. result_buffer )
  322. line_offset += new_line_offset
  323. char_offset += new_char_offset
  324. eq_( [ "aAa", "aECccccdd", "ddaa" ], result_buffer )
  325. eq_( line_offset, 0 )
  326. eq_( char_offset, -2 )
  327. def ReplaceChunk_MultipleLinesToSameMultipleLines_test():
  328. result_buffer = [ "aAa", "aBa", "aCa", "aDe" ]
  329. start, end = _BuildLocations( 2, 2, 3, 2 )
  330. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'Eb\nbF',
  331. 0, 0, result_buffer )
  332. expected_buffer = [ "aAa", "aEb", "bFCa", "aDe" ]
  333. eq_( expected_buffer, result_buffer )
  334. eq_( line_offset, 0 )
  335. eq_( char_offset, 1 )
  336. def ReplaceChunk_MultipleLinesToMoreMultipleLines_test():
  337. result_buffer = [ "aAa", "aBa", "aCa", "aDe" ]
  338. start, end = _BuildLocations( 2, 2, 3, 2 )
  339. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  340. end,
  341. 'Eb\nbFb\nbG',
  342. 0,
  343. 0,
  344. result_buffer )
  345. expected_buffer = [ "aAa", "aEb", "bFb", "bGCa", "aDe" ]
  346. eq_( expected_buffer, result_buffer )
  347. eq_( line_offset, 1 )
  348. eq_( char_offset, 1 )
  349. def ReplaceChunk_MultipleLinesToLessMultipleLines_test():
  350. result_buffer = [ "aAa", "aBa", "aCa", "aDe" ]
  351. start, end = _BuildLocations( 1, 2, 3, 2 )
  352. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'Eb\nbF',
  353. 0, 0, result_buffer )
  354. expected_buffer = [ "aEb", "bFCa", "aDe" ]
  355. eq_( expected_buffer, result_buffer )
  356. eq_( line_offset, -1 )
  357. eq_( char_offset, 1 )
  358. def ReplaceChunk_MultipleLinesToEvenLessMultipleLines_test():
  359. result_buffer = [ "aAa", "aBa", "aCa", "aDe" ]
  360. start, end = _BuildLocations( 1, 2, 4, 2 )
  361. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'Eb\nbF',
  362. 0, 0, result_buffer )
  363. expected_buffer = [ "aEb", "bFDe" ]
  364. eq_( expected_buffer, result_buffer )
  365. eq_( line_offset, -2 )
  366. eq_( char_offset, 1 )
  367. def ReplaceChunk_SpanBufferEdge_test():
  368. result_buffer = [ "aAa", "aBa", "aCa" ]
  369. start, end = _BuildLocations( 1, 1, 1, 3 )
  370. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'bDb',
  371. 0, 0, result_buffer )
  372. expected_buffer = [ "bDba", "aBa", "aCa" ]
  373. eq_( expected_buffer, result_buffer )
  374. eq_( line_offset, 0 )
  375. eq_( char_offset, 1 )
  376. def ReplaceChunk_DeleteTextInLine_test():
  377. result_buffer = [ "aAa", "aBa", "aCa" ]
  378. start, end = _BuildLocations( 2, 2, 2, 3 )
  379. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, '',
  380. 0, 0, result_buffer )
  381. expected_buffer = [ "aAa", "aa", "aCa" ]
  382. eq_( expected_buffer, result_buffer )
  383. eq_( line_offset, 0 )
  384. eq_( char_offset, -1 )
  385. def ReplaceChunk_AddTextInLine_test():
  386. result_buffer = [ "aAa", "aBa", "aCa" ]
  387. start, end = _BuildLocations( 2, 2, 2, 2 )
  388. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'bDb',
  389. 0, 0, result_buffer )
  390. expected_buffer = [ "aAa", "abDbBa", "aCa" ]
  391. eq_( expected_buffer, result_buffer )
  392. eq_( line_offset, 0 )
  393. eq_( char_offset, 3 )
  394. def ReplaceChunk_ReplaceTextInLine_test():
  395. result_buffer = [ "aAa", "aBa", "aCa" ]
  396. start, end = _BuildLocations( 2, 2, 2, 3 )
  397. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'bDb',
  398. 0, 0, result_buffer )
  399. expected_buffer = [ "aAa", "abDba", "aCa" ]
  400. eq_( expected_buffer, result_buffer )
  401. eq_( line_offset, 0 )
  402. eq_( char_offset, 2 )
  403. def ReplaceChunk_SingleLineOffsetWorks_test():
  404. result_buffer = [ "aAa", "aBa", "aCa" ]
  405. start, end = _BuildLocations( 1, 1, 1, 2 )
  406. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'bDb',
  407. 1, 1, result_buffer )
  408. expected_buffer = [ "aAa", "abDba", "aCa" ]
  409. eq_( expected_buffer, result_buffer )
  410. eq_( line_offset, 0 )
  411. eq_( char_offset, 2 )
  412. def ReplaceChunk_SingleLineToMultipleLinesOffsetWorks_test():
  413. result_buffer = [ "aAa", "aBa", "aCa" ]
  414. start, end = _BuildLocations( 1, 1, 1, 2 )
  415. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'Db\nE',
  416. 1, 1, result_buffer )
  417. expected_buffer = [ "aAa", "aDb", "Ea", "aCa" ]
  418. eq_( expected_buffer, result_buffer )
  419. eq_( line_offset, 1 )
  420. eq_( char_offset, -1 )
  421. def ReplaceChunk_MultipleLinesToSingleLineOffsetWorks_test():
  422. result_buffer = [ "aAa", "aBa", "aCa" ]
  423. start, end = _BuildLocations( 1, 1, 2, 2 )
  424. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'bDb',
  425. 1, 1, result_buffer )
  426. expected_buffer = [ "aAa", "abDbCa" ]
  427. eq_( expected_buffer, result_buffer )
  428. eq_( line_offset, -1 )
  429. eq_( char_offset, 3 )
  430. def ReplaceChunk_MultipleLineOffsetWorks_test():
  431. result_buffer = [ "aAa", "aBa", "aCa" ]
  432. start, end = _BuildLocations( 3, 1, 4, 3 )
  433. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  434. end,
  435. 'bDb\nbEb\nbFb',
  436. -1,
  437. 1,
  438. result_buffer )
  439. expected_buffer = [ "aAa", "abDb", "bEb", "bFba" ]
  440. eq_( expected_buffer, result_buffer )
  441. eq_( line_offset, 1 )
  442. eq_( char_offset, 1 )
  443. def _BuildLocations( start_line, start_column, end_line, end_column ):
  444. return {
  445. 'line_num' : start_line,
  446. 'column_num': start_column,
  447. }, {
  448. 'line_num' : end_line,
  449. 'column_num': end_column,
  450. }
  451. def ReplaceChunksInBuffer_SortedChunks_test():
  452. chunks = [
  453. _BuildChunk( 1, 4, 1, 4, '('),
  454. _BuildChunk( 1, 11, 1, 11, ')' )
  455. ]
  456. result_buffer = [ "CT<10 >> 2> ct" ]
  457. vimsupport.ReplaceChunksInBuffer( chunks, result_buffer, None )
  458. expected_buffer = [ "CT<(10 >> 2)> ct" ]
  459. eq_( expected_buffer, result_buffer )
  460. def ReplaceChunksInBuffer_UnsortedChunks_test():
  461. chunks = [
  462. _BuildChunk( 1, 11, 1, 11, ')'),
  463. _BuildChunk( 1, 4, 1, 4, '(' )
  464. ]
  465. result_buffer = [ "CT<10 >> 2> ct" ]
  466. vimsupport.ReplaceChunksInBuffer( chunks, result_buffer, None )
  467. expected_buffer = [ "CT<(10 >> 2)> ct" ]
  468. eq_( expected_buffer, result_buffer )
  469. class MockBuffer( ):
  470. """An object that looks like a vim.buffer object, enough for ReplaceChunk to
  471. generate a location list"""
  472. def __init__( self, lines, name, number ):
  473. self.lines = lines
  474. self.name = name
  475. self.number = number
  476. def __getitem__( self, index ):
  477. return self.lines[ index ]
  478. def __len__( self ):
  479. return len( self.lines )
  480. def __setitem__( self, key, value ):
  481. return self.lines.__setitem__( key, value )
  482. @patch( 'ycm.vimsupport.GetBufferNumberForFilename',
  483. return_value=1,
  484. new_callable=ExtendedMock )
  485. @patch( 'ycm.vimsupport.BufferIsVisible',
  486. return_value=True,
  487. new_callable=ExtendedMock )
  488. @patch( 'ycm.vimsupport.OpenFilename' )
  489. @patch( 'ycm.vimsupport.EchoTextVimWidth', new_callable=ExtendedMock )
  490. @patch( 'vim.eval', new_callable=ExtendedMock )
  491. @patch( 'vim.command', new_callable=ExtendedMock )
  492. def ReplaceChunks_SingleFile_Open_test( vim_command,
  493. vim_eval,
  494. echo_text_vim_width,
  495. open_filename,
  496. buffer_is_visible,
  497. get_buffer_number_for_filename ):
  498. chunks = [
  499. _BuildChunk( 1, 1, 2, 1, 'replacement', 'single_file' )
  500. ]
  501. result_buffer = MockBuffer( [
  502. 'line1',
  503. 'line2',
  504. 'line3',
  505. ], 'single_file', 1 )
  506. with patch( 'vim.buffers', [ None, result_buffer, None ] ):
  507. vimsupport.ReplaceChunks( chunks )
  508. # Ensure that we applied the replacement correctly
  509. eq_( result_buffer.lines, [
  510. 'replacementline2',
  511. 'line3',
  512. ] )
  513. # GetBufferNumberForFilename is called twice:
  514. # - once to the check if we would require opening the file (so that we can
  515. # raise a warning)
  516. # - once whilst applying the changes
  517. get_buffer_number_for_filename.assert_has_exact_calls( [
  518. call( 'single_file', False ),
  519. call( 'single_file', False ),
  520. ] )
  521. # BufferIsVisible is called twice for the same reasons as above
  522. buffer_is_visible.assert_has_exact_calls( [
  523. call( 1 ),
  524. call( 1 ),
  525. ] )
  526. # we don't attempt to open any files
  527. open_filename.assert_not_called()
  528. # But we do set the quickfix list
  529. vim_eval.assert_has_exact_calls( [
  530. call( 'setqflist( {0} )'.format( json.dumps( [ {
  531. 'bufnr': 1,
  532. 'filename': 'single_file',
  533. 'lnum': 1,
  534. 'col': 1,
  535. 'text': 'replacement',
  536. 'type': 'F'
  537. } ] ) ) ),
  538. ] )
  539. vim_command.assert_has_calls( [
  540. call( 'copen 1' )
  541. ] )
  542. # And it is ReplaceChunks that prints the message showing the number of
  543. # changes
  544. echo_text_vim_width.assert_has_exact_calls( [
  545. call( 'Applied 1 changes' ),
  546. ] )
  547. @patch( 'ycm.vimsupport.GetBufferNumberForFilename',
  548. side_effect=[ -1, -1, 1 ],
  549. new_callable=ExtendedMock )
  550. @patch( 'ycm.vimsupport.BufferIsVisible',
  551. side_effect=[ False, False, True ],
  552. new_callable=ExtendedMock )
  553. @patch( 'ycm.vimsupport.OpenFilename',
  554. new_callable=ExtendedMock )
  555. @patch( 'ycm.vimsupport.EchoTextVimWidth', new_callable=ExtendedMock )
  556. @patch( 'ycm.vimsupport.Confirm',
  557. return_value=True,
  558. new_callable=ExtendedMock )
  559. @patch( 'vim.eval', return_value=10, new_callable=ExtendedMock )
  560. @patch( 'vim.command', new_callable=ExtendedMock )
  561. def ReplaceChunks_SingleFile_NotOpen_test( vim_command,
  562. vim_eval,
  563. confirm,
  564. echo_text_vim_width,
  565. open_filename,
  566. buffer_is_visible,
  567. get_buffer_number_for_filename ):
  568. chunks = [
  569. _BuildChunk( 1, 1, 2, 1, 'replacement', 'single_file' )
  570. ]
  571. result_buffer = MockBuffer( [
  572. 'line1',
  573. 'line2',
  574. 'line3',
  575. ], 'single_file', 1 )
  576. with patch( 'vim.buffers', [ None, result_buffer, None ] ):
  577. vimsupport.ReplaceChunks( chunks )
  578. # We checked if it was OK to open the file
  579. confirm.assert_has_exact_calls( [
  580. call( vimsupport.FIXIT_OPENING_BUFFERS_MESSAGE_FORMAT.format( 1 ) )
  581. ] )
  582. # Ensure that we applied the replacement correctly
  583. eq_( result_buffer.lines, [
  584. 'replacementline2',
  585. 'line3',
  586. ] )
  587. # GetBufferNumberForFilename is called 3 times. The return values are set in
  588. # the @patch call above:
  589. # - once to the check if we would require opening the file (so that we can
  590. # raise a warning) (-1 return)
  591. # - once whilst applying the changes (-1 return)
  592. # - finally after calling OpenFilename (1 return)
  593. get_buffer_number_for_filename.assert_has_exact_calls( [
  594. call( 'single_file', False ),
  595. call( 'single_file', False ),
  596. call( 'single_file', False ),
  597. ] )
  598. # BufferIsVisible is called 3 times for the same reasons as above, with the
  599. # return of each one
  600. buffer_is_visible.assert_has_exact_calls( [
  601. call( -1 ),
  602. call( -1 ),
  603. call( 1 ),
  604. ] )
  605. # We open 'single_file' as expected.
  606. open_filename.assert_called_with( 'single_file', {
  607. 'focus': True,
  608. 'fix': True,
  609. 'size': 10
  610. } )
  611. # And close it again, then show the preview window (note, we don't check exact
  612. # calls because there are other calls which are checked elsewhere)
  613. vim_command.assert_has_calls( [
  614. call( 'lclose' ),
  615. call( 'hide' ),
  616. call( 'copen 1' ),
  617. ] )
  618. # And update the quickfix list
  619. vim_eval.assert_has_exact_calls( [
  620. call( '&previewheight' ),
  621. call( 'setqflist( {0} )'.format( json.dumps( [ {
  622. 'bufnr': 1,
  623. 'filename': 'single_file',
  624. 'lnum': 1,
  625. 'col': 1,
  626. 'text': 'replacement',
  627. 'type': 'F'
  628. } ] ) ) ),
  629. ] )
  630. # And it is ReplaceChunks that prints the message showing the number of
  631. # changes
  632. echo_text_vim_width.assert_has_exact_calls( [
  633. call( 'Applied 1 changes' ),
  634. ] )
  635. @patch( 'ycm.vimsupport.GetBufferNumberForFilename',
  636. side_effect=[ -1, -1, 1 ],
  637. new_callable=ExtendedMock )
  638. @patch( 'ycm.vimsupport.BufferIsVisible',
  639. side_effect=[ False, False, True ],
  640. new_callable=ExtendedMock )
  641. @patch( 'ycm.vimsupport.OpenFilename',
  642. new_callable=ExtendedMock )
  643. @patch( 'ycm.vimsupport.EchoTextVimWidth',
  644. new_callable=ExtendedMock )
  645. @patch( 'ycm.vimsupport.Confirm',
  646. return_value=False,
  647. new_callable=ExtendedMock )
  648. @patch( 'vim.eval',
  649. return_value=10,
  650. new_callable=ExtendedMock )
  651. @patch( 'vim.command', new_callable=ExtendedMock )
  652. def ReplaceChunks_User_Declines_To_Open_File_test(
  653. vim_command,
  654. vim_eval,
  655. confirm,
  656. echo_text_vim_width,
  657. open_filename,
  658. buffer_is_visible,
  659. get_buffer_number_for_filename ):
  660. # Same as above, except the user selects Cancel when asked if they should
  661. # allow us to open lots of (ahem, 1) file.
  662. chunks = [
  663. _BuildChunk( 1, 1, 2, 1, 'replacement', 'single_file' )
  664. ]
  665. result_buffer = MockBuffer( [
  666. 'line1',
  667. 'line2',
  668. 'line3',
  669. ], 'single_file', 1 )
  670. with patch( 'vim.buffers', [ None, result_buffer, None ] ):
  671. vimsupport.ReplaceChunks( chunks )
  672. # We checked if it was OK to open the file
  673. confirm.assert_has_exact_calls( [
  674. call( vimsupport.FIXIT_OPENING_BUFFERS_MESSAGE_FORMAT.format( 1 ) )
  675. ] )
  676. # Ensure that buffer is not changed
  677. eq_( result_buffer.lines, [
  678. 'line1',
  679. 'line2',
  680. 'line3',
  681. ] )
  682. # GetBufferNumberForFilename is called once. The return values are set in
  683. # the @patch call above:
  684. # - once to the check if we would require opening the file (so that we can
  685. # raise a warning) (-1 return)
  686. get_buffer_number_for_filename.assert_has_exact_calls( [
  687. call( 'single_file', False ),
  688. ] )
  689. # BufferIsVisible is called once for the above file, which wasn't visible.
  690. buffer_is_visible.assert_has_exact_calls( [
  691. call( -1 ),
  692. ] )
  693. # We don't attempt to open any files or update any quickfix list or anything
  694. # like that
  695. open_filename.assert_not_called()
  696. vim_eval.assert_not_called()
  697. vim_command.assert_not_called()
  698. echo_text_vim_width.assert_not_called()
  699. @patch( 'ycm.vimsupport.GetBufferNumberForFilename',
  700. side_effect=[ -1, -1, 1 ],
  701. new_callable=ExtendedMock )
  702. # Key difference is here: In the final check, BufferIsVisible returns False
  703. @patch( 'ycm.vimsupport.BufferIsVisible',
  704. side_effect=[ False, False, False ],
  705. new_callable=ExtendedMock )
  706. @patch( 'ycm.vimsupport.OpenFilename',
  707. new_callable=ExtendedMock )
  708. @patch( 'ycm.vimsupport.EchoTextVimWidth',
  709. new_callable=ExtendedMock )
  710. @patch( 'ycm.vimsupport.Confirm',
  711. return_value=True,
  712. new_callable=ExtendedMock )
  713. @patch( 'vim.eval',
  714. return_value=10,
  715. new_callable=ExtendedMock )
  716. @patch( 'vim.command',
  717. new_callable=ExtendedMock )
  718. def ReplaceChunks_User_Aborts_Opening_File_test(
  719. vim_command,
  720. vim_eval,
  721. confirm,
  722. echo_text_vim_width,
  723. open_filename,
  724. buffer_is_visible,
  725. get_buffer_number_for_filename ):
  726. # Same as above, except the user selects Abort or Quick during the
  727. # "swap-file-found" dialog
  728. chunks = [
  729. _BuildChunk( 1, 1, 2, 1, 'replacement', 'single_file' )
  730. ]
  731. result_buffer = MockBuffer( [
  732. 'line1',
  733. 'line2',
  734. 'line3',
  735. ], 'single_file', 1 )
  736. with patch( 'vim.buffers', [ None, result_buffer, None ] ):
  737. assert_that( calling( vimsupport.ReplaceChunks ).with_args( chunks ),
  738. raises( RuntimeError,
  739. 'Unable to open file: single_file\nFixIt/Refactor operation '
  740. 'aborted prior to completion. Your files have not been '
  741. 'fully updated. Please use undo commands to revert the '
  742. 'applied changes.' ) )
  743. # We checked if it was OK to open the file
  744. confirm.assert_has_exact_calls( [
  745. call( vimsupport.FIXIT_OPENING_BUFFERS_MESSAGE_FORMAT.format( 1 ) )
  746. ] )
  747. # Ensure that buffer is not changed
  748. eq_( result_buffer.lines, [
  749. 'line1',
  750. 'line2',
  751. 'line3',
  752. ] )
  753. # We tried to open this file
  754. open_filename.assert_called_with( "single_file", {
  755. 'focus': True,
  756. 'fix': True,
  757. 'size': 10
  758. } )
  759. vim_eval.assert_called_with( "&previewheight" )
  760. # But raised an exception before issuing the message at the end
  761. echo_text_vim_width.assert_not_called()
  762. @patch( 'ycm.vimsupport.GetBufferNumberForFilename', side_effect=[
  763. 22, # first_file (check)
  764. -1, # another_file (check)
  765. 22, # first_file (apply)
  766. -1, # another_file (apply)
  767. 19, # another_file (check after open)
  768. ],
  769. new_callable=ExtendedMock )
  770. @patch( 'ycm.vimsupport.BufferIsVisible', side_effect=[
  771. True, # first_file (check)
  772. False, # second_file (check)
  773. True, # first_file (apply)
  774. False, # second_file (apply)
  775. True, # side_effect (check after open)
  776. ],
  777. new_callable=ExtendedMock)
  778. @patch( 'ycm.vimsupport.OpenFilename',
  779. new_callable=ExtendedMock)
  780. @patch( 'ycm.vimsupport.EchoTextVimWidth',
  781. new_callable=ExtendedMock)
  782. @patch( 'ycm.vimsupport.Confirm', return_value=True,
  783. new_callable=ExtendedMock)
  784. @patch( 'vim.eval', return_value=10,
  785. new_callable=ExtendedMock)
  786. @patch( 'vim.command',
  787. new_callable=ExtendedMock)
  788. def ReplaceChunks_MultiFile_Open_test( vim_command,
  789. vim_eval,
  790. confirm,
  791. echo_text_vim_width,
  792. open_filename,
  793. buffer_is_visible,
  794. get_buffer_number_for_filename ):
  795. # Chunks are split across 2 files, one is already open, one isn't
  796. chunks = [
  797. _BuildChunk( 1, 1, 2, 1, 'first_file_replacement ', '1_first_file' ),
  798. _BuildChunk( 2, 1, 2, 1, 'second_file_replacement ', '2_another_file' ),
  799. ]
  800. first_file = MockBuffer( [
  801. 'line1',
  802. 'line2',
  803. 'line3',
  804. ], '1_first_file', 22 )
  805. another_file = MockBuffer( [
  806. 'another line1',
  807. 'ACME line2',
  808. ], '2_another_file', 19 )
  809. vim_buffers = [ None ] * 23
  810. vim_buffers[ 22 ] = first_file
  811. vim_buffers[ 19 ] = another_file
  812. with patch( 'vim.buffers', vim_buffers ):
  813. vimsupport.ReplaceChunks( chunks )
  814. # We checked for the right file names
  815. get_buffer_number_for_filename.assert_has_exact_calls( [
  816. call( '1_first_file', False ),
  817. call( '2_another_file', False ),
  818. call( '1_first_file', False ),
  819. call( '2_another_file', False ),
  820. call( '2_another_file', False ),
  821. ] )
  822. # We checked if it was OK to open the file
  823. confirm.assert_has_exact_calls( [
  824. call( vimsupport.FIXIT_OPENING_BUFFERS_MESSAGE_FORMAT.format( 1 ) )
  825. ] )
  826. # Ensure that buffers are updated
  827. eq_( another_file.lines, [
  828. 'another line1',
  829. 'second_file_replacement ACME line2',
  830. ] )
  831. eq_( first_file.lines, [
  832. 'first_file_replacement line2',
  833. 'line3',
  834. ] )
  835. # We open '2_another_file' as expected.
  836. open_filename.assert_called_with( '2_another_file', {
  837. 'focus': True,
  838. 'fix': True,
  839. 'size': 10
  840. } )
  841. # And close it again, then show the preview window (note, we don't check exact
  842. # calls because there are other calls which are checked elsewhere)
  843. vim_command.assert_has_calls( [
  844. call( 'lclose' ),
  845. call( 'hide' ),
  846. call( 'copen 2' ),
  847. ] )
  848. # And update the quickfix list with each entry
  849. vim_eval.assert_has_exact_calls( [
  850. call( '&previewheight' ),
  851. call( 'setqflist( {0} )'.format( json.dumps( [ {
  852. 'bufnr': 22,
  853. 'filename': '1_first_file',
  854. 'lnum': 1,
  855. 'col': 1,
  856. 'text': 'first_file_replacement ',
  857. 'type': 'F'
  858. }, {
  859. 'bufnr': 19,
  860. 'filename': '2_another_file',
  861. 'lnum': 2,
  862. 'col': 1,
  863. 'text': 'second_file_replacement ',
  864. 'type': 'F'
  865. } ] ) ) ),
  866. ] )
  867. # And it is ReplaceChunks that prints the message showing the number of
  868. # changes
  869. echo_text_vim_width.assert_has_exact_calls( [
  870. call( 'Applied 2 changes' ),
  871. ] )
  872. def _BuildChunk( start_line,
  873. start_column,
  874. end_line,
  875. end_column,
  876. replacement_text, filepath='test_file_name' ):
  877. return {
  878. 'range': {
  879. 'start': {
  880. 'filepath': filepath,
  881. 'line_num': start_line,
  882. 'column_num': start_column,
  883. },
  884. 'end': {
  885. 'filepath': filepath,
  886. 'line_num': end_line,
  887. 'column_num': end_column,
  888. },
  889. },
  890. 'replacement_text': replacement_text
  891. }
  892. @patch( 'vim.command', new_callable=ExtendedMock )
  893. @patch( 'vim.current', new_callable=ExtendedMock)
  894. def WriteToPreviewWindow_test( vim_current, vim_command ):
  895. vim_current.window.options.__getitem__ = MagicMock( return_value = True )
  896. vimsupport.WriteToPreviewWindow( "test" )
  897. vim_command.assert_has_exact_calls( [
  898. call( 'silent! pclose!' ),
  899. call( 'silent! pedit! _TEMP_FILE_' ),
  900. call( 'silent! wincmd P' ),
  901. call( 'silent! wincmd p' ) ] )
  902. vim_current.buffer.__setitem__.assert_called_with(
  903. slice( None, None, None ), [ 'test' ] )
  904. vim_current.buffer.options.__setitem__.assert_has_exact_calls( [
  905. call( 'modifiable', True ),
  906. call( 'readonly', False ),
  907. call( 'buftype', 'nofile' ),
  908. call( 'swapfile', False ),
  909. call( 'modifiable', False ),
  910. call( 'modified', False ),
  911. call( 'readonly', True ),
  912. ], any_order = True )
  913. @patch( 'vim.current' )
  914. def WriteToPreviewWindow_MultiLine_test( vim_current ):
  915. vim_current.window.options.__getitem__ = MagicMock( return_value = True )
  916. vimsupport.WriteToPreviewWindow( "test\ntest2" )
  917. vim_current.buffer.__setitem__.assert_called_with(
  918. slice( None, None, None ), [ 'test', 'test2' ] )
  919. @patch( 'vim.command', new_callable=ExtendedMock )
  920. @patch( 'vim.current', new_callable=ExtendedMock )
  921. def WriteToPreviewWindow_JumpFail_test( vim_current, vim_command ):
  922. vim_current.window.options.__getitem__ = MagicMock( return_value = False )
  923. vimsupport.WriteToPreviewWindow( "test" )
  924. vim_command.assert_has_exact_calls( [
  925. call( 'silent! pclose!' ),
  926. call( 'silent! pedit! _TEMP_FILE_' ),
  927. call( 'silent! wincmd P' ),
  928. call( "echom 'test'" ),
  929. ] )
  930. vim_current.buffer.__setitem__.assert_not_called()
  931. vim_current.buffer.options.__setitem__.assert_not_called()
  932. @patch( 'vim.command', new_callable=ExtendedMock )
  933. @patch( 'vim.current', new_callable=ExtendedMock )
  934. def WriteToPreviewWindow_JumpFail_MultiLine_test( vim_current, vim_command ):
  935. vim_current.window.options.__getitem__ = MagicMock( return_value = False )
  936. vimsupport.WriteToPreviewWindow( "test\ntest2" )
  937. vim_command.assert_has_exact_calls( [
  938. call( 'silent! pclose!' ),
  939. call( 'silent! pedit! _TEMP_FILE_' ),
  940. call( 'silent! wincmd P' ),
  941. call( "echom 'test'" ),
  942. call( "echom 'test2'" ),
  943. ] )
  944. vim_current.buffer.__setitem__.assert_not_called()
  945. vim_current.buffer.options.__setitem__.assert_not_called()
  946. def CheckFilename_test():
  947. assert_that(
  948. calling( vimsupport.CheckFilename ).with_args( None ),
  949. raises( RuntimeError, "'None' is not a valid filename" )
  950. )
  951. assert_that(
  952. calling( vimsupport.CheckFilename ).with_args( 'nonexistent_file' ),
  953. raises( RuntimeError,
  954. "filename 'nonexistent_file' cannot be opened. "
  955. "\[Errno 2\] No such file or directory: 'nonexistent_file'" )
  956. )
  957. assert_that( vimsupport.CheckFilename( __file__ ), none() )
  958. def BufferIsVisibleForFilename_test():
  959. buffers = [
  960. {
  961. 'number': 1,
  962. 'filename': os.path.realpath( 'visible_filename' ),
  963. 'window': 1
  964. },
  965. {
  966. 'number': 2,
  967. 'filename': os.path.realpath( 'hidden_filename' ),
  968. }
  969. ]
  970. with patch( 'vim.buffers', buffers ):
  971. eq_( vimsupport.BufferIsVisibleForFilename( 'visible_filename' ), True )
  972. eq_( vimsupport.BufferIsVisibleForFilename( 'hidden_filename' ), False )
  973. eq_( vimsupport.BufferIsVisibleForFilename( 'another_filename' ), False )
  974. @patch( 'vim.command',
  975. side_effect = MockVimCommand,
  976. new_callable=ExtendedMock )
  977. def CloseBuffersForFilename_test( vim_command ):
  978. buffers = [
  979. {
  980. 'number': 2,
  981. 'filename': os.path.realpath( 'some_filename' ),
  982. },
  983. {
  984. 'number': 5,
  985. 'filename': os.path.realpath( 'some_filename' ),
  986. },
  987. {
  988. 'number': 1,
  989. 'filename': os.path.realpath( 'another_filename' )
  990. }
  991. ]
  992. with patch( 'vim.buffers', buffers ):
  993. vimsupport.CloseBuffersForFilename( 'some_filename' )
  994. vim_command.assert_has_exact_calls( [
  995. call( 'silent! bwipeout! 2' ),
  996. call( 'silent! bwipeout! 5' )
  997. ], any_order = True )
  998. @patch( 'vim.command', new_callable=ExtendedMock )
  999. @patch( 'vim.current', new_callable=ExtendedMock )
  1000. def OpenFilename_test( vim_current, vim_command ):
  1001. # Options used to open a logfile
  1002. options = {
  1003. 'size': vimsupport.GetIntValue( '&previewheight' ),
  1004. 'fix': True,
  1005. 'watch': True,
  1006. 'position': 'end'
  1007. }
  1008. vimsupport.OpenFilename( __file__, options )
  1009. vim_command.assert_has_exact_calls( [
  1010. call( '12split {0}'.format( __file__ ) ),
  1011. call( "exec "
  1012. "'au BufEnter <buffer> :silent! checktime {0}'".format( __file__ ) ),
  1013. call( 'silent! normal G zz' ),
  1014. call( 'silent! wincmd p' )
  1015. ] )
  1016. vim_current.buffer.options.__setitem__.assert_has_exact_calls( [
  1017. call( 'autoread', True ),
  1018. ] )
  1019. vim_current.window.options.__setitem__.assert_has_exact_calls( [
  1020. call( 'winfixheight', True )
  1021. ] )