コード例 #1
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))
コード例 #2
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")
コード例 #3
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")
コード例 #4
0
 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)
コード例 #5
0
 def test_move_forward_command(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)
     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.move_forward())[0], False)
コード例 #6
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()
コード例 #7
0
 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")
コード例 #8
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")
コード例 #9
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
コード例 #10
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")
コード例 #11
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")
コード例 #12
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")
コード例 #13
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")
コード例 #14
0
 def test_place_with_invalid_direction_raises_error(self):
     robot = ToyRobot()
     with self.assertRaises(InputError):
         robot.place(0, 3, "DOWN")
コード例 #15
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")
コード例 #16
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")
コード例 #17
0
 def setUp(self):
     self.toy_robot = ToyRobot()
コード例 #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
ファイル: app.py プロジェクト: vijayrohin/toy_robot_simulator
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)"
                )
コード例 #20
0
	def test__is_placed(self):
		toyRobot = ToyRobot()
		self.assertFalse(toyRobot._is_placed)

		toyRobot.place(0, 0, Direction.NORTH)
		self.assertTrue(toyRobot._is_placed)
コード例 #21
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")
コード例 #22
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")
コード例 #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 setUp(self):
     self.toy_robot = ToyRobot()
     self.toy_robot.set_position(0, 0, 'EAST')
コード例 #25
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")
コード例 #26
0
 def test_place_with_invalid_coordinates_raises_error(self):
     robot = ToyRobot()
     with self.assertRaises(InputError):
         robot.place(6, 3, "WEST")
コード例 #27
0
 def __init__(self):
     """
     Pick a ToyRobot object and CommandType (which provide classification of command)
     """
     self.robot = ToyRobot()
     self.cmd_type = CommandType()
コード例 #28
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))