feat!: implement proper error handling, drop most expect() & unwrap() usages

This commit is contained in:
2025-08-11 20:23:39 -05:00
parent 5e9bb3535e
commit 27079e127d
20 changed files with 555 additions and 194 deletions

View File

@@ -16,16 +16,16 @@ fn test_blinking_texture() {
let tile = mock_atlas_tile(1);
let mut texture = BlinkingTexture::new(tile, 0.5);
assert_eq!(texture.is_on(), true);
assert!(texture.is_on());
texture.tick(0.5);
assert_eq!(texture.is_on(), false);
assert!(!texture.is_on());
texture.tick(0.5);
assert_eq!(texture.is_on(), true);
assert!(texture.is_on());
texture.tick(0.5);
assert_eq!(texture.is_on(), false);
assert!(!texture.is_on());
}
#[test]
@@ -34,7 +34,7 @@ fn test_blinking_texture_partial_duration() {
let mut texture = BlinkingTexture::new(tile, 0.5);
texture.tick(0.625);
assert_eq!(texture.is_on(), false);
assert!(!texture.is_on());
assert_eq!(texture.time_bank(), 0.125);
}
@@ -44,6 +44,6 @@ fn test_blinking_texture_negative_time() {
let mut texture = BlinkingTexture::new(tile, 0.5);
texture.tick(-0.1);
assert_eq!(texture.is_on(), true);
assert!(texture.is_on());
assert_eq!(texture.time_bank(), -0.1);
}

View File

