示例#1
0
 def test_place_with_valid_coordinates_and_direction_places_robot_in_valid_place(
         self):
     robot = ToyRobot()
     robot.place(3, 3, "NORTH")
     self.assertEqual(robot.x, 3)
     self.assertEqual(robot.y, 3)
     self.assertEqual(robot.direction, "NORTH")
示例#2
0
 def test_right_rotates_robot_90_degrees_to_the_right_when_facing_west(
         self):
     robot = ToyRobot(4, 4, "WEST")
     robot.right()
     self.assertEqual(robot.x, 4)
     self.assertEqual(robot.y, 4)
     self.assertEqual(robot.direction, "NORTH")
示例#3
0
 def __init__(self, command_text):
     self.commands = get_commands_from_input(command_text)
     self.output_texts = []
     self.toy_robot = ToyRobot()
     self.movement_dict = {
         'LEFT': self.toy_robot.rotate_left,
         'RIGHT': self.toy_robot.rotate_right,
         'MOVE': self.toy_robot.move
     }
     self.execute_commands()
     self.output_results()
 def test_turn_position(self):
     toy_robot_obj = ToyRobot()
     toy_robot_obj.set_placement_position(0,1,"EAST")
     self.assertEqual((toy_robot_obj.move_forward())[0], True)
     self.assertEqual((toy_robot_obj.move_forward())[0], True)
     toy_robot_obj.turn_position("LEFT")
     self.assertEqual((toy_robot_obj.move_forward())[0], True)
     self.assertEqual((toy_robot_obj.move_forward())[0], True)
     self.assertEqual((toy_robot_obj.move_forward())[0], True)
     self.assertEqual((toy_robot_obj.move_forward())[0], False)
示例#5
0
class Simulation():
    """Simulation
    Represents one simulation in which for a given text one toy_robot is created and
    does the commands specified.
    After executing all the commands the results of each REPORT command are outputed to stdout.
    """
    def __init__(self, command_text):
        self.commands = get_commands_from_input(command_text)
        self.output_texts = []
        self.toy_robot = ToyRobot()
        self.movement_dict = {
            'LEFT': self.toy_robot.rotate_left,
            'RIGHT': self.toy_robot.rotate_right,
            'MOVE': self.toy_robot.move
        }
        self.execute_commands()
        self.output_results()

    def execute_commands(self):
        """execute_commands
        For each command in self.commands call the appropriate ToyRobot action
        """

        for command in self.commands:
            if command in self.movement_dict:
                self.movement_dict[command]()
            elif command == 'REPORT':
                report = self.toy_robot.report()
                if report:
                    self.output_texts.append(report)
            elif command.startswith('PLACE'):
                try:
                    x, y, direction = parse_place_command(command)
                    self.toy_robot.set_position(x, y, direction)
                except InputException:
                    '''Exception
                    Silently ignore it to continue robot movement
                    '''
                    continue

    def output_results(self):
        """output_results
        Output all the texts available in self.output_texts which were filled
        in execute_commands
        """

        for output_text in self.output_texts:
            print(output_text)
示例#6
0
	def test__is_position_valid(self):
		toyRobot = ToyRobot()
		self.assertTrue(toyRobot._is_position_valid(0, 0))
		self.assertTrue(toyRobot._is_position_valid(4, 4))
		self.assertFalse(toyRobot._is_position_valid(5, 0))
		self.assertFalse(toyRobot._is_position_valid(0, 5))
		self.assertFalse(toyRobot._is_position_valid(-1, 0))
		self.assertFalse(toyRobot._is_position_valid(0, -1))
		self.assertFalse(toyRobot._is_position_valid(None, 0))
		self.assertFalse(toyRobot._is_position_valid(0, None))

		toyRobot = ToyRobot(10, 10)
		self.assertTrue(toyRobot._is_position_valid(0, 0))
		self.assertTrue(toyRobot._is_position_valid(9, 9))
		self.assertFalse(toyRobot._is_position_valid(10, 0))
		self.assertFalse(toyRobot._is_position_valid(0, 10))
 def test_validate_place_command(self):
     toy_robot_obj = ToyRobot()
     self.assertEqual(toy_robot_obj.validate_place_command(0,1,"NORTH"), True)
     self.assertEqual(toy_robot_obj.validate_place_command(0,1,"North"), True)
     self.assertEqual(toy_robot_obj.validate_place_command(0,1,"NOTH"), False)
     self.assertEqual(toy_robot_obj.validate_place_command(5,1,"NORTH"), False)
     self.assertEqual(toy_robot_obj.validate_place_command(0,1,"SOUTH-WEST"), False)
     self.assertEqual(toy_robot_obj.validate_place_command(5,1,"NORTH"), False)
