completion_request_test.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. # Copyright (C) 2015-2019 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. import json
  18. from hamcrest import assert_that, equal_to
  19. from unittest import TestCase
  20. from ycm.tests import UserOptions
  21. from ycm.tests.test_utils import MockVimModule
  22. vim_mock = MockVimModule()
  23. from ycm.client import completion_request
  24. class ConvertCompletionResponseToVimDatasTest( TestCase ):
  25. """ This class tests the
  26. completion_request.ConvertCompletionResponseToVimDatas method """
  27. def _Check( self, completion_data, expected_vim_data ):
  28. vim_data = completion_request.ConvertCompletionDataToVimData(
  29. completion_data )
  30. try:
  31. assert_that( vim_data, equal_to( expected_vim_data ) )
  32. except Exception:
  33. print( "Expected:\n"
  34. f"'{ expected_vim_data }'\n"
  35. "when parsing:\n'"
  36. f"{ completion_data }'\n"
  37. "But found:\n"
  38. f"'{ vim_data }'" )
  39. raise
  40. def test_AllFields( self ):
  41. extra_data = {
  42. 'doc_string': 'DOC STRING',
  43. }
  44. self._Check( {
  45. 'insertion_text': 'INSERTION TEXT',
  46. 'menu_text': 'MENU TEXT',
  47. 'extra_menu_info': 'EXTRA MENU INFO',
  48. 'kind': 'K',
  49. 'detailed_info': 'DETAILED INFO',
  50. 'extra_data': extra_data,
  51. }, {
  52. 'word' : 'INSERTION TEXT',
  53. 'abbr' : 'MENU TEXT',
  54. 'menu' : 'EXTRA MENU INFO',
  55. 'kind' : 'k',
  56. 'info' : 'DETAILED INFO\nDOC STRING',
  57. 'equal' : 1,
  58. 'dup' : 1,
  59. 'empty' : 1,
  60. 'user_data': json.dumps( extra_data ),
  61. } )
  62. def test_OnlyInsertionTextField( self ):
  63. self._Check( {
  64. 'insertion_text': 'INSERTION TEXT'
  65. }, {
  66. 'word' : 'INSERTION TEXT',
  67. 'abbr' : '',
  68. 'menu' : '',
  69. 'kind' : '',
  70. 'info' : '',
  71. 'equal' : 1,
  72. 'dup' : 1,
  73. 'empty' : 1,
  74. 'user_data': '{}',
  75. } )
  76. def test_JustDetailedInfo( self ):
  77. self._Check( {
  78. 'insertion_text': 'INSERTION TEXT',
  79. 'menu_text': 'MENU TEXT',
  80. 'extra_menu_info': 'EXTRA MENU INFO',
  81. 'kind': 'K',
  82. 'detailed_info': 'DETAILED INFO',
  83. }, {
  84. 'word' : 'INSERTION TEXT',
  85. 'abbr' : 'MENU TEXT',
  86. 'menu' : 'EXTRA MENU INFO',
  87. 'kind' : 'k',
  88. 'info' : 'DETAILED INFO',
  89. 'equal' : 1,
  90. 'dup' : 1,
  91. 'empty' : 1,
  92. 'user_data': '{}',
  93. } )
  94. def test_JustDocString( self ):
  95. extra_data = {
  96. 'doc_string': 'DOC STRING',
  97. }
  98. self._Check( {
  99. 'insertion_text': 'INSERTION TEXT',
  100. 'menu_text': 'MENU TEXT',
  101. 'extra_menu_info': 'EXTRA MENU INFO',
  102. 'kind': 'K',
  103. 'extra_data': extra_data,
  104. }, {
  105. 'word' : 'INSERTION TEXT',
  106. 'abbr' : 'MENU TEXT',
  107. 'menu' : 'EXTRA MENU INFO',
  108. 'kind' : 'k',
  109. 'info' : 'DOC STRING',
  110. 'equal' : 1,
  111. 'dup' : 1,
  112. 'empty' : 1,
  113. 'user_data': json.dumps( extra_data ),
  114. } )
  115. def test_ExtraInfoNoDocString( self ):
  116. self._Check( {
  117. 'insertion_text': 'INSERTION TEXT',
  118. 'menu_text': 'MENU TEXT',
  119. 'extra_menu_info': 'EXTRA MENU INFO',
  120. 'kind': 'K',
  121. 'extra_data': {
  122. },
  123. }, {
  124. 'word' : 'INSERTION TEXT',
  125. 'abbr' : 'MENU TEXT',
  126. 'menu' : 'EXTRA MENU INFO',
  127. 'kind' : 'k',
  128. 'info' : '',
  129. 'equal' : 1,
  130. 'dup' : 1,
  131. 'empty' : 1,
  132. 'user_data': '{}',
  133. } )
  134. def test_NullCharactersInExtraInfoAndDocString( self ):
  135. extra_data = {
  136. 'doc_string': 'DOC\x00STRING'
  137. }
  138. self._Check( {
  139. 'insertion_text': 'INSERTION TEXT',
  140. 'menu_text': 'MENU TEXT',
  141. 'extra_menu_info': 'EXTRA MENU INFO',
  142. 'kind': 'K',
  143. 'detailed_info': 'DETAILED\x00INFO',
  144. 'extra_data': extra_data,
  145. }, {
  146. 'word' : 'INSERTION TEXT',
  147. 'abbr' : 'MENU TEXT',
  148. 'menu' : 'EXTRA MENU INFO',
  149. 'kind' : 'k',
  150. 'info' : 'DETAILEDINFO\nDOCSTRING',
  151. 'equal' : 1,
  152. 'dup' : 1,
  153. 'empty' : 1,
  154. 'user_data': json.dumps( extra_data ),
  155. } )
  156. def test_ExtraInfoNoDocStringWithDetailedInfo( self ):
  157. self._Check( {
  158. 'insertion_text': 'INSERTION TEXT',
  159. 'menu_text': 'MENU TEXT',
  160. 'extra_menu_info': 'EXTRA MENU INFO',
  161. 'kind': 'K',
  162. 'detailed_info': 'DETAILED INFO',
  163. 'extra_data': {
  164. },
  165. }, {
  166. 'word' : 'INSERTION TEXT',
  167. 'abbr' : 'MENU TEXT',
  168. 'menu' : 'EXTRA MENU INFO',
  169. 'kind' : 'k',
  170. 'info' : 'DETAILED INFO',
  171. 'equal' : 1,
  172. 'dup' : 1,
  173. 'empty' : 1,
  174. 'user_data': '{}',
  175. } )
  176. def test_EmptyInsertionText( self ):
  177. extra_data = {
  178. 'doc_string': 'DOC STRING',
  179. }
  180. self._Check( {
  181. 'insertion_text': '',
  182. 'menu_text': 'MENU TEXT',
  183. 'extra_menu_info': 'EXTRA MENU INFO',
  184. 'kind': 'K',
  185. 'detailed_info': 'DETAILED INFO',
  186. 'extra_data': extra_data,
  187. }, {
  188. 'word' : '',
  189. 'abbr' : 'MENU TEXT',
  190. 'menu' : 'EXTRA MENU INFO',
  191. 'kind' : 'k',
  192. 'info' : 'DETAILED INFO\nDOC STRING',
  193. 'equal' : 1,
  194. 'dup' : 1,
  195. 'empty' : 1,
  196. 'user_data': json.dumps( extra_data ),
  197. } )
  198. def test_TruncateForPopup( self, *args ):
  199. with UserOptions( { '&columns': 60, '&completeopt': b'popup,menuone' } ):
  200. extra_data = {
  201. 'doc_string': 'DOC STRING',
  202. }
  203. self._Check( {
  204. 'insertion_text': '',
  205. 'menu_text': 'MENU TEXT',
  206. 'extra_menu_info': 'ESPECIALLY LONG EXTRA MENU INFO LOREM IPSUM DOLOR',
  207. 'kind': 'K',
  208. 'detailed_info': 'DETAILED INFO',
  209. 'extra_data': extra_data,
  210. }, {
  211. 'word' : '',
  212. 'abbr' : 'MENU TEXT',
  213. 'menu' : 'ESPECIALLY LONG E...',
  214. 'kind' : 'k',
  215. 'info' : 'ESPECIALLY LONG EXTRA MENU INFO LOREM IPSUM DOLOR\n\n' +
  216. 'DETAILED INFO\nDOC STRING',
  217. 'equal' : 1,
  218. 'dup' : 1,
  219. 'empty' : 1,
  220. 'user_data': json.dumps( extra_data ),
  221. } )
  222. def test_OnlyTruncateForPopupIfNecessary( self, *args ):
  223. with UserOptions( { '&columns': 60, '&completeopt': b'popup,menuone' } ):
  224. extra_data = {
  225. 'doc_string': 'DOC STRING',
  226. }
  227. self._Check( {
  228. 'insertion_text': '',
  229. 'menu_text': 'MENU TEXT',
  230. 'extra_menu_info': 'EXTRA MENU INFO',
  231. 'kind': 'K',
  232. 'detailed_info': 'DETAILED INFO',
  233. 'extra_data': extra_data,
  234. }, {
  235. 'word' : '',
  236. 'abbr' : 'MENU TEXT',
  237. 'menu' : 'EXTRA MENU INFO',
  238. 'kind' : 'k',
  239. 'info' : 'DETAILED INFO\nDOC STRING',
  240. 'equal' : 1,
  241. 'dup' : 1,
  242. 'empty' : 1,
  243. 'user_data': json.dumps( extra_data ),
  244. } )
  245. def test_DontTruncateIfNotPopup( self, *args ):
  246. with UserOptions( { '&columns': 60, '&completeopt': b'preview,menuone' } ):
  247. extra_data = {
  248. 'doc_string': 'DOC STRING',
  249. }
  250. self._Check( {
  251. 'insertion_text': '',
  252. 'menu_text': 'MENU TEXT',
  253. 'extra_menu_info': 'ESPECIALLY LONG EXTRA MENU INFO LOREM IPSUM DOLOR',
  254. 'kind': 'K',
  255. 'detailed_info': 'DETAILED INFO',
  256. 'extra_data': extra_data,
  257. }, {
  258. 'word' : '',
  259. 'abbr' : 'MENU TEXT',
  260. 'menu' : 'ESPECIALLY LONG EXTRA MENU INFO LOREM IPSUM DOLOR',
  261. 'kind' : 'k',
  262. 'info' : 'DETAILED INFO\nDOC STRING',
  263. 'equal' : 1,
  264. 'dup' : 1,
  265. 'empty' : 1,
  266. 'user_data': json.dumps( extra_data ),
  267. } )
  268. def test_TruncateForPopupWithoutDuplication( self, *args ):
  269. with UserOptions( { '&columns': 60, '&completeopt': b'popup,menuone' } ):
  270. extra_data = {
  271. 'doc_string': 'DOC STRING',
  272. }
  273. self._Check( {
  274. 'insertion_text': '',
  275. 'menu_text': 'MENU TEXT',
  276. 'extra_menu_info': 'ESPECIALLY LONG METHOD SIGNATURE LOREM IPSUM',
  277. 'kind': 'K',
  278. 'detailed_info': 'ESPECIALLY LONG METHOD SIGNATURE LOREM IPSUM',
  279. 'extra_data': extra_data,
  280. }, {
  281. 'word' : '',
  282. 'abbr' : 'MENU TEXT',
  283. 'menu' : 'ESPECIALLY LONG M...',
  284. 'kind' : 'k',
  285. 'info' : 'ESPECIALLY LONG METHOD SIGNATURE LOREM IPSUM\n' +
  286. 'DOC STRING',
  287. 'equal' : 1,
  288. 'dup' : 1,
  289. 'empty' : 1,
  290. 'user_data': json.dumps( extra_data ),
  291. } )