def test_step6_replay_range_invalid(self): with captured_io( StringIO( 'HAL\nforward 3\nforward 2\nforward 1\nreplay 3--a\noff\n') ) as (out, err): robot.robot_start() output = out.getvalue().strip() self.assertEqual( """What do you want to name your robot? HAL: Hello kiddo! HAL: What must I do next? > HAL moved forward by 3 steps. > HAL now at position (0,3). HAL: What must I do next? > HAL moved forward by 2 steps. > HAL now at position (0,5). HAL: What must I do next? > HAL moved forward by 1 steps. > HAL now at position (0,6). HAL: What must I do next? HAL: Sorry, I did not understand 'replay 3--a'. HAL: What must I do next? HAL: Shutting down..""", output)
def test_step2_replay_basic(self): with captured_io(StringIO('HAL\nforward 10\nforward 5\nreplay\noff\n')) as (out, err): robot.robot_start() self.maxDiff = None output = out.getvalue().strip() self.assertEqual("""What do you want to name your robot? HAL: Hello kiddo! HAL: What must I do next? > HAL moved forward by 10 steps. > HAL now at position (0,10). HAL: What must I do next? > HAL moved forward by 5 steps. > HAL now at position (0,15). HAL: What must I do next? > HAL moved forward by 10 steps. > HAL now at position (0,25). > HAL moved forward by 5 steps. > HAL now at position (0,30). > HAL replayed 2 commands. > HAL now at position (0,30). HAL: What must I do next? HAL: Shutting down..""", output)
def test_step3_replay_silent_invalid(self): with captured_io( StringIO( 'HAL\nforward 10\nforward 5\nREPLAY SILENT abd\nreplay silent\noff\n' )) as (out, err): robot.robot_start() output = out.getvalue().strip() self.assertEqual( """What do you want to name your robot? HAL: Hello kiddo! HAL: What must I do next? > HAL moved forward by 10 steps. > HAL now at position (0,10). HAL: What must I do next? > HAL moved forward by 5 steps. > HAL now at position (0,15). HAL: What must I do next? HAL: Sorry, I did not understand 'REPLAY SILENT abd'. HAL: What must I do next? > HAL replayed 2 commands silently. > HAL now at position (0,30). HAL: What must I do next? HAL: Shutting down..""", output)
def test_replay_reversed_silent_n(self): with captured_io( StringIO( 'hal\nforward 10\nright\nreplay reversed silent 2\noff') ) as (out, err): robot.robot_start() output = out.getvalue() # print(output) value = '''What do you want to name your robot? hal: Hello kiddo! hal: What must I do next? > hal moved forward by 10 steps. > hal now at position (0,10). hal: What must I do next? > hal turned right. > hal now at position (0,10). hal: What must I do next? > hal replayed 2 commands in reverse silently. > hal now at position (0,0). hal: What must I do next? hal: Shutting down.. ''' self.maxDiff == None self.assertEqual(output, value)
def test_replay_invalid_two_ranges(self): """tests that the robot replays within the given range values correctly""" with patch('sys.stdout', new=StringIO()) as fake_out: robot.robot_start() output = fake_out.getvalue().strip() self.assertEqual( """What do you want to name your robot? BMO: Hello kiddo! BMO: What must I do next? > BMO moved forward by 1 steps. > BMO now at position (0,1). BMO: What must I do next? > BMO moved forward by 2 steps. > BMO now at position (0,3). BMO: What must I do next? > BMO moved forward by 3 steps. > BMO now at position (0,6). BMO: What must I do next? > BMO moved forward by 4 steps. > BMO now at position (0,10). BMO: What must I do next? > BMO moved forward by 5 steps. > BMO now at position (0,15). BMO: What must I do next? BMO: Sorry, I did not understand 'replay 2-a'. BMO: What must I do next? BMO: Shutting down..""", output)
def test_replay_range_with_multiple_arguments(self): sys.stdout = StringIO() robot.robot_start() self.assertEqual( sys.stdout.getvalue(), '''What do you want to name your robot? HAL: Hello kiddo! HAL: Loaded obstacles. HAL: What must I do next? > HAL moved forward by 10 steps. > HAL now at position (0,10). HAL: What must I do next? > HAL turned right. > HAL now at position (0,10). HAL: What must I do next? > HAL moved forward by 10 steps. > HAL now at position (10,10). HAL: What must I do next? > HAL turned right. > HAL now at position (10,10). > HAL replayed 1 commands. > HAL now at position (10,10). HAL: What must I do next? HAL: Shutting down.. ''')
def test_step8_right_then_fwd10_then_right_then_fwd5_off(self): with captured_io( StringIO('HAL\nright\nforward 10\nright\nforward 5\noff\n') ) as (out, err): robot.robot_start() output = out.getvalue().strip() self.assertEqual( """HAL: What must I do next? > HAL turned right. > HAL now at position (0,0). HAL: What must I do next? > HAL moved forward by 10 steps. > HAL now at position (10,0). HAL: What must I do next? > HAL turned right. > HAL now at position (10,0). HAL: What must I do next? > HAL moved forward by 5 steps. > HAL now at position (10,-5). HAL: What must I do next? HAL: Shutting down..""", output[-383:])
def test_replay_command_then_off(self): with patch("sys.stdout", StringIO()) as output: obstacles.random.randint = lambda a, b: 0 robot.robot_start() self.assertEqual( """What do you want to name your robot? fly: Hello kiddo!\nfly: 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 moved forward by 10 steps. > fly now at position (0,25). > fly moved forward by 5 steps. > fly now at position (0,30). > fly replayed 2 commands. > fly now at position (0,30). fly: What must I do next? fly: Shutting down..""", output.getvalue().strip())
def test_replay_reversed(self): """tests that the robot replays in reverse correctly""" with patch('sys.stdout', new=StringIO()) as fake_out: robot.robot_start() self.assertEqual( fake_out.getvalue(), """What do you want to name your robot? BMO: Hello kiddo! BMO: What must I do next? > BMO moved forward by 10 steps. > BMO now at position (0,10). BMO: What must I do next? > BMO moved back by 5 steps. > BMO now at position (0,5). BMO: What must I do next? > BMO moved back by 5 steps. > BMO now at position (0,0). > BMO moved forward by 10 steps. > BMO now at position (0,10). > BMO replayed 2 commands in reverse. > BMO now at position (0,10). BMO: What must I do next? BMO: Shutting down.. """)
def test_left_left_left_forward_10_off(self): with captured_io( StringIO('hal\nleft\nleft\nleft\nforward 10\noff')) as (out, err): robot.robot_start() output = out.getvalue() # print(output) value = '''What do you want to name your robot? hal: Hello kiddo! hal: What must I do next? > hal turned left. > hal now at position (0,0). hal: What must I do next? > hal turned left. > hal now at position (0,0). hal: What must I do next? > hal turned left. > hal now at position (0,0). hal: What must I do next? > hal moved forward by 10 steps. > hal now at position (10,0). hal: What must I do next? hal: Shutting down.. ''' self.assertEqual(output, value)
def test_step6_replay_range_basic_reversed(self): with captured_io(StringIO('HAL\nforward 3\nforward 2\nforward 1\nreplay 2 reversed\noff\n')) as (out, err): robot.robot_start() output = out.getvalue().strip() self.assertEqual("""What do you want to name your robot? HAL: Hello kiddo! HAL: What must I do next? > HAL moved forward by 3 steps. > HAL now at position (0,3). HAL: What must I do next? > HAL moved forward by 2 steps. > HAL now at position (0,5). HAL: What must I do next? > HAL moved forward by 1 steps. > HAL now at position (0,6). HAL: What must I do next? > HAL moved forward by 2 steps. > HAL now at position (0,8). > HAL moved forward by 3 steps. > HAL now at position (0,11). > HAL replayed 2 commands in reverse. > HAL now at position (0,11). HAL: What must I do next? HAL: Shutting down..""", output)
def test_reverse_silent(self): with captured_io( StringIO( 'HAL\nforward 10\nforward 5\nreplay reversed silent\noff\n' )) as (out, err): robot.robot_start() output = out.getvalue().strip() self.assertEquals( output, """What do you want to name your robot? HAL: Hello kiddo! HAL: What must I do next? > HAL moved forward by 10 steps. > HAL now at position (0,10). HAL: What must I do next? > HAL moved forward by 5 steps. > HAL now at position (0,15). HAL: What must I do next? > HAL replayed 2 commands in reverse silently. > HAL now at position (0,30). HAL: What must I do next? HAL: Shutting down..""")
def test_replay_reversed2(self): with patch("sys.stdout", StringIO()) as out: robot.robot_start() output = out.getvalue().strip() self.assertEqual( """What do you want to name your robot? HAL: Hello kiddo! HAL: What must I do next? > HAL moved forward by 10 steps. > HAL now at position (0,10). HAL: What must I do next? > HAL turned right. > HAL now at position (0,10). HAL: What must I do next? > HAL moved forward by 5 steps. > HAL now at position (5,10). HAL: What must I do next? > HAL turned right. > HAL now at position (5,10). > HAL moved forward by 10 steps. > HAL now at position (5,0). > HAL replayed 2 commands in reverse. > HAL now at position (5,0). HAL: What must I do next? HAL: Shutting down..""", output)
def test_replay_reversed_n_m(self): with captured_io( StringIO('hal\nforward 10\nright\nreplay reversed 2-1\noff') ) as (out, err): obstacles.random.randint = lambda a, b: 0 robot.robot_start() output = out.getvalue() value = '''What do you want to name your robot? hal: Hello kiddo! hal: What must I do next? > hal moved forward by 10 steps. > hal now at position (0,10). hal: What must I do next? > hal turned right. > hal now at position (0,10). hal: What must I do next? > hal turned right. > hal now at position (0,10). > hal replayed 1 commands in reverse. > hal now at position (0,10). hal: What must I do next? hal: Shutting down.. ''' self.maxDiff == None self.assertEqual(output, value)
def test_left_forward_back(self): output = StringIO() sys.stdout = output robot.robot_start() self.assertEqual( output.getvalue(), """What do you want to name your robot? Jay: Hello kiddo! Jay: What must I do next? > Jay moved forward by 10 steps. > Jay now at position (0,10). Jay: What must I do next? > Jay turned left. > Jay now at position (0,10). Jay: What must I do next? > Jay moved back by 10 steps. > Jay now at position (10,10). Jay: What must I do next? > Jay turned left. > Jay now at position (10,10). Jay: What must I do next? > Jay moved forward by 10 steps. > Jay now at position (10,0). Jay: What must I do next? Jay: Shutting down..\n""")
def test_step9_left_then_fwd10_then_left_then_fwd5_off(self): with captured_io( StringIO('HAL\nleft\nforward 10\nleft\nforward 5\noff\n')) as ( out, err): robot.robot_start() output = out.getvalue().strip() self.assertEqual( """What do you want to name your robot? HAL: Hello kiddo! HAL: What must I do next? > HAL turned left. > HAL now at position (0,0). HAL: What must I do next? > HAL moved forward by 10 steps. > HAL now at position (-10,0). HAL: What must I do next? > HAL turned left. > HAL now at position (-10,0). HAL: What must I do next? > HAL moved forward by 5 steps. > HAL now at position (-10,-5). HAL: What must I do next? HAL: Shutting down..""", output)
def test_do_replay_3(self): with patch("sys.stdout", StringIO()) as out: robot.robot_start() output = out.getvalue().strip() self.assertEqual( """What do you want to name your robot? HAL: Hello kiddo! HAL: What must I do next? > HAL moved forward by 1 steps. > HAL now at position (0,1). HAL: What must I do next? > HAL moved forward by 2 steps. > HAL now at position (0,3). HAL: What must I do next? > HAL moved forward by 3 steps. > HAL now at position (0,6). HAL: What must I do next? > HAL moved forward by 2 steps. > HAL now at position (0,8). > HAL moved forward by 3 steps. > HAL now at position (0,11). > HAL replayed 2 commands. > HAL now at position (0,11). HAL: What must I do next? HAL: Shutting down..""", output)
def test_help(self): with patch("sys.stdout", StringIO()) as out: robot.robot_start() output = out.getvalue().strip() self.assertEqual( """What do you want to name your robot? HAL: Hello kiddo! HAL: What must I do next? I can understand these commands: OFF - Shut down robot HELP - provide information about commands FORWARD - move forward by specified number of steps, e.g. 'FORWARD 10' BACK - move backward by specified number of steps, e.g. 'BACK 10' RIGHT - turn right by 90 degrees LEFT - turn left by 90 degrees SPRINT - sprint forward according to a formula REPLAY - replays all movement commands from history [FORWARD, BACK, RIGHT, LEFT, SPRINT] > HAL now at position (0,0). HAL: What must I do next? HAL: Shutting down..""", output)
def test_mazerun_bottom(self): """tests that the mazerun function works correcty with the mazerun bottom command""" obstacles.random.randint = lambda a, b: 1 with patch('sys.stdout', new=StringIO()) as fake_out: robot.robot_start() self.assertEqual( fake_out.getvalue(), """What do you want to name your robot? BMO: Hello kiddo! BMO: Loaded obstacles. There are some obstacles: - At position 1,1 (to 5,5) BMO: What must I do next? > BMO starting maze run.. > BMO turned right. > BMO now at position (0,0). > BMO turned right. > BMO now at position (0,0). > BMO moved forward by 200 steps. > BMO now at position (0,-200). BMO: I am at the bottom edge. BMO: What must I do next? BMO: Shutting down..\n""")
def test_replay_range_reversed(self): """tests that the robot replays within the reversed range correctly""" with patch('sys.stdout', new=StringIO()) as fake_out: robot.robot_start() output = fake_out.getvalue().strip() self.assertEqual( """What do you want to name your robot? BMO: Hello kiddo! BMO: What must I do next? > BMO moved forward by 1 steps. > BMO now at position (0,1). BMO: What must I do next? > BMO moved forward by 2 steps. > BMO now at position (0,3). BMO: What must I do next? > BMO moved forward by 3 steps. > BMO now at position (0,6). BMO: What must I do next? > BMO moved forward by 2 steps. > BMO now at position (0,8). > BMO moved forward by 1 steps. > BMO now at position (0,9). > BMO replayed 2 commands in reverse. > BMO now at position (0,9). BMO: What must I do next? BMO: Shutting down..""", output)
def test_help_off(self): self.maxDiff == None with captured_io(StringIO('hal\nhelp\noff')) as (out, err): robot.robot_start() output = out.getvalue() value = '''What do you want to name your robot? hal: Hello kiddo! hal: What must I do next? I can understand these commands: OFF - Shut down robot HELP - provide information about commands FORWARD - move forward by specified number of steps, e.g. 'FORWARD 10' BACK - move backward by specified number of steps, e.g. 'BACK 10' RIGHT - turn right by 90 degrees LEFT - turn left by 90 degrees SPRINT - sprint forward according to a formula REPLAY - replays all movement commands from history [FORWARD, BACK, RIGHT, LEFT, SPRINT] > hal now at position (0,0). hal: What must I do next? hal: Shutting down.. ''' self.assertEqual(output, value)
def test_step6_replay_range_basic_silent(self): with captured_io( StringIO( 'HAL\nforward 3\nforward 2\nforward 1\nreplay 2 silent\noff\n' )) as (out, err): obstacles.random.randint = lambda a, b: 0 robot.robot_start() output = out.getvalue().strip() self.assertEqual( """What do you want to name your robot? HAL: Hello kiddo! HAL: What must I do next? > HAL moved forward by 3 steps. > HAL now at position (0,3). HAL: What must I do next? > HAL moved forward by 2 steps. > HAL now at position (0,5). HAL: What must I do next? > HAL moved forward by 1 steps. > HAL now at position (0,6). HAL: What must I do next? > HAL replayed 2 commands silently. > HAL now at position (0,9). HAL: What must I do next? HAL: Shutting down..""", output)
def test_step9_left_then_fwd10_then_left_then_fwd5_off(self): with captured_io( StringIO('HAL\nleft\nforward 10\nleft\nforward 5\noff\n')) as ( out, err): obstacles.random.randint = lambda a, b: 0 robot.robot_start() output = out.getvalue().strip() self.assertEqual( """HAL: What must I do next? > HAL turned left. > HAL now at position (0,0). HAL: What must I do next? > HAL moved forward by 10 steps. > HAL now at position (-10,0). HAL: What must I do next? > HAL turned left. > HAL now at position (-10,0). HAL: What must I do next? > HAL moved forward by 5 steps. > HAL now at position (-10,-5). HAL: What must I do next? HAL: Shutting down..""", output[-384:])
def test_help(self): self.maxDiff = None output = StringIO() sys.stdout = output robot.robot_start() self.assertEqual( """What do you want to name your robot? HAL: Hello kiddo! HAL: What must I do next? I can understand these commands: OFF - Shut down robot HELP - provide information about commands FORWARD - move forward by specified number of steps, e.g. 'FORWARD 10' BACK - move backward by specified number of steps, e.g. 'BACK 10' RIGHT - turn right by 90 degrees LEFT - turn left by 90 degrees SPRINT - sprint forward according to a formula REPLAY - replays all commands entered REPLAY SILENT - replays all commands entered silently REPLAY REVERSED - replays all commands entered in reverse REPLAY REVERSED SILENT - replays all commands entered in reverse silently HAL: What must I do next? HAL: Shutting down..\n""", output.getvalue())
def test_replay_n(self): with captured_io( StringIO('hal\nforward 10\nforward 5\nreplay 2\noff')) as ( out, err): robot.robot_start() output = out.getvalue() # print(output) value = '''What do you want to name your robot? hal: Hello kiddo! hal: What must I do next? > hal moved forward by 10 steps. > hal now at position (0,10). hal: What must I do next? > hal moved forward by 5 steps. > hal now at position (0,15). hal: What must I do next? > hal moved forward by 10 steps. > hal now at position (0,25). > hal moved forward by 5 steps. > hal now at position (0,30). > hal replayed 2 commands. > hal now at position (0,30). hal: What must I do next? hal: Shutting down.. ''' self.assertEqual(output, value)
def test_replay_reversed_range(self): sys.stdout = StringIO() robot.robot_start() self.assertEqual( sys.stdout.getvalue(), '''What do you want to name your robot? HAL: Hello kiddo! HAL: Loaded obstacles. HAL: What must I do next? > HAL moved forward by 5 steps. > HAL now at position (0,5). HAL: What must I do next? > HAL turned left. > HAL now at position (0,5). HAL: What must I do next? > HAL moved forward by 5 steps. > HAL now at position (-5,5). HAL: What must I do next? > HAL turned left. > HAL now at position (-5,5). > HAL moved forward by 5 steps. > HAL now at position (-5,0). > HAL replayed 2 commands in reverse. > HAL now at position (-5,0). HAL: What must I do next? HAL: Shutting down.. ''')
def test_4(self): f = StringIO() with redirect_stdout(f): world.random.randint = lambda a, b: (1 if b == 10 else 10) robot.robot_start() output = f.getvalue() self.assertEqual( """What do you want to name your robot? HAL: Hello kiddo! There are some obstacles: - At position 10,10 (to 14,14) HAL: What must I do next? > HAL moved forward by 11 steps. > HAL now at position (0,11). HAL: What must I do next? > HAL turned right. > HAL now at position (0,11). HAL: What must I do next? Sorry, there is an obstacle in the way. > HAL now at position (0,11). HAL: What must I do next? HAL: Shutting down.. """, output) world.clean_global() world.clean_globals()
def test_step4_replay_reversed_upper(self): with captured_io( StringIO('HAL\nforward 10\nforward 5\nreplay REVERSED\noff\n') ) as (out, err): obstacles.random.randint = lambda a, b: 0 robot.robot_start() output = out.getvalue().strip() self.assertEqual( """What do you want to name your robot? HAL: Hello kiddo! HAL: What must I do next? > HAL moved forward by 10 steps. > HAL now at position (0,10). HAL: What must I do next? > HAL moved forward by 5 steps. > HAL now at position (0,15). HAL: What must I do next? > HAL moved forward by 5 steps. > HAL now at position (0,20). > HAL moved forward by 10 steps. > HAL now at position (0,30). > HAL replayed 2 commands in reverse. > HAL now at position (0,30). HAL: What must I do next? HAL: Shutting down..""", output)
def test_replay_movement(self): """tests that the robot replays correctly""" with patch('sys.stdout', new=StringIO()) as fake_out: obstacles.random.randint = lambda a, b: 0 robot.robot_start() self.assertEqual( fake_out.getvalue(), """What do you want to name your robot? BMO: Hello kiddo! BMO: Loaded obstacles. BMO: What must I do next? > BMO moved forward by 10 steps. > BMO now at position (0,10). BMO: What must I do next? > BMO moved back by 5 steps. > BMO now at position (0,5). BMO: What must I do next? > BMO moved forward by 10 steps. > BMO now at position (0,15). > BMO moved back by 5 steps. > BMO now at position (0,10). > BMO replayed 2 commands. > BMO now at position (0,10). BMO: What must I do next? BMO: Shutting down.. """)
def test_11_robot_track_position_test(self): with output_capture(StringIO('HAL\nforward 10\nright\nforward 10\nright\nforward 10\nright\nforward 10\noff\n')) as (out,err): robot.robot_start() result = out.getvalue().strip() expected = """What do you want to name your robot? HAL: Hello kiddo! HAL: What must I do next? > HAL moved forward by 10 steps. > HAL now at position (0,10). HAL: What must I do next? > HAL turned right. > HAL now at position (0,10). HAL: What must I do next? > HAL moved forward by 10 steps. > HAL now at position (10,10). HAL: What must I do next? > HAL turned right. > HAL now at position (10,10). HAL: What must I do next? > HAL moved forward by 10 steps. > HAL now at position (10,0). HAL: What must I do next? > HAL turned right. > HAL now at position (10,0). HAL: What must I do next? > HAL moved forward by 10 steps. > HAL now at position (0,0). HAL: What must I do next? HAL: Shutting down..""" self.assertEqual(result,expected)