base_test.py 8.8 KB

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