completion_test.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. # Copyright (C) 2016 YouCompleteMe contributors
  2. #
  3. # This file is part of YouCompleteMe.
  4. #
  5. # YouCompleteMe is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # YouCompleteMe is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  17. from ycm.tests.test_utils import ( CurrentWorkingDirectory, ExtendedMock,
  18. MockVimModule, MockVimBuffers, VimBuffer )
  19. MockVimModule()
  20. import contextlib
  21. from hamcrest import ( assert_that,
  22. contains_exactly,
  23. empty,
  24. equal_to,
  25. has_entries )
  26. from unittest import TestCase
  27. from unittest.mock import call, MagicMock, patch
  28. from ycm.tests import PathToTestFile, YouCompleteMeInstance
  29. from ycmd.responses import ServerError
  30. import json
  31. @contextlib.contextmanager
  32. def MockCompletionRequest( response_method ):
  33. """Mock out the CompletionRequest, replacing the response handler
  34. JsonFromFuture with the |response_method| parameter."""
  35. # We don't want the requests to actually be sent to the server, just have it
  36. # return success.
  37. with patch( 'ycm.client.completer_available_request.'
  38. 'CompleterAvailableRequest.PostDataToHandler',
  39. return_value = True ):
  40. with patch( 'ycm.client.completion_request.CompletionRequest.'
  41. 'PostDataToHandlerAsync',
  42. return_value = MagicMock( return_value=True ) ):
  43. # We set up a fake response.
  44. with patch( 'ycm.client.base_request._JsonFromFuture',
  45. side_effect = response_method ):
  46. yield
  47. @contextlib.contextmanager
  48. def MockResolveRequest( response_method ):
  49. """Mock out the CompletionRequest, replacing the response handler
  50. JsonFromFuture with the |response_method| parameter."""
  51. with patch( 'ycm.client.resolve_completion_request.ResolveCompletionRequest.'
  52. 'PostDataToHandlerAsync',
  53. return_value = MagicMock( return_value=True ) ):
  54. # We set up a fake response.
  55. with patch( 'ycm.client.base_request._JsonFromFuture',
  56. side_effect = response_method ):
  57. yield
  58. class CompletionTest( TestCase ):
  59. @YouCompleteMeInstance()
  60. def test_SendCompletionRequest_UnicodeWorkingDirectory( self, ycm ):
  61. unicode_dir = PathToTestFile( 'uni¢od€' )
  62. current_buffer = VimBuffer( PathToTestFile( 'uni¢𐍈d€', 'current_buffer' ) )
  63. def ServerResponse( *args ):
  64. return { 'completions': [], 'completion_start_column': 1 }
  65. with CurrentWorkingDirectory( unicode_dir ):
  66. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  67. with MockCompletionRequest( ServerResponse ):
  68. ycm.SendCompletionRequest()
  69. assert_that( ycm.CompletionRequestReady() )
  70. assert_that(
  71. ycm.GetCompletionResponse(),
  72. has_entries( {
  73. 'completions': empty(),
  74. 'completion_start_column': 1
  75. } )
  76. )
  77. @YouCompleteMeInstance()
  78. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  79. def test_SendCompletionRequest_ResponseContainingError(
  80. self, ycm, post_vim_message ):
  81. current_buffer = VimBuffer( 'buffer' )
  82. def ServerResponse( *args ):
  83. return {
  84. 'completions': [ {
  85. 'insertion_text': 'insertion_text',
  86. 'menu_text': 'menu_text',
  87. 'extra_menu_info': 'extra_menu_info',
  88. 'detailed_info': 'detailed_info',
  89. 'kind': 'kind',
  90. 'extra_data': {
  91. 'doc_string': 'doc_string'
  92. }
  93. } ],
  94. 'completion_start_column': 3,
  95. 'errors': [ {
  96. 'exception': {
  97. 'TYPE': 'Exception'
  98. },
  99. 'message': 'message',
  100. 'traceback': 'traceback'
  101. } ]
  102. }
  103. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  104. with MockCompletionRequest( ServerResponse ):
  105. ycm.SendCompletionRequest()
  106. assert_that( ycm.CompletionRequestReady() )
  107. response = ycm.GetCompletionResponse()
  108. post_vim_message.assert_has_exact_calls( [
  109. call( 'Exception: message', truncate = True )
  110. ] )
  111. assert_that(
  112. response,
  113. has_entries( {
  114. 'completions': contains_exactly( has_entries( {
  115. 'word': 'insertion_text',
  116. 'abbr': 'menu_text',
  117. 'menu': 'extra_menu_info',
  118. 'info': 'detailed_info\ndoc_string',
  119. 'kind': 'k',
  120. 'dup': 1,
  121. 'empty': 1
  122. } ) ),
  123. 'completion_start_column': 3
  124. } )
  125. )
  126. @YouCompleteMeInstance()
  127. @patch( 'ycm.client.base_request._logger', autospec = True )
  128. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  129. def test_SendCompletionRequest_ErrorFromServer( self,
  130. ycm,
  131. post_vim_message,
  132. logger ):
  133. current_buffer = VimBuffer( 'buffer' )
  134. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  135. with MockCompletionRequest( ServerError( 'Server error' ) ):
  136. ycm.SendCompletionRequest()
  137. assert_that( ycm.CompletionRequestReady() )
  138. response = ycm.GetCompletionResponse()
  139. logger.exception.assert_called_with( 'Error while handling server '
  140. 'response' )
  141. post_vim_message.assert_has_exact_calls( [
  142. call( 'Server error', truncate = True )
  143. ] )
  144. assert_that(
  145. response,
  146. has_entries( {
  147. 'completions': empty(),
  148. 'completion_start_column': -1
  149. } )
  150. )
  151. @YouCompleteMeInstance()
  152. @patch( 'ycm.client.base_request._logger', autospec = True )
  153. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  154. def test_ResolveCompletionRequest_Resolves( self,
  155. ycm,
  156. post_vim_message,
  157. logger ):
  158. def CompletionResponse( *args ):
  159. return {
  160. 'completions': [ {
  161. 'insertion_text': 'insertion_text',
  162. 'menu_text': 'menu_text',
  163. 'extra_menu_info': 'extra_menu_info',
  164. 'detailed_info': 'detailed_info',
  165. 'kind': 'kind',
  166. 'extra_data': {
  167. 'doc_string': 'doc_string',
  168. 'resolve': 10
  169. }
  170. } ],
  171. 'completion_start_column': 3,
  172. 'errors': []
  173. }
  174. def ResolveResponse( *args ):
  175. return {
  176. 'completion': {
  177. 'insertion_text': 'insertion_text',
  178. 'menu_text': 'menu_text',
  179. 'extra_menu_info': 'extra_menu_info',
  180. 'detailed_info': 'detailed_info',
  181. 'kind': 'kind',
  182. 'extra_data': {
  183. 'doc_string': 'doc_string with more info'
  184. }
  185. },
  186. 'errors': []
  187. }
  188. current_buffer = VimBuffer( 'buffer' )
  189. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  190. with MockCompletionRequest( CompletionResponse ):
  191. ycm.SendCompletionRequest()
  192. assert_that( ycm.CompletionRequestReady() )
  193. response = ycm.GetCompletionResponse()
  194. post_vim_message.assert_not_called()
  195. assert_that(
  196. response,
  197. has_entries( {
  198. 'completions': contains_exactly( has_entries( {
  199. 'word': 'insertion_text',
  200. 'abbr': 'menu_text',
  201. 'menu': 'extra_menu_info',
  202. 'info': 'detailed_info\ndoc_string',
  203. 'kind': 'k',
  204. 'dup': 1,
  205. 'empty': 1
  206. } ) ),
  207. 'completion_start_column': 3
  208. } )
  209. )
  210. item = response[ 'completions' ][ 0 ]
  211. assert_that( json.loads( item[ 'user_data' ] ),
  212. has_entries( { 'resolve': 10 } ) )
  213. with MockResolveRequest( ResolveResponse ):
  214. assert_that( ycm.ResolveCompletionItem( item ), equal_to( True ) )
  215. assert_that( ycm.CompletionRequestReady() )
  216. response = ycm.GetCompletionResponse()
  217. post_vim_message.assert_not_called()
  218. assert_that(
  219. response,
  220. has_entries( {
  221. 'completion': has_entries( {
  222. 'word': 'insertion_text',
  223. 'abbr': 'menu_text',
  224. 'menu': 'extra_menu_info',
  225. 'info': 'detailed_info\ndoc_string with more info',
  226. 'kind': 'k',
  227. 'dup': 1,
  228. 'empty': 1
  229. } )
  230. } )
  231. )
  232. item = response[ 'completion' ]
  233. with MockResolveRequest( ServerError( 'must not be called' ) ):
  234. assert_that( ycm.ResolveCompletionItem( item ), equal_to( False ) )
  235. post_vim_message.assert_not_called()
  236. @YouCompleteMeInstance()
  237. @patch( 'ycm.client.base_request._logger', autospec = True )
  238. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  239. def test_ResolveCompletionRequest_ResponseContainsErrors( self,
  240. ycm,
  241. post_vim_message,
  242. logger ):
  243. def CompletionResponse( *args ):
  244. return {
  245. 'completions': [ {
  246. 'insertion_text': 'insertion_text',
  247. 'menu_text': 'menu_text',
  248. 'extra_menu_info': 'extra_menu_info',
  249. 'detailed_info': 'detailed_info',
  250. 'kind': 'kind',
  251. 'extra_data': {
  252. 'doc_string': 'doc_string',
  253. 'resolve': 10
  254. }
  255. } ],
  256. 'completion_start_column': 3,
  257. 'errors': []
  258. }
  259. def ResolveResponse( *args ):
  260. return {
  261. 'completion': {
  262. 'insertion_text': 'insertion_text',
  263. 'menu_text': 'menu_text',
  264. 'extra_menu_info': 'extra_menu_info',
  265. 'detailed_info': 'detailed_info',
  266. 'kind': 'kind',
  267. 'extra_data': {
  268. 'doc_string': 'doc_string with more info'
  269. }
  270. },
  271. 'errors': [ {
  272. 'exception': {
  273. 'TYPE': 'Exception'
  274. },
  275. 'message': 'message',
  276. 'traceback': 'traceback'
  277. } ]
  278. }
  279. current_buffer = VimBuffer( 'buffer' )
  280. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  281. with MockCompletionRequest( CompletionResponse ):
  282. ycm.SendCompletionRequest()
  283. assert_that( ycm.CompletionRequestReady() )
  284. response = ycm.GetCompletionResponse()
  285. post_vim_message.assert_not_called()
  286. assert_that(
  287. response,
  288. has_entries( {
  289. 'completions': contains_exactly( has_entries( {
  290. 'word': 'insertion_text',
  291. 'abbr': 'menu_text',
  292. 'menu': 'extra_menu_info',
  293. 'info': 'detailed_info\ndoc_string',
  294. 'kind': 'k',
  295. 'dup': 1,
  296. 'empty': 1
  297. } ) ),
  298. 'completion_start_column': 3
  299. } )
  300. )
  301. item = response[ 'completions' ][ 0 ]
  302. assert_that( json.loads( item[ 'user_data' ] ),
  303. has_entries( { 'resolve': 10 } ) )
  304. with MockResolveRequest( ResolveResponse ):
  305. assert_that( ycm.ResolveCompletionItem( item ), equal_to( True ) )
  306. assert_that( ycm.CompletionRequestReady() )
  307. response = ycm.GetCompletionResponse()
  308. post_vim_message.assert_has_exact_calls( [
  309. call( 'Exception: message', truncate = True )
  310. ] )
  311. assert_that(
  312. response,
  313. has_entries( {
  314. 'completion': has_entries( {
  315. 'word': 'insertion_text',
  316. 'abbr': 'menu_text',
  317. 'menu': 'extra_menu_info',
  318. 'info': 'detailed_info\ndoc_string with more info',
  319. 'kind': 'k',
  320. 'dup': 1,
  321. 'empty': 1
  322. } )
  323. } )
  324. )
  325. @YouCompleteMeInstance()
  326. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  327. def test_ResolveCompletionItem_NoUserData( self, ycm, post_vim_message ):
  328. def CompletionResponse( *args ):
  329. return {
  330. 'completions': [ {
  331. 'insertion_text': 'insertion_text',
  332. 'menu_text': 'menu_text',
  333. 'extra_menu_info': 'extra_menu_info',
  334. 'detailed_info': 'detailed_info',
  335. 'kind': 'kind'
  336. } ],
  337. 'completion_start_column': 3,
  338. 'errors': []
  339. }
  340. current_buffer = VimBuffer( 'buffer' )
  341. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  342. with MockCompletionRequest( CompletionResponse ):
  343. ycm.SendCompletionRequest()
  344. assert_that( ycm.CompletionRequestReady() )
  345. response = ycm.GetCompletionResponse()
  346. post_vim_message.assert_not_called()
  347. assert_that(
  348. response,
  349. has_entries( {
  350. 'completions': contains_exactly( has_entries( {
  351. 'word': 'insertion_text',
  352. 'abbr': 'menu_text',
  353. 'menu': 'extra_menu_info',
  354. 'info': 'detailed_info',
  355. 'kind': 'k',
  356. 'dup': 1,
  357. 'empty': 1
  358. } ) ),
  359. 'completion_start_column': 3
  360. } )
  361. )
  362. item = response[ 'completions' ][ 0 ]
  363. item.pop( 'user_data' )
  364. with MockResolveRequest( ServerError( 'must not be called' ) ):
  365. assert_that( ycm.ResolveCompletionItem( item ), equal_to( False ) )
  366. post_vim_message.assert_not_called()
  367. @YouCompleteMeInstance()
  368. def test_ResolveCompletionItem_NoRequest( self, ycm ):
  369. assert_that( ycm.GetCurrentCompletionRequest(), equal_to( None ) )
  370. assert_that( ycm.ResolveCompletionItem( {} ), equal_to( False ) )
  371. @YouCompleteMeInstance()
  372. @patch( 'ycm.client.base_request._logger', autospec = True )
  373. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  374. def test_ResolveCompletionRequest_ServerError(
  375. self, ycm, post_vim_message, logger ):
  376. def ServerResponse( *args ):
  377. return {
  378. 'completions': [ {
  379. 'insertion_text': 'insertion_text',
  380. 'menu_text': 'menu_text',
  381. 'extra_menu_info': 'extra_menu_info',
  382. 'detailed_info': 'detailed_info',
  383. 'kind': 'kind',
  384. 'extra_data': {
  385. 'doc_string': 'doc_string',
  386. 'resolve': 10
  387. }
  388. } ],
  389. 'completion_start_column': 3,
  390. 'errors': []
  391. }
  392. current_buffer = VimBuffer( 'buffer' )
  393. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  394. with MockCompletionRequest( ServerResponse ):
  395. ycm.SendCompletionRequest()
  396. assert_that( ycm.CompletionRequestReady() )
  397. response = ycm.GetCompletionResponse()
  398. post_vim_message.assert_not_called()
  399. assert_that(
  400. response,
  401. has_entries( {
  402. 'completions': contains_exactly( has_entries( {
  403. 'word': 'insertion_text',
  404. 'abbr': 'menu_text',
  405. 'menu': 'extra_menu_info',
  406. 'info': 'detailed_info\ndoc_string',
  407. 'kind': 'k',
  408. 'dup': 1,
  409. 'empty': 1
  410. } ) ),
  411. 'completion_start_column': 3
  412. } )
  413. )
  414. item = response[ 'completions' ][ 0 ]
  415. assert_that( json.loads( item[ 'user_data' ] ),
  416. has_entries( { 'resolve': 10 } ) )
  417. with MockResolveRequest( ServerError( 'Server error' ) ):
  418. ycm.ResolveCompletionItem( item )
  419. assert_that( ycm.CompletionRequestReady() )
  420. response = ycm.GetCompletionResponse()
  421. logger.exception.assert_called_with( 'Error while handling server '
  422. 'response' )
  423. post_vim_message.assert_has_exact_calls( [
  424. call( 'Server error', truncate = True )
  425. ] )