base_test.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. # coding: utf-8
  2. #
  3. # Copyright (C) 2013 Google Inc.
  4. # 2016 YouCompleteMe contributors
  5. #
  6. # This file is part of YouCompleteMe.
  7. #
  8. # YouCompleteMe is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # YouCompleteMe is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  20. from __future__ import unicode_literals
  21. from __future__ import print_function
  22. from __future__ import division
  23. from __future__ import absolute_import
  24. # Not installing aliases from python-future; it's unreliable and slow.
  25. from builtins import * # noqa
  26. import contextlib
  27. from nose.tools import eq_, ok_
  28. from mock import patch
  29. from ycm.tests.test_utils import MockVimModule
  30. vim_mock = MockVimModule()
  31. from ycm import base
  32. @contextlib.contextmanager
  33. def MockCurrentFiletypes( filetypes = [''] ):
  34. with patch( 'ycm.vimsupport.CurrentFiletypes', return_value = filetypes ):
  35. yield
  36. @contextlib.contextmanager
  37. def MockCurrentColumnAndLineContents( column, line_contents ):
  38. with patch( 'ycm.vimsupport.CurrentColumn', return_value = column ):
  39. with patch( 'ycm.vimsupport.CurrentLineContents',
  40. return_value = line_contents ):
  41. yield
  42. @contextlib.contextmanager
  43. def MockTextAfterCursor( text ):
  44. with patch( 'ycm.vimsupport.TextAfterCursor', return_value = text ):
  45. yield
  46. def AdjustCandidateInsertionText_Basic_test():
  47. with MockTextAfterCursor( 'bar' ):
  48. eq_( [ { 'abbr': 'foobar', 'word': 'foo' } ],
  49. base.AdjustCandidateInsertionText( [ 'foobar' ] ) )
  50. def AdjustCandidateInsertionText_ParenInTextAfterCursor_test():
  51. with MockTextAfterCursor( 'bar(zoo' ):
  52. eq_( [ { 'abbr': 'foobar', 'word': 'foo' } ],
  53. base.AdjustCandidateInsertionText( [ 'foobar' ] ) )
  54. def AdjustCandidateInsertionText_PlusInTextAfterCursor_test():
  55. with MockTextAfterCursor( 'bar+zoo' ):
  56. eq_( [ { 'abbr': 'foobar', 'word': 'foo' } ],
  57. base.AdjustCandidateInsertionText( [ 'foobar' ] ) )
  58. def AdjustCandidateInsertionText_WhitespaceInTextAfterCursor_test():
  59. with MockTextAfterCursor( 'bar zoo' ):
  60. eq_( [ { 'abbr': 'foobar', 'word': 'foo' } ],
  61. base.AdjustCandidateInsertionText( [ 'foobar' ] ) )
  62. def AdjustCandidateInsertionText_MoreThanWordMatchingAfterCursor_test():
  63. with MockTextAfterCursor( 'bar.h' ):
  64. eq_( [ { 'abbr': 'foobar.h', 'word': 'foo' } ],
  65. base.AdjustCandidateInsertionText( [ 'foobar.h' ] ) )
  66. with MockTextAfterCursor( 'bar(zoo' ):
  67. eq_( [ { 'abbr': 'foobar(zoo', 'word': 'foo' } ],
  68. base.AdjustCandidateInsertionText( [ 'foobar(zoo' ] ) )
  69. def AdjustCandidateInsertionText_NotSuffix_test():
  70. with MockTextAfterCursor( 'bar' ):
  71. eq_( [ { 'abbr': 'foofoo', 'word': 'foofoo' } ],
  72. base.AdjustCandidateInsertionText( [ 'foofoo' ] ) )
  73. def AdjustCandidateInsertionText_NothingAfterCursor_test():
  74. with MockTextAfterCursor( '' ):
  75. eq_( [ 'foofoo',
  76. 'zobar' ],
  77. base.AdjustCandidateInsertionText( [ 'foofoo',
  78. 'zobar' ] ) )
  79. def AdjustCandidateInsertionText_MultipleStrings_test():
  80. with MockTextAfterCursor( 'bar' ):
  81. eq_( [ { 'abbr': 'foobar', 'word': 'foo' },
  82. { 'abbr': 'zobar', 'word': 'zo' },
  83. { 'abbr': 'qbar', 'word': 'q' },
  84. { 'abbr': 'bar', 'word': '' }, ],
  85. base.AdjustCandidateInsertionText( [ 'foobar',
  86. 'zobar',
  87. 'qbar',
  88. 'bar' ] ) )
  89. def AdjustCandidateInsertionText_DictInput_test():
  90. with MockTextAfterCursor( 'bar' ):
  91. eq_( [ { 'abbr': 'foobar', 'word': 'foo' } ],
  92. base.AdjustCandidateInsertionText(
  93. [ { 'word': 'foobar' } ] ) )
  94. def AdjustCandidateInsertionText_DontTouchAbbr_test():
  95. with MockTextAfterCursor( 'bar' ):
  96. eq_( [ { 'abbr': '1234', 'word': 'foo' } ],
  97. base.AdjustCandidateInsertionText(
  98. [ { 'abbr': '1234', 'word': 'foobar' } ] ) )
  99. def OverlapLength_Basic_test():
  100. eq_( 3, base.OverlapLength( 'foo bar', 'bar zoo' ) )
  101. eq_( 3, base.OverlapLength( 'foobar', 'barzoo' ) )
  102. def OverlapLength_BasicWithUnicode_test():
  103. eq_( 3, base.OverlapLength( u'bar fäö', u'fäö bar' ) )
  104. eq_( 3, base.OverlapLength( u'zoofäö', u'fäözoo' ) )
  105. def OverlapLength_OneCharOverlap_test():
  106. eq_( 1, base.OverlapLength( 'foo b', 'b zoo' ) )
  107. def OverlapLength_SameStrings_test():
  108. eq_( 6, base.OverlapLength( 'foobar', 'foobar' ) )
  109. def OverlapLength_Substring_test():
  110. eq_( 6, base.OverlapLength( 'foobar', 'foobarzoo' ) )
  111. eq_( 6, base.OverlapLength( 'zoofoobar', 'foobar' ) )
  112. def OverlapLength_LongestOverlap_test():
  113. eq_( 7, base.OverlapLength( 'bar foo foo', 'foo foo bar' ) )
  114. def OverlapLength_EmptyInput_test():
  115. eq_( 0, base.OverlapLength( '', 'goobar' ) )
  116. eq_( 0, base.OverlapLength( 'foobar', '' ) )
  117. eq_( 0, base.OverlapLength( '', '' ) )
  118. def OverlapLength_NoOverlap_test():
  119. eq_( 0, base.OverlapLength( 'foobar', 'goobar' ) )
  120. eq_( 0, base.OverlapLength( 'foobar', '(^($@#$#@' ) )
  121. eq_( 0, base.OverlapLength( 'foo bar zoo', 'foo zoo bar' ) )
  122. def LastEnteredCharIsIdentifierChar_Basic_test():
  123. with MockCurrentFiletypes():
  124. with MockCurrentColumnAndLineContents( 3, 'abc' ):
  125. ok_( base.LastEnteredCharIsIdentifierChar() )
  126. with MockCurrentColumnAndLineContents( 2, 'abc' ):
  127. ok_( base.LastEnteredCharIsIdentifierChar() )
  128. with MockCurrentColumnAndLineContents( 1, 'abc' ):
  129. ok_( base.LastEnteredCharIsIdentifierChar() )
  130. def LastEnteredCharIsIdentifierChar_FiletypeHtml_test():
  131. with MockCurrentFiletypes( ['html'] ):
  132. with MockCurrentColumnAndLineContents( 3, 'ab-' ):
  133. ok_( base.LastEnteredCharIsIdentifierChar() )
  134. def LastEnteredCharIsIdentifierChar_ColumnIsZero_test():
  135. with MockCurrentColumnAndLineContents( 0, 'abc' ):
  136. ok_( not base.LastEnteredCharIsIdentifierChar() )
  137. def LastEnteredCharIsIdentifierChar_LineEmpty_test():
  138. with MockCurrentFiletypes():
  139. with MockCurrentColumnAndLineContents( 3, '' ):
  140. ok_( not base.LastEnteredCharIsIdentifierChar() )
  141. with MockCurrentColumnAndLineContents( 0, '' ):
  142. ok_( not base.LastEnteredCharIsIdentifierChar() )
  143. def LastEnteredCharIsIdentifierChar_NotIdentChar_test():
  144. with MockCurrentFiletypes():
  145. with MockCurrentColumnAndLineContents( 3, 'ab;' ):
  146. ok_( not base.LastEnteredCharIsIdentifierChar() )
  147. with MockCurrentColumnAndLineContents( 1, ';' ):
  148. ok_( not base.LastEnteredCharIsIdentifierChar() )
  149. with MockCurrentColumnAndLineContents( 3, 'ab-' ):
  150. ok_( not base.LastEnteredCharIsIdentifierChar() )
  151. def LastEnteredCharIsIdentifierChar_Unicode_test():
  152. with MockCurrentFiletypes():
  153. # CurrentColumn returns a byte offset and character ø is 2 bytes length.
  154. with MockCurrentColumnAndLineContents( 5, 'føo(' ):
  155. ok_( not base.LastEnteredCharIsIdentifierChar() )
  156. with MockCurrentColumnAndLineContents( 4, 'føo(' ):
  157. ok_( base.LastEnteredCharIsIdentifierChar() )
  158. with MockCurrentColumnAndLineContents( 3, 'føo(' ):
  159. ok_( base.LastEnteredCharIsIdentifierChar() )
  160. with MockCurrentColumnAndLineContents( 1, 'føo(' ):
  161. ok_( base.LastEnteredCharIsIdentifierChar() )
  162. def CurrentIdentifierFinished_Basic_test():
  163. with MockCurrentFiletypes():
  164. with MockCurrentColumnAndLineContents( 3, 'ab;' ):
  165. ok_( base.CurrentIdentifierFinished() )
  166. with MockCurrentColumnAndLineContents( 2, 'ab;' ):
  167. ok_( not base.CurrentIdentifierFinished() )
  168. with MockCurrentColumnAndLineContents( 1, 'ab;' ):
  169. ok_( not base.CurrentIdentifierFinished() )
  170. def CurrentIdentifierFinished_NothingBeforeColumn_test():
  171. with MockCurrentColumnAndLineContents( 0, 'ab;' ):
  172. ok_( base.CurrentIdentifierFinished() )
  173. with MockCurrentColumnAndLineContents( 0, '' ):
  174. ok_( base.CurrentIdentifierFinished() )
  175. def CurrentIdentifierFinished_InvalidColumn_test():
  176. with MockCurrentFiletypes():
  177. with MockCurrentColumnAndLineContents( 5, '' ):
  178. ok_( base.CurrentIdentifierFinished() )
  179. with MockCurrentColumnAndLineContents( 5, 'abc' ):
  180. ok_( not base.CurrentIdentifierFinished() )
  181. with MockCurrentColumnAndLineContents( 4, 'ab;' ):
  182. ok_( base.CurrentIdentifierFinished() )
  183. def CurrentIdentifierFinished_InMiddleOfLine_test():
  184. with MockCurrentFiletypes():
  185. with MockCurrentColumnAndLineContents( 4, 'bar.zoo' ):
  186. ok_( base.CurrentIdentifierFinished() )
  187. with MockCurrentColumnAndLineContents( 4, 'bar(zoo' ):
  188. ok_( base.CurrentIdentifierFinished() )
  189. with MockCurrentColumnAndLineContents( 4, 'bar-zoo' ):
  190. ok_( base.CurrentIdentifierFinished() )
  191. def CurrentIdentifierFinished_Html_test():
  192. with MockCurrentFiletypes( ['html'] ):
  193. with MockCurrentColumnAndLineContents( 4, 'bar-zoo' ):
  194. ok_( not base.CurrentIdentifierFinished() )
  195. def CurrentIdentifierFinished_WhitespaceOnly_test():
  196. with MockCurrentFiletypes():
  197. with MockCurrentColumnAndLineContents( 1, '\n' ):
  198. ok_( base.CurrentIdentifierFinished() )
  199. with MockCurrentColumnAndLineContents( 3, '\n ' ):
  200. ok_( base.CurrentIdentifierFinished() )
  201. with MockCurrentColumnAndLineContents( 3, '\t\t\t\t' ):
  202. ok_( base.CurrentIdentifierFinished() )
  203. def CurrentIdentifierFinished_Unicode_test():
  204. with MockCurrentFiletypes():
  205. # CurrentColumn returns a byte offset and character ø is 2 bytes length.
  206. with MockCurrentColumnAndLineContents( 6, 'føo ' ):
  207. ok_( base.CurrentIdentifierFinished() )
  208. with MockCurrentColumnAndLineContents( 5, 'føo ' ):
  209. ok_( base.CurrentIdentifierFinished() )
  210. with MockCurrentColumnAndLineContents( 4, 'føo ' ):
  211. ok_( not base.CurrentIdentifierFinished() )
  212. with MockCurrentColumnAndLineContents( 3, 'føo ' ):
  213. ok_( not base.CurrentIdentifierFinished() )