def insert_after_other_imports(module, code): last_import = code_of(module, 'last_import') if last_import: insert_after(last_import, code) else: # Add an extra newline separating imports from the code. code_of(module).insert_child(0, Newline()) code_of(module).insert_child(0, code) # Just inserted import becomes the last one. module.store_reference('last_import', code)
def add_test_case(test_suite, test_case): if isinstance(test_suite, Module): # If the main_snippet exists we have to put the new test case # before it. If it doesn't we put the test case at the end. main_snippet = code_of(test_suite, 'main_snippet') if main_snippet: insert_before(main_snippet, test_case.code) else: code_of(test_suite).append_child(test_case.code) elif isinstance(test_suite, TestClass): # Append to the right node, so that indentation level of the # new method is good. if code_of(test_suite).children and is_node_of_type( code_of(test_suite).children[-1], 'suite'): remove_trailing_whitespace(test_case.code) suite = code_of(test_suite).children[-1] # Prefix the definition with the right amount of whitespace. node = find_last_leaf(suite.children[-2]) ident = get_starting_whitespace(suite) # There's no need to have extra newlines. if node.prefix.endswith("\n"): node.prefix += ident.lstrip("\n") else: node.prefix += ident # Insert before the class contents dedent. suite.insert_child(-1, test_case.code) else: code_of(test_suite).append_child(test_case.code) else: raise TypeError("Tried to add a test case to %r." % test_suite) add_test_case_without_append(test_suite, test_case) test_suite.mark_as_changed()
def add_test_case(test_suite, test_case): if isinstance(test_suite, Module): # If the main_snippet exists we have to put the new test case # before it. If it doesn't we put the test case at the end. main_snippet = code_of(test_suite, 'main_snippet') if main_snippet: insert_before(main_snippet, test_case.code) else: code_of(test_suite).append_child(test_case.code) elif isinstance(test_suite, TestClass): # Append to the right node, so that indentation level of the # new method is good. if code_of(test_suite).children and is_node_of_type(code_of(test_suite).children[-1], 'suite'): remove_trailing_whitespace(test_case.code) suite = code_of(test_suite).children[-1] # Prefix the definition with the right amount of whitespace. node = find_last_leaf(suite.children[-2]) ident = get_starting_whitespace(suite) # There's no need to have extra newlines. if node.prefix.endswith("\n"): node.prefix += ident.lstrip("\n") else: node.prefix += ident # Insert before the class contents dedent. suite.insert_child(-1, test_case.code) else: code_of(test_suite).append_child(test_case.code) else: raise TypeError("Tried to add a test case to %r." % test_suite) add_test_case_without_append(test_suite, test_case) test_suite.mark_as_changed()
def ensure_main_snippet(module, main_snippet, force=False): """Make sure the main_snippet is present. Won't overwrite the snippet unless force flag is set. """ if not main_snippet: return current_main_snippet = code_of(module, 'main_snippet') if not current_main_snippet: code_of(module).append_child(main_snippet) module.store_reference('main_snippet', main_snippet) module.mark_as_changed() elif force: current_main_snippet.replace(main_snippet) module.store_reference('main_snippet', main_snippet) module.mark_as_changed()
def test_code_of_method(self): klass = Class('Class', module=self.module) method = Method('method', klass=klass) method_code = object() self.code_tree.add_object(method, method_code) assert_equal(method_code, code_of(method))
def test_code_of_test_method(self): test_class = TestClass('TestClass', parent=self.module) test_method = TestMethod('test_method', parent=test_class) test_method_code = object() self.code_tree.add_object(test_method, test_method_code) assert_equal(test_method_code, code_of(test_method))
def replace_test_case(test_suite, old_test_case, new_test_case): """Replace one test case object with another. As a side effect, AST of the new test case will replace part of the AST in the old test case parent. `Code` attribute of the new test case object will be removed. """ # The easiest way to get the new code inside the AST is to call # replace() on the old test case code. # It is destructive, but since we're discarding the old test case # anyway, it doesn't matter. code_of(old_test_case).replace(new_test_case.code) test_suite.remove_test_case(old_test_case) add_test_case_without_append(test_suite, new_test_case) test_suite.mark_as_changed()
def test_inspects_test_classes_inside_application_modules(self): module = self._inspect_code(application_module_with_test_class) assert_equal_sets(["os", "unittest"], module.imports) assert_equal(application_module_with_test_class, module.get_content()) assert code_of(module, 'main_snippet') is not None assert_equal(["TestFib"], get_names(module.test_classes)) assert_equal(["fib"], get_names(module.functions))
def test_inspects_test_classes_inside_application_modules(self): module = self._inspect_code(application_module_with_test_class) assert_equal_sets(["os", "unittest"], module.imports) assert_equal(application_module_with_test_class, module.get_content()) assert code_of(module, "main_snippet") is not None assert_equal(["TestFib"], get_names(module.test_classes)) assert_equal(["fib"], get_names(module.functions))
def test_recognizes_nose_style_test_code(self): module = self._inspect_code(nose_style_test_functions) assert_equal(["nose"], module.imports) assert_equal(nose_style_test_functions, module.get_content()) assert_equal(None, code_of(module, 'main_snippet'))
def test_recognizes_nose_style_test_code(self): module = self._inspect_code(nose_style_test_functions) assert_equal(["nose"], module.imports) assert_equal(nose_style_test_functions, module.get_content()) assert_equal(None, code_of(module, "main_snippet"))
def test_code_of_test_class(self): test_class = TestClass('TestClass', parent=self.module) test_class_code = object() self.code_tree.add_object(test_class, test_class_code) assert_equal(test_class_code, code_of(test_class))
def test_code_of_function(self): function = Function('fun', module=self.module) function_code = object() self.code_tree.add_object(function, function_code) assert_equal(function_code, code_of(function))
def dotpath2ast_dyn(src_code, func_dotpath): """ dynamic analysis: covert dotpath to python ast """ return code_of(dotpath2obj(src_code, dotpath))
def test_code_of_module(self): assert_equal(self.code, code_of(self.module))
def test_code_of_class(self): klass = Class('Class', module=self.module) class_code = object() self.code_tree.add_object(klass, class_code) assert_equal(class_code, code_of(klass))