command_test.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # Copyright (C) 2016-2018 YouCompleteMe contributors
  2. #
  3. # This file is part of YouCompleteMe.
  4. #
  5. # YouCompleteMe is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # YouCompleteMe is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  17. from ycm.tests.test_utils import MockVimModule, MockVimBuffers, VimBuffer
  18. MockVimModule()
  19. from hamcrest import assert_that, contains_exactly, has_entries
  20. from unittest.mock import patch
  21. from unittest import TestCase
  22. from ycm.tests import YouCompleteMeInstance
  23. class CommandTest( TestCase ):
  24. @YouCompleteMeInstance( { 'g:ycm_extra_conf_vim_data': [ 'tempname()' ] } )
  25. def test_SendCommandRequest_ExtraConfVimData_Works( self, ycm ):
  26. current_buffer = VimBuffer( 'buffer' )
  27. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  28. with patch( 'ycm.youcompleteme.SendCommandRequest' ) as send_request:
  29. ycm.SendCommandRequest( [ 'GoTo' ], 'aboveleft', False, 1, 1 )
  30. assert_that(
  31. # Positional arguments passed to SendCommandRequest.
  32. send_request.call_args[ 0 ],
  33. contains_exactly(
  34. contains_exactly( 'GoTo' ),
  35. 'aboveleft',
  36. 'same-buffer',
  37. has_entries( {
  38. 'options': has_entries( {
  39. 'tab_size': 2,
  40. 'insert_spaces': True,
  41. } ),
  42. 'extra_conf_data': has_entries( {
  43. 'tempname()': '_TEMP_FILE_'
  44. } ),
  45. } ),
  46. )
  47. )
  48. @YouCompleteMeInstance( {
  49. 'g:ycm_extra_conf_vim_data': [ 'undefined_value' ] } )
  50. def test_SendCommandRequest_ExtraConfData_UndefinedValue( self, ycm ):
  51. current_buffer = VimBuffer( 'buffer' )
  52. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  53. with patch( 'ycm.youcompleteme.SendCommandRequest' ) as send_request:
  54. ycm.SendCommandRequest( [ 'GoTo' ], 'belowright', False, 1, 1 )
  55. assert_that(
  56. # Positional arguments passed to SendCommandRequest.
  57. send_request.call_args[ 0 ],
  58. contains_exactly(
  59. contains_exactly( 'GoTo' ),
  60. 'belowright',
  61. 'same-buffer',
  62. has_entries( {
  63. 'options': has_entries( {
  64. 'tab_size': 2,
  65. 'insert_spaces': True,
  66. } )
  67. } ),
  68. )
  69. )
  70. @YouCompleteMeInstance()
  71. def test_SendCommandRequest_BuildRange_NoVisualMarks( self, ycm, *args ):
  72. current_buffer = VimBuffer( 'buffer', contents = [ 'first line',
  73. 'second line' ] )
  74. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  75. with patch( 'ycm.youcompleteme.SendCommandRequest' ) as send_request:
  76. ycm.SendCommandRequest( [ 'GoTo' ], '', True, 1, 2 )
  77. send_request.assert_called_once_with(
  78. [ 'GoTo' ],
  79. '',
  80. 'same-buffer',
  81. {
  82. 'options': {
  83. 'tab_size': 2,
  84. 'insert_spaces': True
  85. },
  86. 'range': {
  87. 'start': {
  88. 'line_num': 1,
  89. 'column_num': 1
  90. },
  91. 'end': {
  92. 'line_num': 2,
  93. 'column_num': 12
  94. }
  95. }
  96. },
  97. )
  98. @YouCompleteMeInstance()
  99. def test_SendCommandRequest_BuildRange_VisualMarks( self, ycm, *args ):
  100. current_buffer = VimBuffer( 'buffer',
  101. contents = [ 'first line',
  102. 'second line' ],
  103. visual_start = [ 1, 4 ],
  104. visual_end = [ 2, 8 ] )
  105. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  106. with patch( 'ycm.youcompleteme.SendCommandRequest' ) as send_request:
  107. ycm.SendCommandRequest( [ 'GoTo' ], 'tab', True, 1, 2 )
  108. send_request.assert_called_once_with(
  109. [ 'GoTo' ],
  110. 'tab',
  111. 'same-buffer',
  112. {
  113. 'options': {
  114. 'tab_size': 2,
  115. 'insert_spaces': True
  116. },
  117. 'range': {
  118. 'start': {
  119. 'line_num': 1,
  120. 'column_num': 5
  121. },
  122. 'end': {
  123. 'line_num': 2,
  124. 'column_num': 9
  125. }
  126. }
  127. },
  128. )
  129. @YouCompleteMeInstance()
  130. def test_SendCommandRequest_IgnoreFileTypeOption( self, ycm, *args ):
  131. current_buffer = VimBuffer( 'buffer' )
  132. with MockVimBuffers( [ current_buffer ], [ current_buffer ] ):
  133. expected_args = (
  134. [ 'GoTo' ],
  135. '',
  136. 'same-buffer',
  137. {
  138. 'completer_target': 'python',
  139. 'options': {
  140. 'tab_size': 2,
  141. 'insert_spaces': True
  142. },
  143. },
  144. )
  145. with patch( 'ycm.youcompleteme.SendCommandRequest' ) as send_request:
  146. ycm.SendCommandRequest( [ 'ft=python', 'GoTo' ], '', False, 1, 1 )
  147. send_request.assert_called_once_with( *expected_args )
  148. with patch( 'ycm.youcompleteme.SendCommandRequest' ) as send_request:
  149. ycm.SendCommandRequest( [ 'GoTo', 'ft=python' ], '', False, 1, 1 )
  150. send_request.assert_called_once_with( *expected_args )