Style refactor and cleanup

This commit is contained in:
dhleong 2016-10-22 11:58:57 -04:00
parent bb793826b6
commit 18a25d45b3
2 changed files with 35 additions and 49 deletions

View File

@ -32,20 +32,18 @@ class DiagnosticFilter( object ):
self._filters = [] self._filters = []
for filter_type in iterkeys( config ): for filter_type in iterkeys( config ):
actual_filter_type = filter_type
compiler = FILTER_COMPILERS.get( filter_type ) compiler = FILTER_COMPILERS.get( filter_type )
if compiler is not None: if compiler is not None:
for filter_config in _ListOf( config[ actual_filter_type ] ): for filter_config in _ListOf( config[ filter_type ] ):
fn = compiler( filter_config ) compiledFilter = compiler( filter_config )
self._filters.append( fn ) self._filters.append( compiledFilter )
def IsAllowed( self, diagnostic ): def IsAllowed( self, diagnostic ):
# NOTE: a diagnostic IsAllowed() ONLY if # NOTE: a diagnostic IsAllowed() ONLY if NO filters match it
# NO filters match it for filterMatches in self._filters:
for f in self._filters: if filterMatches( diagnostic ):
if f( diagnostic ):
return False return False
return True return True
@ -57,8 +55,8 @@ class DiagnosticFilter( object ):
all_filters = dict( user_options.get( 'filter_diagnostics', {} ) ) all_filters = dict( user_options.get( 'filter_diagnostics', {} ) )
for typeSpec, filterValue in iteritems( dict( all_filters ) ): for typeSpec, filterValue in iteritems( dict( all_filters ) ):
if typeSpec.find(',') != -1: if typeSpec.find(',') != -1:
for ft in typeSpec.split(','): for filetype in typeSpec.split(','):
all_filters[ ft ] = filterValue all_filters[ filetype ] = filterValue
for filetype in filetypes: for filetype in filetypes:
type_specific = all_filters.get( filetype, {} ) type_specific = all_filters.get( filetype, {} )
@ -77,8 +75,8 @@ def _ListOf( config_entry ):
def _Merge( into, other ): def _Merge( into, other ):
for k in iterkeys( other ): for key in iterkeys( other ):
into[k] = _ListOf( into.get( k ) ) + _ListOf( other[ k ] ) into[ key ] = _ListOf( into.get( key ) ) + _ListOf( other[ key ] )
return into return into
@ -95,7 +93,7 @@ def _CompileRegex( raw_regex ):
def _CompileLevel( level ): def _CompileLevel( level ):
# valid kinds are WARNING and ERROR; # valid kinds are WARNING and ERROR;
# expected input levels are `warning` and `error` # expected input levels are `warning` and `error`
# NB: we don't validate the input... # NOTE: we don't validate the input...
expected_kind = level.upper() expected_kind = level.upper()
def FilterLevel( diagnostic ): def FilterLevel( diagnostic ):

View File

@ -23,9 +23,6 @@ from future import standard_library
standard_library.install_aliases() standard_library.install_aliases()
from builtins import * # noqa from builtins import * # noqa
from ycm.test_utils import MockVimModule
MockVimModule()
from hamcrest import assert_that, equal_to from hamcrest import assert_that, equal_to
from ycm.diagnostic_filter import DiagnosticFilter from ycm.diagnostic_filter import DiagnosticFilter
@ -57,49 +54,40 @@ def RegexFilter_test():
_assert_accepts( f, 'This is a Burrito' ) _assert_accepts( f, 'This is a Burrito' )
class ListOrSingle_test(): def RegexSingleList_test():
# NB: we already test the single config above opts = _JavaFilter( { 'regex' : [ 'taco' ] } )
f = DiagnosticFilter.from_filetype( opts, [ 'java' ] )
def ListOrSingle_SingleList_test( self ): _assert_rejects( f, 'This is a Taco' )
# NB: if the filetype doesn't override the global, _assert_accepts( f, 'This is a Burrito' )
# we would reject burrito and accept taco
opts = _JavaFilter( { 'regex' : [ 'taco' ] } )
f = DiagnosticFilter.from_filetype( opts, [ 'java' ] )
_assert_rejects( f, 'This is a Taco' )
_assert_accepts( f, 'This is a Burrito' )
def ListOrSingle_MultiList_test( self ): def RegexMultiList_test():
# NB: if the filetype doesn't override the global, opts = _JavaFilter( { 'regex' : [ 'taco', 'burrito' ] } )
# we would reject burrito and accept taco f = DiagnosticFilter.from_filetype( opts, [ 'java' ] )
opts = _JavaFilter( { 'regex' : [ 'taco', 'burrito' ] } )
f = DiagnosticFilter.from_filetype( opts, [ 'java' ] )
_assert_rejects( f, 'This is a Taco' ) _assert_rejects( f, 'This is a Taco' )
_assert_rejects( f, 'This is a Burrito' ) _assert_rejects( f, 'This is a Burrito' )
class Level_test(): def LevelWarnings_test():
opts = _JavaFilter( { 'level' : 'warning' } )
f = DiagnosticFilter.from_filetype( opts, [ 'java' ] )
def Level_warnings_test( self ): _assert_rejects( f, { 'text' : 'This is an unimportant taco',
opts = _JavaFilter( { 'level' : 'warning' } ) 'kind' : 'WARNING' } )
f = DiagnosticFilter.from_filetype( opts, [ 'java' ] ) _assert_accepts( f, { 'text' : 'This taco will be shown',
'kind' : 'ERROR' } )
_assert_rejects( f, { 'text' : 'This is an unimportant taco',
'kind' : 'WARNING' } )
_assert_accepts( f, { 'text' : 'This taco will be shown',
'kind' : 'ERROR' } )
def Level_errors_test( self ): def LevelErrors_test():
opts = _JavaFilter( { 'level' : 'error' } ) opts = _JavaFilter( { 'level' : 'error' } )
f = DiagnosticFilter.from_filetype( opts, [ 'java' ] ) f = DiagnosticFilter.from_filetype( opts, [ 'java' ] )
_assert_accepts( f, { 'text' : 'This is an IMPORTANT taco', _assert_accepts( f, { 'text' : 'This is an IMPORTANT taco',
'kind' : 'WARNING' } ) 'kind' : 'WARNING' } )
_assert_rejects( f, { 'text' : 'This taco will NOT be shown', _assert_rejects( f, { 'text' : 'This taco will NOT be shown',
'kind' : 'ERROR' } ) 'kind' : 'ERROR' } )
def MultipleFilterTypesTypeTest_test(): def MultipleFilterTypesTypeTest_test():