base_test.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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_( [ { 'word': 'foo', 'abbr': 'foobar' } ],
  49. base.AdjustCandidateInsertionText( [
  50. { 'word': 'foobar', 'abbr': '' } ] ) )
  51. def AdjustCandidateInsertionText_ParenInTextAfterCursor_test():
  52. with MockTextAfterCursor( 'bar(zoo' ):
  53. eq_( [ { 'word': 'foo', 'abbr': 'foobar' } ],
  54. base.AdjustCandidateInsertionText( [
  55. { 'word': 'foobar', 'abbr': '' } ] ) )
  56. def AdjustCandidateInsertionText_PlusInTextAfterCursor_test():
  57. with MockTextAfterCursor( 'bar+zoo' ):
  58. eq_( [ { 'word': 'foo', 'abbr': 'foobar' } ],
  59. base.AdjustCandidateInsertionText( [
  60. { 'word': 'foobar', 'abbr': '' } ] ) )
  61. def AdjustCandidateInsertionText_WhitespaceInTextAfterCursor_test():
  62. with MockTextAfterCursor( 'bar zoo' ):
  63. eq_( [ { 'word': 'foo', 'abbr': 'foobar' } ],
  64. base.AdjustCandidateInsertionText( [
  65. { 'word': 'foobar', 'abbr': '' } ] ) )
  66. def AdjustCandidateInsertionText_MoreThanWordMatchingAfterCursor_test():
  67. with MockTextAfterCursor( 'bar.h' ):
  68. eq_( [ { 'word': 'foo', 'abbr': 'foobar.h' } ],
  69. base.AdjustCandidateInsertionText( [
  70. { 'word': 'foobar.h', 'abbr': '' } ] ) )
  71. with MockTextAfterCursor( 'bar(zoo' ):
  72. eq_( [ { 'word': 'foo', 'abbr': 'foobar(zoo' } ],
  73. base.AdjustCandidateInsertionText( [
  74. { 'word': 'foobar(zoo', 'abbr': '' } ] ) )
  75. def AdjustCandidateInsertionText_NotSuffix_test():
  76. with MockTextAfterCursor( 'bar' ):
  77. eq_( [ { 'word': 'foofoo', 'abbr': 'foofoo' } ],
  78. base.AdjustCandidateInsertionText( [
  79. { 'word': 'foofoo', 'abbr': '' } ] ) )
  80. def AdjustCandidateInsertionText_NothingAfterCursor_test():
  81. with MockTextAfterCursor( '' ):
  82. eq_( [ { 'word': 'foofoo', 'abbr': '' },
  83. { 'word': 'zobar', 'abbr': '' } ],
  84. base.AdjustCandidateInsertionText( [
  85. { 'word': 'foofoo', 'abbr': '' },
  86. { 'word': 'zobar', 'abbr': '' } ] ) )
  87. def AdjustCandidateInsertionText_MultipleStrings_test():
  88. with MockTextAfterCursor( 'bar' ):
  89. eq_( [ { 'word': 'foo', 'abbr': 'foobar' },
  90. { 'word': 'zo', 'abbr': 'zobar' },
  91. { 'word': 'q', 'abbr': 'qbar' },
  92. { 'word': '', 'abbr': 'bar' }, ],
  93. base.AdjustCandidateInsertionText( [
  94. { 'word': 'foobar', 'abbr': '' },
  95. { 'word': 'zobar', 'abbr': '' },
  96. { 'word': 'qbar', 'abbr': '' },
  97. { 'word': 'bar', 'abbr': '' } ] ) )
  98. def AdjustCandidateInsertionText_DontTouchAbbr_test():
  99. with MockTextAfterCursor( 'bar' ):
  100. eq_( [ { 'word': 'foo', 'abbr': '1234' } ],
  101. base.AdjustCandidateInsertionText( [
  102. { 'word': 'foobar', 'abbr': '1234' } ] ) )
  103. def AdjustCandidateInsertionText_NoAbbr_test():
  104. with MockTextAfterCursor( 'bar' ):
  105. eq_( [ { 'word': 'foo', 'abbr': 'foobar' } ],
  106. base.AdjustCandidateInsertionText( [
  107. { 'word': 'foobar' } ] ) )
  108. def OverlapLength_Basic_test():
  109. eq_( 3, base.OverlapLength( 'foo bar', 'bar zoo' ) )
  110. eq_( 3, base.OverlapLength( 'foobar', 'barzoo' ) )
  111. def OverlapLength_BasicWithUnicode_test():
  112. eq_( 3, base.OverlapLength( u'bar fäö', u'fäö bar' ) )
  113. eq_( 3, base.OverlapLength( u'zoofäö', u'fäözoo' ) )
  114. def OverlapLength_OneCharOverlap_test():
  115. eq_( 1, base.OverlapLength( 'foo b', 'b zoo' ) )
  116. def OverlapLength_SameStrings_test():
  117. eq_( 6, base.OverlapLength( 'foobar', 'foobar' ) )
  118. def OverlapLength_Substring_test():
  119. eq_( 6, base.OverlapLength( 'foobar', 'foobarzoo' ) )
  120. eq_( 6, base.OverlapLength( 'zoofoobar', 'foobar' ) )
  121. def OverlapLength_LongestOverlap_test():
  122. eq_( 7, base.OverlapLength( 'bar foo foo', 'foo foo bar' ) )
  123. def OverlapLength_EmptyInput_test():
  124. eq_( 0, base.OverlapLength( '', 'goobar' ) )
  125. eq_( 0, base.OverlapLength( 'foobar', '' ) )
  126. eq_( 0, base.OverlapLength( '', '' ) )
  127. def OverlapLength_NoOverlap_test():
  128. eq_( 0, base.OverlapLength( 'foobar', 'goobar' ) )
  129. eq_( 0, base.OverlapLength( 'foobar', '(^($@#$#@' ) )
  130. eq_( 0, base.OverlapLength( 'foo bar zoo', 'foo zoo bar' ) )
  131. def LastEnteredCharIsIdentifierChar_Basic_test():
  132. with MockCurrentFiletypes():
  133. with MockCurrentColumnAndLineContents( 3, 'abc' ):
  134. ok_( base.LastEnteredCharIsIdentifierChar() )
  135. with MockCurrentColumnAndLineContents( 2, 'abc' ):
  136. ok_( base.LastEnteredCharIsIdentifierChar() )
  137. with MockCurrentColumnAndLineContents( 1, 'abc' ):
  138. ok_( base.LastEnteredCharIsIdentifierChar() )
  139. def LastEnteredCharIsIdentifierChar_FiletypeHtml_test():
  140. with MockCurrentFiletypes( [ 'html' ] ):
  141. with MockCurrentColumnAndLineContents( 3, 'ab-' ):
  142. ok_( base.LastEnteredCharIsIdentifierChar() )
  143. def LastEnteredCharIsIdentifierChar_ColumnIsZero_test():
  144. with MockCurrentColumnAndLineContents( 0, 'abc' ):
  145. ok_( not base.LastEnteredCharIsIdentifierChar() )
  146. def LastEnteredCharIsIdentifierChar_LineEmpty_test():
  147. with MockCurrentFiletypes():
  148. with MockCurrentColumnAndLineContents( 3, '' ):
  149. ok_( not base.LastEnteredCharIsIdentifierChar() )
  150. with MockCurrentColumnAndLineContents( 0, '' ):
  151. ok_( not base.LastEnteredCharIsIdentifierChar() )
  152. def LastEnteredCharIsIdentifierChar_NotIdentChar_test():
  153. with MockCurrentFiletypes():
  154. with MockCurrentColumnAndLineContents( 3, 'ab;' ):
  155. ok_( not base.LastEnteredCharIsIdentifierChar() )
  156. with MockCurrentColumnAndLineContents( 1, ';' ):
  157. ok_( not base.LastEnteredCharIsIdentifierChar() )
  158. with MockCurrentColumnAndLineContents( 3, 'ab-' ):
  159. ok_( not base.LastEnteredCharIsIdentifierChar() )
  160. def LastEnteredCharIsIdentifierChar_Unicode_test():
  161. with MockCurrentFiletypes():
  162. # CurrentColumn returns a byte offset and character ø is 2 bytes length.
  163. with MockCurrentColumnAndLineContents( 5, 'føo(' ):
  164. ok_( not base.LastEnteredCharIsIdentifierChar() )
  165. with MockCurrentColumnAndLineContents( 4, 'føo(' ):
  166. ok_( base.LastEnteredCharIsIdentifierChar() )
  167. with MockCurrentColumnAndLineContents( 3, 'føo(' ):
  168. ok_( base.LastEnteredCharIsIdentifierChar() )
  169. with MockCurrentColumnAndLineContents( 1, 'føo(' ):
  170. ok_( base.LastEnteredCharIsIdentifierChar() )
  171. def CurrentIdentifierFinished_Basic_test():
  172. with MockCurrentFiletypes():
  173. with MockCurrentColumnAndLineContents( 3, 'ab;' ):
  174. ok_( base.CurrentIdentifierFinished() )
  175. with MockCurrentColumnAndLineContents( 2, 'ab;' ):
  176. ok_( not base.CurrentIdentifierFinished() )
  177. with MockCurrentColumnAndLineContents( 1, 'ab;' ):
  178. ok_( not base.CurrentIdentifierFinished() )
  179. def CurrentIdentifierFinished_NothingBeforeColumn_test():
  180. with MockCurrentColumnAndLineContents( 0, 'ab;' ):
  181. ok_( base.CurrentIdentifierFinished() )
  182. with MockCurrentColumnAndLineContents( 0, '' ):
  183. ok_( base.CurrentIdentifierFinished() )
  184. def CurrentIdentifierFinished_InvalidColumn_test():
  185. with MockCurrentFiletypes():
  186. with MockCurrentColumnAndLineContents( 5, '' ):
  187. ok_( base.CurrentIdentifierFinished() )
  188. with MockCurrentColumnAndLineContents( 5, 'abc' ):
  189. ok_( not base.CurrentIdentifierFinished() )
  190. with MockCurrentColumnAndLineContents( 4, 'ab;' ):
  191. ok_( base.CurrentIdentifierFinished() )
  192. def CurrentIdentifierFinished_InMiddleOfLine_test():
  193. with MockCurrentFiletypes():
  194. with MockCurrentColumnAndLineContents( 4, 'bar.zoo' ):
  195. ok_( base.CurrentIdentifierFinished() )
  196. with MockCurrentColumnAndLineContents( 4, 'bar(zoo' ):
  197. ok_( base.CurrentIdentifierFinished() )
  198. with MockCurrentColumnAndLineContents( 4, 'bar-zoo' ):
  199. ok_( base.CurrentIdentifierFinished() )
  200. def CurrentIdentifierFinished_Html_test():
  201. with MockCurrentFiletypes( [ 'html' ] ):
  202. with MockCurrentColumnAndLineContents( 4, 'bar-zoo' ):
  203. ok_( not base.CurrentIdentifierFinished() )
  204. def CurrentIdentifierFinished_WhitespaceOnly_test():
  205. with MockCurrentFiletypes():
  206. with MockCurrentColumnAndLineContents( 1, '\n' ):
  207. ok_( base.CurrentIdentifierFinished() )
  208. with MockCurrentColumnAndLineContents( 3, '\n ' ):
  209. ok_( base.CurrentIdentifierFinished() )
  210. with MockCurrentColumnAndLineContents( 3, '\t\t\t\t' ):
  211. ok_( base.CurrentIdentifierFinished() )
  212. def CurrentIdentifierFinished_Unicode_test():
  213. with MockCurrentFiletypes():
  214. # CurrentColumn returns a byte offset and character ø is 2 bytes length.
  215. with MockCurrentColumnAndLineContents( 6, 'føo ' ):
  216. ok_( base.CurrentIdentifierFinished() )
  217. with MockCurrentColumnAndLineContents( 5, 'føo ' ):
  218. ok_( base.CurrentIdentifierFinished() )
  219. with MockCurrentColumnAndLineContents( 4, 'føo ' ):
  220. ok_( not base.CurrentIdentifierFinished() )
  221. with MockCurrentColumnAndLineContents( 3, 'føo ' ):
  222. ok_( not base.CurrentIdentifierFinished() )