1
0

vimsupport_test.py 44 KB

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