class TestGoal(unittest.TestCase): """This test class unit tests the Goal class.""" def setUp(self): """Create a simple Goal for testing.""" self.test_goal = Goal(["A", "B"], Point(0, 0), 0) def test_complete(self): """Test that the complete method behaves as expected.""" # Confirm the Goal is currently incomplete. self.assertFalse(self.test_goal.is_complete) # Confirm the current value of the Goal's sprite. self.assertEqual(self.test_goal.sprite, "A") # Call the Goal's complete method. self.test_goal.complete() # Confirm the Goal is now complete. self.assertTrue(self.test_goal.is_complete) # Confirm the new value of the Goal's sprite. self.assertEqual(self.test_goal.sprite, "B") @classmethod def test_init(cls): """Test the init method of the Goal class.""" _unused_goal = Goal([None], Point(0, 0), 0) def test_is_equal_to(self): """Test the is_equal_to_method.""" # Create a test sprite list. goal_sprite_list = [pygame.image.load('img/Default/goal1.jpg')] goal = Goal(goal_sprite_list, Point(1, 1), 150) # Create a Goal equal to goal goal_copy = Goal(goal_sprite_list, Point(1, 1), 150) # Create a Goal in a different logical place goal_diff_logical_position = Goal(goal_sprite_list, Point(2, 2), 150) # Create a Goal that has been completed. completed_goal = Goal(goal_sprite_list, Point(1, 1), 150) completed_goal.complete() # Create a Goal with a different sprite list. obstacle_sprite_list = [pygame.image.load('img/Default/obstacle1.jpg')] goal_diff_sprite = Goal(obstacle_sprite_list, Point(1, 1), 150) # Create an Obstacle, with the same sprite to try and break the test obstacle = Obstacle(goal_sprite_list, Point(1, 1), 150) # Check that equal goals are infact eqaul self.assertTrue(goal.is_equal_to(goal_copy)) # Check that a goal in a different logical place is not equal self.assertFalse(goal.is_equal_to(goal_diff_logical_position)) # Check an incomplete Goal is not equal to a completed Goal self.assertFalse(goal.is_equal_to(completed_goal)) # Check Goals with different sprites are not equal self.assertFalse(goal.is_equal_to(goal_diff_sprite)) # Check a Goal is not equal to an Obstacle self.assertFalse(goal.is_equal_to(obstacle))
def test_loaded_scenario(self): with open('scenarios/Test.scibot', 'rb') as test_scenario: loaded_scenario = pickle.load(test_scenario) # Assert all the values are as we would expect self.assertEqual(loaded_scenario.get_board_step(), 150) self.assertEqual(loaded_scenario.get_logical_width(), 5) self.assertEqual(loaded_scenario.get_logical_height(), 8) self.assertEqual(loaded_scenario.get_beebot_start_position(), (3, 1)) # To compare the loaded Sprite with the Sprite from the scenario, # we have to access _elements directly here we cant compare surfaces # for equality and the get method would turn the saved # picklable image as a surface saved_sprite_surface = loaded_scenario._elements['BeeBotSprite'] self._compare_image_to_pickle('img/Default/robot.jpg', saved_sprite_surface) self.assertEqual(loaded_scenario.get_beebot_heading(), Heading.NORTH) saved_sprite_surface = loaded_scenario._elements['Background'] self._compare_image_to_pickle('img/Default/background.jpg', saved_sprite_surface) self.assertEqual(loaded_scenario.get_border_colour(), (0, 0, 0)) # To compare ObstacleGroups we must craft one first obstacle_group = ObstacleGroup() obstacle_1 = Obstacle(None, Point(2, 1), 150) obstacle_2 = Obstacle(None, Point(2, 3), 150) obstacle_group.add(obstacle_1) obstacle_group.add(obstacle_2) self.assertTrue( loaded_scenario.get_obstacle_group().is_equal_to(obstacle_group)) # To compare GoalGroups we must craft one first goal_group = GoalGroup() goal_1 = Goal(None, Point(1, 2), 150) goal_2 = Goal(None, Point(2, 0), 150) goal_group.add(goal_1) goal_group.add(goal_2) goal_group.is_ordered = False # Not sure how to test this yet self.assertTrue( loaded_scenario.get_goal_group().is_equal_to(goal_group)) self.assertEqual(loaded_scenario.get_ordered_goals(), False) saved_sprite_surface = loaded_scenario._elements['BeeBotFailSprite'] self._compare_image_to_pickle('img/Default/robotx.jpg', saved_sprite_surface) self.assertEqual(loaded_scenario.get_license(), 'Test License')
def test_is_equal_to(self): """Test the is_equal_to_method.""" # Create a test sprite obstacle_sprite_list = [pygame.image.load('img/Default/obstacle1.jpg')] goal_sprite_list = [pygame.image.load('img/Default/goal1.jpg')] obstacle = Obstacle(obstacle_sprite_list, Point(1, 1), 150) # Create a Obstacle equal to obstacle obstacle_copy = Obstacle(obstacle_sprite_list, Point(1, 1), 150) # Create a Obstacle in a different logical place obstacle_diff_logical_position = Obstacle(obstacle_sprite_list, Point(2, 2), 150) # Create a Obstacle with a different sprite obstacle_diff_sprite = Obstacle(goal_sprite_list, Point(1, 1), 150) # Create an Goal, with the same sprite to try and break the test goal = Goal(obstacle_sprite_list, Point(1, 1), 150) # Check that equal Obstacles are infact eqaul self.assertTrue(obstacle.is_equal_to(obstacle_copy)) # Check that a Obstacles in a different logical place is not equal self.assertFalse(obstacle.is_equal_to(obstacle_diff_logical_position)) # Check an Obstacle with a different Sprite is not equal self.assertFalse(obstacle.is_equal_to(obstacle_diff_sprite)) # Check a Obstacle is not equal to an Goal self.assertFalse(obstacle.is_equal_to(goal))
def setUp(self): """Create GoalGroups and Goals used in testing.""" # Create a test sprite list. self.sprite_list = [pygame.image.load('img/Default/goal1.jpg')] # Create Goals self.goal1 = Goal(self.sprite_list, Point(1, 1), 150) self.goal2 = Goal(self.sprite_list, Point(2, 2), 150) self.goal3 = Goal(self.sprite_list, Point(3, 2), 150) # Create a GoalGroup 'ordered' one way self.goal_group1 = GoalGroup() self.goal_group1.add(self.goal1) self.goal_group1.add(self.goal2) # Create a GoalGroup 'ordered' the other way self.goal_group2 = GoalGroup() self.goal_group2.add(self.goal2) self.goal_group2.add(self.goal1)
def get_goal_group(self): """Return the Scenario's GoalGroup.""" # no guarantee OrderGoals is defined, so using get_element is_group_ordered = self._get_element('OrderedGoals') # Create a temp, empty, GoalGroup if is_group_ordered is None: goal_group = GoalGroup() # use the class default elif is_group_ordered: goal_group = GoalGroup(True) else: goal_group = GoalGroup(False) # For each pickled goal, unpickle and add to the temp GoalGroup for pickled_goal in self._get_element('GoalGroup'): sprite_list = pickled_goal[0] position = Point(pickled_goal[1], pickled_goal[2]) # Only v1.2+ Scerarios provide the should_increment_beebot_sprite # variable, so handle the cases when it's not provided. try: should_increment_beebot_sprite = pickled_goal[3] except IndexError: should_increment_beebot_sprite = False # Handle the different use cases for reading Component sprites. sprite_list = self.listify(sprite_list) # Un-pickle the sprite list. sprite_list = self.format_pickle_list_to_surface(sprite_list) # Create an un-pickled Goal from the pickled data. goal = Goal( sprite_list, position, self._get_element('BoardStep'), should_increment_beebot_sprite, ) # Add unpickled goal to temp GoalGroup goal_group.add(goal) # Return temp GoalGroup return goal_group
def get_goal_group(self): """Return the Scenario's GoalGroup.""" # no guarantee OrderGoals is defined, so using get_element is_group_ordered = self._get_element('OrderedGoals') # Create a temp, empty, GoalGroup if is_group_ordered is None: goal_group = GoalGroup() # use the class default elif is_group_ordered: goal_group = GoalGroup(True) else: goal_group = GoalGroup(False) # For each pickled goal, unpickle and add to the temp GoalGroup for pickled_goal in self._get_element('GoalGroup'): goal_sprite = self.format_pickle_to_surface(pickled_goal[0]) goal = Goal(goal_sprite, Point(pickled_goal[1], pickled_goal[2]), self._get_element('BoardStep')) # Add unpickled goal to temp GoalGroup goal_group.add(goal) # Return temp GoalGroup return goal_group
def test_is_equal_to(self): """Test the is_equal_to_method.""" # Create a test sprite list. goal_sprite_list = [pygame.image.load('img/Default/goal1.jpg')] goal = Goal(goal_sprite_list, Point(1, 1), 150) # Create a Goal equal to goal goal_copy = Goal(goal_sprite_list, Point(1, 1), 150) # Create a Goal in a different logical place goal_diff_logical_position = Goal(goal_sprite_list, Point(2, 2), 150) # Create a Goal that has been completed. completed_goal = Goal(goal_sprite_list, Point(1, 1), 150) completed_goal.complete() # Create a Goal with a different sprite list. obstacle_sprite_list = [pygame.image.load('img/Default/obstacle1.jpg')] goal_diff_sprite = Goal(obstacle_sprite_list, Point(1, 1), 150) # Create an Obstacle, with the same sprite to try and break the test obstacle = Obstacle(goal_sprite_list, Point(1, 1), 150) # Check that equal goals are infact eqaul self.assertTrue(goal.is_equal_to(goal_copy)) # Check that a goal in a different logical place is not equal self.assertFalse(goal.is_equal_to(goal_diff_logical_position)) # Check an incomplete Goal is not equal to a completed Goal self.assertFalse(goal.is_equal_to(completed_goal)) # Check Goals with different sprites are not equal self.assertFalse(goal.is_equal_to(goal_diff_sprite)) # Check a Goal is not equal to an Obstacle self.assertFalse(goal.is_equal_to(obstacle))
def test_init(cls): """Test the init method of the Goal class.""" _unused_goal = Goal([None], Point(0, 0), 0)
def setUp(self): """Create a simple Goal for testing.""" self.test_goal = Goal(["A", "B"], Point(0, 0), 0)