def test_contains_binds(self): items = [1, 2, 3] m = base_matchers.Contains(base_matchers.Bind('foo', 1)) expected = matcher.MatchInfo( match.ObjectMatch(items), {'foo': matcher.BoundValue(match.ObjectMatch(1))}) self.assertEqual(m.match(_FAKE_CONTEXT, items), expected)
def _function_containing(matcher): """Returns a ast_matchers matcher for a function where any statement in the body matches `matcher`.""" return syntax_matchers.NamedFunctionDefinition(body=base_matchers.Contains( syntax_matchers.IsOrHasDescendant(matcher)))
message= 'Use logging.exception inside an except handler to automatically log the full stack trace of the error', url= 'https://refex.readthedocs.io/en/latest/guide/fixers/logging_exceptions.html', significant=True, category=_LOGGING_EXCEPTION_CATEGORY, matcher=base_matchers.AllOf( ast_matchers.Call( func=base_matchers.Bind( 'logging_error', syntax_matchers.ExprPattern('logging.error')), args=base_matchers.Contains( base_matchers.AllOf( _in_exception_handler( 'e', on_conflict=matcher_.BindConflict.MERGE_IDENTICAL), ast_matchers.Name(id=base_matchers.Bind( 'e', on_conflict=matcher_.BindConflict.MERGE_IDENTICAL)) )), keywords=base_matchers.Unless( base_matchers.Contains( ast_matchers.keyword(arg='exc_info'))), ), ), replacement=dict(logging_error=syntactic_template.PythonStmtTemplate( 'logging.exception')), example_fragment=textwrap.dedent(""" try: x = bar() + baz() except KeyError as e: logging.error('Bad thing happened: %s', e)
def test_contains_wrongtype(self): """It's useful to run a Contains() check against arbitrary objects.""" m = base_matchers.Contains(base_matchers.Anything()) self.assertIsNone(m.match(_FAKE_CONTEXT, object()))
def test_contains_miss(self): items = ['item1', 'item2', 'item3'] m = base_matchers.Contains('notthere') self.assertIsNone(m.match(_FAKE_CONTEXT, items))
def test_contains(self): items = ['item1', 'item2', 'item3'] m = base_matchers.Contains('item2') expected = matcher.MatchInfo(match.ObjectMatch(items)) self.assertEqual(m.match(_FAKE_CONTEXT, items), expected)