omni_completer_test.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. # encoding: utf-8
  2. #
  3. # Copyright (C) 2016 YouCompleteMe contributors
  4. #
  5. # This file is part of YouCompleteMe.
  6. #
  7. # YouCompleteMe is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # YouCompleteMe is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  19. from __future__ import unicode_literals
  20. from __future__ import print_function
  21. from __future__ import division
  22. from __future__ import absolute_import
  23. # Not installing aliases from python-future; it's unreliable and slow.
  24. from builtins import * # noqa
  25. from hamcrest import assert_that, contains, contains_string, empty, has_entries
  26. from ycm.tests.test_utils import ( ExpectedFailure, MockVimBuffers,
  27. MockVimModule, ToBytesOnPY2, VimBuffer )
  28. MockVimModule()
  29. from ycm.tests import YouCompleteMeInstance
  30. @YouCompleteMeInstance( { 'cache_omnifunc': 1 } )
  31. def OmniCompleter_GetCompletions_Cache_List_test( ycm ):
  32. def Omnifunc( findstart, base ):
  33. if findstart:
  34. return 5
  35. return [ 'a', 'b', 'cdef' ]
  36. current_buffer = VimBuffer( 'buffer',
  37. contents = [ 'test.' ],
  38. filetype = 'java',
  39. omnifunc = Omnifunc )
  40. with MockVimBuffers( [ current_buffer ], current_buffer, ( 1, 5 ) ):
  41. # Make sure there is an omnifunc set up.
  42. ycm.OnFileReadyToParse()
  43. ycm.SendCompletionRequest()
  44. assert_that(
  45. ycm.GetCompletionResponse(),
  46. has_entries( {
  47. 'completions': ToBytesOnPY2( [ 'a', 'b', 'cdef' ] ),
  48. 'completion_start_column': 6
  49. } )
  50. )
  51. @YouCompleteMeInstance( { 'cache_omnifunc': 1 } )
  52. def OmniCompleter_GetCompletions_Cache_ListFilter_test( ycm ):
  53. def Omnifunc( findstart, base ):
  54. if findstart:
  55. return 5
  56. return [ 'a', 'b', 'cdef' ]
  57. current_buffer = VimBuffer( 'buffer',
  58. contents = [ 'test.t' ],
  59. filetype = 'java',
  60. omnifunc = Omnifunc )
  61. with MockVimBuffers( [ current_buffer ], current_buffer, ( 1, 6 ) ):
  62. # Make sure there is an omnifunc set up.
  63. ycm.OnFileReadyToParse()
  64. ycm.SendCompletionRequest()
  65. assert_that(
  66. ycm.GetCompletionResponse(),
  67. has_entries( {
  68. 'completions': empty(),
  69. 'completion_start_column': 6
  70. } )
  71. )
  72. @YouCompleteMeInstance( { 'cache_omnifunc': 0 } )
  73. def OmniCompleter_GetCompletions_NoCache_List_test( ycm ):
  74. def Omnifunc( findstart, base ):
  75. if findstart:
  76. return 5
  77. return [ 'a', 'b', 'cdef' ]
  78. current_buffer = VimBuffer( 'buffer',
  79. contents = [ 'test.' ],
  80. filetype = 'java',
  81. omnifunc = Omnifunc )
  82. with MockVimBuffers( [ current_buffer ], current_buffer, ( 1, 5 ) ):
  83. # Make sure there is an omnifunc set up.
  84. ycm.OnFileReadyToParse()
  85. ycm.SendCompletionRequest()
  86. assert_that(
  87. ycm.GetCompletionResponse(),
  88. has_entries( {
  89. 'completions': ToBytesOnPY2( [ 'a', 'b', 'cdef' ] ),
  90. 'completion_start_column': 6
  91. } )
  92. )
  93. @YouCompleteMeInstance( { 'cache_omnifunc': 0 } )
  94. def OmniCompleter_GetCompletions_NoCache_ListFilter_test( ycm ):
  95. def Omnifunc( findstart, base ):
  96. if findstart:
  97. return 5
  98. return [ 'a', 'b', 'cdef' ]
  99. current_buffer = VimBuffer( 'buffer',
  100. contents = [ 'test.t' ],
  101. filetype = 'java',
  102. omnifunc = Omnifunc )
  103. with MockVimBuffers( [ current_buffer ], current_buffer, ( 1, 6 ) ):
  104. # Make sure there is an omnifunc set up.
  105. ycm.OnFileReadyToParse()
  106. ycm.SendCompletionRequest()
  107. # Actual result is that the results are not filtered, as we expect the
  108. # omnifunc or vim itself to do this filtering.
  109. assert_that(
  110. ycm.GetCompletionResponse(),
  111. has_entries( {
  112. 'completions': ToBytesOnPY2( [ 'a', 'b', 'cdef' ] ),
  113. 'completion_start_column': 6
  114. } )
  115. )
  116. @YouCompleteMeInstance( { 'cache_omnifunc': 0 } )
  117. def OmniCompleter_GetCompletions_NoCache_UseFindStart_test( ycm ):
  118. def Omnifunc( findstart, base ):
  119. if findstart:
  120. return 0
  121. return [ 'a', 'b', 'cdef' ]
  122. current_buffer = VimBuffer( 'buffer',
  123. contents = [ 'test.t' ],
  124. filetype = 'java',
  125. omnifunc = Omnifunc )
  126. with MockVimBuffers( [ current_buffer ], current_buffer, ( 1, 6 ) ):
  127. # Make sure there is an omnifunc set up.
  128. ycm.OnFileReadyToParse()
  129. ycm.SendCompletionRequest()
  130. # Actual result is that the results are not filtered, as we expect the
  131. # omnifunc or vim itself to do this filtering.
  132. assert_that(
  133. ycm.GetCompletionResponse(),
  134. has_entries( {
  135. 'completions': ToBytesOnPY2( [ 'a', 'b', 'cdef' ] ),
  136. 'completion_start_column': 1
  137. } )
  138. )
  139. @YouCompleteMeInstance( { 'cache_omnifunc': 1 } )
  140. def OmniCompleter_GetCompletions_Cache_UseFindStart_test( ycm ):
  141. def Omnifunc( findstart, base ):
  142. if findstart:
  143. return 0
  144. return [ 'a', 'b', 'cdef' ]
  145. current_buffer = VimBuffer( 'buffer',
  146. contents = [ 'test.t' ],
  147. filetype = 'java',
  148. omnifunc = Omnifunc )
  149. with MockVimBuffers( [ current_buffer ], current_buffer, ( 1, 6 ) ):
  150. # Make sure there is an omnifunc set up.
  151. ycm.OnFileReadyToParse()
  152. ycm.SendCompletionRequest()
  153. # There are no results because the query 'test.t' doesn't match any
  154. # candidate (and cache_omnifunc=1, so we FilterAndSortCandidates).
  155. assert_that(
  156. ycm.GetCompletionResponse(),
  157. has_entries( {
  158. 'completions': empty(),
  159. 'completion_start_column': 1
  160. } )
  161. )
  162. @YouCompleteMeInstance( { 'cache_omnifunc': 1 } )
  163. def OmniCompleter_GetCompletions_Cache_Object_test( ycm ):
  164. def Omnifunc( findstart, base ):
  165. if findstart:
  166. return 5
  167. return { 'words': [ 'a', 'b', 'CDtEF' ] }
  168. current_buffer = VimBuffer( 'buffer',
  169. contents = [ 'test.t' ],
  170. filetype = 'java',
  171. omnifunc = Omnifunc )
  172. with MockVimBuffers( [ current_buffer ], current_buffer, ( 1, 6 ) ):
  173. # Make sure there is an omnifunc set up.
  174. ycm.OnFileReadyToParse()
  175. ycm.SendCompletionRequest()
  176. assert_that(
  177. ycm.GetCompletionResponse(),
  178. has_entries( {
  179. 'completions': [ 'CDtEF' ],
  180. 'completion_start_column': 6
  181. } )
  182. )
  183. @YouCompleteMeInstance( { 'cache_omnifunc': 1 } )
  184. def OmniCompleter_GetCompletions_Cache_ObjectList_test( ycm ):
  185. def Omnifunc( findstart, base ):
  186. if findstart:
  187. return 5
  188. return [
  189. {
  190. 'word': 'a',
  191. 'abbr': 'ABBR',
  192. 'menu': 'MENU',
  193. 'info': 'INFO',
  194. 'kind': 'K'
  195. },
  196. {
  197. 'word': 'test',
  198. 'abbr': 'ABBRTEST',
  199. 'menu': 'MENUTEST',
  200. 'info': 'INFOTEST',
  201. 'kind': 'T'
  202. }
  203. ]
  204. current_buffer = VimBuffer( 'buffer',
  205. contents = [ 'test.tt' ],
  206. filetype = 'java',
  207. omnifunc = Omnifunc )
  208. with MockVimBuffers( [ current_buffer ], current_buffer, ( 1, 7 ) ):
  209. # Make sure there is an omnifunc set up.
  210. ycm.OnFileReadyToParse()
  211. ycm.SendCompletionRequest()
  212. assert_that(
  213. ycm.GetCompletionResponse(),
  214. has_entries( {
  215. 'completions': contains( {
  216. 'word': 'test',
  217. 'abbr': 'ABBRTEST',
  218. 'menu': 'MENUTEST',
  219. 'info': 'INFOTEST',
  220. 'kind': 'T'
  221. } ),
  222. 'completion_start_column': 6
  223. } )
  224. )
  225. @YouCompleteMeInstance( { 'cache_omnifunc': 0 } )
  226. def OmniCompleter_GetCompletions_NoCache_ObjectList_test( ycm ):
  227. def Omnifunc( findstart, base ):
  228. if findstart:
  229. return 5
  230. return [
  231. {
  232. 'word': 'a',
  233. 'abbr': 'ABBR',
  234. 'menu': 'MENU',
  235. 'info': 'INFO',
  236. 'kind': 'K'
  237. },
  238. {
  239. 'word': 'test',
  240. 'abbr': 'ABBRTEST',
  241. 'menu': 'MENUTEST',
  242. 'info': 'INFOTEST',
  243. 'kind': 'T'
  244. }
  245. ]
  246. current_buffer = VimBuffer( 'buffer',
  247. contents = [ 'test.tt' ],
  248. filetype = 'java',
  249. omnifunc = Omnifunc )
  250. with MockVimBuffers( [ current_buffer ], current_buffer, ( 1, 7 ) ):
  251. # Make sure there is an omnifunc set up.
  252. ycm.OnFileReadyToParse()
  253. ycm.SendCompletionRequest()
  254. # We don't filter the result - we expect the omnifunc to do that
  255. # based on the query we supplied (Note: that means no fuzzy matching!).
  256. assert_that(
  257. ycm.GetCompletionResponse(),
  258. has_entries( {
  259. 'completions': ToBytesOnPY2( [ {
  260. 'word': 'a',
  261. 'abbr': 'ABBR',
  262. 'menu': 'MENU',
  263. 'info': 'INFO',
  264. 'kind': 'K'
  265. }, {
  266. 'word': 'test',
  267. 'abbr': 'ABBRTEST',
  268. 'menu': 'MENUTEST',
  269. 'info': 'INFOTEST',
  270. 'kind': 'T'
  271. } ] ),
  272. 'completion_start_column': 6
  273. } )
  274. )
  275. @YouCompleteMeInstance( { 'cache_omnifunc': 1 } )
  276. def OmniCompleter_GetCompletions_Cache_ObjectListObject_test( ycm ):
  277. def Omnifunc( findstart, base ):
  278. if findstart:
  279. return 5
  280. return { 'words': [
  281. {
  282. 'word': 'a',
  283. 'abbr': 'ABBR',
  284. 'menu': 'MENU',
  285. 'info': 'INFO',
  286. 'kind': 'K'
  287. },
  288. {
  289. 'word': 'test',
  290. 'abbr': 'ABBRTEST',
  291. 'menu': 'MENUTEST',
  292. 'info': 'INFOTEST',
  293. 'kind': 'T'
  294. }
  295. ] }
  296. current_buffer = VimBuffer( 'buffer',
  297. contents = [ 'test.tt' ],
  298. filetype = 'java',
  299. omnifunc = Omnifunc )
  300. with MockVimBuffers( [ current_buffer ], current_buffer, ( 1, 7 ) ):
  301. # Make sure there is an omnifunc set up.
  302. ycm.OnFileReadyToParse()
  303. ycm.SendCompletionRequest()
  304. assert_that(
  305. ycm.GetCompletionResponse(),
  306. has_entries( {
  307. 'completions': ToBytesOnPY2( [ {
  308. 'word': 'test',
  309. 'abbr': 'ABBRTEST',
  310. 'menu': 'MENUTEST',
  311. 'info': 'INFOTEST',
  312. 'kind': 'T'
  313. } ] ),
  314. 'completion_start_column': 6
  315. } )
  316. )
  317. @YouCompleteMeInstance( { 'cache_omnifunc': 0 } )
  318. def OmniCompleter_GetCompletions_NoCache_ObjectListObject_test( ycm ):
  319. def Omnifunc( findstart, base ):
  320. if findstart:
  321. return 5
  322. return { 'words': [
  323. {
  324. 'word': 'a',
  325. 'abbr': 'ABBR',
  326. 'menu': 'MENU',
  327. 'info': 'INFO',
  328. 'kind': 'K'
  329. },
  330. {
  331. 'word': 'test',
  332. 'abbr': 'ABBRTEST',
  333. 'menu': 'MENUTEST',
  334. 'info': 'INFOTEST',
  335. 'kind': 'T'
  336. }
  337. ] }
  338. current_buffer = VimBuffer( 'buffer',
  339. contents = [ 'test.tt' ],
  340. filetype = 'java',
  341. omnifunc = Omnifunc )
  342. with MockVimBuffers( [ current_buffer ], current_buffer, ( 1, 7 ) ):
  343. # Make sure there is an omnifunc set up.
  344. ycm.OnFileReadyToParse()
  345. ycm.SendCompletionRequest()
  346. # No FilterAndSortCandidates for cache_omnifunc=0 (we expect the omnifunc
  347. # to do the filtering?)
  348. assert_that(
  349. ycm.GetCompletionResponse(),
  350. has_entries( {
  351. 'completions': ToBytesOnPY2( [ {
  352. 'word': 'a',
  353. 'abbr': 'ABBR',
  354. 'menu': 'MENU',
  355. 'info': 'INFO',
  356. 'kind': 'K'
  357. }, {
  358. 'word': 'test',
  359. 'abbr': 'ABBRTEST',
  360. 'menu': 'MENUTEST',
  361. 'info': 'INFOTEST',
  362. 'kind': 'T'
  363. } ] ),
  364. 'completion_start_column': 6
  365. } )
  366. )
  367. @YouCompleteMeInstance( { 'cache_omnifunc': 1 } )
  368. def OmniCompleter_GetCompletions_Cache_List_Unicode_test( ycm ):
  369. def Omnifunc( findstart, base ):
  370. if findstart:
  371. return 12
  372. return [ '†est', 'å_unicode_identifier', 'πππππππ yummy πie' ]
  373. current_buffer = VimBuffer( 'buffer',
  374. contents = [ '†åsty_π.' ],
  375. filetype = 'java',
  376. omnifunc = Omnifunc )
  377. with MockVimBuffers( [ current_buffer ], current_buffer, ( 1, 12 ) ):
  378. # Make sure there is an omnifunc set up.
  379. ycm.OnFileReadyToParse()
  380. ycm.SendCompletionRequest()
  381. assert_that(
  382. ycm.GetCompletionResponse(),
  383. has_entries( {
  384. 'completions': ToBytesOnPY2( [ '†est',
  385. 'å_unicode_identifier',
  386. 'πππππππ yummy πie' ] ),
  387. 'completion_start_column': 13
  388. } )
  389. )
  390. @YouCompleteMeInstance( { 'cache_omnifunc': 0 } )
  391. def OmniCompleter_GetCompletions_NoCache_List_Unicode_test( ycm ):
  392. def Omnifunc( findstart, base ):
  393. if findstart:
  394. return 12
  395. return [ '†est', 'å_unicode_identifier', 'πππππππ yummy πie' ]
  396. current_buffer = VimBuffer( 'buffer',
  397. contents = [ '†åsty_π.' ],
  398. filetype = 'java',
  399. omnifunc = Omnifunc )
  400. with MockVimBuffers( [ current_buffer ], current_buffer, ( 1, 12 ) ):
  401. # Make sure there is an omnifunc set up.
  402. ycm.OnFileReadyToParse()
  403. ycm.SendCompletionRequest()
  404. assert_that(
  405. ycm.GetCompletionResponse(),
  406. has_entries( {
  407. 'completions': ToBytesOnPY2( [ '†est',
  408. 'å_unicode_identifier',
  409. 'πππππππ yummy πie' ] ),
  410. 'completion_start_column': 13
  411. } )
  412. )
  413. @ExpectedFailure( 'Filtering on unicode is not supported by the server',
  414. contains_string( "value for 'completions' was <[]>" ) )
  415. @YouCompleteMeInstance( { 'cache_omnifunc': 1 } )
  416. def OmniCompleter_GetCompletions_Cache_List_Filter_Unicode_test( ycm ):
  417. def Omnifunc( findstart, base ):
  418. if findstart:
  419. return 12
  420. return [ '†est', 'å_unicode_identifier', 'πππππππ yummy πie' ]
  421. current_buffer = VimBuffer( 'buffer',
  422. contents = [ '†åsty_π.ππ' ],
  423. filetype = 'java',
  424. omnifunc = Omnifunc )
  425. with MockVimBuffers( [ current_buffer ], current_buffer, ( 1, 17 ) ):
  426. # Make sure there is an omnifunc set up.
  427. ycm.OnFileReadyToParse()
  428. ycm.SendCompletionRequest()
  429. assert_that(
  430. ycm.GetCompletionResponse(),
  431. has_entries( {
  432. 'completions': ToBytesOnPY2( [ 'πππππππ yummy πie' ] ),
  433. 'completion_start_column': 13
  434. } )
  435. )
  436. @YouCompleteMeInstance( { 'cache_omnifunc': 0 } )
  437. def OmniCompleter_GetCompletions_NoCache_List_Filter_Unicode_test( ycm ):
  438. def Omnifunc( findstart, base ):
  439. if findstart:
  440. return 12
  441. return [ 'πππππππ yummy πie' ]
  442. current_buffer = VimBuffer( 'buffer',
  443. contents = [ '†åsty_π.ππ' ],
  444. filetype = 'java',
  445. omnifunc = Omnifunc )
  446. with MockVimBuffers( [ current_buffer ], current_buffer, ( 1, 17 ) ):
  447. # Make sure there is an omnifunc set up.
  448. ycm.OnFileReadyToParse()
  449. ycm.SendCompletionRequest()
  450. assert_that(
  451. ycm.GetCompletionResponse(),
  452. has_entries( {
  453. 'completions': ToBytesOnPY2( [ 'πππππππ yummy πie' ] ),
  454. 'completion_start_column': 13
  455. } )
  456. )
  457. @ExpectedFailure( 'Filtering on unicode is not supported by the server',
  458. contains_string( "value for 'completions' was <[]>" ) )
  459. @YouCompleteMeInstance( { 'cache_omnifunc': 1 } )
  460. def OmniCompleter_GetCompletions_Cache_ObjectList_Unicode_test( ycm ):
  461. def Omnifunc( findstart, base ):
  462. if findstart:
  463. return 12
  464. return [
  465. {
  466. 'word': 'ålpha∫et',
  467. 'abbr': 'å∫∫®',
  468. 'menu': 'µ´~¨á',
  469. 'info': '^~fo',
  470. 'kind': '˚'
  471. },
  472. {
  473. 'word': 'π†´ß†π',
  474. 'abbr': 'ÅııÂʉÍÊ',
  475. 'menu': '˜‰ˆËʉÍÊ',
  476. 'info': 'ȈÏØʉÍÊ',
  477. 'kind': 'Ê'
  478. }
  479. ]
  480. current_buffer = VimBuffer( 'buffer',
  481. contents = [ '†åsty_π.ππ' ],
  482. filetype = 'java',
  483. omnifunc = Omnifunc )
  484. with MockVimBuffers( [ current_buffer ], current_buffer, ( 1, 17 ) ):
  485. # Make sure there is an omnifunc set up.
  486. ycm.OnFileReadyToParse()
  487. ycm.SendCompletionRequest()
  488. assert_that(
  489. ycm.GetCompletionResponse(),
  490. has_entries( {
  491. 'completions': ToBytesOnPY2( [ {
  492. 'word': 'π†´ß†π',
  493. 'abbr': 'ÅııÂʉÍÊ',
  494. 'menu': '˜‰ˆËʉÍÊ',
  495. 'info': 'ȈÏØʉÍÊ',
  496. 'kind': 'Ê'
  497. } ] ),
  498. 'completion_start_column': 13
  499. } )
  500. )
  501. @YouCompleteMeInstance( { 'cache_omnifunc': 1 } )
  502. def OmniCompleter_GetCompletions_Cache_ObjectListObject_Unicode_test( ycm ):
  503. def Omnifunc( findstart, base ):
  504. if findstart:
  505. return 12
  506. return {
  507. 'words': [
  508. {
  509. 'word': 'ålpha∫et',
  510. 'abbr': 'å∫∫®',
  511. 'menu': 'µ´~¨á',
  512. 'info': '^~fo',
  513. 'kind': '˚'
  514. },
  515. {
  516. 'word': 'π†´ß†π',
  517. 'abbr': 'ÅııÂʉÍÊ',
  518. 'menu': '˜‰ˆËʉÍÊ',
  519. 'info': 'ȈÏØʉÍÊ',
  520. 'kind': 'Ê'
  521. },
  522. {
  523. 'word': 'test',
  524. 'abbr': 'ÅııÂʉÍÊ',
  525. 'menu': '˜‰ˆËʉÍÊ',
  526. 'info': 'ȈÏØʉÍÊ',
  527. 'kind': 'Ê'
  528. }
  529. ]
  530. }
  531. current_buffer = VimBuffer( 'buffer',
  532. contents = [ '†åsty_π.t' ],
  533. filetype = 'java',
  534. omnifunc = Omnifunc )
  535. with MockVimBuffers( [ current_buffer ], current_buffer, ( 1, 13 ) ):
  536. # Make sure there is an omnifunc set up.
  537. ycm.OnFileReadyToParse()
  538. ycm.SendCompletionRequest()
  539. assert_that(
  540. ycm.GetCompletionResponse(),
  541. has_entries( {
  542. 'completions': contains( {
  543. 'word': 'test',
  544. 'abbr': 'ÅııÂʉÍÊ',
  545. 'menu': '˜‰ˆËʉÍÊ',
  546. 'info': 'ȈÏØʉÍÊ',
  547. 'kind': 'Ê'
  548. } ),
  549. 'completion_start_column': 13
  550. } )
  551. )