def test_has_assert(self): asserting_programs = [ '\n'.join([ 'def f():', ' assert False, "Always fail."', ]), '\n'.join([ 'def f():', ' if True:', ' assert False, "Always fail."', ]), '\n'.join([ 'def f():', ' with open("/dev/null", "rb") as fin:', ' if "thevoid" in fin.read():', ' assert False, "Never fail."', ]), ] nonasserting_programs = [ '\n'.join([ 'def f():', ' return "hey"', ]), '\n'.join([ 'def f():', ' if x < 10:', ' global x', ' x += 1', ]), '\n'.join([ 'def f():', ' with open("/dev/null", "rb") as fin:', ' if "thevoid" in fin.read():', ' return True', ]), ] for program in asserting_programs: tree = ast.parse(program) function = get_function_descriptions(tree)[0] self.assertTrue( function.raises_assert, ) for program in nonasserting_programs: tree = ast.parse(program) function = get_function_descriptions(tree)[0] self.assertFalse( function.raises_assert, )
def test_empty_description_error(self): program_template = '\n'.join([ 'def f():', ' """Makes the thing scream.' ' ', ' :{}:', ' """', ' scream()', ]) for section in [ 'param x', 'return', 'var x', 'type x', 'vartype x', 'raises Exception', 'yield', 'ytype', 'rtype' ]: program = program_template.format(section) tree = ast.parse(program) function = get_function_descriptions(tree)[0] checker = IntegrityChecker(self.config) checker.run_checks(function) errors = checker.errors self.assertTrue( len(errors) > 0, 'EmptyDescriptionError not defined for {}'.format(section), ) self.assertTrue( any([ isinstance(error, EmptyDescriptionError) for error in errors ]), 'EmptyDescriptionError not defined for {}: {}'.format( section, errors, ), )
def test_missing_parameter_types(self): program = '\n'.join([ 'def function_with_excess_parameter(extra):', ' """We have an extra parameter below, extra.', '', ' Args:', ' extra: This shouldn\'t be here.', '', ' """', ' print(\'Hey!\')', ]) tree = ast.parse(program) functions = get_function_descriptions(tree) checker = IntegrityChecker(config=Configuration( ignore=[], message_template=None, style=DocstringStyle.GOOGLE, strictness=Strictness.FULL_DESCRIPTION, enable=['DAR104'] )) checker.run_checks(functions[0]) errors = checker.errors self.assertEqual(len(errors), 1) self.assertTrue(isinstance(errors[0], ParameterTypeMissingError))
def test_ignore_private_methods(self): program = '\n'.join([ 'def function_with_missing_parameter(x):', ' """We\'re missing a description of x."""', ' print(x / 2)', '' 'def _same_error_but_private_method(x):', ' """We\'re missing a description of x."""', ' print(x / 2)', ]) tree = ast.parse(program) functions = get_function_descriptions(tree) checker = IntegrityChecker( config=Configuration( ignore=[], message_template=None, style=DocstringStyle.GOOGLE, strictness=Strictness.FULL_DESCRIPTION, ignore_regex=r'^_(.*)' ) ) checker.run_checks(functions[0]) checker.run_checks(functions[1]) errors = checker.errors self.assertEqual(len(errors), 1)
def test_nested_functions_partition_signatures(self): program = reindent(r''' def f(x): """Always raise an exception from another function.""" def g(y): """Always raise an exception.""" raise Exception('Always fail') return g(y) ''') tree = ast.parse(program) functions = get_function_descriptions(tree) self.assertEqual( len(functions), 2, ) outer_function = [x for x in functions if x.name == 'f'][0] inner_function = [x for x in functions if x.name == 'g'][0] self.assertEqual( outer_function.argument_names, ['x'], ) self.assertTrue(outer_function.has_return, ) self.assertEqual( outer_function.raises, set(), ) self.assertEqual( inner_function.argument_names, ['y'], ) self.assertEqual( inner_function.raises, {'Exception'}, ) self.assertFalse(inner_function.has_return, )
def get_function_with(self, docstring): program = '\n'.join([ 'def cons(x, l):', ' """{}"""'.format(docstring), ' return [x] + l', ]) tree = ast.parse(program) return get_function_descriptions(tree)[0]
def has_no_errors(self, program): tree = ast.parse(program) functions = get_function_descriptions(tree) checker = IntegrityChecker() checker.run_checks(functions[0]) self.assertEqual( len(checker.errors), 0, 'Expected there to be no errors, but there were {}'.format( len(checker.errors)))