vimsupport_test.py 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992
  1. # coding: utf-8
  2. #
  3. # Copyright (C) 2015-2018 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. # Intentionally not importing unicode_literals!
  20. from __future__ import print_function
  21. from __future__ import division
  22. from __future__ import absolute_import
  23. # Not installing aliases from python-future; it's unreliable and slow.
  24. from builtins import * # noqa
  25. from ycm.tests import PathToTestFile
  26. from ycm.tests.test_utils import ( CurrentWorkingDirectory, ExtendedMock,
  27. MockVimBuffers, MockVimModule, Version,
  28. VimBuffer, VimError, WindowsAndMacOnly )
  29. MockVimModule()
  30. from ycm import vimsupport
  31. from nose.tools import eq_
  32. from hamcrest import ( assert_that, calling, contains, empty, equal_to,
  33. has_entry, raises )
  34. from mock import MagicMock, call, patch
  35. from ycmd.utils import ToBytes
  36. import os
  37. import json
  38. @patch( 'vim.eval', new_callable = ExtendedMock )
  39. def SetLocationListsForBuffer_Current_test( vim_eval ):
  40. diagnostics = [ {
  41. 'bufnr': 3,
  42. 'filename': 'some_filename',
  43. 'lnum': 5,
  44. 'col': 22,
  45. 'type': 'E',
  46. 'valid': 1
  47. } ]
  48. current_buffer = VimBuffer( '/test', number = 3 )
  49. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  50. vimsupport.SetLocationListsForBuffer( 3, diagnostics )
  51. vim_eval.assert_has_exact_calls( [
  52. call( 'setloclist( 1, {0} )'.format( json.dumps( diagnostics ) ) )
  53. ] )
  54. @patch( 'vim.eval', new_callable = ExtendedMock )
  55. def SetLocationListsForBuffer_NotCurrent_test( vim_eval ):
  56. diagnostics = [ {
  57. 'bufnr': 3,
  58. 'filename': 'some_filename',
  59. 'lnum': 5,
  60. 'col': 22,
  61. 'type': 'E',
  62. 'valid': 1
  63. } ]
  64. current_buffer = VimBuffer( '/test', number = 3 )
  65. other_buffer = VimBuffer( '/notcurrent', number = 1 )
  66. with MockVimBuffers( [ current_buffer, other_buffer ], [ current_buffer ] ):
  67. vimsupport.SetLocationListsForBuffer( 1, diagnostics )
  68. vim_eval.assert_not_called()
  69. @patch( 'vim.eval', new_callable = ExtendedMock, side_effect = [ -1, 1 ] )
  70. def SetLocationListsForBuffer_NotVisible_test( vim_eval ):
  71. diagnostics = [ {
  72. 'bufnr': 3,
  73. 'filename': 'some_filename',
  74. 'lnum': 5,
  75. 'col': 22,
  76. 'type': 'E',
  77. 'valid': 1
  78. } ]
  79. current_buffer = VimBuffer( '/test', number = 3 )
  80. other_buffer = VimBuffer( '/notcurrent', number = 1 )
  81. with MockVimBuffers( [ current_buffer, other_buffer ], [ current_buffer ] ):
  82. vimsupport.SetLocationListsForBuffer( 1, diagnostics )
  83. vim_eval.assert_not_called()
  84. @patch( 'vim.eval', new_callable = ExtendedMock, side_effect = [ -1, 1 ] )
  85. def SetLocationListsForBuffer_MultipleWindows_test( vim_eval ):
  86. diagnostics = [ {
  87. 'bufnr': 3,
  88. 'filename': 'some_filename',
  89. 'lnum': 5,
  90. 'col': 22,
  91. 'type': 'E',
  92. 'valid': 1
  93. } ]
  94. current_buffer = VimBuffer( '/test', number = 3 )
  95. other_buffer = VimBuffer( '/notcurrent', number = 1 )
  96. with MockVimBuffers( [ current_buffer, other_buffer ],
  97. [ current_buffer, other_buffer ] ):
  98. vimsupport.SetLocationListsForBuffer( 1, diagnostics )
  99. vim_eval.assert_has_exact_calls( [
  100. call( 'setloclist( 2, {0} )'.format( json.dumps( diagnostics ) ) )
  101. ] )
  102. @patch( 'vim.eval', new_callable = ExtendedMock )
  103. def SetLocationList_test( vim_eval ):
  104. diagnostics = [ {
  105. 'bufnr': 3,
  106. 'filename': 'some_filename',
  107. 'lnum': 5,
  108. 'col': 22,
  109. 'type': 'E',
  110. 'valid': 1
  111. } ]
  112. current_buffer = VimBuffer( '/test', number = 3 )
  113. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ):
  114. vimsupport.SetLocationList( diagnostics )
  115. vim_eval.assert_has_calls( [
  116. call( 'setloclist( 0, {0} )'.format( json.dumps( diagnostics ) ) ),
  117. ] )
  118. @patch( 'vim.eval', new_callable = ExtendedMock )
  119. def SetLocationList_NotCurrent_test( vim_eval ):
  120. diagnostics = [ {
  121. 'bufnr': 3,
  122. 'filename': 'some_filename',
  123. 'lnum': 5,
  124. 'col': 22,
  125. 'type': 'E',
  126. 'valid': 1
  127. } ]
  128. current_buffer = VimBuffer( '/test', number = 3 )
  129. other_buffer = VimBuffer( '/notcurrent', number = 1 )
  130. with MockVimBuffers( [ current_buffer, other_buffer ],
  131. [ current_buffer, other_buffer ],
  132. ( 1, 1 ) ):
  133. vimsupport.SetLocationList( diagnostics )
  134. # This version does not check the current buffer and just sets the current win
  135. vim_eval.assert_has_exact_calls( [
  136. call( 'setloclist( 0, {0} )'.format( json.dumps( diagnostics ) ) ),
  137. ] )
  138. @patch( 'ycm.vimsupport.VariableExists', return_value = True )
  139. @patch( 'ycm.vimsupport.SetFittingHeightForCurrentWindow' )
  140. @patch( 'vim.command', new_callable = ExtendedMock )
  141. def OpenLocationList_test( vim_command, fitting_height, variable_exists ):
  142. vimsupport.OpenLocationList( focus = False, autoclose = True )
  143. vim_command.assert_has_exact_calls( [
  144. call( 'lopen' ),
  145. call( 'augroup ycmlocation' ),
  146. call( 'autocmd! * <buffer>' ),
  147. call( 'autocmd WinLeave <buffer> '
  148. 'if bufnr( "%" ) == expand( "<abuf>" ) | q | endif' ),
  149. call( 'augroup END' ),
  150. call( 'doautocmd User YcmLocationOpened' ),
  151. call( 'silent! wincmd p' )
  152. ] )
  153. fitting_height.assert_called_once_with()
  154. variable_exists.assert_called_once_with( '#User#YcmLocationOpened' )
  155. @patch( 'vim.command' )
  156. def SetFittingHeightForCurrentWindow_LineWrapOn_test( vim_command, *args ):
  157. # Create a two lines buffer whose first line is longer than the window width.
  158. current_buffer = VimBuffer( 'buffer',
  159. contents = [ 'a' * 140, 'b' * 80 ] )
  160. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ) as vim:
  161. vim.current.window.width = 120
  162. vim.current.window.options[ 'wrap' ] = True
  163. vimsupport.SetFittingHeightForCurrentWindow()
  164. vim_command.assert_called_once_with( '3wincmd _' )
  165. @patch( 'vim.command' )
  166. def SetFittingHeightForCurrentWindow_LineWrapOff_test( vim_command, *args ):
  167. # Create a two lines buffer whose first line is longer than the window width.
  168. current_buffer = VimBuffer( 'buffer',
  169. contents = [ 'a' * 140, 'b' * 80 ] )
  170. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ) as vim:
  171. vim.current.window.width = 120
  172. vim.current.window.options[ 'wrap' ] = False
  173. vimsupport.SetFittingHeightForCurrentWindow()
  174. vim_command.assert_called_once_with( '2wincmd _' )
  175. def AssertBuffersAreEqualAsBytes( result_buffer, expected_buffer ):
  176. eq_( len( result_buffer ), len( expected_buffer ) )
  177. for result_line, expected_line in zip( result_buffer, expected_buffer ):
  178. eq_( ToBytes( result_line ), ToBytes( expected_line ) )
  179. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  180. def ReplaceChunk_SingleLine_Repl_1_test():
  181. # Replace with longer range
  182. result_buffer = VimBuffer( 'buffer', contents = [ 'This is a string' ] )
  183. start, end = _BuildLocations( 1, 11, 1, 17 )
  184. vimsupport.ReplaceChunk( start, end, 'pie', result_buffer )
  185. AssertBuffersAreEqualAsBytes( [ 'This is a pie' ], result_buffer )
  186. # and replace again
  187. start, end = _BuildLocations( 1, 10, 1, 11 )
  188. vimsupport.ReplaceChunk( start, end, ' piece of ', result_buffer )
  189. AssertBuffersAreEqualAsBytes( [ 'This is a piece of pie' ], result_buffer )
  190. # and once more, for luck
  191. start, end = _BuildLocations( 1, 1, 1, 5 )
  192. vimsupport.ReplaceChunk( start, end, 'How long', result_buffer )
  193. AssertBuffersAreEqualAsBytes( [ 'How long is a piece of pie' ],
  194. result_buffer )
  195. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  196. def ReplaceChunk_SingleLine_Repl_2_test():
  197. # Replace with shorter range
  198. result_buffer = VimBuffer( 'buffer', contents = [ 'This is a string' ] )
  199. start, end = _BuildLocations( 1, 11, 1, 17 )
  200. vimsupport.ReplaceChunk( start, end, 'test', result_buffer )
  201. AssertBuffersAreEqualAsBytes( [ 'This is a test' ], result_buffer )
  202. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  203. def ReplaceChunk_SingleLine_Repl_3_test():
  204. # Replace with equal range
  205. result_buffer = VimBuffer( 'buffer', contents = [ 'This is a string' ] )
  206. start, end = _BuildLocations( 1, 6, 1, 8 )
  207. vimsupport.ReplaceChunk( start, end, 'be', result_buffer )
  208. AssertBuffersAreEqualAsBytes( [ 'This be a string' ], result_buffer )
  209. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  210. def ReplaceChunk_SingleLine_Add_1_test():
  211. # Insert at start
  212. result_buffer = VimBuffer( 'buffer', contents = [ 'is a string' ] )
  213. start, end = _BuildLocations( 1, 1, 1, 1 )
  214. vimsupport.ReplaceChunk( start, end, 'This ', result_buffer )
  215. AssertBuffersAreEqualAsBytes( [ 'This is a string' ], result_buffer )
  216. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  217. def ReplaceChunk_SingleLine_Add_2_test():
  218. # Insert at end
  219. result_buffer = VimBuffer( 'buffer', contents = [ 'This is a ' ] )
  220. start, end = _BuildLocations( 1, 11, 1, 11 )
  221. vimsupport.ReplaceChunk( start, end, 'string', result_buffer )
  222. AssertBuffersAreEqualAsBytes( [ 'This is a string' ], result_buffer )
  223. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  224. def ReplaceChunk_SingleLine_Add_3_test():
  225. # Insert in the middle
  226. result_buffer = VimBuffer( 'buffer', contents = [ 'This is a string' ] )
  227. start, end = _BuildLocations( 1, 8, 1, 8 )
  228. vimsupport.ReplaceChunk( start, end, ' not', result_buffer )
  229. AssertBuffersAreEqualAsBytes( [ 'This is not a string' ], result_buffer )
  230. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  231. def ReplaceChunk_SingleLine_Del_1_test():
  232. # Delete from start
  233. result_buffer = VimBuffer( 'buffer', contents = [ 'This is a string' ] )
  234. start, end = _BuildLocations( 1, 1, 1, 6 )
  235. vimsupport.ReplaceChunk( start, end, '', result_buffer )
  236. AssertBuffersAreEqualAsBytes( [ 'is a string' ], result_buffer )
  237. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  238. def ReplaceChunk_SingleLine_Del_2_test():
  239. # Delete from end
  240. result_buffer = VimBuffer( 'buffer', contents = [ 'This is a string' ] )
  241. start, end = _BuildLocations( 1, 10, 1, 18 )
  242. vimsupport.ReplaceChunk( start, end, '', result_buffer )
  243. AssertBuffersAreEqualAsBytes( [ 'This is a' ], result_buffer )
  244. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  245. def ReplaceChunk_SingleLine_Del_3_test():
  246. # Delete from middle
  247. result_buffer = VimBuffer( 'buffer', contents = [ 'This is not a string' ] )
  248. start, end = _BuildLocations( 1, 9, 1, 13 )
  249. vimsupport.ReplaceChunk( start, end, '', result_buffer )
  250. AssertBuffersAreEqualAsBytes( [ 'This is a string' ], result_buffer )
  251. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  252. def ReplaceChunk_SingleLine_Unicode_ReplaceUnicodeChars_test():
  253. # Replace Unicode characters.
  254. result_buffer = VimBuffer(
  255. 'buffer', contents = [ 'This Uniçø∂‰ string is in the middle' ] )
  256. start, end = _BuildLocations( 1, 6, 1, 20 )
  257. vimsupport.ReplaceChunk( start, end, 'Unicode ', result_buffer )
  258. AssertBuffersAreEqualAsBytes( [ 'This Unicode string is in the middle' ],
  259. result_buffer )
  260. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  261. def ReplaceChunk_SingleLine_Unicode_ReplaceAfterUnicode_test():
  262. # Replace ASCII characters after Unicode characters in the line.
  263. result_buffer = VimBuffer(
  264. 'buffer', contents = [ 'This Uniçø∂‰ string is in the middle' ] )
  265. start, end = _BuildLocations( 1, 30, 1, 43 )
  266. vimsupport.ReplaceChunk( start, end, 'fåke', result_buffer )
  267. AssertBuffersAreEqualAsBytes( [ 'This Uniçø∂‰ string is fåke' ],
  268. result_buffer )
  269. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  270. def ReplaceChunk_SingleLine_Unicode_Grown_test():
  271. # Replace ASCII characters after Unicode characters in the line.
  272. result_buffer = VimBuffer( 'buffer', contents = [ 'a' ] )
  273. start, end = _BuildLocations( 1, 1, 1, 2 )
  274. vimsupport.ReplaceChunk( start, end, 'å', result_buffer )
  275. AssertBuffersAreEqualAsBytes( [ 'å' ], result_buffer )
  276. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  277. def ReplaceChunk_RemoveSingleLine_test():
  278. result_buffer = VimBuffer( 'buffer', contents = [ 'aAa',
  279. 'aBa',
  280. 'aCa' ] )
  281. start, end = _BuildLocations( 2, 1, 3, 1 )
  282. vimsupport.ReplaceChunk( start, end, '', result_buffer )
  283. # First line is not affected.
  284. AssertBuffersAreEqualAsBytes( [ 'aAa',
  285. 'aCa' ], result_buffer )
  286. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  287. def ReplaceChunk_SingleToMultipleLines_test():
  288. result_buffer = VimBuffer( 'buffer', contents = [ 'aAa',
  289. 'aBa',
  290. 'aCa' ] )
  291. start, end = _BuildLocations( 2, 3, 2, 4 )
  292. vimsupport.ReplaceChunk( start, end, 'cccc', result_buffer )
  293. AssertBuffersAreEqualAsBytes( [ 'aAa',
  294. 'aBcccc',
  295. 'aCa' ], result_buffer )
  296. # now make another change to the second line
  297. start, end = _BuildLocations( 2, 2, 2, 2 )
  298. vimsupport.ReplaceChunk( start, end, 'Eb\nbF', result_buffer )
  299. AssertBuffersAreEqualAsBytes( [ 'aAa',
  300. 'aEb',
  301. 'bFBcccc',
  302. 'aCa' ], result_buffer )
  303. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  304. def ReplaceChunk_SingleToMultipleLines2_test():
  305. result_buffer = VimBuffer( 'buffer', contents = [ 'aAa',
  306. 'aBa',
  307. 'aCa' ] )
  308. start, end = _BuildLocations( 2, 2, 2, 2 )
  309. vimsupport.ReplaceChunk( start, end, 'Eb\nbFb\nG', result_buffer )
  310. AssertBuffersAreEqualAsBytes( [ 'aAa',
  311. 'aEb',
  312. 'bFb',
  313. 'GBa',
  314. 'aCa' ], result_buffer )
  315. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  316. def ReplaceChunk_SingleToMultipleLines3_test():
  317. result_buffer = VimBuffer( 'buffer', contents = [ 'aAa',
  318. 'aBa',
  319. 'aCa' ] )
  320. start, end = _BuildLocations( 2, 2, 2, 2 )
  321. vimsupport.ReplaceChunk( start, end, 'Eb\nbFb\nbGb', result_buffer )
  322. AssertBuffersAreEqualAsBytes( [ 'aAa',
  323. 'aEb',
  324. 'bFb',
  325. 'bGbBa',
  326. 'aCa' ], result_buffer )
  327. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  328. def ReplaceChunk_SingleToMultipleLinesReplace_test():
  329. result_buffer = VimBuffer( 'buffer', contents = [ 'aAa',
  330. 'aBa',
  331. 'aCa' ] )
  332. start, end = _BuildLocations( 1, 2, 1, 4 )
  333. vimsupport.ReplaceChunk( start, end, 'Eb\nbFb\nbGb', result_buffer )
  334. AssertBuffersAreEqualAsBytes( [ 'aEb',
  335. 'bFb',
  336. 'bGb',
  337. 'aBa',
  338. 'aCa' ], result_buffer )
  339. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  340. def ReplaceChunk_SingleToMultipleLinesReplace_2_test():
  341. result_buffer = VimBuffer( 'buffer', contents = [ 'aAa',
  342. 'aBa',
  343. 'aCa' ] )
  344. start, end = _BuildLocations( 1, 4, 1, 4 )
  345. vimsupport.ReplaceChunk( start, end, 'cccc', result_buffer )
  346. AssertBuffersAreEqualAsBytes( [ 'aAacccc',
  347. 'aBa',
  348. 'aCa', ], result_buffer )
  349. # now do a subsequent change (insert in the middle of the first line)
  350. start, end = _BuildLocations( 1, 2, 1, 4 )
  351. vimsupport.ReplaceChunk( start, end, 'Eb\nbFb\nbGb', result_buffer )
  352. AssertBuffersAreEqualAsBytes( [ 'aEb',
  353. 'bFb',
  354. 'bGbcccc',
  355. 'aBa',
  356. 'aCa' ], result_buffer )
  357. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  358. def ReplaceChunk_MultipleLinesToSingleLine_test():
  359. result_buffer = VimBuffer( 'buffer', contents = [ 'aAa',
  360. 'aBa',
  361. 'aCaaaa' ] )
  362. start, end = _BuildLocations( 3, 4, 3, 5 )
  363. vimsupport.ReplaceChunk( start, end, 'dd\ndd', result_buffer )
  364. AssertBuffersAreEqualAsBytes( [ 'aAa',
  365. 'aBa',
  366. 'aCadd',
  367. 'ddaa' ], result_buffer )
  368. # make another modification applying offsets
  369. start, end = _BuildLocations( 3, 3, 3, 4 )
  370. vimsupport.ReplaceChunk( start, end, 'cccc', result_buffer )
  371. AssertBuffersAreEqualAsBytes( [ 'aAa',
  372. 'aBa',
  373. 'aCccccdd',
  374. 'ddaa' ], result_buffer )
  375. # and another, for luck
  376. start, end = _BuildLocations( 2, 2, 3, 2 )
  377. vimsupport.ReplaceChunk( start, end, 'E', result_buffer )
  378. AssertBuffersAreEqualAsBytes( [ 'aAa',
  379. 'aECccccdd',
  380. 'ddaa' ], result_buffer )
  381. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  382. def ReplaceChunk_MultipleLinesToSameMultipleLines_test():
  383. result_buffer = VimBuffer( 'buffer', contents = [ 'aAa',
  384. 'aBa',
  385. 'aCa',
  386. 'aDe' ] )
  387. start, end = _BuildLocations( 2, 2, 3, 2 )
  388. vimsupport.ReplaceChunk( start, end, 'Eb\nbF', result_buffer )
  389. AssertBuffersAreEqualAsBytes( [ 'aAa',
  390. 'aEb',
  391. 'bFCa',
  392. 'aDe' ], result_buffer )
  393. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  394. def ReplaceChunk_MultipleLinesToMoreMultipleLines_test():
  395. result_buffer = VimBuffer( 'buffer', contents = [ 'aAa',
  396. 'aBa',
  397. 'aCa',
  398. 'aDe' ] )
  399. start, end = _BuildLocations( 2, 2, 3, 2 )
  400. vimsupport.ReplaceChunk( start, end, 'Eb\nbFb\nbG', result_buffer )
  401. AssertBuffersAreEqualAsBytes( [ 'aAa',
  402. 'aEb',
  403. 'bFb',
  404. 'bGCa',
  405. 'aDe' ], result_buffer )
  406. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  407. def ReplaceChunk_MultipleLinesToLessMultipleLines_test():
  408. result_buffer = VimBuffer( 'buffer', contents = [ 'aAa',
  409. 'aBa',
  410. 'aCa',
  411. 'aDe' ] )
  412. start, end = _BuildLocations( 1, 2, 3, 2 )
  413. vimsupport.ReplaceChunk( start, end, 'Eb\nbF', result_buffer )
  414. AssertBuffersAreEqualAsBytes( [ 'aEb',
  415. 'bFCa',
  416. 'aDe' ], result_buffer )
  417. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  418. def ReplaceChunk_MultipleLinesToEvenLessMultipleLines_test():
  419. result_buffer = VimBuffer( 'buffer', contents = [ 'aAa',
  420. 'aBa',
  421. 'aCa',
  422. 'aDe' ] )
  423. start, end = _BuildLocations( 1, 2, 4, 2 )
  424. vimsupport.ReplaceChunk( start, end, 'Eb\nbF', result_buffer )
  425. AssertBuffersAreEqualAsBytes( [ 'aEb',
  426. 'bFDe' ], result_buffer )
  427. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  428. def ReplaceChunk_SpanBufferEdge_test():
  429. result_buffer = VimBuffer( 'buffer', contents = [ 'aAa',
  430. 'aBa',
  431. 'aCa' ] )
  432. start, end = _BuildLocations( 1, 1, 1, 3 )
  433. vimsupport.ReplaceChunk( start, end, 'bDb', result_buffer )
  434. AssertBuffersAreEqualAsBytes( [ 'bDba',
  435. 'aBa',
  436. 'aCa' ], result_buffer )
  437. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  438. def ReplaceChunk_DeleteTextInLine_test():
  439. result_buffer = VimBuffer( 'buffer', contents = [ 'aAa',
  440. 'aBa',
  441. 'aCa' ] )
  442. start, end = _BuildLocations( 2, 2, 2, 3 )
  443. vimsupport.ReplaceChunk( start, end, '', result_buffer )
  444. AssertBuffersAreEqualAsBytes( [ 'aAa',
  445. 'aa',
  446. 'aCa' ], result_buffer )
  447. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  448. def ReplaceChunk_AddTextInLine_test():
  449. result_buffer = VimBuffer( 'buffer', contents = [ 'aAa',
  450. 'aBa',
  451. 'aCa' ] )
  452. start, end = _BuildLocations( 2, 2, 2, 2 )
  453. vimsupport.ReplaceChunk( start, end, 'bDb', result_buffer )
  454. AssertBuffersAreEqualAsBytes( [ 'aAa',
  455. 'abDbBa',
  456. 'aCa' ], result_buffer )
  457. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  458. def ReplaceChunk_ReplaceTextInLine_test():
  459. result_buffer = VimBuffer( 'buffer', contents = [ 'aAa',
  460. 'aBa',
  461. 'aCa' ] )
  462. start, end = _BuildLocations( 2, 2, 2, 3 )
  463. vimsupport.ReplaceChunk( start, end, 'bDb', result_buffer )
  464. AssertBuffersAreEqualAsBytes( [ 'aAa',
  465. 'abDba',
  466. 'aCa' ], result_buffer )
  467. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  468. def ReplaceChunk_NewlineChunk_test():
  469. result_buffer = VimBuffer( 'buffer', contents = [ 'first line',
  470. 'second line' ] )
  471. start, end = _BuildLocations( 1, 11, 2, 1 )
  472. vimsupport.ReplaceChunk( start, end, '\n', result_buffer )
  473. AssertBuffersAreEqualAsBytes( [ 'first line',
  474. 'second line' ], result_buffer )
  475. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  476. def ReplaceChunk_BeyondEndOfFile_test():
  477. result_buffer = VimBuffer( 'buffer', contents = [ 'first line',
  478. 'second line' ] )
  479. start, end = _BuildLocations( 1, 11, 3, 1 )
  480. vimsupport.ReplaceChunk( start, end, '\n', result_buffer )
  481. AssertBuffersAreEqualAsBytes( [ 'first line' ], result_buffer )
  482. @patch( 'vim.current.window.cursor', ( 1, 3 ) )
  483. def ReplaceChunk_CursorPosition_test():
  484. result_buffer = VimBuffer( 'buffer', contents = [ 'bar' ] )
  485. start, end = _BuildLocations( 1, 1, 1, 1 )
  486. vimsupport.ReplaceChunk( start,
  487. end,
  488. 'xyz\nfoo',
  489. result_buffer )
  490. AssertBuffersAreEqualAsBytes( [ 'xyz', 'foobar' ], result_buffer )
  491. # Cursor line is 0-based.
  492. assert_that( vimsupport.CurrentLineAndColumn(), contains( 1, 6 ) )
  493. def _BuildLocations( start_line, start_column, end_line, end_column ):
  494. return {
  495. 'line_num' : start_line,
  496. 'column_num': start_column,
  497. }, {
  498. 'line_num' : end_line,
  499. 'column_num': end_column,
  500. }
  501. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  502. def ReplaceChunksInBuffer_SortedChunks_test():
  503. chunks = [
  504. _BuildChunk( 1, 4, 1, 4, '(' ),
  505. _BuildChunk( 1, 11, 1, 11, ')' )
  506. ]
  507. result_buffer = VimBuffer( 'buffer', contents = [ 'CT<10 >> 2> ct' ] )
  508. vimsupport.ReplaceChunksInBuffer( chunks, result_buffer )
  509. AssertBuffersAreEqualAsBytes( [ 'CT<(10 >> 2)> ct' ], result_buffer )
  510. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  511. def ReplaceChunksInBuffer_UnsortedChunks_test():
  512. chunks = [
  513. _BuildChunk( 1, 11, 1, 11, ')' ),
  514. _BuildChunk( 1, 4, 1, 4, '(' )
  515. ]
  516. result_buffer = VimBuffer( 'buffer', contents = [ 'CT<10 >> 2> ct' ] )
  517. vimsupport.ReplaceChunksInBuffer( chunks, result_buffer )
  518. AssertBuffersAreEqualAsBytes( [ 'CT<(10 >> 2)> ct' ], result_buffer )
  519. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  520. def ReplaceChunksInBuffer_LineOverlappingChunks_test():
  521. chunks = [
  522. _BuildChunk( 1, 11, 2, 1, '\n ' ),
  523. _BuildChunk( 2, 12, 3, 1, '\n ' ),
  524. _BuildChunk( 3, 11, 4, 1, '\n ' )
  525. ]
  526. result_buffer = VimBuffer( 'buffer', contents = [ 'first line',
  527. 'second line',
  528. 'third line',
  529. 'fourth line' ] )
  530. vimsupport.ReplaceChunksInBuffer( chunks, result_buffer )
  531. AssertBuffersAreEqualAsBytes( [ 'first line',
  532. ' second line',
  533. ' third line',
  534. ' fourth line' ], result_buffer )
  535. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  536. def ReplaceChunksInBuffer_OutdentChunks_test():
  537. chunks = [
  538. _BuildChunk( 1, 1, 1, 5, ' ' ),
  539. _BuildChunk( 1, 15, 2, 9, '\n ' ),
  540. _BuildChunk( 2, 20, 3, 3, '\n' )
  541. ]
  542. result_buffer = VimBuffer( 'buffer', contents = [ ' first line',
  543. ' second line',
  544. ' third line' ] )
  545. vimsupport.ReplaceChunksInBuffer( chunks, result_buffer )
  546. AssertBuffersAreEqualAsBytes( [ ' first line',
  547. ' second line',
  548. ' third line' ], result_buffer )
  549. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  550. def ReplaceChunksInBuffer_OneLineIndentingChunks_test():
  551. chunks = [
  552. _BuildChunk( 1, 8, 2, 1, '\n ' ),
  553. _BuildChunk( 2, 9, 2, 10, '\n ' ),
  554. _BuildChunk( 2, 19, 2, 20, '\n ' )
  555. ]
  556. result_buffer = VimBuffer( 'buffer', contents = [ 'class {',
  557. 'method { statement }',
  558. '}' ] )
  559. vimsupport.ReplaceChunksInBuffer( chunks, result_buffer )
  560. AssertBuffersAreEqualAsBytes( [ 'class {',
  561. ' method {',
  562. ' statement',
  563. ' }',
  564. '}' ], result_buffer )
  565. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  566. def ReplaceChunksInBuffer_SameLocation_test():
  567. chunks = [
  568. _BuildChunk( 1, 1, 1, 1, 'this ' ),
  569. _BuildChunk( 1, 1, 1, 1, 'is ' ),
  570. _BuildChunk( 1, 1, 1, 1, 'pure ' )
  571. ]
  572. result_buffer = VimBuffer( 'buffer', contents = [ 'folly' ] )
  573. vimsupport.ReplaceChunksInBuffer( chunks, result_buffer )
  574. AssertBuffersAreEqualAsBytes( [ 'this is pure folly' ], result_buffer )
  575. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  576. @patch( 'ycm.vimsupport.VariableExists', return_value = False )
  577. @patch( 'ycm.vimsupport.SetFittingHeightForCurrentWindow' )
  578. @patch( 'ycm.vimsupport.GetBufferNumberForFilename',
  579. return_value = 1,
  580. new_callable = ExtendedMock )
  581. @patch( 'ycm.vimsupport.BufferIsVisible',
  582. return_value = True,
  583. new_callable = ExtendedMock )
  584. @patch( 'ycm.vimsupport.OpenFilename' )
  585. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  586. @patch( 'vim.eval', new_callable = ExtendedMock )
  587. @patch( 'vim.command', new_callable = ExtendedMock )
  588. def ReplaceChunks_SingleFile_Open_test( vim_command,
  589. vim_eval,
  590. post_vim_message,
  591. open_filename,
  592. buffer_is_visible,
  593. get_buffer_number_for_filename,
  594. set_fitting_height,
  595. variable_exists ):
  596. single_buffer_name = os.path.realpath( 'single_file' )
  597. chunks = [
  598. _BuildChunk( 1, 1, 2, 1, 'replacement', single_buffer_name )
  599. ]
  600. result_buffer = VimBuffer(
  601. single_buffer_name,
  602. contents = [
  603. 'line1',
  604. 'line2',
  605. 'line3'
  606. ]
  607. )
  608. with patch( 'vim.buffers', [ None, result_buffer, None ] ):
  609. vimsupport.ReplaceChunks( chunks )
  610. # Ensure that we applied the replacement correctly
  611. eq_( result_buffer.GetLines(), [
  612. 'replacementline2',
  613. 'line3',
  614. ] )
  615. # GetBufferNumberForFilename is called twice:
  616. # - once to the check if we would require opening the file (so that we can
  617. # raise a warning)
  618. # - once whilst applying the changes
  619. get_buffer_number_for_filename.assert_has_exact_calls( [
  620. call( single_buffer_name ),
  621. call( single_buffer_name ),
  622. ] )
  623. # BufferIsVisible is called twice for the same reasons as above
  624. buffer_is_visible.assert_has_exact_calls( [
  625. call( 1 ),
  626. call( 1 ),
  627. ] )
  628. # we don't attempt to open any files
  629. open_filename.assert_not_called()
  630. # But we do set the quickfix list
  631. vim_eval.assert_has_exact_calls( [
  632. call( 'setqflist( {0} )'.format( json.dumps( [ {
  633. 'bufnr': 1,
  634. 'filename': single_buffer_name,
  635. 'lnum': 1,
  636. 'col': 1,
  637. 'text': 'replacement',
  638. 'type': 'F'
  639. } ] ) ) ),
  640. ] )
  641. # And it is ReplaceChunks that prints the message showing the number of
  642. # changes
  643. post_vim_message.assert_has_exact_calls( [
  644. call( 'Applied 1 changes', warning = False ),
  645. ] )
  646. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  647. @patch( 'ycm.vimsupport.VariableExists', return_value = False )
  648. @patch( 'ycm.vimsupport.SetFittingHeightForCurrentWindow' )
  649. @patch( 'ycm.vimsupport.GetBufferNumberForFilename',
  650. side_effect = [ -1, -1, 1 ],
  651. new_callable = ExtendedMock )
  652. @patch( 'ycm.vimsupport.BufferIsVisible',
  653. side_effect = [ False, False, True ],
  654. new_callable = ExtendedMock )
  655. @patch( 'ycm.vimsupport.OpenFilename',
  656. new_callable = ExtendedMock )
  657. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  658. @patch( 'ycm.vimsupport.Confirm',
  659. return_value = True,
  660. new_callable = ExtendedMock )
  661. @patch( 'vim.eval', return_value = 10, new_callable = ExtendedMock )
  662. @patch( 'vim.command', new_callable = ExtendedMock )
  663. def ReplaceChunks_SingleFile_NotOpen_test( vim_command,
  664. vim_eval,
  665. confirm,
  666. post_vim_message,
  667. open_filename,
  668. buffer_is_visible,
  669. get_buffer_number_for_filename,
  670. set_fitting_height,
  671. variable_exists ):
  672. single_buffer_name = os.path.realpath( 'single_file' )
  673. chunks = [
  674. _BuildChunk( 1, 1, 2, 1, 'replacement', single_buffer_name )
  675. ]
  676. result_buffer = VimBuffer(
  677. single_buffer_name,
  678. contents = [
  679. 'line1',
  680. 'line2',
  681. 'line3'
  682. ]
  683. )
  684. with patch( 'vim.buffers', [ None, result_buffer, None ] ):
  685. vimsupport.ReplaceChunks( chunks )
  686. # We checked if it was OK to open the file
  687. confirm.assert_has_exact_calls( [
  688. call( vimsupport.FIXIT_OPENING_BUFFERS_MESSAGE_FORMAT.format( 1 ) )
  689. ] )
  690. # Ensure that we applied the replacement correctly
  691. eq_( result_buffer.GetLines(), [
  692. 'replacementline2',
  693. 'line3',
  694. ] )
  695. # GetBufferNumberForFilename is called 3 times. The return values are set in
  696. # the @patch call above:
  697. # - once to the check if we would require opening the file (so that we can
  698. # raise a warning) (-1 return)
  699. # - once whilst applying the changes (-1 return)
  700. # - finally after calling OpenFilename (1 return)
  701. get_buffer_number_for_filename.assert_has_exact_calls( [
  702. call( single_buffer_name ),
  703. call( single_buffer_name ),
  704. call( single_buffer_name ),
  705. ] )
  706. # BufferIsVisible is called 3 times for the same reasons as above, with the
  707. # return of each one
  708. buffer_is_visible.assert_has_exact_calls( [
  709. call( -1 ),
  710. call( -1 ),
  711. call( 1 ),
  712. ] )
  713. # We open 'single_file' as expected.
  714. open_filename.assert_called_with( single_buffer_name, {
  715. 'focus': True,
  716. 'fix': True,
  717. 'size': 10
  718. } )
  719. # And close it again, then show the quickfix window.
  720. vim_command.assert_has_exact_calls( [
  721. call( 'lclose' ),
  722. call( 'hide' ),
  723. ] )
  724. # And update the quickfix list
  725. vim_eval.assert_has_exact_calls( [
  726. call( '&previewheight' ),
  727. call( 'setqflist( {0} )'.format( json.dumps( [ {
  728. 'bufnr': 1,
  729. 'filename': single_buffer_name,
  730. 'lnum': 1,
  731. 'col': 1,
  732. 'text': 'replacement',
  733. 'type': 'F'
  734. } ] ) ) ),
  735. ] )
  736. # And it is ReplaceChunks that prints the message showing the number of
  737. # changes
  738. post_vim_message.assert_has_exact_calls( [
  739. call( 'Applied 1 changes', warning = False ),
  740. ] )
  741. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  742. @patch( 'ycm.vimsupport.VariableExists', return_value = False )
  743. @patch( 'ycm.vimsupport.SetFittingHeightForCurrentWindow' )
  744. @patch( 'ycm.vimsupport.GetBufferNumberForFilename',
  745. side_effect = [ -1, 1 ],
  746. new_callable = ExtendedMock )
  747. @patch( 'ycm.vimsupport.BufferIsVisible',
  748. side_effect = [ False, True ],
  749. new_callable = ExtendedMock )
  750. @patch( 'ycm.vimsupport.OpenFilename',
  751. new_callable = ExtendedMock )
  752. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  753. @patch( 'ycm.vimsupport.Confirm',
  754. return_value = True,
  755. new_callable = ExtendedMock )
  756. @patch( 'vim.eval', return_value = 10, new_callable = ExtendedMock )
  757. @patch( 'vim.command', new_callable = ExtendedMock )
  758. def ReplaceChunks_SingleFile_NotOpen_Silent_test(
  759. vim_command,
  760. vim_eval,
  761. confirm,
  762. post_vim_message,
  763. open_filename,
  764. buffer_is_visible,
  765. get_buffer_number_for_filename,
  766. set_fitting_height,
  767. variable_exists ):
  768. # This test is the same as ReplaceChunks_SingleFile_NotOpen_test, but we pass
  769. # the silent flag, as used by post-complete actions, and shows the stuff we
  770. # _don't_ call in that case.
  771. single_buffer_name = os.path.realpath( 'single_file' )
  772. chunks = [
  773. _BuildChunk( 1, 1, 2, 1, 'replacement', single_buffer_name )
  774. ]
  775. result_buffer = VimBuffer(
  776. single_buffer_name,
  777. contents = [
  778. 'line1',
  779. 'line2',
  780. 'line3'
  781. ]
  782. )
  783. with patch( 'vim.buffers', [ None, result_buffer, None ] ):
  784. vimsupport.ReplaceChunks( chunks, silent=True )
  785. # We didn't check if it was OK to open the file (silent)
  786. confirm.assert_not_called()
  787. # Ensure that we applied the replacement correctly
  788. eq_( result_buffer.GetLines(), [
  789. 'replacementline2',
  790. 'line3',
  791. ] )
  792. # GetBufferNumberForFilename is called 2 times. The return values are set in
  793. # the @patch call above:
  794. # - once whilst applying the changes (-1 return)
  795. # - finally after calling OpenFilename (1 return)
  796. get_buffer_number_for_filename.assert_has_exact_calls( [
  797. call( single_buffer_name ),
  798. call( single_buffer_name ),
  799. ] )
  800. # BufferIsVisible is called 2 times for the same reasons as above, with the
  801. # return of each one
  802. buffer_is_visible.assert_has_exact_calls( [
  803. call( -1 ),
  804. call( 1 ),
  805. ] )
  806. # We open 'single_file' as expected.
  807. open_filename.assert_called_with( single_buffer_name, {
  808. 'focus': True,
  809. 'fix': True,
  810. 'size': 10
  811. } )
  812. # And close it again, but don't show the quickfix window
  813. vim_command.assert_has_exact_calls( [
  814. call( 'lclose' ),
  815. call( 'hide' ),
  816. ] )
  817. set_fitting_height.assert_not_called()
  818. # But we _don't_ update the QuickFix list
  819. vim_eval.assert_has_exact_calls( [
  820. call( '&previewheight' ),
  821. ] )
  822. # And we don't print a message either
  823. post_vim_message.assert_not_called()
  824. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  825. @patch( 'ycm.vimsupport.GetBufferNumberForFilename',
  826. side_effect = [ -1, -1, 1 ],
  827. new_callable = ExtendedMock )
  828. @patch( 'ycm.vimsupport.BufferIsVisible',
  829. side_effect = [ False, False, True ],
  830. new_callable = ExtendedMock )
  831. @patch( 'ycm.vimsupport.OpenFilename',
  832. new_callable = ExtendedMock )
  833. @patch( 'ycm.vimsupport.PostVimMessage',
  834. new_callable = ExtendedMock )
  835. @patch( 'ycm.vimsupport.Confirm',
  836. return_value = False,
  837. new_callable = ExtendedMock )
  838. @patch( 'vim.eval',
  839. return_value = 10,
  840. new_callable = ExtendedMock )
  841. @patch( 'vim.command', new_callable = ExtendedMock )
  842. def ReplaceChunks_User_Declines_To_Open_File_test(
  843. vim_command,
  844. vim_eval,
  845. confirm,
  846. post_vim_message,
  847. open_filename,
  848. buffer_is_visible,
  849. get_buffer_number_for_filename ):
  850. # Same as above, except the user selects Cancel when asked if they should
  851. # allow us to open lots of (ahem, 1) file.
  852. single_buffer_name = os.path.realpath( 'single_file' )
  853. chunks = [
  854. _BuildChunk( 1, 1, 2, 1, 'replacement', single_buffer_name )
  855. ]
  856. result_buffer = VimBuffer(
  857. single_buffer_name,
  858. contents = [
  859. 'line1',
  860. 'line2',
  861. 'line3'
  862. ]
  863. )
  864. with patch( 'vim.buffers', [ None, result_buffer, None ] ):
  865. vimsupport.ReplaceChunks( chunks )
  866. # We checked if it was OK to open the file
  867. confirm.assert_has_exact_calls( [
  868. call( vimsupport.FIXIT_OPENING_BUFFERS_MESSAGE_FORMAT.format( 1 ) )
  869. ] )
  870. # Ensure that buffer is not changed
  871. eq_( result_buffer.GetLines(), [
  872. 'line1',
  873. 'line2',
  874. 'line3',
  875. ] )
  876. # GetBufferNumberForFilename is called once. The return values are set in
  877. # the @patch call above:
  878. # - once to the check if we would require opening the file (so that we can
  879. # raise a warning) (-1 return)
  880. get_buffer_number_for_filename.assert_has_exact_calls( [
  881. call( single_buffer_name ),
  882. ] )
  883. # BufferIsVisible is called once for the above file, which wasn't visible.
  884. buffer_is_visible.assert_has_exact_calls( [
  885. call( -1 ),
  886. ] )
  887. # We don't attempt to open any files or update any quickfix list or anything
  888. # like that
  889. open_filename.assert_not_called()
  890. vim_eval.assert_not_called()
  891. vim_command.assert_not_called()
  892. post_vim_message.assert_not_called()
  893. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  894. @patch( 'ycm.vimsupport.GetBufferNumberForFilename',
  895. side_effect = [ -1, -1, 1 ],
  896. new_callable = ExtendedMock )
  897. # Key difference is here: In the final check, BufferIsVisible returns False
  898. @patch( 'ycm.vimsupport.BufferIsVisible',
  899. side_effect = [ False, False, False ],
  900. new_callable = ExtendedMock )
  901. @patch( 'ycm.vimsupport.OpenFilename',
  902. new_callable = ExtendedMock )
  903. @patch( 'ycm.vimsupport.PostVimMessage',
  904. new_callable = ExtendedMock )
  905. @patch( 'ycm.vimsupport.Confirm',
  906. return_value = True,
  907. new_callable = ExtendedMock )
  908. @patch( 'vim.eval',
  909. return_value = 10,
  910. new_callable = ExtendedMock )
  911. @patch( 'vim.command',
  912. new_callable = ExtendedMock )
  913. def ReplaceChunks_User_Aborts_Opening_File_test(
  914. vim_command,
  915. vim_eval,
  916. confirm,
  917. post_vim_message,
  918. open_filename,
  919. buffer_is_visible,
  920. get_buffer_number_for_filename ):
  921. # Same as above, except the user selects Abort or Quick during the
  922. # "swap-file-found" dialog
  923. single_buffer_name = os.path.realpath( 'single_file' )
  924. chunks = [
  925. _BuildChunk( 1, 1, 2, 1, 'replacement', single_buffer_name )
  926. ]
  927. result_buffer = VimBuffer(
  928. single_buffer_name,
  929. contents = [
  930. 'line1',
  931. 'line2',
  932. 'line3'
  933. ]
  934. )
  935. with patch( 'vim.buffers', [ None, result_buffer, None ] ):
  936. assert_that( calling( vimsupport.ReplaceChunks ).with_args( chunks ),
  937. raises( RuntimeError,
  938. 'Unable to open file: .+single_file\n'
  939. 'FixIt/Refactor operation aborted prior to completion. '
  940. 'Your files have not been fully updated. '
  941. 'Please use undo commands to revert the applied changes.' ) )
  942. # We checked if it was OK to open the file
  943. confirm.assert_has_exact_calls( [
  944. call( vimsupport.FIXIT_OPENING_BUFFERS_MESSAGE_FORMAT.format( 1 ) )
  945. ] )
  946. # Ensure that buffer is not changed
  947. eq_( result_buffer.GetLines(), [
  948. 'line1',
  949. 'line2',
  950. 'line3',
  951. ] )
  952. # We tried to open this file
  953. open_filename.assert_called_with( single_buffer_name, {
  954. 'focus': True,
  955. 'fix': True,
  956. 'size': 10
  957. } )
  958. vim_eval.assert_called_with( "&previewheight" )
  959. # But raised an exception before issuing the message at the end
  960. post_vim_message.assert_not_called()
  961. @patch( 'vim.current.window.cursor', ( 1, 1 ) )
  962. @patch( 'ycm.vimsupport.VariableExists', return_value = False )
  963. @patch( 'ycm.vimsupport.SetFittingHeightForCurrentWindow' )
  964. @patch( 'ycm.vimsupport.GetBufferNumberForFilename', side_effect = [
  965. 22, # first_file (check)
  966. -1, # second_file (check)
  967. 22, # first_file (apply)
  968. -1, # second_file (apply)
  969. 19, # second_file (check after open)
  970. ],
  971. new_callable = ExtendedMock )
  972. @patch( 'ycm.vimsupport.BufferIsVisible', side_effect = [
  973. True, # first_file (check)
  974. False, # second_file (check)
  975. True, # first_file (apply)
  976. False, # second_file (apply)
  977. True, # side_effect (check after open)
  978. ],
  979. new_callable = ExtendedMock )
  980. @patch( 'ycm.vimsupport.OpenFilename',
  981. new_callable = ExtendedMock )
  982. @patch( 'ycm.vimsupport.PostVimMessage',
  983. new_callable = ExtendedMock )
  984. @patch( 'ycm.vimsupport.Confirm', return_value = True,
  985. new_callable = ExtendedMock )
  986. @patch( 'vim.eval', return_value = 10,
  987. new_callable = ExtendedMock )
  988. @patch( 'vim.command',
  989. new_callable = ExtendedMock )
  990. def ReplaceChunks_MultiFile_Open_test( vim_command,
  991. vim_eval,
  992. confirm,
  993. post_vim_message,
  994. open_filename,
  995. buffer_is_visible,
  996. get_buffer_number_for_filename,
  997. set_fitting_height,
  998. variable_exists ):
  999. # Chunks are split across 2 files, one is already open, one isn't
  1000. first_buffer_name = os.path.realpath( '1_first_file' )
  1001. second_buffer_name = os.path.realpath( '2_second_file' )
  1002. chunks = [
  1003. _BuildChunk( 1, 1, 2, 1, 'first_file_replacement ', first_buffer_name ),
  1004. _BuildChunk( 2, 1, 2, 1, 'second_file_replacement ', second_buffer_name ),
  1005. ]
  1006. first_file = VimBuffer(
  1007. first_buffer_name,
  1008. number = 22,
  1009. contents = [
  1010. 'line1',
  1011. 'line2',
  1012. 'line3',
  1013. ]
  1014. )
  1015. second_file = VimBuffer(
  1016. second_buffer_name,
  1017. number = 19,
  1018. contents = [
  1019. 'another line1',
  1020. 'ACME line2',
  1021. ]
  1022. )
  1023. vim_buffers = [ None ] * 23
  1024. vim_buffers[ 22 ] = first_file
  1025. vim_buffers[ 19 ] = second_file
  1026. with patch( 'vim.buffers', vim_buffers ):
  1027. vimsupport.ReplaceChunks( chunks )
  1028. # We checked for the right file names
  1029. get_buffer_number_for_filename.assert_has_exact_calls( [
  1030. call( first_buffer_name ),
  1031. call( second_buffer_name ),
  1032. call( first_buffer_name ),
  1033. call( second_buffer_name ),
  1034. call( second_buffer_name ),
  1035. ] )
  1036. # We checked if it was OK to open the file
  1037. confirm.assert_has_exact_calls( [
  1038. call( vimsupport.FIXIT_OPENING_BUFFERS_MESSAGE_FORMAT.format( 1 ) )
  1039. ] )
  1040. # Ensure that buffers are updated
  1041. eq_( second_file.GetLines(), [
  1042. 'another line1',
  1043. 'second_file_replacement ACME line2',
  1044. ] )
  1045. eq_( first_file.GetLines(), [
  1046. 'first_file_replacement line2',
  1047. 'line3',
  1048. ] )
  1049. # We open '2_second_file' as expected.
  1050. open_filename.assert_called_with( second_buffer_name, {
  1051. 'focus': True,
  1052. 'fix': True,
  1053. 'size': 10
  1054. } )
  1055. # And close it again, then show the quickfix window.
  1056. vim_command.assert_has_exact_calls( [
  1057. call( 'lclose' ),
  1058. call( 'hide' ),
  1059. ] )
  1060. # And update the quickfix list with each entry
  1061. vim_eval.assert_has_exact_calls( [
  1062. call( '&previewheight' ),
  1063. call( 'setqflist( {0} )'.format( json.dumps( [ {
  1064. 'bufnr': 22,
  1065. 'filename': first_buffer_name,
  1066. 'lnum': 1,
  1067. 'col': 1,
  1068. 'text': 'first_file_replacement ',
  1069. 'type': 'F'
  1070. }, {
  1071. 'bufnr': 19,
  1072. 'filename': second_buffer_name,
  1073. 'lnum': 2,
  1074. 'col': 1,
  1075. 'text': 'second_file_replacement ',
  1076. 'type': 'F'
  1077. } ] ) ) ),
  1078. ] )
  1079. # And it is ReplaceChunks that prints the message showing the number of
  1080. # changes
  1081. post_vim_message.assert_has_exact_calls( [
  1082. call( 'Applied 2 changes', warning = False ),
  1083. ] )
  1084. def _BuildChunk( start_line,
  1085. start_column,
  1086. end_line,
  1087. end_column,
  1088. replacement_text, filepath='test_file_name' ):
  1089. return {
  1090. 'range': {
  1091. 'start': {
  1092. 'filepath': filepath,
  1093. 'line_num': start_line,
  1094. 'column_num': start_column,
  1095. },
  1096. 'end': {
  1097. 'filepath': filepath,
  1098. 'line_num': end_line,
  1099. 'column_num': end_column,
  1100. },
  1101. },
  1102. 'replacement_text': replacement_text
  1103. }
  1104. def GetDiagnosticMatchPattern_ErrorInMiddleOfLine_test():
  1105. current_buffer = VimBuffer(
  1106. 'some_file',
  1107. contents = [ 'Highlight this error please' ]
  1108. )
  1109. with patch( 'vim.current.buffer', current_buffer ):
  1110. assert_that(
  1111. vimsupport.GetDiagnosticMatchPattern( 1, 16, 1, 21 ),
  1112. equal_to( '\\%1l\\%16c\\_.\\{-}\\%1l\\%21c' )
  1113. )
  1114. def AddDiagnosticSyntaxMatch_WarningAtEndOfLine_test():
  1115. current_buffer = VimBuffer(
  1116. 'some_file',
  1117. contents = [ 'Highlight this warning' ]
  1118. )
  1119. with patch( 'vim.current.buffer', current_buffer ):
  1120. assert_that(
  1121. vimsupport.GetDiagnosticMatchPattern( 1, 16, 1, 23 ),
  1122. equal_to( '\\%1l\\%16c\\_.\\{-}\\%1l\\%23c' )
  1123. )
  1124. def AddDiagnosticSyntaxMatch_UnicodeAtEndOfLine_test():
  1125. current_buffer = VimBuffer(
  1126. 'some_file',
  1127. contents = [ 'Highlight unicøde' ]
  1128. )
  1129. with patch( 'vim.current.buffer', current_buffer ):
  1130. assert_that(
  1131. vimsupport.GetDiagnosticMatchPattern( 1, 16, 1, 19 ),
  1132. equal_to( '\\%1l\\%16c\\_.\\{-}\\%1l\\%19c' )
  1133. )
  1134. @patch( 'vim.command', new_callable=ExtendedMock )
  1135. @patch( 'vim.current', new_callable=ExtendedMock )
  1136. def WriteToPreviewWindow_test( vim_current, vim_command ):
  1137. vim_current.window.options.__getitem__ = MagicMock( return_value = True )
  1138. vimsupport.WriteToPreviewWindow( "test" )
  1139. vim_command.assert_has_exact_calls( [
  1140. call( 'silent! pclose!' ),
  1141. call( 'silent! pedit! _TEMP_FILE_' ),
  1142. call( 'silent! wincmd P' ),
  1143. call( 'silent! wincmd p' ) ] )
  1144. vim_current.buffer.__setitem__.assert_called_with(
  1145. slice( None, None, None ), [ 'test' ] )
  1146. vim_current.buffer.options.__setitem__.assert_has_exact_calls( [
  1147. call( 'modifiable', True ),
  1148. call( 'readonly', False ),
  1149. call( 'buftype', 'nofile' ),
  1150. call( 'bufhidden', 'wipe' ),
  1151. call( 'buflisted', False ),
  1152. call( 'swapfile', False ),
  1153. call( 'modifiable', False ),
  1154. call( 'modified', False ),
  1155. call( 'readonly', True ),
  1156. ], any_order = True )
  1157. @patch( 'vim.current' )
  1158. def WriteToPreviewWindow_MultiLine_test( vim_current ):
  1159. vim_current.window.options.__getitem__ = MagicMock( return_value = True )
  1160. vimsupport.WriteToPreviewWindow( "test\ntest2" )
  1161. vim_current.buffer.__setitem__.assert_called_with(
  1162. slice( None, None, None ), [ 'test', 'test2' ] )
  1163. @patch( 'vim.command', new_callable=ExtendedMock )
  1164. @patch( 'vim.current', new_callable=ExtendedMock )
  1165. def WriteToPreviewWindow_JumpFail_test( vim_current, vim_command ):
  1166. vim_current.window.options.__getitem__ = MagicMock( return_value = False )
  1167. vimsupport.WriteToPreviewWindow( "test" )
  1168. vim_command.assert_has_exact_calls( [
  1169. call( 'silent! pclose!' ),
  1170. call( 'silent! pedit! _TEMP_FILE_' ),
  1171. call( 'silent! wincmd P' ),
  1172. call( 'redraw' ),
  1173. call( "echo 'test'" ),
  1174. ] )
  1175. vim_current.buffer.__setitem__.assert_not_called()
  1176. vim_current.buffer.options.__setitem__.assert_not_called()
  1177. @patch( 'vim.command', new_callable=ExtendedMock )
  1178. @patch( 'vim.current', new_callable=ExtendedMock )
  1179. def WriteToPreviewWindow_JumpFail_MultiLine_test( vim_current, vim_command ):
  1180. vim_current.window.options.__getitem__ = MagicMock( return_value = False )
  1181. vimsupport.WriteToPreviewWindow( "test\ntest2" )
  1182. vim_command.assert_has_exact_calls( [
  1183. call( 'silent! pclose!' ),
  1184. call( 'silent! pedit! _TEMP_FILE_' ),
  1185. call( 'silent! wincmd P' ),
  1186. call( 'redraw' ),
  1187. call( "echo 'test'" ),
  1188. call( "echo 'test2'" ),
  1189. ] )
  1190. vim_current.buffer.__setitem__.assert_not_called()
  1191. vim_current.buffer.options.__setitem__.assert_not_called()
  1192. def BufferIsVisibleForFilename_test():
  1193. visible_buffer = VimBuffer( 'visible_filename', number = 1 )
  1194. hidden_buffer = VimBuffer( 'hidden_filename', number = 2 )
  1195. with MockVimBuffers( [ visible_buffer, hidden_buffer ], [ visible_buffer ] ):
  1196. eq_( vimsupport.BufferIsVisibleForFilename( 'visible_filename' ), True )
  1197. eq_( vimsupport.BufferIsVisibleForFilename( 'hidden_filename' ), False )
  1198. eq_( vimsupport.BufferIsVisibleForFilename( 'another_filename' ), False )
  1199. def CloseBuffersForFilename_test():
  1200. current_buffer = VimBuffer( 'some_filename', number = 2 )
  1201. other_buffer = VimBuffer( 'some_filename', number = 5 )
  1202. with MockVimBuffers( [ current_buffer, other_buffer ],
  1203. [ current_buffer ] ) as vim:
  1204. vimsupport.CloseBuffersForFilename( 'some_filename' )
  1205. assert_that( vim.buffers, empty() )
  1206. @patch( 'vim.command', new_callable = ExtendedMock )
  1207. @patch( 'vim.current', new_callable = ExtendedMock )
  1208. def OpenFilename_test( vim_current, vim_command ):
  1209. # Options used to open a logfile.
  1210. options = {
  1211. 'size': vimsupport.GetIntValue( '&previewheight' ),
  1212. 'fix': True,
  1213. 'focus': False,
  1214. 'watch': True,
  1215. 'position': 'end'
  1216. }
  1217. vimsupport.OpenFilename( __file__, options )
  1218. vim_command.assert_has_exact_calls( [
  1219. call( '12split {0}'.format( __file__ ) ),
  1220. call( "exec "
  1221. "'au BufEnter <buffer> :silent! checktime {0}'".format( __file__ ) ),
  1222. call( 'silent! normal! Gzz' ),
  1223. call( 'silent! wincmd p' )
  1224. ] )
  1225. vim_current.buffer.options.__setitem__.assert_has_exact_calls( [
  1226. call( 'autoread', True ),
  1227. ] )
  1228. vim_current.window.options.__setitem__.assert_has_exact_calls( [
  1229. call( 'winfixheight', True )
  1230. ] )
  1231. def GetUnsavedAndSpecifiedBufferData_EncodedUnicodeCharsInBuffers_test():
  1232. filepath = os.path.realpath( 'filename' )
  1233. contents = [ ToBytes( u'abc' ), ToBytes( u'fДa' ) ]
  1234. vim_buffer = VimBuffer( filepath, contents = contents )
  1235. with patch( 'vim.buffers', [ vim_buffer ] ):
  1236. assert_that( vimsupport.GetUnsavedAndSpecifiedBufferData( vim_buffer,
  1237. filepath ),
  1238. has_entry( filepath,
  1239. has_entry( u'contents', u'abc\nfДa\n' ) ) )
  1240. def GetBufferFilepath_NoBufferName_UnicodeWorkingDirectory_test():
  1241. vim_buffer = VimBuffer( '', number = 42 )
  1242. unicode_dir = PathToTestFile( u'uni¢𐍈d€' )
  1243. with CurrentWorkingDirectory( unicode_dir ):
  1244. assert_that( vimsupport.GetBufferFilepath( vim_buffer ),
  1245. equal_to( os.path.join( unicode_dir, '42' ) ) )
  1246. # NOTE: Vim returns byte offsets for columns, not actual character columns. This
  1247. # makes 'ДД' have 4 columns: column 0, column 2 and column 4.
  1248. @patch( 'vim.current.line', ToBytes( 'ДДaa' ) )
  1249. @patch( 'ycm.vimsupport.CurrentColumn', side_effect = [ 4 ] )
  1250. def TextBeforeCursor_EncodedUnicode_test( *args ):
  1251. eq_( vimsupport.TextBeforeCursor(), u'ДД' )
  1252. # NOTE: Vim returns byte offsets for columns, not actual character columns. This
  1253. # makes 'ДД' have 4 columns: column 0, column 2 and column 4.
  1254. @patch( 'vim.current.line', ToBytes( 'aaДД' ) )
  1255. @patch( 'ycm.vimsupport.CurrentColumn', side_effect = [ 2 ] )
  1256. def TextAfterCursor_EncodedUnicode_test( *args ):
  1257. eq_( vimsupport.TextAfterCursor(), u'ДД' )
  1258. @patch( 'vim.current.line', ToBytes( 'fДa' ) )
  1259. def CurrentLineContents_EncodedUnicode_test( *args ):
  1260. eq_( vimsupport.CurrentLineContents(), u'fДa' )
  1261. @patch( 'vim.eval', side_effect = lambda x: x )
  1262. def VimExpressionToPythonType_IntAsUnicode_test( *args ):
  1263. eq_( vimsupport.VimExpressionToPythonType( '123' ), 123 )
  1264. @patch( 'vim.eval', side_effect = lambda x: x )
  1265. def VimExpressionToPythonType_IntAsBytes_test( *args ):
  1266. eq_( vimsupport.VimExpressionToPythonType( ToBytes( '123' ) ), 123 )
  1267. @patch( 'vim.eval', side_effect = lambda x: x )
  1268. def VimExpressionToPythonType_StringAsUnicode_test( *args ):
  1269. eq_( vimsupport.VimExpressionToPythonType( 'foo' ), 'foo' )
  1270. @patch( 'vim.eval', side_effect = lambda x: x )
  1271. def VimExpressionToPythonType_StringAsBytes_test( *args ):
  1272. eq_( vimsupport.VimExpressionToPythonType( ToBytes( 'foo' ) ), 'foo' )
  1273. @patch( 'vim.eval', side_effect = lambda x: x )
  1274. def VimExpressionToPythonType_ListPassthrough_test( *args ):
  1275. eq_( vimsupport.VimExpressionToPythonType( [ 1, 2 ] ), [ 1, 2 ] )
  1276. @patch( 'vim.eval', side_effect = lambda x: x )
  1277. def VimExpressionToPythonType_ObjectPassthrough_test( *args ):
  1278. eq_( vimsupport.VimExpressionToPythonType( { 1: 2 } ), { 1: 2 } )
  1279. @patch( 'vim.eval', side_effect = lambda x: x )
  1280. def VimExpressionToPythonType_GeneratorPassthrough_test( *args ):
  1281. gen = ( x**2 for x in [ 1, 2, 3 ] )
  1282. eq_( vimsupport.VimExpressionToPythonType( gen ), gen )
  1283. @patch( 'vim.eval',
  1284. new_callable = ExtendedMock,
  1285. side_effect = [ None, 2, None ] )
  1286. def SelectFromList_LastItem_test( vim_eval ):
  1287. eq_( vimsupport.SelectFromList( 'test', [ 'a', 'b' ] ),
  1288. 1 )
  1289. vim_eval.assert_has_exact_calls( [
  1290. call( 'inputsave()' ),
  1291. call( 'inputlist( ["test", "1: a", "2: b"] )' ),
  1292. call( 'inputrestore()' )
  1293. ] )
  1294. @patch( 'vim.eval',
  1295. new_callable = ExtendedMock,
  1296. side_effect = [ None, 1, None ] )
  1297. def SelectFromList_FirstItem_test( vim_eval ):
  1298. eq_( vimsupport.SelectFromList( 'test', [ 'a', 'b' ] ),
  1299. 0 )
  1300. vim_eval.assert_has_exact_calls( [
  1301. call( 'inputsave()' ),
  1302. call( 'inputlist( ["test", "1: a", "2: b"] )' ),
  1303. call( 'inputrestore()' )
  1304. ] )
  1305. @patch( 'vim.eval', side_effect = [ None, 3, None ] )
  1306. def SelectFromList_OutOfRange_test( vim_eval ):
  1307. assert_that( calling( vimsupport.SelectFromList ).with_args( 'test',
  1308. [ 'a', 'b' ] ),
  1309. raises( RuntimeError, vimsupport.NO_SELECTION_MADE_MSG ) )
  1310. @patch( 'vim.eval', side_effect = [ None, 0, None ] )
  1311. def SelectFromList_SelectPrompt_test( vim_eval ):
  1312. assert_that( calling( vimsupport.SelectFromList ).with_args( 'test',
  1313. [ 'a', 'b' ] ),
  1314. raises( RuntimeError, vimsupport.NO_SELECTION_MADE_MSG ) )
  1315. @patch( 'vim.eval', side_effect = [ None, -199, None ] )
  1316. def SelectFromList_Negative_test( vim_eval ):
  1317. assert_that( calling( vimsupport.SelectFromList ).with_args( 'test',
  1318. [ 'a', 'b' ] ),
  1319. raises( RuntimeError, vimsupport.NO_SELECTION_MADE_MSG ) )
  1320. def Filetypes_IntegerFiletype_test():
  1321. current_buffer = VimBuffer( 'buffer', number = 1, filetype = '42' )
  1322. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  1323. assert_that( vimsupport.CurrentFiletypes(), contains( '42' ) )
  1324. assert_that( vimsupport.GetBufferFiletypes( 1 ), contains( '42' ) )
  1325. assert_that( vimsupport.FiletypesForBuffer( current_buffer ),
  1326. contains( '42' ) )
  1327. @patch( 'ycm.vimsupport.VariableExists', return_value = False )
  1328. @patch( 'ycm.vimsupport.SearchInCurrentBuffer', return_value = 0 )
  1329. @patch( 'vim.current' )
  1330. def InsertNamespace_insert_test( vim_current, *args ):
  1331. contents = [ '',
  1332. 'namespace Taqueria {',
  1333. '',
  1334. ' int taco = Math' ]
  1335. vim_current.buffer = VimBuffer( '', contents = contents )
  1336. vim_current.window.cursor = ( 1, 1 )
  1337. vimsupport.InsertNamespace( 'System' )
  1338. expected_buffer = [ 'using System;',
  1339. '',
  1340. 'namespace Taqueria {',
  1341. '',
  1342. ' int taco = Math' ]
  1343. AssertBuffersAreEqualAsBytes( expected_buffer, vim_current.buffer )
  1344. @patch( 'ycm.vimsupport.VariableExists', return_value = False )
  1345. @patch( 'ycm.vimsupport.SearchInCurrentBuffer', return_value = 2 )
  1346. @patch( 'vim.current' )
  1347. def InsertNamespace_append_test( vim_current, *args ):
  1348. contents = [ 'namespace Taqueria {',
  1349. ' using System;',
  1350. '',
  1351. ' class Tasty {',
  1352. ' int taco;',
  1353. ' List salad = new List' ]
  1354. vim_current.buffer = VimBuffer( '', contents = contents )
  1355. vim_current.window.cursor = ( 1, 1 )
  1356. vimsupport.InsertNamespace( 'System.Collections' )
  1357. expected_buffer = [ 'namespace Taqueria {',
  1358. ' using System;',
  1359. ' using System.Collections;',
  1360. '',
  1361. ' class Tasty {',
  1362. ' int taco;',
  1363. ' List salad = new List' ]
  1364. AssertBuffersAreEqualAsBytes( expected_buffer, vim_current.buffer )
  1365. @patch( 'vim.command', new_callable = ExtendedMock )
  1366. def JumpToLocation_SameFile_SameBuffer_NoSwapFile_test( vim_command ):
  1367. # No 'u' prefix for the current buffer name string to simulate Vim returning
  1368. # bytes on Python 2 but unicode on Python 3.
  1369. current_buffer = VimBuffer( 'uni¢𐍈d€' )
  1370. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ) as vim:
  1371. vimsupport.JumpToLocation( os.path.realpath( u'uni¢𐍈d€' ),
  1372. 2,
  1373. 5,
  1374. 'aboveleft',
  1375. 'same-buffer' )
  1376. assert_that( vim.current.window.cursor, equal_to( ( 2, 4 ) ) )
  1377. vim_command.assert_has_exact_calls( [
  1378. call( 'normal! m\'' ),
  1379. call( 'normal! zz' )
  1380. ] )
  1381. @patch( 'vim.command', new_callable = ExtendedMock )
  1382. def JumpToLocation_DifferentFile_SameBuffer_Unmodified_test( vim_command ):
  1383. current_buffer = VimBuffer( 'uni¢𐍈d€' )
  1384. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ) as vim:
  1385. target_name = os.path.realpath( u'different_uni¢𐍈d€' )
  1386. vimsupport.JumpToLocation( target_name, 2, 5, 'belowright', 'same-buffer' )
  1387. assert_that( vim.current.window.cursor, equal_to( ( 2, 4 ) ) )
  1388. vim_command.assert_has_exact_calls( [
  1389. call( 'normal! m\'' ),
  1390. call( u'keepjumps belowright edit {0}'.format( target_name ) ),
  1391. call( 'normal! zz' )
  1392. ] )
  1393. @patch( 'vim.command', new_callable = ExtendedMock )
  1394. def JumpToLocation_DifferentFile_SameBuffer_Modified_CannotHide_test(
  1395. vim_command ):
  1396. current_buffer = VimBuffer( 'uni¢𐍈d€', modified = True )
  1397. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ) as vim:
  1398. target_name = os.path.realpath( u'different_uni¢𐍈d€' )
  1399. vimsupport.JumpToLocation( target_name, 2, 5, 'botright', 'same-buffer' )
  1400. assert_that( vim.current.window.cursor, equal_to( ( 2, 4 ) ) )
  1401. vim_command.assert_has_exact_calls( [
  1402. call( 'normal! m\'' ),
  1403. call( u'keepjumps botright split {0}'.format( target_name ) ),
  1404. call( 'normal! zz' )
  1405. ] )
  1406. @patch( 'vim.command', new_callable = ExtendedMock )
  1407. def JumpToLocation_DifferentFile_SameBuffer_Modified_CanHide_test(
  1408. vim_command ):
  1409. current_buffer = VimBuffer( 'uni¢𐍈d€', modified = True, bufhidden = "hide" )
  1410. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ) as vim:
  1411. target_name = os.path.realpath( u'different_uni¢𐍈d€' )
  1412. vimsupport.JumpToLocation( target_name, 2, 5, 'leftabove', 'same-buffer' )
  1413. assert_that( vim.current.window.cursor, equal_to( ( 2, 4 ) ) )
  1414. vim_command.assert_has_exact_calls( [
  1415. call( 'normal! m\'' ),
  1416. call( u'keepjumps leftabove edit {0}'.format( target_name ) ),
  1417. call( 'normal! zz' )
  1418. ] )
  1419. @patch( 'vim.command',
  1420. side_effect = [ None, VimError( 'Unknown code' ), None ] )
  1421. def JumpToLocation_DifferentFile_SameBuffer_SwapFile_Unexpected_test(
  1422. vim_command ):
  1423. current_buffer = VimBuffer( 'uni¢𐍈d€' )
  1424. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  1425. assert_that(
  1426. calling( vimsupport.JumpToLocation ).with_args(
  1427. os.path.realpath( u'different_uni¢𐍈d€' ),
  1428. 2,
  1429. 5,
  1430. 'rightbelow',
  1431. 'same-buffer' ),
  1432. raises( VimError, 'Unknown code' )
  1433. )
  1434. @patch( 'vim.command',
  1435. new_callable = ExtendedMock,
  1436. side_effect = [ None, VimError( 'E325' ), None ] )
  1437. def JumpToLocation_DifferentFile_SameBuffer_SwapFile_Quit_test( vim_command ):
  1438. current_buffer = VimBuffer( 'uni¢𐍈d€' )
  1439. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  1440. target_name = os.path.realpath( u'different_uni¢𐍈d€' )
  1441. vimsupport.JumpToLocation( target_name, 2, 5, 'topleft', 'same-buffer' )
  1442. vim_command.assert_has_exact_calls( [
  1443. call( 'normal! m\'' ),
  1444. call( u'keepjumps topleft edit {0}'.format( target_name ) )
  1445. ] )
  1446. @patch( 'vim.command',
  1447. new_callable = ExtendedMock,
  1448. side_effect = [ None, KeyboardInterrupt, None ] )
  1449. def JumpToLocation_DifferentFile_SameBuffer_SwapFile_Abort_test( vim_command ):
  1450. current_buffer = VimBuffer( 'uni¢𐍈d€' )
  1451. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  1452. target_name = os.path.realpath( u'different_uni¢𐍈d€' )
  1453. vimsupport.JumpToLocation( target_name, 2, 5, 'vertical', 'same-buffer' )
  1454. vim_command.assert_has_exact_calls( [
  1455. call( 'normal! m\'' ),
  1456. call( u'keepjumps vertical edit {0}'.format( target_name ) )
  1457. ] )
  1458. @patch( 'vim.command', new_callable = ExtendedMock )
  1459. def JumpToLocation_DifferentFile_Split_CurrentTab_NotAlreadyOpened_test(
  1460. vim_command ):
  1461. current_buffer = VimBuffer( 'uni¢𐍈d€' )
  1462. current_window = MagicMock( buffer = current_buffer )
  1463. current_tab = MagicMock( windows = [ current_window ] )
  1464. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ) as vim:
  1465. vim.current.tabpage = current_tab
  1466. target_name = os.path.realpath( u'different_uni¢𐍈d€' )
  1467. vimsupport.JumpToLocation( target_name,
  1468. 2,
  1469. 5,
  1470. 'aboveleft',
  1471. 'split-or-existing-window' )
  1472. vim_command.assert_has_exact_calls( [
  1473. call( 'normal! m\'' ),
  1474. call( u'keepjumps aboveleft split {0}'.format( target_name ) ),
  1475. call( 'normal! zz' )
  1476. ] )
  1477. @patch( 'vim.command', new_callable = ExtendedMock )
  1478. def JumpToLocation_DifferentFile_Split_CurrentTab_AlreadyOpened_test(
  1479. vim_command ):
  1480. current_buffer = VimBuffer( 'uni¢𐍈d€' )
  1481. different_buffer = VimBuffer( 'different_uni¢𐍈d€' )
  1482. current_window = MagicMock( buffer = current_buffer )
  1483. different_window = MagicMock( buffer = different_buffer )
  1484. current_tab = MagicMock( windows = [ current_window, different_window ] )
  1485. with MockVimBuffers( [ current_buffer, different_buffer ],
  1486. [ current_buffer ] ) as vim:
  1487. vim.current.tabpage = current_tab
  1488. vimsupport.JumpToLocation( os.path.realpath( u'different_uni¢𐍈d€' ),
  1489. 2,
  1490. 5,
  1491. 'belowright',
  1492. 'split-or-existing-window' )
  1493. assert_that( vim.current.tabpage, equal_to( current_tab ) )
  1494. assert_that( vim.current.window, equal_to( different_window ) )
  1495. assert_that( vim.current.window.cursor, equal_to( ( 2, 4 ) ) )
  1496. vim_command.assert_has_exact_calls( [
  1497. call( 'normal! m\'' ),
  1498. call( 'normal! zz' )
  1499. ] )
  1500. @WindowsAndMacOnly
  1501. @patch( 'vim.command', new_callable = ExtendedMock )
  1502. def JumpToLocation_DifferentFile_Split_CurrentTab_AlreadyOpened_Case_test(
  1503. vim_command ):
  1504. current_buffer = VimBuffer( 'current_buffer' )
  1505. different_buffer = VimBuffer( 'AnotHer_buFfeR' )
  1506. current_window = MagicMock( buffer = current_buffer )
  1507. different_window = MagicMock( buffer = different_buffer )
  1508. current_tab = MagicMock( windows = [ current_window, different_window ] )
  1509. with MockVimBuffers( [ current_buffer, different_buffer ],
  1510. [ current_buffer ] ) as vim:
  1511. vim.current.tabpage = current_tab
  1512. vimsupport.JumpToLocation( os.path.realpath( 'anOther_BuffEr' ),
  1513. 4,
  1514. 1,
  1515. 'belowright',
  1516. 'split-or-existing-window' )
  1517. assert_that( vim.current.tabpage, equal_to( current_tab ) )
  1518. assert_that( vim.current.window, equal_to( different_window ) )
  1519. assert_that( vim.current.window.cursor, equal_to( ( 4, 0 ) ) )
  1520. vim_command.assert_has_exact_calls( [
  1521. call( 'normal! m\'' ),
  1522. call( 'normal! zz' )
  1523. ] )
  1524. @patch( 'vim.command', new_callable = ExtendedMock )
  1525. def JumpToLocation_DifferentFile_Split_AllTabs_NotAlreadyOpened_test(
  1526. vim_command ):
  1527. current_buffer = VimBuffer( 'uni¢𐍈d€' )
  1528. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  1529. target_name = os.path.realpath( u'different_uni¢𐍈d€' )
  1530. vimsupport.JumpToLocation( target_name,
  1531. 2,
  1532. 5,
  1533. 'tab',
  1534. 'split-or-existing-window' )
  1535. vim_command.assert_has_exact_calls( [
  1536. call( 'normal! m\'' ),
  1537. call( u'keepjumps tab split {0}'.format( target_name ) ),
  1538. call( 'normal! zz' )
  1539. ] )
  1540. @patch( 'vim.command', new_callable = ExtendedMock )
  1541. def JumpToLocation_DifferentFile_Split_AllTabs_AlreadyOpened_test(
  1542. vim_command ):
  1543. current_buffer = VimBuffer( 'uni¢𐍈d€' )
  1544. different_buffer = VimBuffer( 'different_uni¢𐍈d€' )
  1545. current_window = MagicMock( buffer = current_buffer )
  1546. different_window = MagicMock( buffer = different_buffer )
  1547. current_tab = MagicMock( windows = [ current_window, different_window ] )
  1548. with patch( 'vim.tabpages', [ current_tab ] ):
  1549. with MockVimBuffers( [ current_buffer, different_buffer ],
  1550. [ current_buffer ] ) as vim:
  1551. vimsupport.JumpToLocation( os.path.realpath( u'different_uni¢𐍈d€' ),
  1552. 2,
  1553. 5,
  1554. 'tab',
  1555. 'split-or-existing-window' )
  1556. assert_that( vim.current.tabpage, equal_to( current_tab ) )
  1557. assert_that( vim.current.window, equal_to( different_window ) )
  1558. assert_that( vim.current.window.cursor, equal_to( ( 2, 4 ) ) )
  1559. vim_command.assert_has_exact_calls( [
  1560. call( 'normal! m\'' ),
  1561. call( 'normal! zz' )
  1562. ] )
  1563. @patch( 'vim.command', new_callable = ExtendedMock )
  1564. def JumpToLocation_DifferentFile_NewOrExistingTab_NotAlreadyOpened_test(
  1565. vim_command ):
  1566. current_buffer = VimBuffer( 'uni¢𐍈d€' )
  1567. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  1568. target_name = os.path.realpath( u'different_uni¢𐍈d€' )
  1569. vimsupport.JumpToLocation( target_name,
  1570. 2,
  1571. 5,
  1572. 'aboveleft vertical',
  1573. 'new-or-existing-tab' )
  1574. vim_command.assert_has_exact_calls( [
  1575. call( 'normal! m\'' ),
  1576. call( u'keepjumps aboveleft vertical tabedit {0}'.format( target_name ) ),
  1577. call( 'normal! zz' )
  1578. ] )
  1579. @patch( 'vim.command', new_callable = ExtendedMock )
  1580. def JumpToLocation_DifferentFile_NewOrExistingTab_AlreadyOpened_test(
  1581. vim_command ):
  1582. current_buffer = VimBuffer( 'uni¢𐍈d€' )
  1583. different_buffer = VimBuffer( 'different_uni¢𐍈d€' )
  1584. current_window = MagicMock( buffer = current_buffer )
  1585. different_window = MagicMock( buffer = different_buffer )
  1586. current_tab = MagicMock( windows = [ current_window, different_window ] )
  1587. with patch( 'vim.tabpages', [ current_tab ] ):
  1588. with MockVimBuffers( [ current_buffer, different_buffer ],
  1589. [ current_buffer ] ) as vim:
  1590. vimsupport.JumpToLocation( os.path.realpath( u'different_uni¢𐍈d€' ),
  1591. 2,
  1592. 5,
  1593. 'belowright tab',
  1594. 'new-or-existing-tab' )
  1595. assert_that( vim.current.tabpage, equal_to( current_tab ) )
  1596. assert_that( vim.current.window, equal_to( different_window ) )
  1597. assert_that( vim.current.window.cursor, equal_to( ( 2, 4 ) ) )
  1598. vim_command.assert_has_exact_calls( [
  1599. call( 'normal! m\'' ),
  1600. call( 'normal! zz' )
  1601. ] )
  1602. @patch( 'ycm.tests.test_utils.VIM_VERSION', Version( 7, 4, 1578 ) )
  1603. def VimVersionAtLeast_test():
  1604. assert_that( vimsupport.VimVersionAtLeast( '7.3.414' ) )
  1605. assert_that( vimsupport.VimVersionAtLeast( '7.4.1578' ) )
  1606. assert_that( not vimsupport.VimVersionAtLeast( '7.4.1579' ) )
  1607. assert_that( not vimsupport.VimVersionAtLeast( '7.4.1898' ) )
  1608. assert_that( not vimsupport.VimVersionAtLeast( '8.1.278' ) )