def visitParallel(self, ctx: xDroneParser.ParallelContext) -> None: parallel_commands = ParallelDroneCommands() for commands in ctx.commands(): self.commands.append([]) # scope - discard updates on existing variables, discard new variables new_symbol_table = copy.deepcopy(self._get_latest_symbol_table()) self.symbol_table.append(new_symbol_table) self.returned.append(False) self.returned_value.append(None) self.visit(commands) returned_value = self.returned_value.pop(-1) self.returned.pop(-1) self.symbol_table.pop(-1) if returned_value is not None: raise CompileError( "Parallel branch should not return anything, but {} is returned" .format(returned_value)) branch = self.commands.pop(-1) try: parallel_commands.add(branch) except RepeatDroneNameException as e: raise CompileError( "Parallel branches should have exclusive drone names, " "but {} appeared in more than one branches".format( e.repeated_names)) self._get_latest_commands().append(parallel_commands)
def test_add_already_involved_drones_should_give_error(self): parallel_commands = ParallelDroneCommands() parallel_commands.add( [SingleDroneCommand("DRONE1", Command.takeoff())]) with self.assertRaises(RepeatDroneNameException) as context: parallel_commands.add( [SingleDroneCommand("DRONE1", Command.takeoff())]) self.assertTrue({"DRONE1"}, context.exception.repeated_names)
def test_repr(self): parallel_commands = ParallelDroneCommands() parallel_commands.add([]) parallel_commands.add([SingleDroneCommand("abc", Command.takeoff())]) self.assertEqual( "ParallelDroneCommands: { [], " + "[SingleDroneCommand: { drone_name: abc, " + "command: Command: { opcode: takeoff, operands: [] } }] }", repr(parallel_commands))
def test_add(self): parallel_commands = ParallelDroneCommands() parallel_commands.add([]) self.assertEqual([[]], parallel_commands.branches) self.assertEqual([set()], parallel_commands.drones_involved_each_branch) parallel_commands.add([SingleDroneCommand("abc", Command.takeoff())]) self.assertEqual([[], [SingleDroneCommand("abc", Command.takeoff())]], parallel_commands.branches) self.assertEqual([set(), {"abc"}], parallel_commands.drones_involved_each_branch)
def test_eq(self): parallel_commands1 = ParallelDroneCommands() parallel_commands1.add([]) parallel_commands1.add([SingleDroneCommand("abc", Command.takeoff())]) parallel_commands2 = ParallelDroneCommands( [[], [SingleDroneCommand("abc", Command.takeoff())]]) parallel_commands3 = ParallelDroneCommands( [[SingleDroneCommand("abc", Command.takeoff())], []]) self.assertEqual(ParallelDroneCommands(), ParallelDroneCommands()) self.assertEqual(parallel_commands1, parallel_commands2) self.assertNotEqual(parallel_commands1, parallel_commands3) self.assertNotEqual(None, parallel_commands1)
def test_to_command_str(self): parallel_commands = ParallelDroneCommands() parallel_commands.add([]) parallel_commands.add([ SingleDroneCommand("abc", Command.takeoff()), SingleDroneCommand("abc", Command.up(1)) ]) self.assertEqual("{ } || { abc.takeoff(); abc.up(1); };", parallel_commands.to_command_str()) outer_parallel_commands = ParallelDroneCommands() outer_parallel_commands.add([]) outer_parallel_commands.add([parallel_commands]) self.assertEqual("{ } || { { } || { abc.takeoff(); abc.up(1); }; };", outer_parallel_commands.to_command_str())