def test_cait_node_is_method(self): contextualize_report("class Dog:\n def bark(self):\n pass\ndef dogs_bark(alod):\n pass") ast = parse_program() functions = ast.find_all('FunctionDef') self.assertTrue(functions[0].is_method()) self.assertFalse(functions[1].is_method()) classes = ast.find_all('ClassDef') self.assertFalse(classes[0].is_method()) passes = ast.find_all('Pass') self.assertFalse(passes[0].is_method()) self.assertFalse(passes[1].is_method())
def all_documented(): ast = parse_program() defs = ast.find_all('FunctionDef') + ast.find_all("ClassDef") for a_def in defs: if a_def.name == "__init__": continue if (a_def.body and (a_def.body[0].ast_name != "Expr" or a_def.body[0].value.ast_name != "Str")): if a_def.ast_name == 'FunctionDef': gently("You have an undocumented function: " + a_def.name) else: gently("You have an undocumented class: " + a_def.name) return False return True
def match_signature(name, length, *parameters): ast = parse_program() defs = ast.find_all('FunctionDef') for a_def in defs: if a_def._name == name: found_length = len(a_def.args.args) if found_length < length: gently( "The function named <code>{}</code> has fewer parameters ({}) than expected ({})." "<br><br><i>(insuff_args)<i>".format( name, found_length, length)) elif found_length > length: gently( "The function named <code>{}</code> has more parameters ({}) than expected ({})." "<br><br><i>(excess_args)<i>".format( name, found_length, length)) elif parameters: for parameter, arg in zip(parameters, a_def.args.args): arg_name = get_arg_name(arg) if arg_name != parameter: gently( "Error in definition of <code>{}</code>. Expected a parameter named {}, instead " "found {}.<br><br><i>(name_missing)<i>".format( name, parameter, arg_name)) return None else: return a_def else: return a_def else: gently("No function named <code>{name}</code> was found." "<br><br><i>(missing_func_{name})<i>".format(name=name)) return None
def match_signature(name, length, *parameters): ast = parse_program() defs = ast.find_all('FunctionDef') for a_def in defs: if a_def._name == name: found_length = len(a_def.args.args) if found_length < length: gently("The function named <code>{}</code> has fewer parameters ({}) than expected ({}). " "<br><br><i>(insuff_args)<i>".format(name, found_length, length)) elif found_length > length: gently("The function named <code>{}</code> has more parameters ({}) than expected ({}). " "<br><br><i>(excess_args)<i>".format(name, found_length, length)) elif parameters: for parameter, arg in zip(parameters, a_def.args.args): arg_name = get_arg_name(arg) if arg_name != parameter: gently("Error in definition of <code>{}</code>. Expected a parameter named {}, instead " "found {}. <br><br><i>(name_missing)<i>".format(name, parameter, arg_name)) return None else: return a_def else: return a_def else: gently("No function named <code>{name}</code> was found." "<br><br><i>(missing_func_{name})<i>".format(name=name)) return None
def match_function(name, root=None): if root is None: ast = parse_program() else: ast = root defs = ast.find_all('FunctionDef') for a_def in defs: if a_def._name == name: return a_def return None
def test_old_style_api(self): set_source("a = open('file.txt')") ast = parse_program() calls = ast.find_all("Call") self.assertEqual(len(calls), 1) self.assertEqual(calls[0].func.ast_name, 'Name') self.assertEqual(calls[0].func.id, 'open') self.assertEqual(len(calls[0].args), 1) clear_report() set_source("def a():\n pass\na()") ast = parse_program() defs = ast.find_all("FunctionDef") self.assertEqual(len(defs), 1) print(defs[0].body) print(defs[0].name) print(defs[0].args) print(defs[0].__getattr__('name')) self.assertEqual(defs[0]._name, "a") print("SUCCESS")
def match_signature_muted(name, length, *parameters): ast = parse_program() defs = ast.find_all('FunctionDef') for a_def in defs: if a_def._name == name: found_length = len(a_def.args.args) if found_length != length: return None elif parameters: for parameter, arg in zip(parameters, a_def.args.args): arg_name = get_arg_name(arg) if arg_name != parameter: return None else: return a_def else: return a_def return None