def test_initialize_formation_with_wrong_syntax(self): source = """ formation = new Formation([{drone:d1,y:1,x:1}]); """ lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) self.assertRaises(Exception, parser.Program) source = """ formation = new Formation([{drone:d1,x:1,z:1}]); """ lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) self.assertRaises(Exception, parser.Program) source = """ formation = new Formation([{y:1,drone:d1,x:1}]); """ lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) self.assertRaises(Exception, parser.Program) source = """ formation = new Formation{[{drone:d1,y:1,x:1}]}; """ lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) self.assertRaises(Exception, parser.Program)
def test_function_declaration_in_symbol_table(self): source = """function special_loop(group, transition_list, number_loops) { group.takeOff(5); group.loop(transition_list, number_loops); group.land(); }""" AST.symbolTable = SymbolTable() lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) ast = parser.Program() symTable = ast.fillSymbolTable() table_items = symTable.getTable() assert 'special_loop' in table_items for key, value in symTable.getTable().items(): assert type(key) is str assert type(value) is SymbolTableEntry assert table_items['special_loop'].identifier == 'special_loop' assert table_items['special_loop'].type == 'FUNCTION' assert type(table_items['special_loop'].value) == dict assert len(table_items['special_loop'].value['args']) == 3 assert table_items['special_loop'].value['args'] == [ 'group', 'transition_list', 'number_loops' ] assert len(table_items['special_loop'].value['asssignments']) == 3 assert type(table_items['special_loop'].value['asssignments'] [0].cmd) == TakeOffCmd assert type(table_items['special_loop'].value['asssignments'] [1].cmd) == LoopCmd assert type(table_items['special_loop'].value['asssignments'] [2].cmd) == LandCmd
def test_initialize_three_drones(self): source = """d1 = new Drone("ip1"); d2 = new Drone("ip2"); d3 = new Drone("ip3");""" lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) assert len(parser.tokens) == len(tokens) result = parser.Program() assert result is not None assert len(result.assignments) == 3 assert result.assignments[0].id == 'd1' assert result.assignments[1].id == 'd2' assert result.assignments[2].id == 'd3' for asg in result.assignments: assert type(asg.expr) == Expression assert type(asg.expr.obj) == Drone assert result.assignments[0].expr.obj.ip == '"ip1"' assert result.assignments[1].expr.obj.ip == '"ip2"' assert result.assignments[2].expr.obj.ip == '"ip3"' assert result.functions == []
def test_function_call(self): source = """function special_loop(group, transition_list, number_loops) { group.takeOff(5); group.loop(transition_list, number_loops); group.land(); } special_loop(group, transition_list, number_loops);""" lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) assert len(parser.tokens) == len(tokens) result = parser.Program() assert result is not None assert len(result.assignments) == 1 assert len(result.functions) == 1 assert type(result.functions[0]) == Function assert type(result.assignments[0]) == FunctionCall assert result.assignments[0].id == 'special_loop' assert len(result.assignments[0].args) == 3 assert result.assignments[0].args[0] == 'group' assert result.assignments[0].args[1] == 'transition_list' assert result.assignments[0].args[2] == 'number_loops'
def test_compiler_fail_only_assignments(self): source = """x = 10;""" AST.symbolTable = SymbolTable() lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) ast = parser.Program() symTable = ast.fillSymbolTable() self.assertRaises(Exception, Compiler)
def test_wrong_group_assignment_raises_error(self): source = """group = new Group(formation);""" AST.symbolTable = SymbolTable() lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) ast = parser.Program() #Program.Assignment.Expression.Group.Formation.fillSymbolTable self.assertRaises( Exception, ast.assignments[0].expr.obj.formation.fillSymbolTable)
def test_variable_aliasing_fails(self): source = """x = "hello world"; x = 5; """ AST.symbolTable = SymbolTable() lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) ast = parser.Program() ast.symbolTable = SymbolTable() self.assertRaises(Exception, ast.assignments[0].expr.fillSymbolTable())
def test_string_assignment_in_symbol_table(self): source = """x = "hello world";""" AST.symbolTable = SymbolTable() lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) ast = parser.Program() symTable = ast.fillSymbolTable() table_items = symTable.getTable() assert 'x' in table_items assert type(table_items['x']) == SymbolTableEntry assert table_items['x'].identifier == 'x' assert table_items['x'].type == 'STRING' assert table_items['x'].value == '"hello world"'
def test_drone_assignment_in_symbol_table(self): source = """d1 = new Drone("ip");""" AST.symbolTable = SymbolTable() lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) ast = parser.Program() symTable = ast.fillSymbolTable() table_items = symTable.getTable() assert 'd1' in symTable.getTable() assert type(table_items['d1']) == SymbolTableEntry assert table_items['d1'].identifier == 'd1' assert table_items['d1'].type == 'Drone' assert table_items['d1'].value == '"ip"'
def test_formation_assignment_in_symbol_table(self): source = """formation = new Formation([{drone:d1,x:0,y:0}]);""" AST.symbolTable = SymbolTable() lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) ast = parser.Program() symTable = ast.fillSymbolTable() table_items = symTable.getTable() assert 'formation' in table_items assert type(table_items['formation']) == SymbolTableEntry assert table_items['formation'].identifier == 'formation' assert table_items['formation'].type == 'Formation' assert table_items['formation'].value == [('d1', 0, 0)]
def test_wrong_assignment(self): source = """Transition;""" lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) self.assertRaises(Exception, parser.Program)
def test_wrong_expression(self): source = """ x = ; """ lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) self.assertRaises(Exception, parser.Program)
def test_initialize_wrong_type(self): source = """transition = new Type(formation, 5, y);""" lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) self.assertRaises(Exception, parser.Program)
def test_initialize_drone_with_wrong_argument_type(self): source = """ d1 = new Drone(5); """ lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) self.assertRaises(Exception, parser.Program)
def test_initialize_transition_with_wrong_syntax(self): source = """ transition = new Transition; """ lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) self.assertRaises(Exception, parser.Program)
def test_group_land_method(self): source = """group.land();""" lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) assert len(parser.tokens) == len(tokens) result = parser.Program() assert result is not None assert len(result.assignments) == 1 assert result.assignments[0].id == 'group' assert result.assignments[0].expr == None assert type(result.assignments[0].cmd) == LandCmd assert result.assignments[0].cmd.type == 'land' assert result.functions == []
def test_initialize_one_drone(self): source = """d1 = new Drone("ip address");""" lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) assert len(parser.tokens) == len(tokens) result = parser.Program() assert result is not None assert len(result.assignments) == 1 assert result.assignments[0].id == 'd1' assert type(result.assignments[0].expr) == Expression assert type(result.assignments[0].expr.obj) == Drone assert result.assignments[0].expr.obj.ip == '"ip address"' assert result.functions == []
def test_initialize_group(self): source = """group = new Group(initial_formation);""" lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) assert len(parser.tokens) == len(tokens) result = parser.Program() assert result is not None assert len(result.assignments) == 1 assert result.assignments[0].id == 'group' assert type(result.assignments[0].expr) == Expression assert type(result.assignments[0].expr.obj) == Group assert result.assignments[ 0].expr.obj.formation.var == 'initial_formation' assert result.functions == []
def test_group_takeOff_method(self): source = """group.takeOff(5);""" lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) assert len(parser.tokens) == len(tokens) result = parser.Program() assert result is not None assert len(result.assignments) == 1 assert result.assignments[0].id == 'group' assert result.assignments[0].expr == None assert type(result.assignments[0].cmd) == TakeOffCmd assert result.assignments[0].cmd.height == 5 assert result.assignments[0].cmd.type == 'takeOff' assert result.functions == []
def test_initialize_transition(self): source = """transition = new Transition(formation, 5);""" lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) assert len(parser.tokens) == len(tokens) result = parser.Program() assert result is not None assert len(result.assignments) == 1 assert result.assignments[0].id == 'transition' assert type(result.assignments[0].expr) == Expression assert type(result.assignments[0].expr.obj) == Transition assert type(result.assignments[0].expr.obj.formation) == Formation assert result.assignments[0].expr.obj.time == 5 assert result.functions == []
def test_group_transition_method(self): source = """group.transition(transition);""" lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) assert len(parser.tokens) == len(tokens) result = parser.Program() assert result is not None assert len(result.assignments) == 1 assert result.assignments[0].id == 'group' assert result.assignments[0].expr == None assert type(result.assignments[0].cmd) == TransitionCmd assert type(result.assignments[0].cmd.transition) == Transition assert result.assignments[0].cmd.type == 'transition' assert result.assignments[0].cmd.transition.var == 'transition' assert result.functions == []
def test_arithmetics(self): source = """b = 5 + 7 + 4 + 10 + 5 / 2 - 10 * 4;""" lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) assert len(parser.tokens) == len(tokens) result = parser.Program() assert result is not None assert len(result.assignments) == 1 assert result.assignments[0].id == 'b' assert type(result.assignments[0].expr) == Expression assert result.assignments[0].expr.obj == None assert type(result.assignments[0].expr.term) == Calc assert len(result.assignments[0].expr.term.operands) == 8 assert len(result.assignments[0].expr.term.operators) == 7 assert result.functions == []
def test_assign_number(self): source = """height = 5;""" lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) assert len(parser.tokens) == len(tokens) result = parser.Program() assert result is not None assert len(result.assignments) == 1 assert result.assignments[0].id == 'height' assert type(result.assignments[0].expr) == Expression assert result.assignments[0].expr.obj == None assert type(result.assignments[0].expr.term) == Term assert result.assignments[0].expr.term.type == 'NUMBER' assert result.assignments[0].expr.term.term == 5 assert result.functions == []
def test_assign_string(self): source = """str = "hello world";""" lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) assert len(parser.tokens) == len(tokens) result = parser.Program() assert result is not None assert len(result.assignments) == 1 assert result.assignments[0].id == 'str' assert type(result.assignments[0].expr) == Expression assert result.assignments[0].expr.obj == None assert type(result.assignments[0].expr.term) == Term assert result.assignments[0].expr.term.type == 'STRING' assert result.assignments[0].expr.term.term == '"hello world"' assert result.functions == []
def test_group_assignment_in_symbol_table(self): source = """formation = new Formation([{drone:d1,x:0,y:0}]); group = new Group(formation);""" AST.symbolTable = SymbolTable() lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) ast = parser.Program() symTable = ast.fillSymbolTable() table_items = symTable.getTable() assert 'formation' in table_items assert 'group' in table_items for key, value in symTable.getTable().items(): assert type(key) is str assert type(value) is SymbolTableEntry assert table_items['group'].identifier == 'group' assert table_items['group'].type == 'Group' assert table_items['group'].value == [('d1', 0, 0)]
def test_group_loop_method(self): source = """group.loop(transition_list, 3);""" lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) assert len(parser.tokens) == len(tokens) result = parser.Program() assert result is not None assert len(result.assignments) == 1 assert result.assignments[0].id == 'group' assert result.assignments[0].expr == None assert type(result.assignments[0].cmd) == LoopCmd assert result.assignments[0].cmd.type == 'loop' assert result.assignments[0].cmd.num_loops == 3 assert result.assignments[0].cmd.transition_list == 'transition_list' assert result.functions == []
def test_initialize_list(self): source = """list = [5, 6];""" lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) assert len(parser.tokens) == len(tokens) result = parser.Program() assert result is not None assert len(result.assignments) == 1 assert result.assignments[0].id == 'list' assert type(result.assignments[0].expr) == Expression assert result.assignments[0].expr.obj == None assert type(result.assignments[0].expr.term) == Term assert len(result.assignments[0].expr.term.list_terms) == 2 assert result.assignments[0].expr.term.list_terms[0] == 5 assert result.assignments[0].expr.term.list_terms[1] == 6 assert result.functions == []
def test_initialize_formation(self): source = """formation = new Formation([{drone:d1,x:1,y:1}]);""" lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) assert len(parser.tokens) == len(tokens) result = parser.Program() assert result is not None assert len(result.assignments) == 1 assert result.assignments[0].id == 'formation' assert type(result.assignments[0].expr) == Expression assert type(result.assignments[0].expr.obj) == Formation assert len(result.assignments[0].expr.obj.drones_list) == 1 assert result.assignments[0].expr.obj.drones_list[0].id == 'd1' assert result.assignments[0].expr.obj.drones_list[0].x == 1 assert result.assignments[0].expr.obj.drones_list[0].y == 1 assert result.functions == []
def test_compiler_collisions(self): source = """d1 = new Drone("ip1"); d2 = new Drone("ip2"); initial_formation = new Formation([{drone:d1,x:1,y:1},{drone:d2,x:2,y:1}]); group = new Group(initial_formation); group.takeOff(10); second_formation = new Formation([{drone:d1,x:2,y:1},{drone:d2,x:2,y:2}]); group.wait(5); group.land();""" AST.symbolTable = SymbolTable() lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) ast = parser.Program() ast.symbolTable = SymbolTable() ast.fillSymbolTable() # Compiler(ast.instructionsTable) self.assertRaises(Exception, Compiler)
def test_compiler_init(self): source = """d1 = new Drone("ip1"); d2 = new Drone("ip2"); initial_formation = new Formation([{drone:d1,x:1,y:1},{drone:d2,x:2,y:1}]); group = new Group(initial_formation); group.takeOff(10); second_formation = new Formation([{drone:d1,x:1,y:2},{drone:d2,x:2,y:2}]); transition = new Transition(second_formation, 10); group.transition(transition); group.wait(5); group.land();""" AST.symbolTable = SymbolTable() AST.instructionsTable = [] lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) ast = parser.Program() ast.fillSymbolTable() compiled = Compiler(ast.instructionsTable) assert type(compiled) is Compiler assert len(compiled.instructions) == 4 assert type(compiled.instructions[0]) == TakeOffCmd assert type(compiled.instructions[1]) == TransitionCmd assert type(compiled.instructions[2]) == WaitCmd assert type(compiled.instructions[3]) == LandCmd assert len(compiled.start_config) == 2 assert 'd1' in compiled.start_config assert 'd2' in compiled.start_config assert len(compiled.udp_commands) == 2 # FIRST DONE # ASCII code for "takeoff" assert compiled.udp_commands[0][0] == [ '74', '61', '6b', '65', '6f', '66', '66' ] # ASCII code for "back 100" assert compiled.udp_commands[0][1] == [ '62', '61', '63', '6b', '20', '31', '30', '30' ] # ASCII code for "command" aka the wait command assert compiled.udp_commands[0][2] == [ '63', '6f', '6d', '6d', '61', '6e', '64' ] # ASCII code for "land" assert compiled.udp_commands[0][3] == ['6c', '61', '6e', '64'] # SECOND DRONE # ASCII code for "takeoff" assert compiled.udp_commands[1][0] == [ '74', '61', '6b', '65', '6f', '66', '66' ] # ASCII code for "back 100" assert compiled.udp_commands[1][1] == [ '62', '61', '63', '6b', '20', '31', '30', '30' ] # ASCII code for "command" aka the wait command assert compiled.udp_commands[1][2] == [ '63', '6f', '6d', '6d', '61', '6e', '64' ] # ASCII code for "land" assert compiled.udp_commands[1][3] == ['6c', '61', '6e', '64']