class TestPathfinder(object): GRID_NODE_COUNT = Vector2(10, 10) GRID_RECT = Rect.from_size(Vector2(10, 10)) SAMPLE_ACTOR_SIZE = 1 @mock.patch('services.pathfinding.pathfinder.Pathfinder.PathSimplifier', autospec = False) def setUp(self, path_simplifier_mock): self.pathfinder = Pathfinder(PathSimplifier()) self._setup_pathfinder_config() def _setup_pathfinder_config(self): self._pathfinder_config = PathfinderConfig() self._pathfinder_config.starting_point = Vector2(0, 0) self._pathfinder_config.destination_point = Vector2(self.GRID_RECT.width, self.GRID_RECT.height) self._pathfinder_config.grid_rect = self.GRID_RECT self._pathfinder_config.grid_node_count = self.GRID_NODE_COUNT self._pathfinder_config.actor_size = self.SAMPLE_ACTOR_SIZE def test_given_no_obstacles_when_finding_path_then_simplified_path_is_found(self): node_path = self.pathfinder.find_path(self._pathfinder_config) assert_equals(node_path.node_count, 2) def test_given_an_obstacle_when_finding_path_then_simplified_path_is_found(self): self._pathfinder_config.add_obstacle(Circle(self.GRID_RECT.center, 2)) node_path = self.pathfinder.find_path(self._pathfinder_config) assert_equals(node_path.node_count, 4)
def setup(self, path_simplifier_mock): path_simplifier_mock_instance = path_simplifier_mock.return_value path_simplifier_mock_instance.simplify_path.side_effect = self.simplify_path_mock self._pathfinder = Pathfinder(path_simplifier_mock_instance) self._setup_pathfinder_config()
def setUp(self, path_simplifier_mock): self.pathfinder = Pathfinder(PathSimplifier()) self._setup_pathfinder_config()
class TestPathfinder(object): GRID_NODE_COUNT = Vector2(10, 10) GRID_RECT = Rect.from_size(Vector2(10, 10)) SAMPLE_ACTOR_SIZE = 1 @mock.patch("services.pathfinding.pathfinder.PathSimplifier.PathSimplifier", autospec=False) def setup(self, path_simplifier_mock): path_simplifier_mock_instance = path_simplifier_mock.return_value path_simplifier_mock_instance.simplify_path.side_effect = self.simplify_path_mock self._pathfinder = Pathfinder(path_simplifier_mock_instance) self._setup_pathfinder_config() def _setup_pathfinder_config(self): self._pathfinder_config = PathfinderConfig() self._pathfinder_config.starting_point = Vector2(0, 0) self._pathfinder_config.grid_rect = self.GRID_RECT self._pathfinder_config.grid_node_count = self.GRID_NODE_COUNT self._pathfinder_config.actor_size = self.SAMPLE_ACTOR_SIZE def simplify_path_mock(self, node_path, grid_graph, max_segment_length): return node_path def test_given_no_obstacles_when_finding_path_then_path_is_found(self): self._pathfinder_config.destination_point = Vector2(self.GRID_RECT.width, self.GRID_RECT.height) node_path = self._pathfinder.find_path(self._pathfinder_config) assert_true(node_path.node_count > 0) def test_given_destination_point_same_as_starting_point_when_finding_path_then_path_is_found_with_single_vertex( self ): self._pathfinder_config.destination_point = Vector2(0, 0) node_path = self._pathfinder.find_path(self._pathfinder_config) assert_equals(node_path.node_count, 1) def test_given_destination_point_beside_starting_point_when_finding_path_then_path_is_found_with_single_segment( self ): self._pathfinder_config.destination_point = Vector2(2, 2) node_path = self._pathfinder.find_path(self._pathfinder_config) assert_equals(node_path.node_count, 2) def test_given_destination_point_out_of_grid_when_finding_path_then_path_is_found(self): self._pathfinder_config.destination_point = Vector2(self.GRID_RECT.width + 1, self.GRID_RECT.height + 1) node_path = self._pathfinder.find_path(self._pathfinder_config) assert_true(node_path.node_count > 0) def test_given_obstacle_when_finding_path_then_path_is_found(self): self._pathfinder_config.destination_point = Vector2(self.GRID_RECT.width, self.GRID_RECT.height) self._pathfinder_config.add_obstacle(Circle(self.GRID_RECT.center, 2)) node_path = self._pathfinder.find_path(self._pathfinder_config) assert_true(node_path.node_count > 0) def test_given_destination_inside_obstacle_when_finding_path_then_path_is_found(self): self._pathfinder_config.destination_point = Vector2(self.GRID_RECT.width, self.GRID_RECT.height) self._pathfinder_config.add_obstacle(Circle(self._pathfinder_config.destination_point, 2)) node_path = self._pathfinder.find_path(self._pathfinder_config) assert_true(node_path.node_count > 0) @raises(NoPathFoundError) def test_given_no_possible_path_when_finding_path_then_error_is_raised(self): self._pathfinder_config.destination_point = Vector2(self.GRID_RECT.width, self.GRID_RECT.height) self._pathfinder_config.add_obstacle(Circle(Vector2(self.GRID_RECT.width / 2, 2), 4)) self._pathfinder_config.add_obstacle(Circle(Vector2(self.GRID_RECT.width / 2, 7), 4)) node_path = self._pathfinder.find_path(self._pathfinder_config) def test_given_path_found_when_drawing_path_then_path_is_successfully_drawn(self): self._pathfinder_config.destination_point = Vector2(self.GRID_RECT.width, self.GRID_RECT.height) node_path = self._pathfinder.find_path(self._pathfinder_config) blank_image = Image.from_attributes(self.GRID_RECT.width, self.GRID_RECT.height, ColorMode.BGR) self._pathfinder.draw(blank_image)