def test_incorrect_command(self): with patch("sys.stdout", StringIO()) as output: robot.get_command_input("fly") self.assertEqual( """fly: What must I do next? fly: Sorry, I did not understand 'of'. fly: What must I do next? fly: Shutting down..""", output.getvalue().strip())
def test_back_command(self): with patch("sys.stdout", StringIO()) as output: robot.get_command_input("fly") self.assertEqual( """fly: What must I do next? > fly moved back by 10 steps. > fly now at position (0,-10).\nfly: What must I do next? > fly moved back by 10 steps. > fly now at position (0,-20).\nfly: What must I do next? fly: Shutting down..""", output.getvalue().strip())
def test_sprint_command(self): with patch("sys.stdout", StringIO()) as output: robot.get_command_input("s") self.assertEqual( """s: What must I do next? > s moved forward by 5 steps. > s moved forward by 4 steps. > s moved forward by 3 steps. > s moved forward by 2 steps. > s moved forward by 1 steps. > s now at position (0,15). s: What must I do next? s: Shutting down..""", output.getvalue().strip())
def test_repaly_silent_then_off(self): with patch("sys.stdout", StringIO()) as output: robot.get_command_input("fly") self.assertEqual( """fly: What must I do next? > fly moved forward by 10 steps. > fly now at position (0,10). fly: What must I do next? > fly moved forward by 5 steps. > fly now at position (0,15). fly: What must I do next? > fly replayed 2 commands silently. > fly now at position (0,30). fly: What must I do next? fly: Shutting down..""", output.getvalue().strip())
def test_help_command_correctness(self): with patch("sys.stdout", StringIO()) as output: robot.get_command_input("fly") self.assertEqual( """fly: What must I do next? I can understand these commands: OFF - Shut down robot\nHELP - provide information about commands FORWARD STEP - move the robot forward BACK STEP- move the robot back RIGHT - turn the robot to the right LEFT - move your robot to left SPRINT STEP - sprint the robot by selected steps fly: What must I do next? fly: Shutting down..""", output.getvalue().strip())
def test_replay_range_silently(self): with patch("sys.stdout", StringIO()) as output: robot.get_command_input('fly') self.assertEqual( """fly: What must I do next? > fly moved forward by 10 steps. > fly now at position (0,10). fly: What must I do next? > fly turned right. > fly now at position (0,10). fly: What must I do next? > fly moved forward by 10 steps. > fly now at position (10,10). fly: What must I do next? > fly turned right. > fly now at position (10,10). fly: What must I do next? > fly moved forward by 5 steps. > fly now at position (10,5). fly: What must I do next? > fly replayed 2 commands silently. > fly now at position (5,5). fly: What must I do next? fly: Shutting down..""", output.getvalue().strip())
def test_help_command_correctness(self): with patch("sys.stdout", StringIO()) as output: robot.get_command_input("fly") self.assertEqual( """fly: What must I do next? I can understand these commands: OFF - Shut down robot\nHELP - provide information about commands FORWARD STEP - move the robot forward BACK STEP- move the robot back RIGHT - turn the robot to the right LEFT - turn your robot to left SPRINT STEP - sprint the robot by selected steps REPLAY - replay the previous commands in history REPLAY SILENT - replay the previous commands in history in silence REPLAY REVERSED - replay the prvious commands in reverse REPLAY SIZE - replay the last size commands REPLAY N-M - replay the previous commands in range of n and m, n must be bigger than m! REPLAY SIZE SILENT - replay the last size commands silent fly: What must I do next? fly: Shutting down..""", output.getvalue().strip())
def test_command_list(self): name = 'BOB' command = robot.get_command_input(name) if command == 'OFF': self.assertEqual(command, 'OFF') elif command == 'HELP': self.assertEqual(command, 'HELP') elif command == 'FORWARD': self.assertEqual(command, 'FORWARD') elif command == 'BACK': self.assertEqual(command, 'BACK') elif command == 'RIGHT': self.assertEqual(command, 'RIGHT') elif command == 'LEFT': self.assertEqual(command, 'LEFT') elif command == 'SPRINT': self.assertEqual(command, 'SPRINT')
def test_off_command(self): with patch("sys.stdout", StringIO()) as output: robot.get_command_input("fly") self.assertEqual("fly: What must I do next? fly: Shutting down..", output.getvalue().strip()) with patch("sys.stdout", StringIO()) as output: robot.get_command_input("fly") self.assertEqual("fly: What must I do next? fly: Shutting down..", output.getvalue().strip()) with patch("sys.stdout", StringIO()) as output: robot.get_command_input("fly") self.assertEqual("fly: What must I do next? fly: Shutting down..", output.getvalue().strip())
def test_left_command(self): with patch(('sys.stdout'), new = StringIO()) as fake: robot.get_command_input('HAL' , 'RIGHT', 0 , 0 , "" , 0) self.assertEqual("HAL: What must I do next? > HAL turned right.\n > HAL now at position (0,0).\n" , fake.getvalue())
def test_OutOfBounds(self): with patch(('sys.stdout'), new = StringIO()) as fake: robot.get_command_input('HAL' , ['SPRINT',' FORWARD'], 0 , 0 , "" , 0) self.assertEqual("HAL: What must I do next? HAL: Sorry, I cannot go outside my safe zone.\n > HAL now at position (0,0).\n" , fake.getvalue())
def test_sprint_command(self): with patch(('sys.stdout'), new = StringIO()) as fake: robot.get_command_input('HAL' , 'SPRINT', 0 , 0 , "" , 0) self.assertEqual("HAL: What must I do next? > HAL moved forward by 4 steps.\n > HAL moved forward by 3 steps.\n > HAL moved forward by 2 steps.\n > HAL moved forward by 1 steps.\n > HAL now at position (0,10).\n" , fake.getvalue())
def test_invalid_command(self): with patch(('sys.stdout'), new = StringIO()) as fake: robot.get_command_input('HAL' , 'FLY', 0 , 0 , "" , 0) self.assertEqual("HAL: What must I do next? HAL: Sorry, I did not understand 'FLY'.\n" , fake.getvalue())
def test_back_command(self): with patch(('sys.stdout'), new = StringIO()) as fake: robot.get_command_input('HAL' , 'BACK', 0 , 0 , "" , 0) self.assertEqual("HAL: What must I do next? > HAL moved back by 4 steps.\n > HAL now at position (0,-4).\n" , fake.getvalue())
def test_help_command(self): with patch(('sys.stdout'), new = StringIO()) as fake: robot.get_command_input('HAL' , 'HELP', 0 ,0 , "" , 0) self.assertEqual("HAL: What must I do next? I can understand these commands:\nOFF - Shut down robot\nHELP - provide information about commands\nFORWARD - move forward\nBACK - move backwards\nRIGHT - move right\nLEFT - move left\nSPRINT - burst of speed\n", fake.getvalue())
def test_off_command(self): with patch(('sys.stdout'), new = StringIO()) as fake: robot.get_command_input('HAL' , 'OFF', 0 ,0 , "anything" , 0) self.assertEqual("HAL: What must I do next? HAL: Shutting down..\n", fake.getvalue())