test_utils.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # Copyright (C) 2011, 2012 Google Inc.
  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 mock import MagicMock
  18. from hamcrest import assert_that, equal_to
  19. import re
  20. import sys
  21. BUFNR_REGEX = re.compile( r"^bufnr\('(.+)', ([0-9]+)\)$" )
  22. BUFWINNR_REGEX = re.compile( r"^bufwinnr\(([0-9]+)\)$" )
  23. BWIPEOUT_REGEX = re.compile( r"^(?:silent! )bwipeout!? ([0-9]+)$" )
  24. # One-and only instance of mocked Vim object. The first 'import vim' that is
  25. # executed binds the vim module to the instance of MagicMock that is created,
  26. # and subsquent assignments to sys.modules[ 'vim' ] don't retrospectively
  27. # update them. The result is that while running the tests, we must assign only
  28. # one instance of MagicMock to sys.modules[ 'vim' ] and always return it.
  29. #
  30. # More explanation is available:
  31. # https://github.com/Valloric/YouCompleteMe/pull/1694
  32. VIM_MOCK = MagicMock()
  33. def MockGetBufferNumber( buffer_filename ):
  34. for buffer in VIM_MOCK.buffers:
  35. if buffer[ 'filename' ] == buffer_filename:
  36. return buffer[ 'number' ]
  37. return -1
  38. def MockGetBufferWindowNumber( buffer_number ):
  39. for buffer in VIM_MOCK.buffers:
  40. if buffer[ 'number' ] == buffer_number and 'window' in buffer:
  41. return buffer[ 'window' ]
  42. return -1
  43. def MockVimEval( value ):
  44. if value == "g:ycm_min_num_of_chars_for_completion":
  45. return 0
  46. if value == "g:ycm_path_to_python_interpreter":
  47. return ''
  48. if value == "tempname()":
  49. return '_TEMP_FILE_'
  50. if value == "&previewheight":
  51. # Default value from Vim
  52. return 12
  53. match = BUFNR_REGEX.search( value )
  54. if match:
  55. return MockGetBufferNumber( match.group( 1 ) )
  56. match = BUFWINNR_REGEX.search( value )
  57. if match:
  58. return MockGetBufferWindowNumber( int( match.group( 1 ) ) )
  59. raise ValueError( 'Unexpected evaluation: ' + value )
  60. def MockWipeoutBuffer( buffer_number ):
  61. buffers = VIM_MOCK.buffers
  62. for index, buffer in enumerate( buffers ):
  63. if buffer[ 'number' ] == buffer_number:
  64. return buffers.pop( index )
  65. def MockVimCommand( command ):
  66. match = BWIPEOUT_REGEX.search( command )
  67. if match:
  68. return MockWipeoutBuffer( int( match.group( 1 ) ) )
  69. raise RuntimeError( 'Unexpected command: ' + command )
  70. def MockVimModule():
  71. """The 'vim' module is something that is only present when running inside the
  72. Vim Python interpreter, so we replace it with a MagicMock for tests. If you
  73. need to add additional mocks to vim module functions, then use 'patch' from
  74. mock module, to ensure that the state of the vim mock is returned before the
  75. next test. That is:
  76. from ycm.test_utils import MockVimModule
  77. from mock import patch
  78. # Do this once
  79. MockVimModule()
  80. @patch( 'vim.eval', return_value='test' )
  81. @patch( 'vim.command', side_effect=ValueError )
  82. def test( vim_command, vim_eval ):
  83. # use vim.command via vim_command, e.g.:
  84. vim_command.assert_has_calls( ... )
  85. Failure to use this approach may lead to unexpected failures in other
  86. tests."""
  87. VIM_MOCK.buffers = {}
  88. VIM_MOCK.eval = MagicMock( side_effect = MockVimEval )
  89. sys.modules[ 'vim' ] = VIM_MOCK
  90. return VIM_MOCK
  91. class ExtendedMock( MagicMock ):
  92. def assert_has_exact_calls( self, calls, any_order = False ):
  93. self.assert_has_calls( calls, any_order )
  94. assert_that( self.call_count, equal_to( len( calls ) ) )