mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-10 10:08:02 -06:00
test: add tons of tests for all easy submodules
This commit is contained in:
112
src/audio.rs
112
src/audio.rs
@@ -94,3 +94,115 @@ impl Audio {
|
||||
self.muted
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::sync::Once;
|
||||
|
||||
static INIT: Once = Once::new();
|
||||
|
||||
fn init_sdl() -> Result<(), String> {
|
||||
INIT.call_once(|| {
|
||||
if let Err(e) = sdl2::init() {
|
||||
eprintln!("Failed to initialize SDL2: {}", e);
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sound_assets_array() {
|
||||
assert_eq!(SOUND_ASSETS.len(), 4);
|
||||
assert_eq!(SOUND_ASSETS[0], Asset::Wav1);
|
||||
assert_eq!(SOUND_ASSETS[1], Asset::Wav2);
|
||||
assert_eq!(SOUND_ASSETS[2], Asset::Wav3);
|
||||
assert_eq!(SOUND_ASSETS[3], Asset::Wav4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_audio_asset_paths() {
|
||||
// Test that all sound assets have valid paths
|
||||
for asset in SOUND_ASSETS.iter() {
|
||||
let path = asset.path();
|
||||
assert!(!path.is_empty());
|
||||
assert!(path.contains("sound/waka/"));
|
||||
assert!(path.ends_with(".ogg"));
|
||||
}
|
||||
}
|
||||
|
||||
// Only run SDL2-dependent tests if SDL2 initialization succeeds
|
||||
#[test]
|
||||
fn test_audio_basic_functionality() {
|
||||
if let Err(_) = init_sdl() {
|
||||
eprintln!("Skipping SDL2-dependent tests due to initialization failure");
|
||||
return;
|
||||
}
|
||||
|
||||
// Test basic audio creation
|
||||
let audio = Audio::new();
|
||||
assert_eq!(audio.is_muted(), false);
|
||||
assert_eq!(audio.next_sound_index, 0);
|
||||
assert_eq!(audio.sounds.len(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_audio_mute_functionality() {
|
||||
if let Err(_) = init_sdl() {
|
||||
eprintln!("Skipping SDL2-dependent tests due to initialization failure");
|
||||
return;
|
||||
}
|
||||
|
||||
let mut audio = Audio::new();
|
||||
|
||||
// Test mute/unmute
|
||||
assert_eq!(audio.is_muted(), false);
|
||||
audio.set_mute(true);
|
||||
assert_eq!(audio.is_muted(), true);
|
||||
audio.set_mute(false);
|
||||
assert_eq!(audio.is_muted(), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_audio_sound_rotation() {
|
||||
if let Err(_) = init_sdl() {
|
||||
eprintln!("Skipping SDL2-dependent tests due to initialization failure");
|
||||
return;
|
||||
}
|
||||
|
||||
let mut audio = Audio::new();
|
||||
let initial_index = audio.next_sound_index;
|
||||
|
||||
// Test sound rotation
|
||||
for i in 0..4 {
|
||||
audio.eat();
|
||||
assert_eq!(audio.next_sound_index, (initial_index + i + 1) % 4);
|
||||
}
|
||||
|
||||
assert_eq!(audio.next_sound_index, initial_index);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_audio_sound_index_bounds() {
|
||||
if let Err(_) = init_sdl() {
|
||||
eprintln!("Skipping SDL2-dependent tests due to initialization failure");
|
||||
return;
|
||||
}
|
||||
|
||||
let audio = Audio::new();
|
||||
assert!(audio.next_sound_index < audio.sounds.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_audio_default_impl() {
|
||||
if let Err(_) = init_sdl() {
|
||||
eprintln!("Skipping SDL2-dependent tests due to initialization failure");
|
||||
return;
|
||||
}
|
||||
|
||||
let audio = Audio::default();
|
||||
assert_eq!(audio.is_muted(), false);
|
||||
assert_eq!(audio.next_sound_index, 0);
|
||||
assert_eq!(audio.sounds.len(), 4);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user