@@ -3,7 +3,7 @@ use pacman::map::Map;
#[test]
fn test_game_map_creation() {
let map = Map::new(RAW_BOARD);
let map = Map::new(RAW_BOARD).unwrap();
assert!(map.graph.node_count() > 0);
assert!(!map.grid_to_node.is_empty());
@@ -16,6 +16,6 @@ fn test_game_map_creation() {
#[test]
fn test_game_score_initialization() {
// This would require creating a full Game instance, but we can test the concept
let map = Map::new(RAW_BOARD);
let map = Map::new(RAW_BOARD).unwrap();
assert!(map.find_starting_position(0).is_some());
}

View File

@@ -41,7 +41,7 @@ fn test_ghost_creation() {
let graph = Graph::new();
let atlas = create_test_atlas();
let ghost = Ghost::new(&graph, 0, GhostType::Blinky, &atlas);
let ghost = Ghost::new(&graph, 0, GhostType::Blinky, &atlas).unwrap();
assert_eq!(ghost.ghost_type, GhostType::Blinky);
assert_eq!(ghost.traverser.position.from_node_id(), 0);

View File

@@ -101,7 +101,7 @@ fn test_traverser_advance() {
let graph = create_test_graph();
let mut traverser = Traverser::new(&graph, 0, Direction::Right, &|_| true);
traverser.advance(&graph, 5.0, &|_| true);
traverser.advance(&graph, 5.0, &|_| true).unwrap();
match traverser.position {
Position::BetweenNodes { from, to, traversed } => {
@@ -112,7 +112,7 @@ fn test_traverser_advance() {
_ => panic!("Expected to be between nodes"),
}
traverser.advance(&graph, 3.0, &|_| true);
traverser.advance(&graph, 3.0, &|_| true).unwrap();
match traverser.position {
Position::BetweenNodes { from, to, traversed } => {
@@ -143,7 +143,9 @@ fn test_traverser_with_permissions() {
matches!(edge.permissions, EdgePermissions::All)
});
traverser.advance(&graph, 5.0, &|edge| matches!(edge.permissions, EdgePermissions::All));
traverser
.advance(&graph, 5.0, &|edge| matches!(edge.permissions, EdgePermissions::All))
.unwrap();
// Should still be at the node since it can't traverse
assert!(traverser.position.is_at_node());

View File

@@ -41,7 +41,7 @@ fn create_minimal_test_board() -> [&'static str; BOARD_CELL_SIZE.y as usize] {
#[test]
fn test_map_creation() {
let board = create_minimal_test_board();
let map = Map::new(board);
let map = Map::new(board).unwrap();
assert!(map.graph.node_count() > 0);
assert!(!map.grid_to_node.is_empty());
@@ -60,7 +60,7 @@ fn test_map_creation() {
#[test]
fn test_map_starting_positions() {
let board = create_minimal_test_board();
let map = Map::new(board);
let map = Map::new(board).unwrap();
let pacman_pos = map.find_starting_position(0);
assert!(pacman_pos.is_some());
@@ -74,7 +74,7 @@ fn test_map_starting_positions() {
#[test]
fn test_map_node_positions() {
let board = create_minimal_test_board();
let map = Map::new(board);
let map = Map::new(board).unwrap();
for (grid_pos, &node_id) in &map.grid_to_node {
let node = map.graph.get_node(node_id).unwrap();

View File

@@ -67,7 +67,7 @@ fn create_test_atlas() -> SpriteAtlas {
fn test_pacman_creation() {
let graph = create_test_graph();
let atlas = create_test_atlas();
let pacman = Pacman::new(&graph, 0, &atlas);
let pacman = Pacman::new(&graph, 0, &atlas).unwrap();
assert!(pacman.traverser.position.is_at_node());
assert_eq!(pacman.traverser.direction, Direction::Left);
@@ -77,7 +77,7 @@ fn test_pacman_creation() {
fn test_pacman_key_handling() {
let graph = create_test_graph();
let atlas = create_test_atlas();
let mut pacman = Pacman::new(&graph, 0, &atlas);
let mut pacman = Pacman::new(&graph, 0, &atlas).unwrap();
let test_cases = [
(Keycode::Up, Direction::Up),
@@ -96,7 +96,7 @@ fn test_pacman_key_handling() {
fn test_pacman_invalid_key() {
let graph = create_test_graph();
let atlas = create_test_atlas();
let mut pacman = Pacman::new(&graph, 0, &atlas);
let mut pacman = Pacman::new(&graph, 0, &atlas).unwrap();
let original_direction = pacman.traverser.direction;
let original_next_direction = pacman.traverser.next_direction;

View File

@@ -37,10 +37,10 @@ fn test_parse_board() {
#[test]
fn test_parse_board_invalid_character() {
let mut invalid_board = RAW_BOARD.clone();
invalid_board[0] = "###########################Z";
let mut invalid_board = RAW_BOARD.map(|s| s.to_string());
invalid_board[0] = "###########################Z".to_string();
let result = MapTileParser::parse_board(invalid_board);
let result = MapTileParser::parse_board(invalid_board.each_ref().map(|s| s.as_str()));
assert!(result.is_err());
assert!(matches!(result.unwrap_err(), ParseError::UnknownCharacter('Z')));
}

View File

@@ -61,14 +61,17 @@ fn test_ghost_pathfinding() {
let atlas = create_test_atlas();
// Create a ghost at node 0
let ghost = Ghost::new(&graph, node0, GhostType::Blinky, &atlas);
let ghost = Ghost::new(&graph, node0, GhostType::Blinky, &atlas).unwrap();
// Test pathfinding from node 0 to node 2
let path = ghost.calculate_path_to_target(&graph, node2);
assert!(path.is_some());
assert!(path.is_ok());
let path = path.unwrap();
assert_eq!(path, vec![node0, node1, node2]);
assert!(
path == vec![node0, node1, node2] || path == vec![node2, node1, node0],
"Path was not what was expected"
);
}
#[test]
@@ -85,12 +88,12 @@ fn test_ghost_pathfinding_no_path() {
// Don't connect the nodes
let atlas = create_test_atlas();
let ghost = Ghost::new(&graph, node0, GhostType::Blinky, &atlas);
let ghost = Ghost::new(&graph, node0, GhostType::Blinky, &atlas).unwrap();
// Test pathfinding when no path exists
let path = ghost.calculate_path_to_target(&graph, node1);
assert!(path.is_none());
assert!(path.is_err());
}
#[test]
@@ -101,10 +104,10 @@ fn test_ghost_debug_colors() {
position: glam::Vec2::new(0.0, 0.0),
});
let blinky = Ghost::new(&graph, node, GhostType::Blinky, &atlas);
let pinky = Ghost::new(&graph, node, GhostType::Pinky, &atlas);
let inky = Ghost::new(&graph, node, GhostType::Inky, &atlas);
let clyde = Ghost::new(&graph, node, GhostType::Clyde, &atlas);
let blinky = Ghost::new(&graph, node, GhostType::Blinky, &atlas).unwrap();
let pinky = Ghost::new(&graph, node, GhostType::Pinky, &atlas).unwrap();
let inky = Ghost::new(&graph, node, GhostType::Inky, &atlas).unwrap();
let clyde = Ghost::new(&graph, node, GhostType::Clyde, &atlas).unwrap();
// Test that each ghost has a different debug color
let colors = std::collections::HashSet::from([