diff --git a/python/ycm/diagnostic_filter.py b/python/ycm/diagnostic_filter.py index 523d605d..6c653911 100644 --- a/python/ycm/diagnostic_filter.py +++ b/python/ycm/diagnostic_filter.py @@ -86,10 +86,24 @@ def _Not( fn ): def _CompileRegex( raw_regex ): pattern = re.compile( raw_regex, re.IGNORECASE ) - def Filter( diagnostic ): + def FilterRegex( diagnostic ): return pattern.search( diagnostic[ 'text' ] ) is not None - return Filter + return FilterRegex -FILTER_COMPILERS = { 'regex' : _CompileRegex } +def _CompileLevel( level ): + # valid kinds are WARNING and ERROR; + # expected input levels are `warnings` and `errors` + # NB: we don't validate the input... + expected_kind = level.upper()[:-1] + + def FilterLevel( diagnostic ): + print( diagnostic, 'matches?', expected_kind ) + return diagnostic[ 'kind' ] == expected_kind + + return FilterLevel + + +FILTER_COMPILERS = { 'regex' : _CompileRegex, + 'level' : _CompileLevel } diff --git a/python/ycm/tests/diagnostic_filter_tests.py b/python/ycm/tests/diagnostic_filter_tests.py index e03d96d8..dc84bcf9 100644 --- a/python/ycm/tests/diagnostic_filter_tests.py +++ b/python/ycm/tests/diagnostic_filter_tests.py @@ -31,8 +31,11 @@ from hamcrest import assert_that, equal_to from ycm.diagnostic_filter import DiagnosticFilter -def _assert_accept_equals( filter, text, expected ): - assert_that( filter.Accept( { 'text': text } ), equal_to( expected ) ) +def _assert_accept_equals( filter, text_or_obj, expected ): + if type( text_or_obj ) is not type( {} ): + text_or_obj = { 'text': text_or_obj } + + assert_that( filter.Accept( text_or_obj ), equal_to( expected ) ) def _assert_accepts( filter, text ): _assert_accept_equals( filter, text, True ) @@ -95,9 +98,30 @@ class ListOrSingle_test(): def Invert_test(): - opts = { 'quiet_messages' : { '!regex': [ 'taco' ] } } + opts = { 'quiet_messages' : { '!regex': 'taco' } } f = DiagnosticFilter.from_filetype( opts, [ 'java' ] ) _assert_accepts( f, 'This is a Taco' ) _assert_rejects( f, 'This is a Burrito' ) + +class Level_test(): + + def Level_warnings_test( self ): + opts = { 'quiet_messages' : { 'level': 'warnings' } } + f = DiagnosticFilter.from_filetype( opts, [ 'java' ] ) + + _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 ): + opts = { 'quiet_messages' : { 'level': 'errors' } } + f = DiagnosticFilter.from_filetype( opts, [ 'java' ] ) + + _assert_accepts( f, { 'text': 'This is an IMPORTANT taco', + 'kind': 'WARNING' } ) + _assert_rejects( f, { 'text': 'This taco will NOT be shown', + 'kind': 'ERROR' } )