Files
Pac-Man/tests/item.rs

160 lines
5.2 KiB
Rust

use pacman::systems::components::EntityType;
// Helper functions that extract the core scoring logic from item_system
// This allows us to test the business rules without ECS complexity
fn calculate_score_for_item(entity_type: EntityType) -> Option<u32> {
match entity_type {
EntityType::Pellet => Some(10),
EntityType::PowerPellet => Some(50),
_ => None,
}
}
fn is_collectible_item(entity_type: EntityType) -> bool {
matches!(entity_type, EntityType::Pellet | EntityType::PowerPellet)
}
fn should_trigger_audio_on_collection(entity_type: EntityType) -> bool {
is_collectible_item(entity_type)
}
#[test]
fn test_pellet_scoring() {
assert_eq!(calculate_score_for_item(EntityType::Pellet), Some(10));
}
#[test]
fn test_power_pellet_scoring() {
assert_eq!(calculate_score_for_item(EntityType::PowerPellet), Some(50));
}
#[test]
fn test_non_collectible_items_no_score() {
assert_eq!(calculate_score_for_item(EntityType::Player), None);
assert_eq!(calculate_score_for_item(EntityType::Ghost), None);
}
#[test]
fn test_collectible_item_detection() {
assert!(is_collectible_item(EntityType::Pellet));
assert!(is_collectible_item(EntityType::PowerPellet));
assert!(!is_collectible_item(EntityType::Player));
assert!(!is_collectible_item(EntityType::Ghost));
}
#[test]
fn test_audio_trigger_for_collectibles() {
assert!(should_trigger_audio_on_collection(EntityType::Pellet));
assert!(should_trigger_audio_on_collection(EntityType::PowerPellet));
assert!(!should_trigger_audio_on_collection(EntityType::Player));
assert!(!should_trigger_audio_on_collection(EntityType::Ghost));
}
#[test]
fn test_score_progression() {
// Test that power pellets are worth more than regular pellets
let pellet_score = calculate_score_for_item(EntityType::Pellet).unwrap();
let power_pellet_score = calculate_score_for_item(EntityType::PowerPellet).unwrap();
assert!(power_pellet_score > pellet_score);
assert_eq!(power_pellet_score / pellet_score, 5); // Power pellets are worth 5x regular pellets
}
#[test]
fn test_entity_type_variants() {
// Test all EntityType variants to ensure they're handled appropriately
let all_types = vec![
EntityType::Player,
EntityType::Ghost,
EntityType::Pellet,
EntityType::PowerPellet,
];
let mut collectible_count = 0;
let mut non_collectible_count = 0;
for entity_type in all_types {
if is_collectible_item(entity_type) {
collectible_count += 1;
// All collectible items should have a score
assert!(calculate_score_for_item(entity_type).is_some());
} else {
non_collectible_count += 1;
// Non-collectible items should not have a score
assert!(calculate_score_for_item(entity_type).is_none());
}
}
// Verify we have the expected number of each type
assert_eq!(collectible_count, 2); // Pellet and PowerPellet
assert_eq!(non_collectible_count, 2); // Player and Ghost
}
#[test]
fn test_score_accumulation() {
// Test score accumulation logic (simulating multiple collections)
let mut total_score = 0u32;
// Collect some items
let collected_items = vec![
EntityType::Pellet,
EntityType::Pellet,
EntityType::PowerPellet,
EntityType::Pellet,
EntityType::PowerPellet,
];
for item in collected_items {
if let Some(score) = calculate_score_for_item(item) {
total_score += score;
}
}
// Expected: 3 pellets (30) + 2 power pellets (100) = 130
assert_eq!(total_score, 130);
}
#[test]
fn test_collision_filtering_logic() {
// Test the logic for determining valid collision pairs
// This mirrors the logic in item_system that checks entity types
let test_cases = vec![
(EntityType::Player, EntityType::Pellet, true),
(EntityType::Player, EntityType::PowerPellet, true),
(EntityType::Player, EntityType::Ghost, false), // Not handled by item system
(EntityType::Player, EntityType::Player, false), // Not a valid collision
(EntityType::Ghost, EntityType::Pellet, false), // Ghosts don't collect items
(EntityType::Pellet, EntityType::PowerPellet, false), // Items don't interact
];
for (entity1, entity2, should_be_valid) in test_cases {
let is_valid_item_collision = (entity1 == EntityType::Player && is_collectible_item(entity2))
|| (entity2 == EntityType::Player && is_collectible_item(entity1));
assert_eq!(
is_valid_item_collision, should_be_valid,
"Failed for collision between {:?} and {:?}",
entity1, entity2
);
}
}
#[test]
fn test_item_collection_side_effects() {
// Test that collecting items should trigger the expected side effects
let collectible_items = vec![EntityType::Pellet, EntityType::PowerPellet];
for item in collectible_items {
// Should provide score
assert!(calculate_score_for_item(item).is_some());
// Should trigger audio
assert!(should_trigger_audio_on_collection(item));
// Should be marked as collectible
assert!(is_collectible_item(item));
}
}