Add event notification tests

Add tests for the BufferVisit and BufferUnload event notifications.
This commit is contained in:
micbou 2016-10-08 10:53:17 +02:00
parent 2fabac5a67
commit 92f1bbda94
No known key found for this signature in database
GPG Key ID: C7E8FD1F3BDA1E05
3 changed files with 305 additions and 148 deletions

View File

@ -33,9 +33,12 @@ import functools
from ycmd.utils import ToUnicode
BUFNR_REGEX = re.compile( r"^bufnr\('(.+)', ([0-9]+)\)$" )
BUFWINNR_REGEX = re.compile( r"^bufwinnr\(([0-9]+)\)$" )
BWIPEOUT_REGEX = re.compile( r"^(?:silent! )bwipeout!? ([0-9]+)$" )
BUFNR_REGEX = re.compile( '^bufnr\(\'(?P<buffer_filename>.+)\', ([01])\)$' )
BUFWINNR_REGEX = re.compile( '^bufwinnr\((?P<buffer_number>[0-9]+)\)$' )
BWIPEOUT_REGEX = re.compile(
'^(?:silent! )bwipeout!? (?P<buffer_number>[0-9]+)$' )
GETBUFVAR_REGEX = re.compile(
'^getbufvar\((?P<buffer_number>[0-9]+), "&(?P<option>.+)"\)$' )
# One-and only instance of mocked Vim object. The first 'import vim' that is
# executed binds the vim module to the instance of MagicMock that is created,
@ -49,46 +52,74 @@ VIM_MOCK = MagicMock()
def MockGetBufferNumber( buffer_filename ):
for buffer in VIM_MOCK.buffers:
if buffer[ 'filename' ] == buffer_filename:
return buffer[ 'number' ]
for vim_buffer in VIM_MOCK.buffers:
if vim_buffer.name == buffer_filename:
return vim_buffer.number
return -1
def MockGetBufferWindowNumber( buffer_number ):
for buffer in VIM_MOCK.buffers:
if buffer[ 'number' ] == buffer_number and 'window' in buffer:
return buffer[ 'window' ]
for vim_buffer in VIM_MOCK.buffers:
if vim_buffer.number == buffer_number and vim_buffer.window:
return vim_buffer.window
return -1
def MockVimEval( value ):
if value == "g:ycm_min_num_of_chars_for_completion":
return 0
if value == "g:ycm_server_python_interpreter":
def MockGetBufferVariable( buffer_number, option ):
for vim_buffer in VIM_MOCK.buffers:
if vim_buffer.number == buffer_number:
if option == 'mod':
return vim_buffer.modified
if option == 'ft':
return vim_buffer.filetype
return ''
if value == "tempname()":
return ''
def MockVimEval( value ):
if value == 'g:ycm_min_num_of_chars_for_completion':
return 0
if value == 'g:ycm_server_python_interpreter':
return ''
if value == 'tempname()':
return '_TEMP_FILE_'
if value == "&previewheight":
if value == '&previewheight':
# Default value from Vim
return 12
if value == '&omnifunc':
return VIM_MOCK.current.buffer.omnifunc
if value == '&filetype':
return VIM_MOCK.current.buffer.filetype
match = BUFNR_REGEX.search( value )
if match:
return MockGetBufferNumber( match.group( 1 ) )
buffer_filename = match.group( 'buffer_filename' )
return MockGetBufferNumber( buffer_filename )
match = BUFWINNR_REGEX.search( value )
if match:
return MockGetBufferWindowNumber( int( match.group( 1 ) ) )
buffer_number = int( match.group( 'buffer_number' ) )
return MockGetBufferWindowNumber( buffer_number )
raise ValueError( 'Unexpected evaluation: ' + value )
match = GETBUFVAR_REGEX.search( value )
if match:
buffer_number = int( match.group( 'buffer_number' ) )
option = match.group( 'option' )
return MockGetBufferVariable( buffer_number, option )
raise ValueError( 'Unexpected evaluation: {0}'.format( value ) )
def MockWipeoutBuffer( buffer_number ):
buffers = VIM_MOCK.buffers
for index, buffer in enumerate( buffers ):
if buffer[ 'number' ] == buffer_number:
if buffer.number == buffer_number:
return buffers.pop( index )
@ -100,6 +131,50 @@ def MockVimCommand( command ):
raise RuntimeError( 'Unexpected command: ' + command )
class VimBuffer( object ):
"""An object that looks like a vim.buffer object:
- |name| : full path of the buffer;
- |number| : buffer number;
- |contents|: list of lines representing the buffer contents;
- |filetype|: buffer filetype. Empty string if no filetype is set;
- |modified|: True if the buffer has unsaved changes, False otherwise;
- |window| : number of the buffer window. None if the buffer is hidden;
- |omnifunc|: omni completion function used by the buffer."""
def __init__( self, name,
number = 1,
contents = [],
filetype = '',
modified = True,
window = None,
omnifunc = '' ):
self.name = name
self.number = number
self.contents = contents
self.filetype = filetype
self.modified = modified
self.window = window
self.omnifunc = omnifunc
def __getitem__( self, index ):
"""Return the bytes for a given line at index |index|."""
return self.contents[ index ]
def __len__( self ):
return len( self.contents )
def __setitem__( self, key, value ):
return self.contents.__setitem__( key, value )
def GetLines( self ):
"""Return the contents of the buffer as a list of unicode strings."""
return [ ToUnicode( x ) for x in self.contents ]
def MockVimModule():
"""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

View File

@ -23,7 +23,7 @@ from future import standard_library
standard_library.install_aliases()
from builtins import * # noqa
from ycm.test_utils import MockVimModule, ExtendedMock
from ycm.test_utils import ExtendedMock, MockVimModule, VimBuffer
MockVimModule()
import contextlib
@ -33,6 +33,7 @@ from ycm.tests.server_test import Server_test
from ycmd.responses import ( BuildDiagnosticData, Diagnostic, Location, Range,
UnknownExtraConf, ServerError )
from hamcrest import assert_that, contains, has_entries
from mock import call, MagicMock, patch
from nose.tools import eq_, ok_
@ -59,45 +60,18 @@ def MockArbitraryBuffer( filetype, native_available = True ):
"""Used via the with statement, set up mocked versions of the vim module such
that a single buffer is open with an arbitrary name and arbirary contents. Its
filetype is set to the supplied filetype"""
with patch( 'vim.current' ) as vim_current:
def VimEval( value ):
"""Local mock of the vim.eval() function, used to ensure we get the
correct behvaiour"""
if value == '&omnifunc':
# The omnicompleter is not required here
return ''
if value == 'getbufvar(0, "&mod")':
# Ensure that we actually send the even to the server
return 1
if value == 'getbufvar(0, "&ft")' or value == '&filetype':
return filetype
if value.startswith( 'bufnr(' ):
return 0
if value.startswith( 'bufwinnr(' ):
return 0
raise ValueError( 'Unexpected evaluation' )
# Arbitrary, but valid, cursor position
vim_current.window.cursor = ( 1, 2 )
# Arbitrary, but valid, single buffer open
current_buffer = MagicMock()
current_buffer.number = 0
current_buffer.filename = os.path.realpath( 'TEST_BUFFER' )
current_buffer.name = 'TEST_BUFFER'
current_buffer.window = 0
# Arbitrary, but valid, single buffer open.
current_buffer = VimBuffer( os.path.realpath( 'TEST_BUFFER' ),
window = 1,
filetype = filetype )
# The rest just mock up the Vim module so that our single arbitrary buffer
# makes sense to vimsupport module.
with patch( 'vim.buffers', [ current_buffer ] ):
with patch( 'vim.current.buffer', current_buffer ):
with patch( 'vim.eval', side_effect=VimEval ):
# Arbitrary but valid cursor position.
with patch( 'vim.current.window.cursor', ( 1, 2 ) ):
yield
@ -310,7 +284,7 @@ class EventNotification_test( Server_test ):
ok_( self._server_state.FileParseRequestReady() )
self._server_state.HandleFileParseRequest()
vim_command.assert_has_calls( [
PlaceSign_Call( 1, 1, 0, True )
PlaceSign_Call( 1, 1, 1, True )
] )
eq_( self._server_state.GetErrorCount(), 1 )
eq_( self._server_state.GetWarningCount(), 0 )
@ -343,8 +317,8 @@ class EventNotification_test( Server_test ):
ok_( self._server_state.FileParseRequestReady() )
self._server_state.HandleFileParseRequest()
vim_command.assert_has_calls( [
PlaceSign_Call( 2, 2, 0, False ),
UnplaceSign_Call( 1, 0 )
PlaceSign_Call( 2, 2, 1, False ),
UnplaceSign_Call( 1, 1 )
] )
eq_( self._server_state.GetErrorCount(), 0 )
eq_( self._server_state.GetWarningCount(), 1 )
@ -369,7 +343,114 @@ class EventNotification_test( Server_test ):
self._server_state.OnFileReadyToParse()
self._server_state.HandleFileParseRequest()
vim_command.assert_has_calls( [
UnplaceSign_Call( 2, 0 )
UnplaceSign_Call( 2, 1 )
] )
eq_( self._server_state.GetErrorCount(), 0 )
eq_( self._server_state.GetWarningCount(), 0 )
@patch( 'ycm.youcompleteme.YouCompleteMe._AddUltiSnipsDataIfNeeded' )
@patch( 'ycm.client.base_request.BaseRequest.PostDataToHandlerAsync',
new_callable = ExtendedMock )
def BufferVisit_BuildRequestForCurrentAndUnsavedBuffers_test(
self, post_data_to_handler_async, *args ):
current_buffer_file = os.path.realpath( 'current_buffer' )
current_buffer = VimBuffer( name = current_buffer_file,
number = 1,
contents = [ 'current_buffer_content' ],
filetype = 'some_filetype',
modified = False )
modified_buffer_file = os.path.realpath( 'modified_buffer' )
modified_buffer = VimBuffer( name = modified_buffer_file,
number = 2,
contents = [ 'modified_buffer_content' ],
filetype = 'some_filetype',
modified = True )
unmodified_buffer_file = os.path.realpath( 'unmodified_buffer' )
unmodified_buffer = VimBuffer( name = unmodified_buffer_file,
number = 3,
contents = [ 'unmodified_buffer_content' ],
filetype = 'some_filetype',
modified = False )
with patch( 'vim.buffers', [ current_buffer,
modified_buffer,
unmodified_buffer ] ):
with patch( 'vim.current.buffer', current_buffer ):
with patch( 'vim.current.window.cursor', ( 3, 5 ) ):
self._server_state.OnBufferVisit()
assert_that(
# Positional arguments passed to PostDataToHandlerAsync.
post_data_to_handler_async.call_args[ 0 ],
contains(
has_entries( {
'filepath': current_buffer_file,
'line_num': 3,
'column_num': 6,
'file_data': has_entries( {
current_buffer_file: has_entries( {
'contents': 'current_buffer_content\n',
'filetypes': [ 'some_filetype' ]
} ),
modified_buffer_file: has_entries( {
'contents': 'modified_buffer_content\n',
'filetypes': [ 'some_filetype' ]
} )
} ),
'event_name': 'BufferVisit'
} ),
'event_notification'
)
)
@patch( 'ycm.client.base_request.BaseRequest.PostDataToHandlerAsync',
new_callable = ExtendedMock )
def BufferUnload_BuildRequestForDeletedAndUnsavedBuffers_test(
self, post_data_to_handler_async ):
current_buffer_file = os.path.realpath( 'current_buffer' )
current_buffer = VimBuffer( name = current_buffer_file,
number = 1,
contents = [ 'current_buffer_content' ],
filetype = 'some_filetype',
modified = True )
deleted_buffer_file = os.path.realpath( 'deleted_buffer' )
deleted_buffer = VimBuffer( name = deleted_buffer_file,
number = 2,
contents = [ 'deleted_buffer_content' ],
filetype = 'some_filetype',
modified = False )
with patch( 'vim.buffers', [ current_buffer, deleted_buffer ] ):
with patch( 'vim.current.buffer', current_buffer ):
self._server_state.OnBufferUnload( deleted_buffer_file )
assert_that(
# Positional arguments passed to PostDataToHandlerAsync.
post_data_to_handler_async.call_args[ 0 ],
contains(
has_entries( {
'filepath': deleted_buffer_file,
'line_num': 1,
'column_num': 1,
'file_data': has_entries( {
current_buffer_file: has_entries( {
'contents': 'current_buffer_content\n',
'filetypes': [ 'some_filetype' ]
} ),
deleted_buffer_file: has_entries( {
'contents': 'deleted_buffer_content\n',
'filetypes': [ 'some_filetype' ]
} )
} ),
'event_name': 'BufferUnload'
} ),
'event_notification'
)
)

View File

@ -25,14 +25,15 @@ from future import standard_library
standard_library.install_aliases()
from builtins import * # noqa
from ycm.test_utils import ExtendedMock, MockVimModule, MockVimCommand
from ycm.test_utils import ( ExtendedMock, MockVimCommand, VimBuffer,
MockVimModule )
MockVimModule()
from ycm import vimsupport
from nose.tools import eq_
from hamcrest import assert_that, calling, raises, none, has_entry
from mock import MagicMock, call, patch
from ycmd.utils import ToBytes, ToUnicode
from ycmd.utils import ToBytes
import os
import json
@ -705,34 +706,6 @@ def ReplaceChunksInBuffer_UnsortedChunks_test():
AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
class MockBuffer( object ):
"""An object that looks like a vim.buffer object, enough for ReplaceChunk to
generate a location list"""
def __init__( self, lines, name, number ):
self.lines = lines
self.name = name
self.number = number
def __getitem__( self, index ):
""" Return the bytes for a given line at index |index| """
return self.lines[ index ]
def __len__( self ):
return len( self.lines )
def __setitem__( self, key, value ):
return self.lines.__setitem__( key, value )
def GetLines( self ):
""" Return the contents of the buffer as a list of unicode strings"""
return [ ToUnicode( x ) for x in self.lines ]
@patch( 'ycm.vimsupport.VariableExists', return_value = False )
@patch( 'ycm.vimsupport.SetFittingHeightForCurrentWindow' )
@patch( 'ycm.vimsupport.GetBufferNumberForFilename',
@ -758,11 +731,14 @@ def ReplaceChunks_SingleFile_Open_test( vim_command,
_BuildChunk( 1, 1, 2, 1, 'replacement', 'single_file' )
]
result_buffer = MockBuffer( [
result_buffer = VimBuffer(
'single_file',
contents = [
'line1',
'line2',
'line3',
], 'single_file', 1 )
'line3'
]
)
with patch( 'vim.buffers', [ None, result_buffer, None ] ):
vimsupport.ReplaceChunks( chunks )
@ -845,11 +821,14 @@ def ReplaceChunks_SingleFile_NotOpen_test( vim_command,
_BuildChunk( 1, 1, 2, 1, 'replacement', 'single_file' )
]
result_buffer = MockBuffer( [
result_buffer = VimBuffer(
'single_file',
contents = [
'line1',
'line2',
'line3',
], 'single_file', 1 )
'line3'
]
)
with patch( 'vim.buffers', [ None, result_buffer, None ] ):
vimsupport.ReplaceChunks( chunks )
@ -954,11 +933,14 @@ def ReplaceChunks_User_Declines_To_Open_File_test(
_BuildChunk( 1, 1, 2, 1, 'replacement', 'single_file' )
]
result_buffer = MockBuffer( [
result_buffer = VimBuffer(
'single_file',
contents = [
'line1',
'line2',
'line3',
], 'single_file', 1 )
'line3'
]
)
with patch( 'vim.buffers', [ None, result_buffer, None ] ):
vimsupport.ReplaceChunks( chunks )
@ -1031,11 +1013,14 @@ def ReplaceChunks_User_Aborts_Opening_File_test(
_BuildChunk( 1, 1, 2, 1, 'replacement', 'single_file' )
]
result_buffer = MockBuffer( [
result_buffer = VimBuffer(
'single_file',
contents = [
'line1',
'line2',
'line3',
], 'single_file', 1 )
'line3'
]
)
with patch( 'vim.buffers', [ None, result_buffer, None ] ):
assert_that( calling( vimsupport.ReplaceChunks ).with_args( chunks ),
@ -1114,15 +1099,23 @@ def ReplaceChunks_MultiFile_Open_test( vim_command,
_BuildChunk( 2, 1, 2, 1, 'second_file_replacement ', '2_another_file' ),
]
first_file = MockBuffer( [
first_file = VimBuffer(
'1_first_file',
number = 22,
contents = [
'line1',
'line2',
'line3',
], '1_first_file', 22 )
another_file = MockBuffer( [
]
)
another_file = VimBuffer(
'2_another_file',
number = 19,
contents = [
'another line1',
'ACME line2',
], '2_another_file', 19 )
]
)
vim_buffers = [ None ] * 23
vim_buffers[ 22 ] = first_file
@ -1222,9 +1215,10 @@ def _BuildChunk( start_line,
@patch( 'vim.eval', new_callable = ExtendedMock )
def AddDiagnosticSyntaxMatch_ErrorInMiddleOfLine_test( vim_eval ):
current_buffer = MockBuffer( [
'Highlight this error please'
], 'some_file', 1 )
current_buffer = VimBuffer(
'some_file',
contents = [ 'Highlight this error please' ]
)
with patch( 'vim.current.buffer', current_buffer ):
vimsupport.AddDiagnosticSyntaxMatch( 1, 16, 1, 21 )
@ -1235,9 +1229,10 @@ def AddDiagnosticSyntaxMatch_ErrorInMiddleOfLine_test( vim_eval ):
@patch( 'vim.eval', new_callable = ExtendedMock )
def AddDiagnosticSyntaxMatch_WarningAtEndOfLine_test( vim_eval ):
current_buffer = MockBuffer( [
'Highlight this warning'
], 'some_file', 1 )
current_buffer = VimBuffer(
'some_file',
contents = [ 'Highlight this warning' ]
)
with patch( 'vim.current.buffer', current_buffer ):
vimsupport.AddDiagnosticSyntaxMatch( 1, 16, 1, 23, is_error = False )
@ -1339,30 +1334,41 @@ def CheckFilename_test():
def BufferIsVisibleForFilename_test():
buffers = [
{
'number': 1,
'filename': os.path.realpath( 'visible_filename' ),
'window': 1
},
{
'number': 2,
'filename': os.path.realpath( 'hidden_filename' ),
}
vim_buffers = [
VimBuffer(
os.path.realpath( 'visible_filename' ),
number = 1,
window = 1
),
VimBuffer(
os.path.realpath( 'hidden_filename' ),
number = 2,
window = None
)
]
with patch( 'vim.buffers', buffers ):
with patch( 'vim.buffers', vim_buffers ):
eq_( vimsupport.BufferIsVisibleForFilename( 'visible_filename' ), True )
eq_( vimsupport.BufferIsVisibleForFilename( 'hidden_filename' ), False )
eq_( vimsupport.BufferIsVisibleForFilename( 'another_filename' ), False )
@patch( 'ycm.vimsupport.GetBufferNumberForFilename',
side_effect = [ 2, 5, -1 ] )
@patch( 'vim.command',
side_effect = MockVimCommand,
new_callable = ExtendedMock )
def CloseBuffersForFilename_test( vim_command, *args ):
vim_buffers = [
VimBuffer(
os.path.realpath( 'some_filename' ),
number = 2
),
VimBuffer(
os.path.realpath( 'some_filename' ),
number = 5
)
]
with patch( 'vim.buffers', vim_buffers ):
vimsupport.CloseBuffersForFilename( 'some_filename' )
vim_command.assert_has_exact_calls( [
@ -1401,17 +1407,12 @@ def OpenFilename_test( vim_current, vim_command ):
] )
@patch( 'ycm.vimsupport.BufferModified', side_effect = [ True ] )
@patch( 'ycm.vimsupport.FiletypesForBuffer', side_effect = [ [ 'cpp' ] ] )
def GetUnsavedAndSpecifiedBufferData_EncodedUnicodeCharsInBuffers_test( *args ):
def GetUnsavedAndSpecifiedBufferData_EncodedUnicodeCharsInBuffers_test():
filepath = os.path.realpath( 'filename' )
contents = [ ToBytes( u'abc' ), ToBytes( u'fДa' ) ]
vim_buffer = VimBuffer( filepath, contents = contents )
mock_buffer = MagicMock()
mock_buffer.name = filepath
mock_buffer.number = 1
mock_buffer.__iter__.return_value = [ ToBytes ( u'abc' ), ToBytes( u'fДa' ) ]
with patch( 'vim.buffers', [ mock_buffer ] ):
with patch( 'vim.buffers', [ vim_buffer ] ):
assert_that( vimsupport.GetUnsavedAndSpecifiedBufferData( filepath ),
has_entry( filepath,
has_entry( u'contents', u'abc\nfДa\n' ) ) )