def funccall(self, node): # TODO: definitely break this into multiple classes/methods arguments = [] # First parameter is a list if type(node[0]) == str: # Function call method_name = node[0] if method_name[0].isupper(): # Constructor call constructor_class = node[0] arguments = node[1] return haxe_generator.method_call({ "method_name": constructor_class, "arguments": arguments, "is_constructor": True }) else: # Method call not on an object, eg. addChild(...) method_name = node[0] arguments = _args_to_list(node[1]) # If this is a constructor calling the base class constructor, remove # the parameters if the second one is "self". Typical form: # super(SubclassType, self).__init__(...) # In Haxe, this would just be "super()" # args to __init__ would remain as-is. That's not processed here. if method_name == "super" and len( arguments) == 2 and arguments[1] == "self": arguments = [] return haxe_generator.method_call({ "method_name": method_name, "arguments": arguments }) else: # Call on an object, eg. a.b(c, d) target = node[0].children[0] method_name = node[0].children[1].value if len(node) > 1: arguments = _args_to_list(node[1]) return haxe_generator.method_call({ "method_name": method_name, "arguments": arguments, "target": target }) return node
def test_method_call_changes_target_from_self_to_this(self): output = haxe_generator.method_call({ "method_name": "fight", "arguments": ["monster"], "target": "self" }) self.assertEqual("this.fight(monster)", output)
def test_method_call_converts_print_to_trace(self): output = haxe_generator.method_call({ "method_name": "print", "arguments": ['"Hello from Python!"'] }) self.assertEqual("trace(\"Hello from Python!\")", output)
def test_method_call_specifies_target(self): output = haxe_generator.method_call({ "method_name": "damage", "arguments": [28], "target": "monster" }) self.assertEqual("monster.damage(28)", output)
def test_method_call_generates_with_parameters(self): output = haxe_generator.method_call({ "method_name": "calculateDistance", "arguments": ["x1", "x2", "player.y", "377"] }) self.assertEqual("calculateDistance(x1, x2, player.y, 377)", output)
def test_method_call_turns_super_call_to_init_into_regular_super_call( self): output = haxe_generator.method_call({ "target": "super", "method_name": "__init__", "arguments": ["x", "y"] }) self.assertEqual("super(x, y)", output)
def test_method_call_generates_constructor_when_is_constructor_is_true( self): output = haxe_generator.method_call({ "method_name": "Monster", "arguments": ['"assets/images/duck.png"', 5, 1], "is_constructor": True }) self.assertEqual('new Monster("assets/images/duck.png", 5, 1)', output)
def test_method_call_has_brackets_when_no_parameters(self): output = haxe_generator.method_call({ "method_name": "destroy", "arguments": [] }) self.assertEqual("destroy()", output)
def test_method_call_generates_constructor_when_method_name_is_init_and_target_is_super( self, target): data = {'method_name': '__init__', 'arguments': [], 'target': target} output = haxe_generator.method_call(data) self.assertEqual("super()", output)