base_test.py 8.9 KB

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