mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-16 10:12:37 -06:00
refactor: move all tests out of src/ into tests/, remove unnecessary tests
This commit is contained in:
@@ -368,162 +368,3 @@ impl Map {
|
||||
.expect("Failed to connect left tunnel hidden node to right tunnel hidden node");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::constants::{BOARD_CELL_SIZE, CELL_SIZE};
|
||||
use glam::{IVec2, Vec2};
|
||||
|
||||
fn create_minimal_test_board() -> [&'static str; BOARD_CELL_SIZE.y as usize] {
|
||||
let mut board = [""; BOARD_CELL_SIZE.y as usize];
|
||||
// Create a minimal valid board with house doors
|
||||
board[0] = "############################";
|
||||
board[1] = "#............##............#";
|
||||
board[2] = "#.####.#####.##.#####.####.#";
|
||||
board[3] = "#o####.#####.##.#####.####o#";
|
||||
board[4] = "#.####.#####.##.#####.####.#";
|
||||
board[5] = "#..........................#";
|
||||
board[6] = "#.####.##.########.##.####.#";
|
||||
board[7] = "#.####.##.########.##.####.#";
|
||||
board[8] = "#......##....##....##......#";
|
||||
board[9] = "######.##### ## #####.######";
|
||||
board[10] = " #.##### ## #####.# ";
|
||||
board[11] = " #.## == ##.# ";
|
||||
board[12] = " #.## ######## ##.# ";
|
||||
board[13] = "######.## ######## ##.######";
|
||||
board[14] = "T . ######## . T";
|
||||
board[15] = "######.## ######## ##.######";
|
||||
board[16] = " #.## ######## ##.# ";
|
||||
board[17] = " #.## ##.# ";
|
||||
board[18] = " #.## ######## ##.# ";
|
||||
board[19] = "######.## ######## ##.######";
|
||||
board[20] = "#............##............#";
|
||||
board[21] = "#.####.#####.##.#####.####.#";
|
||||
board[22] = "#.####.#####.##.#####.####.#";
|
||||
board[23] = "#o..##.......X .......##..o#";
|
||||
board[24] = "###.##.##.########.##.##.###";
|
||||
board[25] = "###.##.##.########.##.##.###";
|
||||
board[26] = "#......##....##....##......#";
|
||||
board[27] = "#.##########.##.##########.#";
|
||||
board[28] = "#.##########.##.##########.#";
|
||||
board[29] = "#..........................#";
|
||||
board[30] = "############################";
|
||||
board
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_map_new() {
|
||||
let board = create_minimal_test_board();
|
||||
let map = Map::new(board);
|
||||
|
||||
assert!(map.graph.node_count() > 0);
|
||||
assert!(!map.grid_to_node.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_starting_position_pacman() {
|
||||
let board = create_minimal_test_board();
|
||||
let map = Map::new(board);
|
||||
|
||||
let pacman_pos = map.find_starting_position(0);
|
||||
assert!(pacman_pos.is_some());
|
||||
|
||||
let pos = pacman_pos.unwrap();
|
||||
// Pacman should be found somewhere in the board
|
||||
assert!(pos.x < BOARD_CELL_SIZE.x);
|
||||
assert!(pos.y < BOARD_CELL_SIZE.y);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_starting_position_ghost() {
|
||||
let board = create_minimal_test_board();
|
||||
let map = Map::new(board);
|
||||
|
||||
// Test for ghost 1 (might not exist in this board)
|
||||
let ghost_pos = map.find_starting_position(1);
|
||||
// Ghost 1 might not exist, so this could be None
|
||||
if let Some(pos) = ghost_pos {
|
||||
assert!(pos.x < BOARD_CELL_SIZE.x);
|
||||
assert!(pos.y < BOARD_CELL_SIZE.y);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_starting_position_nonexistent() {
|
||||
let board = create_minimal_test_board();
|
||||
let map = Map::new(board);
|
||||
|
||||
let pos = map.find_starting_position(99); // Non-existent entity
|
||||
assert!(pos.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_map_graph_construction() {
|
||||
let board = create_minimal_test_board();
|
||||
let map = Map::new(board);
|
||||
|
||||
// Check that nodes were created
|
||||
assert!(map.graph.node_count() > 0);
|
||||
|
||||
// Check that grid_to_node mapping was created
|
||||
assert!(!map.grid_to_node.is_empty());
|
||||
|
||||
// Check that some connections were made
|
||||
let mut has_connections = false;
|
||||
for intersection in &map.graph.adjacency_list {
|
||||
if intersection.edges().next().is_some() {
|
||||
has_connections = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert!(has_connections);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_map_grid_to_node_mapping() {
|
||||
let board = create_minimal_test_board();
|
||||
let map = Map::new(board);
|
||||
|
||||
// Check that Pac-Man's position is mapped
|
||||
let pacman_pos = map.find_starting_position(0).unwrap();
|
||||
let grid_pos = IVec2::new(pacman_pos.x as i32, pacman_pos.y as i32);
|
||||
|
||||
assert!(map.grid_to_node.contains_key(&grid_pos));
|
||||
let node_id = map.grid_to_node[&grid_pos];
|
||||
assert!(map.graph.get_node(node_id).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_map_node_positions() {
|
||||
let board = create_minimal_test_board();
|
||||
let map = Map::new(board);
|
||||
|
||||
// Check that node positions are correctly calculated
|
||||
for (grid_pos, &node_id) in &map.grid_to_node {
|
||||
let node = map.graph.get_node(node_id).unwrap();
|
||||
let expected_pos = Vec2::new((grid_pos.x * CELL_SIZE as i32) as f32, (grid_pos.y * CELL_SIZE as i32) as f32)
|
||||
+ Vec2::splat(CELL_SIZE as f32 / 2.0);
|
||||
|
||||
assert_eq!(node.position, expected_pos);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_map_adjacent_connections() {
|
||||
let board = create_minimal_test_board();
|
||||
let map = Map::new(board);
|
||||
|
||||
// Check that adjacent walkable tiles are connected
|
||||
// Find any node that has connections
|
||||
let mut found_connected_node = false;
|
||||
for &node_id in map.grid_to_node.values() {
|
||||
let intersection = &map.graph.adjacency_list[node_id];
|
||||
if intersection.edges().next().is_some() {
|
||||
found_connected_node = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert!(found_connected_node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,56 +118,3 @@ impl MapTileParser {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::constants::RAW_BOARD;
|
||||
|
||||
#[test]
|
||||
fn test_parse_character() {
|
||||
assert!(matches!(MapTileParser::parse_character('#').unwrap(), MapTile::Wall));
|
||||
assert!(matches!(MapTileParser::parse_character('.').unwrap(), MapTile::Pellet));
|
||||
assert!(matches!(MapTileParser::parse_character('o').unwrap(), MapTile::PowerPellet));
|
||||
assert!(matches!(MapTileParser::parse_character(' ').unwrap(), MapTile::Empty));
|
||||
assert!(matches!(MapTileParser::parse_character('T').unwrap(), MapTile::Tunnel));
|
||||
assert!(matches!(MapTileParser::parse_character('X').unwrap(), MapTile::Empty));
|
||||
assert!(matches!(MapTileParser::parse_character('=').unwrap(), MapTile::Wall));
|
||||
|
||||
// Test invalid character
|
||||
assert!(MapTileParser::parse_character('Z').is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_board() {
|
||||
let result = MapTileParser::parse_board(RAW_BOARD);
|
||||
assert!(result.is_ok());
|
||||
|
||||
let parsed = result.unwrap();
|
||||
|
||||
// Verify we have tiles
|
||||
assert_eq!(parsed.tiles.len(), BOARD_CELL_SIZE.x as usize);
|
||||
assert_eq!(parsed.tiles[0].len(), BOARD_CELL_SIZE.y as usize);
|
||||
|
||||
// Verify we found house door positions
|
||||
assert!(parsed.house_door[0].is_some());
|
||||
assert!(parsed.house_door[1].is_some());
|
||||
|
||||
// Verify we found tunnel ends
|
||||
assert!(parsed.tunnel_ends[0].is_some());
|
||||
assert!(parsed.tunnel_ends[1].is_some());
|
||||
|
||||
// Verify we found Pac-Man's starting position
|
||||
assert!(parsed.pacman_start.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_board_invalid_character() {
|
||||
let mut invalid_board = RAW_BOARD.clone();
|
||||
invalid_board[0] = "###########################Z";
|
||||
|
||||
let result = MapTileParser::parse_board(invalid_board);
|
||||
assert!(result.is_err());
|
||||
assert!(matches!(result.unwrap_err(), ParseError::UnknownCharacter('Z')));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,80 +65,3 @@ impl MapRenderer {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::entity::graph::{Graph, Node};
|
||||
use crate::texture::sprite::{AtlasMapper, MapperFrame};
|
||||
use std::collections::HashMap;
|
||||
|
||||
fn create_test_graph() -> Graph {
|
||||
let mut graph = Graph::new();
|
||||
let node1 = graph.add_node(Node {
|
||||
position: glam::Vec2::new(0.0, 0.0),
|
||||
});
|
||||
let node2 = graph.add_node(Node {
|
||||
position: glam::Vec2::new(16.0, 0.0),
|
||||
});
|
||||
let node3 = graph.add_node(Node {
|
||||
position: glam::Vec2::new(0.0, 16.0),
|
||||
});
|
||||
|
||||
graph
|
||||
.connect(node1, node2, false, None, crate::entity::direction::Direction::Right)
|
||||
.unwrap();
|
||||
graph
|
||||
.connect(node1, node3, false, None, crate::entity::direction::Direction::Down)
|
||||
.unwrap();
|
||||
|
||||
graph
|
||||
}
|
||||
|
||||
fn create_test_atlas() -> SpriteAtlas {
|
||||
let mut frames = HashMap::new();
|
||||
frames.insert(
|
||||
"maze/full.png".to_string(),
|
||||
MapperFrame {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 224,
|
||||
height: 248,
|
||||
},
|
||||
);
|
||||
let mapper = AtlasMapper { frames };
|
||||
let dummy_texture = unsafe { std::mem::zeroed() };
|
||||
SpriteAtlas::new(dummy_texture, mapper)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_render_map_does_not_panic() {
|
||||
// This test just ensures the function doesn't panic
|
||||
// We can't easily test the actual rendering without SDL context
|
||||
let atlas = create_test_atlas();
|
||||
let _map_texture = SpriteAtlas::get_tile(&atlas, "maze/full.png").unwrap();
|
||||
|
||||
// The function should not panic even with dummy data
|
||||
// Note: We can't actually call render_map without a canvas, but we can test the logic
|
||||
assert!(true); // Placeholder test
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_debug_render_nodes_does_not_panic() {
|
||||
// This test just ensures the function doesn't panic
|
||||
// We can't easily test the actual rendering without SDL context
|
||||
let _graph = create_test_graph();
|
||||
|
||||
// The function should not panic even with dummy data
|
||||
// Note: We can't actually call debug_render_nodes without a canvas, but we can test the logic
|
||||
assert!(true); // Placeholder test
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_map_renderer_structure() {
|
||||
// Test that MapRenderer is a unit struct
|
||||
let _renderer = MapRenderer;
|
||||
// This should compile and not panic
|
||||
assert!(true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user