class TestToyRobotMovement(unittest.TestCase):
    def setUp(self):
        self.toy_robot = ToyRobot()
        self.toy_robot.set_position(0, 0, 'EAST')

    def test_move_and_rotate(self):
        self.toy_robot.move()
        self.toy_robot.move()
        self.toy_robot.rotate_left()
        self.toy_robot.move()
        self.toy_robot.move()
        self.assertEqual(self.toy_robot.position.x, 2)
        self.assertEqual(self.toy_robot.position.y, 2)
        self.assertEqual(self.toy_robot.position.direction, 'NORTH')
class TestToyRobotWithoutPosition(unittest.TestCase):
    def setUp(self):
        self.toy_robot = ToyRobot()

    def test_commands_while_position_is_not_set(self):
        self.toy_robot.move()
        self.toy_robot.rotate_right()
        self.toy_robot.rotate_left()
        self.assertIsNone(self.toy_robot.position)
示例#10
0
class TestToyRobotPosition(unittest.TestCase):
    def setUp(self):
        self.toy_robot = ToyRobot()
        self.toy_robot.set_position(0, 0, 'EAST')

    def test_rotate_left(self):
        self.toy_robot.rotate_left()
        self.assertEqual(self.toy_robot.position.direction, 'NORTH')

    def test_rotate_right(self):
        self.toy_robot.rotate_right()
        self.assertEqual(self.toy_robot.position.direction, 'SOUTH')

    def test_set_position(self):
        self.assertEqual(self.toy_robot.position.x, 0)
        self.assertEqual(self.toy_robot.position.y, 0)
        self.assertEqual(self.toy_robot.position.direction, 'EAST')
示例#11
0
 def test_move_robot_adds_1_step_towards_west(self):
     robot = ToyRobot(1, 3, "WEST")
     robot.move()
     self.assertEqual(robot.x, 0)
     self.assertEqual(robot.y, 3)
     self.assertEqual(robot.direction, "WEST")
示例#12
0
 def test_move_robot_adds_1_step_towards_east(self):
     robot = ToyRobot(0, 3, "EAST")
     robot.move()
     self.assertEqual(robot.x, 1)
     self.assertEqual(robot.y, 3)
     self.assertEqual(robot.direction, "EAST")
示例#13
0
 def test_move_robot_adds_1_step_towards_south(self):
     robot = ToyRobot(0, 3, "SOUTH")
     robot.move()
     self.assertEqual(robot.x, 0)
     self.assertEqual(robot.y, 2)
     self.assertEqual(robot.direction, "SOUTH")
示例#14
0
 def test_move_robot_adds_1_step_towards_north(self):
     robot = ToyRobot(0, 3, "NORTH")
     robot.move()
     self.assertEqual(robot.x, 0)
     self.assertEqual(robot.y, 4)
     self.assertEqual(robot.direction, "NORTH")
示例#15
0
 def test_place_with_invalid_direction_raises_error(self):
     robot = ToyRobot()
     with self.assertRaises(InputError):
         robot.place(0, 3, "DOWN")
示例#16
0
 def test_place_with_invalid_coordinates_raises_error(self):
     robot = ToyRobot()
     with self.assertRaises(InputError):
         robot.place(6, 3, "WEST")
示例#17
0
	def test__is_placed(self):
		toyRobot = ToyRobot()
		self.assertFalse(toyRobot._is_placed)

		toyRobot.place(0, 0, Direction.NORTH)
		self.assertTrue(toyRobot._is_placed)
示例#18
0
 def test_init_sets_correct_non_default_attributes(self):
     robot = ToyRobot(4, 2, "EAST")
     self.assertEqual(robot.x, 4)
     self.assertEqual(robot.y, 2)
     self.assertEqual(robot.direction, "EAST")
示例#19
0
 def test_move_prevents_robot_from_falling_west(self):
     robot = ToyRobot(0, 4, "WEST")
     robot.move()
     self.assertEqual(robot.x, 0)
     self.assertEqual(robot.y, 4)
     self.assertEqual(robot.direction, "WEST")
