def setUp(self): self.fixture = AttributeHandler() self.classdef_handler = ClassDefHandler() self.functiondef_handler = FunctionDefHandler() self.tracker = DefinitionTracker.get_instance() self.source = 'class SomeClass:\n\tdef __init__():\n\t\tself.property = 42\n\t\tself.property2 = 84' self.tree = ast.parse(self.source) add_parents(self.tree) self.tree = self.classdef_handler.handle(self.tree)
def setUp(self): self.fixture = ClassDefHandler() self.tracker = DefinitionTracker.get_instance() self.tracker.clear_definitions() self.source = 'class TestClass(BaseClass):\n\t' \ 'eighty_four = 84\n\t' \ 'def __init__(self):\n\t\t' \ 'self.forty_two = 42\n\t' \ 'def some_method(self):\n\t\t' \ 'pass' self.tree = ast.parse(self.source)
def setUp(self): self.fixture = FunctionDefHandler() self.classdef_handler = ClassDefHandler() self.tracker = DefinitionTracker.get_instance() self.source = 'def FirstFunction(param_1, param_2):\n\t' \ 'first_variable = 42\n\t' \ 'second_variable = param_1 + param_2\n\t' \ 'return second_variable\n' \ 'class SomeClass():\n\t' \ 'def some_method():\n\t\t' \ 'pass' self.tree = ast.parse(self.source) add_parents(self.tree) self.tree = self.classdef_handler.handle(self.tree)
class AttributeHandlerTest(unittest.TestCase): def setUp(self): self.fixture = AttributeHandler() self.classdef_handler = ClassDefHandler() self.functiondef_handler = FunctionDefHandler() self.tracker = DefinitionTracker.get_instance() self.source = 'class SomeClass:\n\tdef __init__():\n\t\tself.property = 42\n\t\tself.property2 = 84' self.tree = ast.parse(self.source) add_parents(self.tree) self.tree = self.classdef_handler.handle(self.tree) def tearDown(self): self.tracker.clear_definitions() def test_visit_Attribute(self): self.tree = self.fixture.handle(self.tree) self.assertEqual(self.tree.body[0].body[0].body[0].targets[0].attr, '_0x385') self.assertEqual(self.tree.body[0].body[0].body[1].targets[0].attr, '_0x3b7') def test_handle_class_properties(self): handled_node = handle_class_properties( self.tree.body[0].body[0].body[0].targets[0], self.tracker) self.assertEqual(handled_node.attr, '_0x385') handled_node = handle_class_properties( self.tree.body[0].body[0].body[1].targets[0], self.tracker) self.assertEqual(handled_node.attr, '_0x3b7') def test_handle_class_properties_outside_init(self): tree = ast.parse( 'class SomeClass:\n\tdef some_method():\n\t\tself.new_property = 42' ) add_parents(tree) tree = self.classdef_handler.handle(tree) handled_node = handle_class_properties( tree.body[0].body[0].body[0].targets[0], self.tracker) self.assertEqual(handled_node.attr, '_0x52e') def test_handle_class_method_calls(self): tree = ast.parse( 'class SomeClass:\n\tdef some_method():\n\t\tpass\na = SomeClass()\na.some_method()' ) add_parents(tree) tree = self.classdef_handler.handle(tree) tree = self.functiondef_handler.handle(tree) handled_node = handle_class_method_calls(tree.body[2].value.func, self.tracker) self.assertEqual(handled_node.attr, '_0x494')
def test_handle_calls_class(self): tree = ast.parse( 'class SomeClass:\n\tpass\nsome_variable = SomeClass()') add_parents(tree) tree = ClassDefHandler().handle(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_calls(tree.body[1].value.func, tracker) self.assertEqual(handled_node.id, '_0x38a')
def setUp(self): self.fixture = NameHandler() self.classdef_handler = ClassDefHandler() self.source = 'class Base1:\n\t' \ 'pass\n\n' \ 'class Base2:\n\t' \ 'pass\n\n' \ 'class SomeClass(Base1, Base2):\n\t' \ 'pass\n' \ 'def first_function():\n\t' \ 'pass\n' \ 'def some_function():\n\t' \ 'a = "a literal"\n\t' \ 'b = first_function()\n\t' \ 'return b' self.tree = ast.parse(self.source) add_parents(self.tree)
def test_handle_class_scope_outside(self): tree = ast.parse('class SomeClass:\n\tpass\nvariable = 42') add_parents(tree) tree = ClassDefHandler().handle(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_class_scope(tree.body[1].targets[0], tracker) self.assertEqual(handled_node, tree.body[1].targets[0]) self.assertEqual( len(tracker.definitions['classes']['SomeClass']['variables']), 0)
def test_handle_returns_class(self): tree = ast.parse( 'class SomeClass:\n\tpass\ndef some_function():\n\treturn SomeClass()' ) add_parents(tree) tree = ClassDefHandler().handle(tree) tree = FunctionDefHandler().handle(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_returns(tree.body[1].body[0].value.func, tracker) self.assertEqual(handled_node.id, '_0x38a')
class CallHandlerTest(unittest.TestCase): def setUp(self): self.fixture = CallHandler() self.tracker = DefinitionTracker.get_instance() self.classDefHandler = ClassDefHandler() self.source = 'class TestClass:\n\tpass\n\na = TestClass()' self.tree = self.classDefHandler.handle(ast.parse(self.source)) def test_visitCall(self): pass
class TestNameHandler(unittest.TestCase): def setUp(self): self.fixture = NameHandler() self.classdef_handler = ClassDefHandler() self.source = 'class Base1:\n\t' \ 'pass\n\n' \ 'class Base2:\n\t' \ 'pass\n\n' \ 'class SomeClass(Base1, Base2):\n\t' \ 'pass\n' \ 'def first_function():\n\t' \ 'pass\n' \ 'def some_function():\n\t' \ 'a = "a literal"\n\t' \ 'b = first_function()\n\t' \ 'return b' self.tree = ast.parse(self.source) add_parents(self.tree) def tearDown(self): tracker = DefinitionTracker.get_instance() tracker.clear_definitions() def test_visitName(self): self.tree = self.classdef_handler.handle(self.tree) self.tree = self.fixture.handle(self.tree) self.assertEqual(self.tree.body[2].bases[0].id, '_0x1ac') self.assertEqual(self.tree.body[2].bases[1].id, '_0x1ad') def test_obscure_class_bases(self): tree = ast.parse('class SomeClass(Base1, Base2):\n\tpass') add_parents(tree) bases = [] tracker = DefinitionTracker.get_instance() tracker.definitions['classes']['Base1'] = { 'prev_name': 'Base1', 'new_name': 'ObscuredBase1' } tracker.definitions['classes']['Base2'] = { 'prev_name': 'Base2', 'new_name': 'ObscuredBase2' } for base in tree.body[0].bases: bases.append(obscure_class_bases(base, tracker)) self.assertEqual(bases[0].id, 'ObscuredBase1') self.assertEqual(bases[1].id, 'ObscuredBase2') def test_handle_function_scope(self): tree = ast.parse('def some_function():\n\ta = 42') add_parents(tree) tree = FunctionDefHandler().handle(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_function_scope(tree.body[0].body[0].targets[0], tracker) self.assertEqual(handled_node.id, '_0x61') def test_handle_function_scope_outside(self): tree = ast.parse('def some_function():\n\tpass\na = 42') add_parents(tree) tree = FunctionDefHandler().handle(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_function_scope(tree.body[1].targets[0], tracker) self.assertEqual(handled_node, tree.body[1].targets[0]) def test_handle_global_scope(self): tree = ast.parse('a = "some string"') add_parents(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_global_scope(tree.body[0].targets[0], tracker) self.assertEqual(handled_node.id, '_0x61') self.assertEqual(len(tracker.definitions['variables']), 1) def test_handle_global_scope_outside(self): tree = ast.parse('def some_function():\n\tsome_variable = 42') add_parents(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_global_scope(tree.body[0].body[0].targets[0], tracker) self.assertEqual(handled_node, tree.body[0].body[0].targets[0]) self.assertEqual(len(tracker.definitions['variables']), 0) def test_handle_class_scope(self): tree = ast.parse('class SomeClass:\n\tclass_variable = 42') add_parents(tree) tree = ClassDefHandler().handle(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_class_scope(tree.body[0].body[0].targets[0], tracker) self.assertEqual(handled_node.id, '_0x5bb') self.assertEqual( len(tracker.definitions['classes']['SomeClass']['variables']), 1) def test_handle_class_scope_outside(self): tree = ast.parse('class SomeClass:\n\tpass\nvariable = 42') add_parents(tree) tree = ClassDefHandler().handle(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_class_scope(tree.body[1].targets[0], tracker) self.assertEqual(handled_node, tree.body[1].targets[0]) self.assertEqual( len(tracker.definitions['classes']['SomeClass']['variables']), 0) def test_handle_calls_class(self): tree = ast.parse( 'class SomeClass:\n\tpass\nsome_variable = SomeClass()') add_parents(tree) tree = ClassDefHandler().handle(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_calls(tree.body[1].value.func, tracker) self.assertEqual(handled_node.id, '_0x38a') def test_handle_calls_function(self): tree = ast.parse( 'def some_function():\n\tpass\nsome_variable = some_function()') add_parents(tree) tree = FunctionDefHandler().handle(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_calls(tree.body[1].value.func, tracker) self.assertEqual(handled_node.id, '_0x579') def test_handle_calls_func_in_func(self): tree = ast.parse( 'def some_function():\n\tdef inside_function():\n\t\tpass\n\tsome_variable = inside_function()' ) add_parents(tree) tree = FunctionDefHandler().handle(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_calls(tree.body[0].body[1].value.func, tracker) self.assertEqual(handled_node.id, '_0x641') def test_handle_calls_outside(self): tree = ast.parse('a = 42') add_parents(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_calls(tree.body[0].targets[0], tracker) self.assertEqual(handled_node, tree.body[0].targets[0]) def test_handle_returns_class(self): tree = ast.parse( 'class SomeClass:\n\tpass\ndef some_function():\n\treturn SomeClass()' ) add_parents(tree) tree = ClassDefHandler().handle(tree) tree = FunctionDefHandler().handle(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_returns(tree.body[1].body[0].value.func, tracker) self.assertEqual(handled_node.id, '_0x38a') def test_handle_returns_function(self): tree = ast.parse( 'def some_function():\n\tpass\ndef another_function():\n\treturn some_function()' ) add_parents(tree) tree = FunctionDefHandler().handle(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_returns(tree.body[1].body[0].value.func, tracker) self.assertEqual(handled_node.id, '_0x579') def test_handle_returns_func_in_func(self): tree = ast.parse( 'def some_function():\n\tdef inside_function():\n\t\tpass\n\treturn inside_function()' ) add_parents(tree) tree = FunctionDefHandler().handle(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_returns(tree.body[0].body[1].value.func, tracker) self.assertEqual(handled_node.id, '_0x641') def test_handle_returns_variable(self): tree = ast.parse( 'def some_function():\n\tsome_variable = 42\n\treturn some_variable' ) add_parents(tree) tree = FunctionDefHandler().handle(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_returns(tree.body[0].body[1].value, tracker) self.assertEqual(handled_node.id, '_0x559') def test_handle_returns_none(self): tree = ast.parse('def some_function():\n\tsome_variable = 42') add_parents(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_returns(tree.body[0].body[0].targets[0], tracker) self.assertEqual(handled_node, tree.body[0].body[0].targets[0])
class FunctionDefHandlerTest(unittest.TestCase): def setUp(self): self.fixture = FunctionDefHandler() self.classdef_handler = ClassDefHandler() self.tracker = DefinitionTracker.get_instance() self.source = 'def FirstFunction(param_1, param_2):\n\t' \ 'first_variable = 42\n\t' \ 'second_variable = param_1 + param_2\n\t' \ 'return second_variable\n' \ 'class SomeClass():\n\t' \ 'def some_method():\n\t\t' \ 'pass' self.tree = ast.parse(self.source) add_parents(self.tree) self.tree = self.classdef_handler.handle(self.tree) def tearDown(self): self.tracker.clear_definitions() def test_visitFunctionDef(self): self.tree = self.fixture.handle(self.tree) self.assertEqual(len(self.tracker.definitions['functions']), 1) function = self.tracker.definitions['functions']['FirstFunction'] self.assertEqual(function['new_name'], '_0x54e') self.assertEqual(function['prev_name'], 'FirstFunction') self.assertEqual(function['variables']['first_variable']['new_name'], '_0x5cd') self.assertEqual(function['args']['param_1']['new_name'], '_0x2a1') self.assertEqual(function['return']['second_variable'], '_0x621') def test_get_args(self): args = get_args(self.tree.body[0]) self.assertEqual(args['param_1']['new_name'], '_0x2a1') self.assertEqual(args['param_2']['new_name'], '_0x2a2') def test_get_args_none(self): tree = ast.parse('def FirstFunction():\n\tpass') args = get_args(tree.body[0]) self.assertTrue(len(args) == 0) def test_get_variables(self): variables = get_variables(self.tree.body[0]) self.assertEqual(variables['first_variable']['new_name'], '_0x5cd') self.assertEqual(variables['second_variable']['new_name'], '_0x621') def test_get_variables_none(self): tree = ast.parse('def FirstFunction():\n\tpass') variables = get_variables(tree.body[0]) self.assertTrue(len(variables) == 0) def test_get_return(self): return_ = get_return(self.tree.body[0]) self.assertEqual(return_['second_variable'], '_0x621') def test_get_return_none(self): tree = ast.parse('def a_function():\n\tpass') return_ = get_return(tree.body[0]) self.assertEqual(len(return_), 0) def test_handle_global_scope(self): tree = ast.parse('def global_function():\n\tpass') add_parents(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_global_scope(tree.body[0], tracker) self.assertTrue('global_function' in tracker.definitions['functions']) def test_handle_global_scope_outside(self): tree = ast.parse( 'def global_function():\n\tdef function_function():\n\t\tpass') add_parents(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_global_scope(tree.body[0].body[0], tracker) self.assertEqual(len(tracker.definitions['functions']), 0) def test_handle_class_scope(self): tree = ast.parse('class SomeClass:\n\tdef some_method():\n\t\tpass') add_parents(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_class_scope(tree.body[0].body[0], tracker) self.assertTrue( len(tracker.definitions['classes']['SomeClass']['methods']), 1) def test_handle_class_scope_outside(self): tree = ast.parse( 'def some_function():\n\tpass\nclass SomeClass:\n\tpass') add_parents(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_class_scope(tree.body[0], tracker) self.assertTrue('some_function' not in tracker.definitions['classes'] ['SomeClass']['methods']) def test_handle_function_scope(self): tree = ast.parse( 'def global_function():\n\tdef function_function():\n\t\tpass') add_parents(tree) tracker = DefinitionTracker.get_instance() handled_node = handle_function_scope(tree.body[0].body[0], tracker) pass def test_handle_function_scope_outside(self): pass
class ClassDefHandlerTest(unittest.TestCase): def setUp(self): self.fixture = ClassDefHandler() self.tracker = DefinitionTracker.get_instance() self.tracker.clear_definitions() self.source = 'class TestClass(BaseClass):\n\t' \ 'eighty_four = 84\n\t' \ 'def __init__(self):\n\t\t' \ 'self.forty_two = 42\n\t' \ 'def some_method(self):\n\t\t' \ 'pass' self.tree = ast.parse(self.source) def test_visitClassDef(self): self.tree = self.fixture.handle(self.tree) self.assertEqual(len(self.tracker.definitions['classes']), 1) def test_create_class_dictionary(self): class_dict = create_class_dictionary(self.tree.body[0]) self.tracker.add_class(class_dict) class_ = self.tracker.definitions['classes']['TestClass'] self.assertEqual(class_['new_name'], '_0x396') self.assertEqual(class_['prev_name'], 'TestClass') self.assertEqual(class_['variables']['eighty_four']['new_name'], '_0x4a5') self.assertEqual(class_['properties']['forty_two'], '_0x3ed') self.assertEqual(class_['methods']['some_method'], '_0x494') self.assertEqual(class_['bases'][0], 'BaseClass') def test_get_variables(self): variables = get_variables(self.tree.body[0]) self.assertEqual(variables['eighty_four']['new_name'], '_0x4a5') def test_get_variables_none(self): tree = ast.parse('class TestClass:\n\tpass') variables = get_variables(tree) self.assertTrue(len(variables) == 0) def test_get_properties(self): properties = get_properties(self.tree.body[0]) self.assertEqual(properties['forty_two'], '_0x3ed') def test_get_properties_none(self): tree = ast.parse('class TestClass:\n\tpass') properties = get_properties(tree) self.assertTrue(len(properties) == 0) def test_get_methods(self): methods = get_methods(self.tree.body[0]) self.assertEqual(methods['some_method'], '_0x494') def test_get_methods_none(self): tree = ast.parse('class TestClass:\n\tpass') methods = get_methods(tree) self.assertTrue(len(methods) == 0) def test_get_base_classes(self): bases = get_base_classes(self.tree.body[0]) self.assertEqual(bases[0], 'BaseClass') def test_get_base_classes_none(self): tree = ast.parse('class TestClass:\n\tpass') bases = get_base_classes(tree) self.assertTrue(len(bases) == 0)
def setUp(self): self.fixture = TestHandler() self.classdef_handler = ClassDefHandler()
def test_replace_handlers(self): handlers = create_handlers(False, False) new_handler = ClassDefHandler() new_handler.test_property = 42 replace_handler(handlers, new_handler, False, False) self.assertEqual(handlers[0].test_property, 42)