def setUp(self): self.jen = a2.Rat(a2.RAT_1_CHAR, 1, 1) self.paul = a2.Rat(a2.RAT_2_CHAR, 1, 4) self.maze = a2.Maze([['#', '#', '#', '#', '#', '#', '#'], ['#', '.', '.', '.', '.', '.', '#'], ['#', '.', '#', '#', '#', '.', '#'], ['#', '.', '.', '@', '#', '.', '#'], ['#', '@', '#', '.', '@', '.', '#'], ['#', '#', '#', '#', '#', '#', '#']], self.jen, self.paul)
def test_init_example2(self): """Test if the value of the instance variable num_sprouts_left is calculated correctly.""" maze_1 = a2.Maze([['#', '#', '#', '#', '#', '#', '#'], ['#', '.', '.', '.', '.', '.', '#'], ['#', '.', '#', '#', '#', '.', '#'], ['#', '.', '.', '@', '#', '.', '#'], ['#', '@', '#', '.', '@', '.', '#'], ['#', '#', '#', '#', '#', '#', '#']], a2.Rat('J', 1, 1), a2.Rat('P', 1, 4)) self.assertEqual(3, maze_1.num_sprouts_left)
def test_is_wall_false(self): """Test if we can catch/calculate walls correctly at a give row and column of the maze.""" maze_1 = a2.Maze([['#', '#', '#', '#', '#', '#', '#'], ['#', '.', '.', '.', '.', '.', '#'], ['#', '.', '#', '#', '#', '.', '#'], ['#', '.', '.', '@', '#', '.', '#'], ['#', '@', '#', '.', '@', '.', '#'], ['#', '#', '#', '#', '#', '#', '#']], a2.Rat('J', 1, 1), a2.Rat('P', 1, 4)) self.assertEqual(False, maze_1.is_wall(1, 2))
def test_get_character_hallway(self): """Return the character in the maze at the given row and column, which in this case is # i.e. is a hallway.""" maze_1 = a2.Maze([['#', '#', '#', '#', '#', '#', '#'], ['#', '.', '.', '.', '.', '.', '#'], ['#', '.', '#', '#', '#', '.', '#'], ['#', '.', '.', '@', '#', '.', '#'], ['#', '@', '#', '.', '@', '.', '#'], ['#', '#', '#', '#', '#', '#', '#']], a2.Rat('J', 1, 1), a2.Rat('P', 1, 4)) self.assertEqual('.', maze_1.get_character(1, 2))
def test_get_character_sprout(self): """Return the character in the maze at the given row and column, which in this case is @ i.e. is a Brussels sprout.""" maze_1 = a2.Maze([['#', '#', '#', '#', '#', '#', '#'], ['#', '.', '.', '.', '.', '.', '#'], ['#', '.', '#', '#', '#', '.', '#'], ['#', '.', '.', '@', '#', '.', '#'], ['#', '@', '#', '.', '@', '.', '#'], ['#', '#', '#', '#', '#', '#', '#']], a2.Rat('J', 1, 1), a2.Rat('P', 1, 4)) self.assertEqual('@', maze_1.get_character(4, 1))
def test_move_nowall(self): """Moving the rat in the direction where there is not a wall but hall.""" down = 1 no_change = 0 rat_1 = a2.Rat('J', 1, 1) maze_1 = a2.Maze([['#', '#', '#', '#', '#', '#', '#'], ['#', '.', '.', '.', '.', '.', '#'], ['#', '.', '#', '#', '#', '.', '#'], ['#', '.', '.', '@', '#', '.', '#'], ['#', '@', '#', '.', '@', '.', '#'], ['#', '#', '#', '#', '#', '#', '#']], a2.Rat('J', 1, 1), a2.Rat('P', 1, 4)) self.assertEqual(True, maze_1.move(rat_1, down, no_change))
def test_move_wall(self): """Moving the rat in the direction where there is a wall.""" no_change = 0 up = -1 rat_1 = a2.Rat('J', 1, 1) maze_1 = a2.Maze([['#', '#', '#', '#', '#', '#', '#'], ['#', '.', '.', '.', '.', '.', '#'], ['#', '.', '#', '#', '#', '.', '#'], ['#', '.', '.', '@', '#', '.', '#'], ['#', '@', '#', '.', '@', '.', '#'], ['#', '#', '#', '#', '#', '#', '#']], a2.Rat('J', 1, 1), a2.Rat('P', 1, 4)) self.assertEqual(False, maze_1.move(rat_1, up, no_change))
def test_maze_is_wall_1(self): """ Test if the maze object identifies a wall value. """ maze_template = [['#', '#', '#', '#', '#', '#', '#'], ['#', '.', '.', '.', '.', '.', '#'], ['#', '.', '#', '#', '#', '.', '#'], ['#', '.', '.', '@', '#', '.', '#'], ['#', '@', '#', '.', '@', '.', '#'], ['#', '#', '#', '#', '#', '#', '#']] rat_J = a2.Rat(a2.RAT_1_CHAR, 1, 1) rat_P = a2.Rat(a2.RAT_2_CHAR, 1, 4) maze = a2.Maze(maze_template, rat_J, rat_P) self.assertEqual(maze.is_wall(0, 0), True)
def test_maze_is_wall_3(self): """ Test if the maze object identifies a that a sprout is not a wall. """ maze_template = [['#', '#', '#', '#', '#', '#', '#'], ['#', '.', '.', '.', '.', '.', '#'], ['#', '.', '#', '#', '#', '.', '#'], ['#', '.', '.', '@', '#', '.', '#'], ['#', '@', '#', '.', '@', '.', '#'], ['#', '#', '#', '#', '#', '#', '#']] rat_J = a2.Rat(a2.RAT_1_CHAR, 1, 1) rat_P = a2.Rat(a2.RAT_2_CHAR, 1, 4) maze = a2.Maze(maze_template, rat_J, rat_P) self.assertEqual(maze.is_wall(4, 1), False)
def test_maze_move_1(self): """ Test if the maze object identifies a wall and denys movement. """ maze_template = [['#', '#', '#', '#', '#', '#', '#'], ['#', '.', '.', '.', '.', '.', '#'], ['#', '.', '#', '#', '#', '.', '#'], ['#', '.', '.', '@', '#', '.', '#'], ['#', '@', '#', '.', '@', '.', '#'], ['#', '#', '#', '#', '#', '#', '#']] rat_J = a2.Rat(a2.RAT_1_CHAR, 1, 1) rat_P = a2.Rat(a2.RAT_2_CHAR, 1, 4) maze = a2.Maze(maze_template, rat_J, rat_P) self.assertEqual(maze.move(rat_J, a2.UP, a2.NO_CHANGE), False)
def test_move_leftsprouts(self): """The number of sprouts in the maze has decreased after the rat has eaten one of them.""" right = 1 no_change = 0 rat_1 = a2.Rat('J', 3, 2) maze_1 = a2.Maze([['#', '#', '#', '#', '#', '#', '#'], ['#', '.', '.', '.', '.', '.', '#'], ['#', '.', '#', '#', '#', '.', '#'], ['#', '.', '.', '@', '#', '.', '#'], ['#', '@', '#', '.', '@', '.', '#'], ['#', '#', '#', '#', '#', '#', '#']], a2.Rat('J', 3, 2), a2.Rat('P', 1, 4)) maze_1.move(rat_1, no_change, right) self.assertEqual(2, maze_1.num_sprouts_left)
def test_maze_template(self): """ Test if the maze object receives the correct maze template. """ maze_template = [['#', '#', '#', '#', '#', '#', '#'], ['#', '.', '.', '.', '.', '.', '#'], ['#', '.', '#', '#', '#', '.', '#'], ['#', '.', '.', '@', '#', '.', '#'], ['#', '@', '#', '.', '@', '.', '#'], ['#', '#', '#', '#', '#', '#', '#']] rat_J = a2.Rat(a2.RAT_1_CHAR, 1, 1) rat_P = a2.Rat(a2.RAT_2_CHAR, 1, 4) maze = a2.Maze(maze_template, rat_J, rat_P) self.assertEqual(maze.template, maze_template)
def test_maze_get_character_1(self): """ Test if the maze object returns a wall value. """ maze_template = [['#', '#', '#', '#', '#', '#', '#'], ['#', '.', '.', '.', '.', '.', '#'], ['#', '.', '#', '#', '#', '.', '#'], ['#', '.', '.', '@', '#', '.', '#'], ['#', '@', '#', '.', '@', '.', '#'], ['#', '#', '#', '#', '#', '#', '#']] rat_J = a2.Rat(a2.RAT_1_CHAR, 1, 1) rat_P = a2.Rat(a2.RAT_2_CHAR, 1, 4) maze = a2.Maze(maze_template, rat_J, rat_P) self.assertEqual(maze.get_character(0, 0), a2.WALL)
def test_maze_str(self): rat1 = a2.Rat('J', 1, 1) rat2 = a2.Rat('P', 1, 4) maze = a2.Maze([['#', '#', '#', '#', '#', '#', '#'], ['#', '.', '.', '.', '.', '.', '#']], rat1, rat2) maze_string = "#######\n#J..P.#\n" rat1_string = rat1.__str__() rat2_string = rat2.__str__() result = maze_string + rat1_string + "\n" + rat2_string self.assertEqual(maze.__str__(), result)
def test_init_example1(self): """Test if the constructor is initialized correctly and the rat(s) symbols(s) appear where they should.""" maze_1 = a2.Maze([['#', '#', '#', '#', '#', '#', '#'], ['#', '.', '.', '.', '.', '.', '#'], ['#', '.', '#', '#', '#', '.', '#'], ['#', '.', '.', '@', '#', '.', '#'], ['#', '@', '#', '.', '@', '.', '#'], ['#', '#', '#', '#', '#', '#', '#']], a2.Rat('J', 1, 1), a2.Rat('P', 1, 4)) self.assertEqual([['#', '#', '#', '#', '#', '#', '#'], ['#', 'J', '.', '.', 'P', '.', '#'], ['#', '.', '#', '#', '#', '.', '#'], ['#', '.', '.', '@', '#', '.', '#'], ['#', '@', '#', '.', '@', '.', '#'], ['#', '#', '#', '#', '#', '#', '#']], maze_1.maze)
def find_rats_replace_hallway(maze_list): """ (list of list of str) -> (Rat, Rat) tuple Return the two Rats in a list. Also modify maze_list so that the rat chars are replaced with HALL chars. """ for r in range(len(maze_list)): for c in range(len(maze_list[r])): if maze_list[r][c] == a2.RAT_1_CHAR: rat_1 = a2.Rat(a2.RAT_1_CHAR, r, c) maze_list[r][c] = a2.HALL elif maze_list[r][c] == a2.RAT_2_CHAR: rat_2 = a2.Rat(a2.RAT_2_CHAR, r, c) maze_list[r][c] = a2.HALL return (rat_1, rat_2)
def test_maze_str_1(self): """ Test if the maze object identifies a wall and denys movement. """ maze_template = [['#', '#', '#', '#', '#', '#', '#'], ['#', '.', '.', '.', '.', '.', '#'], ['#', '.', '#', '#', '#', '.', '#'], ['#', '.', '.', '@', '#', '.', '#'], ['#', '@', '#', '.', '@', '.', '#'], ['#', '#', '#', '#', '#', '#', '#']] rat_J = a2.Rat(a2.RAT_1_CHAR, 1, 1) rat_P = a2.Rat(a2.RAT_2_CHAR, 1, 4) maze = a2.Maze(maze_template, rat_J, rat_P) maze_str = "#######\n#J..P.#\n#.###.#\n#..@#.#\n#@#.@.#\n#######\nJ at (1, 1) ate 0 sprouts.\nP at (1, 4) ate 0 sprouts." self.assertEqual(str(maze), maze_str)
def test_maze_move_5(self): """ Test if the maze object does not remove a sprout when movement fails. """ maze_template = [['#', '#', '#', '#', '#', '#', '#'], ['#', '.', '.', '.', '.', '.', '#'], ['#', '.', '#', '#', '#', '.', '#'], ['#', '.', '.', '@', '#', '.', '#'], ['#', '@', '#', '.', '@', '.', '#'], ['#', '#', '#', '#', '#', '#', '#']] rat_J = a2.Rat(a2.RAT_1_CHAR, 1, 1) rat_P = a2.Rat(a2.RAT_2_CHAR, 1, 4) maze = a2.Maze(maze_template, rat_J, rat_P) old_sprout_count = maze.num_sprouts_left maze.move(rat_J, a2.DOWN, a2.RIGHT) self.assertEqual(maze.num_sprouts_left, old_sprout_count)
def test_rat_symbol(self): """ Test if the rat object receives the correct symbol. """ rat_name = a2.RAT_1_CHAR rat_row = 1 rat_col = 4 rat = a2.Rat(rat_name, rat_row, rat_col) self.assertEqual(rat.symbol, rat_name)
def test_rat_num_sprouts_eaten(self): """ Test if the rat object inits with zero sprouts eaten. """ rat_name = a2.RAT_1_CHAR rat_row = 1 rat_col = 4 rat = a2.Rat(rat_name, rat_row, rat_col) self.assertEqual(rat.num_sprouts_eaten, 0)
def test_rat_eat_sprout_1(self): """ Test if rat.num_sprout_eaten increses. """ rat_name = a2.RAT_1_CHAR rat_row = 1 rat_col = 4 rat = a2.Rat(rat_name, rat_row, rat_col) rat.eat_sprout() self.assertEqual(rat.num_sprouts_eaten, 1)
def test_move_makehall(self): """Moving the rat in the direction where there is a wall.""" right = 1 no_change = 0 up = -1 down = 1 rat_1 = a2.Rat('J', 3, 2) maze_1 = a2.Maze([['#', '#', '#', '#', '#', '#', '#'], ['#', '.', '.', '.', '.', '.', '#'], ['#', '.', '#', '#', '#', '.', '#'], ['#', '.', '.', '@', '#', '.', '#'], ['#', '@', '#', '.', '@', '.', '#'], ['#', '#', '#', '#', '#', '#', '#']], a2.Rat('J', 3, 2), a2.Rat('P', 1, 4)) maze_1.move(rat_1, no_change, right) maze_1.move(rat_1, down, no_change) self.assertEqual( '.', maze_1.get_character(rat_1.row + up, rat_1.col + no_change))
def test_maze_move_4(self): """ Test if the maze object removes a eaten sprout. """ maze_template = [['#', '#', '#', '#', '#', '#', '#'], ['#', '.', '.', '.', '.', '.', '#'], ['#', '.', '#', '#', '#', '.', '#'], ['#', '.', '.', '@', '#', '.', '#'], ['#', '@', '#', '.', '@', '.', '#'], ['#', '#', '#', '#', '#', '#', '#']] rat_J = a2.Rat(a2.RAT_1_CHAR, 1, 1) rat_P = a2.Rat(a2.RAT_2_CHAR, 1, 4) maze = a2.Maze(maze_template, rat_J, rat_P) old_sprout_count = maze.num_sprouts_left maze.move(rat_J, a2.DOWN, a2.NO_CHANGE) maze.move(rat_J, a2.DOWN, a2.NO_CHANGE) maze.move(rat_J, a2.DOWN, a2.NO_CHANGE) self.assertEqual(maze.num_sprouts_left, old_sprout_count - 1)
def test_rat_set_location_1(self): """ Test if the rat.row and rat.col do not change if set with current values. """ rat_name = a2.RAT_1_CHAR rat_row = 1 rat_col = 4 rat = a2.Rat(rat_name, rat_row, rat_col) rat.set_location(rat_row, rat_col) self.assertEqual((rat_row, rat_col), (rat.row, rat.col))
def test_rat_set_location_3(self): """ Test if the rat.col changes and rat.row don't. """ rat_name = a2.RAT_1_CHAR old_row = 1 old_col = 4 rat = a2.Rat(rat_name, old_row, old_col) new_col = 2 rat.set_location(old_row, new_col) self.assertEqual((old_row, new_col), (rat.row, rat.col))
def test_rat_eat_sprout_2(self): """ Test if rat.num_sprout_eaten maps to correct value. """ rat_name = a2.RAT_1_CHAR rat_row = 1 rat_col = 4 rat = a2.Rat(rat_name, rat_row, rat_col) for i in range(10): rat.eat_sprout() self.assertEqual(rat.num_sprouts_eaten, 10)
def test_rat_set_location_4(self): """ Test if the rat.col and rat.row changes at same time. """ rat_name = a2.RAT_1_CHAR old_row = 1 old_col = 4 rat = a2.Rat(rat_name, old_row, old_col) new_row = 3 new_col = 2 rat.set_location(new_row, new_col) self.assertEqual((new_row, new_col), (rat.row, rat.col))
def setUp(self): maze_str = [] rat_1 = None rat_2 = None maze = None with open(maze_path, 'r') as m: for line in m: maze_str.append([ch for ch in line.strip()]) for r in range(len(maze_str)): for c in range(len(maze_str[r])): if rat_1 == None and maze_str[r][c] == a2.RAT_1_CHAR: rat_1 = a2.Rat(a2.RAT_1_CHAR, r, c) maze_str[r][c] = a2.HALL elif rat_2 == None and maze_str[r][c] == a2.RAT_2_CHAR: rat_2 = a2.Rat(a2.RAT_2_CHAR, r, c) maze_str[r][c] = a2.HALL if rat_1 is not None and rat_2 is not None: maze = a2.Maze(maze_str, rat_1, rat_2) self.maze = maze self.log = logging.getLogger("TestMaze")
class TestMaze(unittest.TestCase): """ Test the class Maze. """ rat1 = a2.Rat('J', 1, 1) rat2 = a2.Rat('P', 1, 4) maze = a2.Maze([['#', '#', '#', '#', '#', '#', '#'], ['#', '.', '.', '.', '.', '.', '#'], ['#', '.', '#', '#', '#', '.', '#'], ['#', '.', '.', '@', '#', '.', '#'], ['#', '@', '#', '.', '@', '.', '#'], ['#', '#', '#', '#', '#', '#', '#']], rat1, rat2) def test_maze_init(self): """ Test the initialization of a maze. """ self.assertEqual(self.maze.num_sprouts_left, 3) self.assertEqual(self.maze.rat_1, self.rat1) self.assertEqual(self.maze.rat_2, self.rat2) def test_is_wall(self): """ Test the is_wall method by: - checking a wall - checking a sprout - checking a hall """ self.assertEqual(self.maze.is_wall(0, 0), True) # wall self.assertEqual(self.maze.is_wall(3, 4), True) # wall self.assertEqual(self.maze.is_wall(1, 1), False) # hall self.assertEqual(self.maze.is_wall(4, 1), False) # sprout def test_get_char(self): """ Test the get_character method by: - checking a location of a sprout - checking a location of a wall - checking a location of a hall - checking a location of a rat """ self.assertEqual(self.maze.get_character(0, 0), "#") # wall self.assertEqual(self.maze.get_character(3, 4), "#") # wall self.assertEqual(self.maze.get_character(1, 2), ".") # hall self.assertEqual(self.maze.get_character(4, 1), "@") # sprout self.assertEqual(self.maze.get_character(1, 1), "J") # rat1 self.assertEqual(self.maze.get_character(1, 4), "P") # rat2 def test_maze_move(self): """ Test the method move by: - moving a rat to a hall - moving a rat to a wall - moving a rat to a sprout """ # The left direction. LEFT = -1 # The right direction. RIGHT = 1 # No change in direction. NO_CHANGE = 0 # The up direction. UP = -1 # The down direction. DOWN = 1 # Do not move rat1. self.maze.move(self.rat1, NO_CHANGE, NO_CHANGE) self.assertEqual(self.rat1.row, 1) self.assertEqual(self.rat1.col, 1) # Move rat1 to a hall. self.maze.move(self.rat1, DOWN, NO_CHANGE) self.assertEqual(self.rat1.row, 2) self.assertEqual(self.rat1.col, 1) # Move rat1 to a wall. The rat should not move. result = self.maze.move(self.rat1, NO_CHANGE, DOWN) self.assertEqual(result, False) self.assertEqual(self.rat1.row, 2) self.assertEqual(self.rat1.col, 1) """ Move rat1 to a sprout: - make the location a HALL - decrease the num_sprouts_left by 1 - return True if and only if there wasn't a wall """ # number of sprouts eaten before a sprout was eaten self.assertEqual(self.rat1.num_sprouts_eaten, 0) # move rat to sprout self.maze.move(self.rat1, DOWN, NO_CHANGE) # rat is at (3, 1) - hall self.maze.move(self.rat1, NO_CHANGE, RIGHT) # rat is at (3, 2) - hall self.assertEqual(self.maze.get_character(3, 3), "@") # check if sprout is at (3, 3) self.maze.move(self.rat1, NO_CHANGE, RIGHT) # rat is at (3, 3) - sprout # check if rat is at (3, 3) self.assertEqual(self.rat1.row, 3) self.assertEqual(self.rat1.col, 3) # check if rat ate sprout self.assertEqual(self.rat1.num_sprouts_eaten, 1) # check if rats location is returned self.assertEqual(self.maze.get_character(3, 3), "J") # move rat and check if brussel sprout changed to hall self.maze.move(self.rat1, DOWN, NO_CHANGE) # rat is at (4, 3) - hall self.assertEqual(self.maze.get_character(3, 3), ".") # check if number of sprouts left is reduced by one self.assertEqual(self.maze.num_sprouts_left, 2) def test_maze_str(self): rat1 = a2.Rat('J', 1, 1) rat2 = a2.Rat('P', 1, 4) maze = a2.Maze([['#', '#', '#', '#', '#', '#', '#'], ['#', '.', '.', '.', '.', '.', '#']], rat1, rat2) maze_string = "#######\n#J..P.#\n" rat1_string = rat1.__str__() rat2_string = rat2.__str__() result = maze_string + rat1_string + "\n" + rat2_string self.assertEqual(maze.__str__(), result)
class TestRat(unittest.TestCase): """ Test class for class Rat. """ rat1 = a2.Rat("Tim", 1, 2) rat2 = a2.Rat("Claudia", 3, 4) def test_rat_init(self): """ Test the initialization of two rats. """ self.assertEqual(self.rat1.name, "Tim") self.assertEqual(self.rat2.name, "Claudia") self.assertEqual(self.rat1.row, 1) self.assertEqual(self.rat1.col, 2) self.assertEqual(self.rat2.row, 3) self.assertEqual(self.rat2.col, 4) def test_set_location(self): """ Test set location method by changing the location of two rats. """ r1 = 3 r2 = 5 self.rat1.set_location(r1, r1) self.rat2.set_location(r2, r2) self.assertEqual(self.rat1.row, r1) self.assertEqual(self.rat1.col, r1) self.assertEqual(self.rat2.row, r2) self.assertEqual(self.rat2.col, r2) def test_eat_sprout(self): """ Test the method eat sprout by: - check number of sprouts eaten after initialization - check number of sprouts eaten after calling method eat_sprouts. """ self.assertEqual(self.rat1.num_sprouts_eaten, 0) self.rat1.eat_sprout() self.assertEqual(self.rat1.num_sprouts_eaten, 1) def test_rat_str(self): """ Test the print function of rat. """ string = '{0} at ({1}, {2}) ate {3} sprouts.'.format(self.rat2.name, self.rat2.row, self.rat2.col, self.rat2.num_sprouts_eaten) self.assertEqual(self.rat2.__str__(), string)