示例#20
0
	def test_place(self):
		toyRobot = ToyRobot()
		toyRobot.place(0, 0, Direction.NORTH)
		self.assertEqual(toyRobot.report(), "0, 0, NORTH")

		toyRobot.place(1, 1, Direction.WEST)
		self.assertEqual(toyRobot.report(), "1, 1, WEST")

		toyRobot.place(0, 1, Direction.EAST)
		self.assertEqual(toyRobot.report(), "0, 1, EAST")

		toyRobot.place(1, 2, Direction.SOUTH)
		self.assertEqual(toyRobot.report(), "1, 2, SOUTH")

		toyRobot.place(-1, 2, Direction.SOUTH)
		self.assertNotEqual(toyRobot.report(), "-1, 2, SOUTH")
 def test_report_overall_position(self):
     toy_robot_obj = ToyRobot()
     toy_robot_obj.set_placement_position(0,0,"NORTH")
     self.assertEqual((toy_robot_obj.move_forward())[0], True)
     toy_robot_obj.turn_position("RIGHT")
     toy_robot_obj.turn_position("LEFT")
     self.assertEqual((toy_robot_obj.move_forward())[0], True)
     toy_robot_obj.turn_position("RIGHT")
     self.assertEqual((toy_robot_obj.move_forward())[0], True)
     self.assertEqual((toy_robot_obj.move_forward())[0], True)
     toy_robot_obj.turn_position("RIGHT")
     self.assertEqual((toy_robot_obj.move_forward())[0], True)
     self.assertEqual((toy_robot_obj.move_forward())[0], True)
     self.assertEqual((toy_robot_obj.move_forward())[0], False)
     self.assertEqual(toy_robot_obj.report_overall_position(), "2,0,SOUTH")
示例#22
0
def execute_main():
    # install_all_packages(["readchar"])
    toy_robot_obj = ToyRobot()
    is_simulator_running = True
    print(
        "-------------------------------------------------------------------------------"
    )
    print(
        "-------------------------Toy Robot Simulator ( 5 X 5 )-------------------------"
    )
    print(
        "-------------------------------------------------------------------------------"
    )
    print(
        "=========================Available Commands and Syntax========================="
    )
    print(
        "-------------------------------------------------------------------------------"
    )
    print(
        "(1) PLACE [x_position],[y_position],[face_direction] - To place it on the table"
    )
    print("*Parameters definitions:*")
    print("**[x_position] -> between 0 and 4")
    print("**[y_position] -> between 0 and 4")
    print("**[face_direction] -> NORTH,SOUTH,EAST,WEST")
    print("(2) MOVE - To move robot one step forward in its facing direction")
    print("(3) RIGHT - To turn robot 90 degrees to its right side")
    print("(4) LEFT - To turn robot 90 degrees to its left side")
    print("(5) REPORT - To print current positions and facing direction")
    print("(6) EXIT - To exit the simulator")
    print(
        "==============================================================================="
    )
    print("Type the input commands for the robot...")
    while is_simulator_running:  # Infinte loop - stops only when user types in EXIT Command
        given_command = input(
            "New Command: "
        )  # Getting input from the user, single command at one instance
        orig_command = given_command
        ansi_escape = re.compile(
            r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])'
        )  # Removing any escape sequence characters included in raw string
        given_command = ansi_escape.sub('', given_command)
        given_command = given_command.strip()
        given_command_split_up = given_command.split(
            " ")  # Splitting up to get the intended command
        invoked_command = (given_command_split_up[0]).upper()
        if invoked_command == "EXIT" and len(
                given_command_split_up) == 1:  # To check if its exit command
            is_simulator_running = False
            print("Thanks for playing toy robot simulator!!!")
        elif invoked_command == TOY_MAIN_COMMANDS[0] and check_space_present(
                given_command) and parse_place_command(
                    given_command
                ) is not None:  # To check if its PLACE command
            parsed_dict = parse_place_command(
                given_command)  # Parsing parameters
            is_placement_valid = toy_robot_obj.validate_place_command(
                parsed_dict["x_pos"], parsed_dict["y_pos"],
                parsed_dict["direction"]
            )  # Validating parameter values for PLACE command
            if is_placement_valid:  # if valid perform assignment for placing toy robot on the table
                toy_robot_obj.set_placement_position(parsed_dict["x_pos"],
                                                     parsed_dict["y_pos"],
                                                     parsed_dict["direction"])
            else:
                print(
                    "Warning: Place command not recognized. Please enter a valid Place command (Refer from list above)"
                )
        elif invoked_command == TOY_MAIN_COMMANDS[1] and len(
                given_command_split_up) == 1:  # To check if its MOVE command
            return_val, comment = toy_robot_obj.move_forward()
            if not return_val:
                print(comment)
        elif invoked_command == TOY_MAIN_COMMANDS[2] and len(
                given_command_split_up) == 1:  # To check if its REPORT command
            result = toy_robot_obj.report_overall_position()
            if result != "":
                print(result)
            else:
                print(
                    "Warning: Toy Robot is not placed on the table. Please use a valid Place command to place it on table."
                )
        elif invoked_command == TOY_MAIN_COMMANDS[3] and len(
                given_command_split_up) == 1:  # To check if its LEFT command
            return_val, comment = toy_robot_obj.turn_position(invoked_command)
            if not return_val:
                print(comment)
        elif invoked_command == TOY_MAIN_COMMANDS[4] and len(
                given_command_split_up) == 1:  # To check if its RIGHT command
            return_val, comment = toy_robot_obj.turn_position(invoked_command)
            if not return_val:
                print(comment)
        else:
            if 'x' in repr(orig_command):
                print(
                    "Warning: Command not recognized. Please enter a valid command (Refer from list above)"
                )
            else:
                print(
                    "Warning: Command not recognized. Please enter a valid command (Refer from list above)"
                )
