base_test.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. # coding: utf-8
  2. #
  3. # Copyright (C) 2013 Google Inc.
  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. from future import standard_library
  24. standard_library.install_aliases()
  25. from builtins import * # noqa
  26. import contextlib
  27. from nose.tools import eq_, ok_
  28. from mock import patch
  29. from ycm.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. ],
  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() )