The 'vim' module is something that is only present when running inside the Vim Python interpreter, so we replace it with a MagicMock for tests. If you need to add additional mocks to vim module functions, then use 'patch' from mock module, to ensure that the state of the vim mock is returned b
()
| 681 | |
| 682 | |
| 683 | def MockVimModule(): |
| 684 | """The 'vim' module is something that is only present when running inside the |
| 685 | Vim Python interpreter, so we replace it with a MagicMock for tests. If you |
| 686 | need to add additional mocks to vim module functions, then use 'patch' from |
| 687 | mock module, to ensure that the state of the vim mock is returned before the |
| 688 | next test. That is: |
| 689 | |
| 690 | from ycm.tests.test_utils import MockVimModule |
| 691 | from unittest.mock import patch |
| 692 | |
| 693 | # Do this once |
| 694 | MockVimModule() |
| 695 | |
| 696 | @patch( 'vim.eval', return_value='test' ) |
| 697 | @patch( 'vim.command', side_effect=ValueError ) |
| 698 | def test( vim_command, vim_eval ): |
| 699 | # use vim.command via vim_command, e.g.: |
| 700 | vim_command.assert_has_calls( ... ) |
| 701 | |
| 702 | Failure to use this approach may lead to unexpected failures in other |
| 703 | tests.""" |
| 704 | |
| 705 | VIM_MOCK.command = MagicMock( side_effect = _MockVimCommand ) |
| 706 | VIM_MOCK.eval = MagicMock( side_effect = _MockVimEval ) |
| 707 | VIM_MOCK.error = VimError |
| 708 | VIM_MOCK.options = MagicMock() |
| 709 | VIM_MOCK.options.__getitem__.side_effect = _MockVimOptions |
| 710 | sys.modules[ 'vim' ] = VIM_MOCK |
| 711 | |
| 712 | return VIM_MOCK |
| 713 | |
| 714 | |
| 715 | class VimError( Exception ): |
no outgoing calls
no test coverage detected