示例#23
0
 def test_init_sets_correct_default_attributes(self):
     robot = ToyRobot()
     self.assertEqual(robot.x, 0)
     self.assertEqual(robot.y, 0)
     self.assertEqual(robot.direction, "NORTH")
示例#24
0
	def test__get_orientation(self):
		toyRobot = ToyRobot()
		self.assertEqual(toyRobot._get_orientation(None), None)
		self.assertEqual(toyRobot._get_orientation(Direction.NORTH), (Direction.WEST, Direction.EAST))
示例#25
0
 def test_move_prevents_robot_from_falling_south(self):
     robot = ToyRobot(2, 0, "SOUTH")
     robot.move()
     self.assertEqual(robot.x, 2)
     self.assertEqual(robot.y, 0)
     self.assertEqual(robot.direction, "SOUTH")
示例#26
0
	def test_move(self):
		toyRobot = ToyRobot()

		toyRobot.place(0, 0, Direction.NORTH)

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "1, 0, NORTH")

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "2, 0, NORTH")

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "3, 0, NORTH")

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "4, 0, NORTH")

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "4, 0, NORTH")  # validate not going over the edge

		toyRobot.place(0, 0, Direction.EAST)

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "0, 1, EAST")

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "0, 2, EAST")

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "0, 3, EAST")

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "0, 4, EAST")

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "0, 4, EAST")  # validate not going over the edge

		toyRobot.place(0, 0, Direction.WEST)

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "0, 0, WEST")  # validate not going over the edge

		toyRobot.place(0, 0, Direction.SOUTH)

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "0, 0, SOUTH")  # validate not going over the edge
示例#27
0
 def test_left_rotates_robot_90_degrees_to_the_left_when_facing_south(self):
     robot = ToyRobot(1, 1, "SOUTH")
     robot.left()
     self.assertEqual(robot.x, 1)
     self.assertEqual(robot.y, 1)
     self.assertEqual(robot.direction, "EAST")
示例#28
0
 def test_report(self):
     robot = ToyRobot(0, 4, "EAST")
     x, y, direction = robot.report()
     self.assertEqual(x, 0)
     self.assertEqual(y, 4)
     self.assertEqual(direction, "EAST")
示例#29
0
 def setUp(self):
     self.toy_robot = ToyRobot()
     self.toy_robot.set_position(0, 0, 'EAST')
示例#30
0
	def test_right(self):
		toyRobot = ToyRobot()

		toyRobot.right() # right must not work before place
		self.assertEqual(toyRobot.report(), None)

		toyRobot.place(0, 0, Direction.NORTH)

		toyRobot.right()
		self.assertEqual(toyRobot.report(), "0, 0, EAST")

		toyRobot.right()
		self.assertEqual(toyRobot.report(), "0, 0, SOUTH")

		toyRobot.right()
		self.assertEqual(toyRobot.report(), "0, 0, WEST")

		toyRobot.right()
		self.assertEqual(toyRobot.report(), "0, 0, NORTH")