base_test.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. from future import standard_library
  25. standard_library.install_aliases()
  26. from builtins import * # noqa
  27. import contextlib
  28. from nose.tools import eq_, ok_
  29. from mock import patch
  30. from ycm.tests.test_utils import MockVimModule
  31. vim_mock = MockVimModule()
  32. from ycm import base
  33. @contextlib.contextmanager
  34. def MockCurrentFiletypes( filetypes = [''] ):
  35. with patch( 'ycm.vimsupport.CurrentFiletypes', return_value = filetypes ):
  36. yield
  37. @contextlib.contextmanager
  38. def MockCurrentColumnAndLineContents( column, line_contents ):
  39. with patch( 'ycm.vimsupport.CurrentColumn', return_value = column ):
  40. with patch( 'ycm.vimsupport.CurrentLineContents',
  41. return_value = line_contents ):
  42. yield
  43. @contextlib.contextmanager
  44. def MockTextAfterCursor( text ):
  45. with patch( 'ycm.vimsupport.TextAfterCursor', return_value = text ):
  46. yield
  47. def AdjustCandidateInsertionText_Basic_test():
  48. with MockTextAfterCursor( 'bar' ):
  49. eq_( [ { 'abbr': 'foobar', 'word': 'foo' } ],
  50. base.AdjustCandidateInsertionText( [ 'foobar' ] ) )
  51. def AdjustCandidateInsertionText_ParenInTextAfterCursor_test():
  52. with MockTextAfterCursor( 'bar(zoo' ):
  53. eq_( [ { 'abbr': 'foobar', 'word': 'foo' } ],
  54. base.AdjustCandidateInsertionText( [ 'foobar' ] ) )
  55. def AdjustCandidateInsertionText_PlusInTextAfterCursor_test():
  56. with MockTextAfterCursor( 'bar+zoo' ):
  57. eq_( [ { 'abbr': 'foobar', 'word': 'foo' } ],
  58. base.AdjustCandidateInsertionText( [ 'foobar' ] ) )
  59. def AdjustCandidateInsertionText_WhitespaceInTextAfterCursor_test():
  60. with MockTextAfterCursor( 'bar zoo' ):
  61. eq_( [ { 'abbr': 'foobar', 'word': 'foo' } ],
  62. base.AdjustCandidateInsertionText( [ 'foobar' ] ) )
  63. def AdjustCandidateInsertionText_MoreThanWordMatchingAfterCursor_test():
  64. with MockTextAfterCursor( 'bar.h' ):
  65. eq_( [ { 'abbr': 'foobar.h', 'word': 'foo' } ],
  66. base.AdjustCandidateInsertionText( [ 'foobar.h' ] ) )
  67. with MockTextAfterCursor( 'bar(zoo' ):
  68. eq_( [ { 'abbr': 'foobar(zoo', 'word': 'foo' } ],
  69. base.AdjustCandidateInsertionText( [ 'foobar(zoo' ] ) )
  70. def AdjustCandidateInsertionText_NotSuffix_test():
  71. with MockTextAfterCursor( 'bar' ):
  72. eq_( [ { 'abbr': 'foofoo', 'word': 'foofoo' } ],
  73. base.AdjustCandidateInsertionText( [ 'foofoo' ] ) )
  74. def AdjustCandidateInsertionText_NothingAfterCursor_test():
  75. with MockTextAfterCursor( '' ):
  76. eq_( [ 'foofoo',
  77. 'zobar' ],
  78. base.AdjustCandidateInsertionText( [ 'foofoo',
  79. 'zobar' ] ) )
  80. def AdjustCandidateInsertionText_MultipleStrings_test():
  81. with MockTextAfterCursor( 'bar' ):
  82. eq_( [ { 'abbr': 'foobar', 'word': 'foo' },
  83. { 'abbr': 'zobar', 'word': 'zo' },
  84. { 'abbr': 'qbar', 'word': 'q' },
  85. { 'abbr': 'bar', 'word': '' }, ],
  86. base.AdjustCandidateInsertionText( [ 'foobar',
  87. 'zobar',
  88. 'qbar',
  89. 'bar' ] ) )
  90. def AdjustCandidateInsertionText_DictInput_test():
  91. with MockTextAfterCursor( 'bar' ):
  92. eq_( [ { 'abbr': 'foobar', 'word': 'foo' } ],
  93. base.AdjustCandidateInsertionText(
  94. [ { 'word': 'foobar' } ] ) )
  95. def AdjustCandidateInsertionText_DontTouchAbbr_test():
  96. with MockTextAfterCursor( 'bar' ):
  97. eq_( [ { 'abbr': '1234', 'word': 'foo' } ],
  98. base.AdjustCandidateInsertionText(
  99. [ { 'abbr': '1234', 'word': 'foobar' } ] ) )
  100. def OverlapLength_Basic_test():
  101. eq_( 3, base.OverlapLength( 'foo bar', 'bar zoo' ) )
  102. eq_( 3, base.OverlapLength( 'foobar', 'barzoo' ) )
  103. def OverlapLength_BasicWithUnicode_test():
  104. eq_( 3, base.OverlapLength( u'bar fäö', u'fäö bar' ) )
  105. eq_( 3, base.OverlapLength( u'zoofäö', u'fäözoo' ) )
  106. def OverlapLength_OneCharOverlap_test():
  107. eq_( 1, base.OverlapLength( 'foo b', 'b zoo' ) )
  108. def OverlapLength_SameStrings_test():
  109. eq_( 6, base.OverlapLength( 'foobar', 'foobar' ) )
  110. def OverlapLength_Substring_test():
  111. eq_( 6, base.OverlapLength( 'foobar', 'foobarzoo' ) )
  112. eq_( 6, base.OverlapLength( 'zoofoobar', 'foobar' ) )
  113. def OverlapLength_LongestOverlap_test():
  114. eq_( 7, base.OverlapLength( 'bar foo foo', 'foo foo bar' ) )
  115. def OverlapLength_EmptyInput_test():
  116. eq_( 0, base.OverlapLength( '', 'goobar' ) )
  117. eq_( 0, base.OverlapLength( 'foobar', '' ) )
  118. eq_( 0, base.OverlapLength( '', '' ) )
  119. def OverlapLength_NoOverlap_test():
  120. eq_( 0, base.OverlapLength( 'foobar', 'goobar' ) )
  121. eq_( 0, base.OverlapLength( 'foobar', '(^($@#$#@' ) )
  122. eq_( 0, base.OverlapLength( 'foo bar zoo', 'foo zoo bar' ) )
  123. def LastEnteredCharIsIdentifierChar_Basic_test():
  124. with MockCurrentFiletypes():
  125. with MockCurrentColumnAndLineContents( 3, 'abc' ):
  126. ok_( base.LastEnteredCharIsIdentifierChar() )
  127. with MockCurrentColumnAndLineContents( 2, 'abc' ):
  128. ok_( base.LastEnteredCharIsIdentifierChar() )
  129. with MockCurrentColumnAndLineContents( 1, 'abc' ):
  130. ok_( base.LastEnteredCharIsIdentifierChar() )
  131. def LastEnteredCharIsIdentifierChar_FiletypeHtml_test():
  132. with MockCurrentFiletypes( ['html'] ):
  133. with MockCurrentColumnAndLineContents( 3, 'ab-' ):
  134. ok_( base.LastEnteredCharIsIdentifierChar() )
  135. def LastEnteredCharIsIdentifierChar_ColumnIsZero_test():
  136. with MockCurrentColumnAndLineContents( 0, 'abc' ):
  137. ok_( not base.LastEnteredCharIsIdentifierChar() )
  138. def LastEnteredCharIsIdentifierChar_LineEmpty_test():
  139. with MockCurrentFiletypes():
  140. with MockCurrentColumnAndLineContents( 3, '' ):
  141. ok_( not base.LastEnteredCharIsIdentifierChar() )
  142. with MockCurrentColumnAndLineContents( 0, '' ):
  143. ok_( not base.LastEnteredCharIsIdentifierChar() )
  144. def LastEnteredCharIsIdentifierChar_NotIdentChar_test():
  145. with MockCurrentFiletypes():
  146. with MockCurrentColumnAndLineContents( 3, 'ab;' ):
  147. ok_( not base.LastEnteredCharIsIdentifierChar() )
  148. with MockCurrentColumnAndLineContents( 1, ';' ):
  149. ok_( not base.LastEnteredCharIsIdentifierChar() )
  150. with MockCurrentColumnAndLineContents( 3, 'ab-' ):
  151. ok_( not base.LastEnteredCharIsIdentifierChar() )
  152. def CurrentIdentifierFinished_Basic_test():
  153. with MockCurrentFiletypes():
  154. with MockCurrentColumnAndLineContents( 3, 'ab;' ):
  155. ok_( base.CurrentIdentifierFinished() )
  156. with MockCurrentColumnAndLineContents( 2, 'ab;' ):
  157. ok_( not base.CurrentIdentifierFinished() )
  158. with MockCurrentColumnAndLineContents( 1, 'ab;' ):
  159. ok_( not base.CurrentIdentifierFinished() )
  160. def CurrentIdentifierFinished_NothingBeforeColumn_test():
  161. with MockCurrentColumnAndLineContents( 0, 'ab;' ):
  162. ok_( base.CurrentIdentifierFinished() )
  163. with MockCurrentColumnAndLineContents( 0, '' ):
  164. ok_( base.CurrentIdentifierFinished() )
  165. def CurrentIdentifierFinished_InvalidColumn_test():
  166. with MockCurrentFiletypes():
  167. with MockCurrentColumnAndLineContents( 5, '' ):
  168. ok_( not base.CurrentIdentifierFinished() )
  169. with MockCurrentColumnAndLineContents( 5, 'abc' ):
  170. ok_( not base.CurrentIdentifierFinished() )
  171. def CurrentIdentifierFinished_InMiddleOfLine_test():
  172. with MockCurrentFiletypes():
  173. with MockCurrentColumnAndLineContents( 4, 'bar.zoo' ):
  174. ok_( base.CurrentIdentifierFinished() )
  175. with MockCurrentColumnAndLineContents( 4, 'bar(zoo' ):
  176. ok_( base.CurrentIdentifierFinished() )
  177. with MockCurrentColumnAndLineContents( 4, 'bar-zoo' ):
  178. ok_( base.CurrentIdentifierFinished() )
  179. def CurrentIdentifierFinished_Html_test():
  180. with MockCurrentFiletypes( ['html'] ):
  181. with MockCurrentColumnAndLineContents( 4, 'bar-zoo' ):
  182. ok_( not base.CurrentIdentifierFinished() )
  183. def CurrentIdentifierFinished_WhitespaceOnly_test():
  184. with MockCurrentFiletypes():
  185. with MockCurrentColumnAndLineContents( 1, '\n' ):
  186. ok_( base.CurrentIdentifierFinished() )
  187. with MockCurrentColumnAndLineContents( 3, '\n ' ):
  188. ok_( base.CurrentIdentifierFinished() )
  189. with MockCurrentColumnAndLineContents( 3, '\t\t\t\t' ):
  190. ok_( base.CurrentIdentifierFinished() )