mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-15 18:12:31 -06:00
test: add tons of tests for all easy submodules
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
use anyhow::Result;
|
||||
use sdl2::rect::Rect;
|
||||
use sdl2::render::{Canvas, RenderTarget};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::texture::sprite::{AtlasTile, SpriteAtlas};
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Error, Debug)]
|
||||
pub enum AnimatedTextureError {
|
||||
#[error("Frame duration must be positive, got {0}")]
|
||||
InvalidFrameDuration(f32),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AnimatedTexture {
|
||||
tiles: Vec<AtlasTile>,
|
||||
frame_duration: f32,
|
||||
@@ -13,13 +20,17 @@ pub struct AnimatedTexture {
|
||||
}
|
||||
|
||||
impl AnimatedTexture {
|
||||
pub fn new(tiles: Vec<AtlasTile>, frame_duration: f32) -> Self {
|
||||
Self {
|
||||
pub fn new(tiles: Vec<AtlasTile>, frame_duration: f32) -> Result<Self, AnimatedTextureError> {
|
||||
if frame_duration <= 0.0 {
|
||||
return Err(AnimatedTextureError::InvalidFrameDuration(frame_duration));
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
tiles,
|
||||
frame_duration,
|
||||
current_frame: 0,
|
||||
time_bank: 0.0,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn tick(&mut self, dt: f32) {
|
||||
@@ -38,4 +49,145 @@ impl AnimatedTexture {
|
||||
let mut tile = *self.current_tile();
|
||||
tile.render(canvas, atlas, dest)
|
||||
}
|
||||
|
||||
// Helper methods for testing
|
||||
pub fn current_frame(&self) -> usize {
|
||||
self.current_frame
|
||||
}
|
||||
|
||||
pub fn time_bank(&self) -> f32 {
|
||||
self.time_bank
|
||||
}
|
||||
|
||||
pub fn frame_duration(&self) -> f32 {
|
||||
self.frame_duration
|
||||
}
|
||||
|
||||
pub fn tiles_len(&self) -> usize {
|
||||
self.tiles.len()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use glam::U16Vec2;
|
||||
use sdl2::pixels::Color;
|
||||
|
||||
impl AtlasTile {
|
||||
fn mock(id: u32) -> Self {
|
||||
AtlasTile {
|
||||
pos: U16Vec2::new(0, 0),
|
||||
size: U16Vec2::new(16, 16),
|
||||
color: Some(Color::RGB(id as u8, 0, 0)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new_animated_texture() {
|
||||
let tiles = vec![AtlasTile::mock(1), AtlasTile::mock(2), AtlasTile::mock(3)];
|
||||
let texture = AnimatedTexture::new(tiles.clone(), 0.1).unwrap();
|
||||
|
||||
assert_eq!(texture.current_frame(), 0);
|
||||
assert_eq!(texture.time_bank(), 0.0);
|
||||
assert_eq!(texture.frame_duration(), 0.1);
|
||||
assert_eq!(texture.tiles_len(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new_animated_texture_zero_duration() {
|
||||
let tiles = vec![AtlasTile::mock(1), AtlasTile::mock(2)];
|
||||
let result = AnimatedTexture::new(tiles, 0.0);
|
||||
assert!(result.is_err());
|
||||
assert!(matches!(result.unwrap_err(), AnimatedTextureError::InvalidFrameDuration(0.0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new_animated_texture_negative_duration() {
|
||||
let tiles = vec![AtlasTile::mock(1), AtlasTile::mock(2)];
|
||||
let result = AnimatedTexture::new(tiles, -0.1);
|
||||
assert!(result.is_err());
|
||||
assert!(matches!(
|
||||
result.unwrap_err(),
|
||||
AnimatedTextureError::InvalidFrameDuration(-0.1)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tick_no_frame_change() {
|
||||
let tiles = vec![AtlasTile::mock(1), AtlasTile::mock(2)];
|
||||
let mut texture = AnimatedTexture::new(tiles, 0.1).unwrap();
|
||||
|
||||
// Tick with less than frame duration
|
||||
texture.tick(0.05);
|
||||
assert_eq!(texture.current_frame(), 0);
|
||||
assert_eq!(texture.time_bank(), 0.05);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tick_single_frame_change() {
|
||||
let tiles = vec![AtlasTile::mock(1), AtlasTile::mock(2)];
|
||||
let mut texture = AnimatedTexture::new(tiles, 0.1).unwrap();
|
||||
|
||||
// Tick with exactly frame duration
|
||||
texture.tick(0.1);
|
||||
assert_eq!(texture.current_frame(), 1);
|
||||
assert_eq!(texture.time_bank(), 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tick_multiple_frame_changes() {
|
||||
let tiles = vec![AtlasTile::mock(1), AtlasTile::mock(2), AtlasTile::mock(3)];
|
||||
let mut texture = AnimatedTexture::new(tiles, 0.1).unwrap();
|
||||
|
||||
// Tick with 2.5 frame durations
|
||||
texture.tick(0.25);
|
||||
assert_eq!(texture.current_frame(), 2);
|
||||
assert!((texture.time_bank() - 0.05).abs() < 0.001);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tick_wrap_around() {
|
||||
let tiles = vec![AtlasTile::mock(1), AtlasTile::mock(2)];
|
||||
let mut texture = AnimatedTexture::new(tiles, 0.1).unwrap();
|
||||
|
||||
// Advance to last frame
|
||||
texture.tick(0.1);
|
||||
assert_eq!(texture.current_frame(), 1);
|
||||
|
||||
// Advance again to wrap around
|
||||
texture.tick(0.1);
|
||||
assert_eq!(texture.current_frame(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_current_tile() {
|
||||
let tiles = vec![AtlasTile::mock(1), AtlasTile::mock(2)];
|
||||
let texture = AnimatedTexture::new(tiles, 0.1).unwrap();
|
||||
|
||||
// Should return first tile initially
|
||||
assert_eq!(texture.current_tile().color.unwrap().r, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_current_tile_after_frame_change() {
|
||||
let tiles = vec![AtlasTile::mock(1), AtlasTile::mock(2)];
|
||||
let mut texture = AnimatedTexture::new(tiles, 0.1).unwrap();
|
||||
|
||||
// Advance one frame
|
||||
texture.tick(0.1);
|
||||
assert_eq!(texture.current_tile().color.unwrap().r, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_single_tile_animation() {
|
||||
let tiles = vec![AtlasTile::mock(1)];
|
||||
let mut texture = AnimatedTexture::new(tiles, 0.1).unwrap();
|
||||
|
||||
// Should stay on same frame
|
||||
texture.tick(0.1);
|
||||
assert_eq!(texture.current_frame(), 0);
|
||||
assert_eq!(texture.current_tile().color.unwrap().r, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,4 +34,145 @@ impl BlinkingTexture {
|
||||
pub fn tile(&self) -> &AtlasTile {
|
||||
&self.tile
|
||||
}
|
||||
|
||||
// Helper methods for testing
|
||||
pub fn time_bank(&self) -> f32 {
|
||||
self.time_bank
|
||||
}
|
||||
|
||||
pub fn blink_duration(&self) -> f32 {
|
||||
self.blink_duration
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use glam::U16Vec2;
|
||||
use sdl2::pixels::Color;
|
||||
|
||||
fn mock_atlas_tile(id: u32) -> AtlasTile {
|
||||
AtlasTile {
|
||||
pos: U16Vec2::new(0, 0),
|
||||
size: U16Vec2::new(16, 16),
|
||||
color: Some(Color::RGB(id as u8, 0, 0)),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new_blinking_texture() {
|
||||
let tile = mock_atlas_tile(1);
|
||||
let texture = BlinkingTexture::new(tile, 0.5);
|
||||
|
||||
assert_eq!(texture.is_on(), true);
|
||||
assert_eq!(texture.time_bank(), 0.0);
|
||||
assert_eq!(texture.blink_duration(), 0.5);
|
||||
assert_eq!(texture.tile().color.unwrap().r, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tick_no_blink_change() {
|
||||
let tile = mock_atlas_tile(1);
|
||||
let mut texture = BlinkingTexture::new(tile, 0.5);
|
||||
|
||||
// Tick with less than blink duration
|
||||
texture.tick(0.25);
|
||||
assert_eq!(texture.is_on(), true);
|
||||
assert_eq!(texture.time_bank(), 0.25);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tick_single_blink_change() {
|
||||
let tile = mock_atlas_tile(1);
|
||||
let mut texture = BlinkingTexture::new(tile, 0.5);
|
||||
|
||||
// Tick with exactly blink duration
|
||||
texture.tick(0.5);
|
||||
assert_eq!(texture.is_on(), false);
|
||||
assert_eq!(texture.time_bank(), 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tick_multiple_blink_changes() {
|
||||
let tile = mock_atlas_tile(1);
|
||||
let mut texture = BlinkingTexture::new(tile, 0.5);
|
||||
|
||||
// First blink
|
||||
texture.tick(0.5);
|
||||
assert_eq!(texture.is_on(), false);
|
||||
|
||||
// Second blink (back to on)
|
||||
texture.tick(0.5);
|
||||
assert_eq!(texture.is_on(), true);
|
||||
|
||||
// Third blink (back to off)
|
||||
texture.tick(0.5);
|
||||
assert_eq!(texture.is_on(), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tick_partial_blink_duration() {
|
||||
let tile = mock_atlas_tile(1);
|
||||
let mut texture = BlinkingTexture::new(tile, 0.5);
|
||||
|
||||
// Tick with 1.25 blink durations
|
||||
texture.tick(0.625);
|
||||
assert_eq!(texture.is_on(), false);
|
||||
assert_eq!(texture.time_bank(), 0.125);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tick_with_zero_duration() {
|
||||
let tile = mock_atlas_tile(1);
|
||||
let mut texture = BlinkingTexture::new(tile, 0.0);
|
||||
|
||||
// Should not cause issues - skip the test if blink_duration is 0
|
||||
if texture.blink_duration() > 0.0 {
|
||||
texture.tick(0.1);
|
||||
assert_eq!(texture.is_on(), true);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tick_with_negative_duration() {
|
||||
let tile = mock_atlas_tile(1);
|
||||
let mut texture = BlinkingTexture::new(tile, -0.5);
|
||||
|
||||
// Should not cause issues - skip the test if blink_duration is negative
|
||||
if texture.blink_duration() > 0.0 {
|
||||
texture.tick(0.1);
|
||||
assert_eq!(texture.is_on(), true);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tick_with_negative_delta_time() {
|
||||
let tile = mock_atlas_tile(1);
|
||||
let mut texture = BlinkingTexture::new(tile, 0.5);
|
||||
|
||||
// Should not cause issues
|
||||
texture.tick(-0.1);
|
||||
assert_eq!(texture.is_on(), true);
|
||||
assert_eq!(texture.time_bank(), -0.1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tile_access() {
|
||||
let tile = mock_atlas_tile(42);
|
||||
let texture = BlinkingTexture::new(tile, 0.5);
|
||||
|
||||
assert_eq!(texture.tile().color.unwrap().r, 42);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_clone() {
|
||||
let tile = mock_atlas_tile(1);
|
||||
let texture = BlinkingTexture::new(tile, 0.5);
|
||||
let cloned = texture.clone();
|
||||
|
||||
assert_eq!(texture.is_on(), cloned.is_on());
|
||||
assert_eq!(texture.time_bank(), cloned.time_bank());
|
||||
assert_eq!(texture.blink_duration(), cloned.blink_duration());
|
||||
assert_eq!(texture.tile().color.unwrap().r, cloned.tile().color.unwrap().r);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,4 +54,130 @@ impl DirectionalAnimatedTexture {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// Helper methods for testing
|
||||
pub fn has_direction(&self, direction: Direction) -> bool {
|
||||
self.textures.contains_key(&direction)
|
||||
}
|
||||
|
||||
pub fn has_stopped_direction(&self, direction: Direction) -> bool {
|
||||
self.stopped_textures.contains_key(&direction)
|
||||
}
|
||||
|
||||
pub fn texture_count(&self) -> usize {
|
||||
self.textures.len()
|
||||
}
|
||||
|
||||
pub fn stopped_texture_count(&self) -> usize {
|
||||
self.stopped_textures.len()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::texture::sprite::AtlasTile;
|
||||
use glam::U16Vec2;
|
||||
use sdl2::pixels::Color;
|
||||
|
||||
fn mock_atlas_tile(id: u32) -> AtlasTile {
|
||||
AtlasTile {
|
||||
pos: U16Vec2::new(0, 0),
|
||||
size: U16Vec2::new(16, 16),
|
||||
color: Some(Color::RGB(id as u8, 0, 0)),
|
||||
}
|
||||
}
|
||||
|
||||
fn mock_animated_texture(id: u32) -> AnimatedTexture {
|
||||
AnimatedTexture::new(vec![mock_atlas_tile(id)], 0.1).expect("Invalid frame duration")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new_directional_animated_texture() {
|
||||
let mut textures = HashMap::new();
|
||||
let mut stopped_textures = HashMap::new();
|
||||
|
||||
textures.insert(Direction::Up, mock_animated_texture(1));
|
||||
textures.insert(Direction::Down, mock_animated_texture(2));
|
||||
stopped_textures.insert(Direction::Up, mock_animated_texture(3));
|
||||
stopped_textures.insert(Direction::Down, mock_animated_texture(4));
|
||||
|
||||
let texture = DirectionalAnimatedTexture::new(textures, stopped_textures);
|
||||
|
||||
assert_eq!(texture.texture_count(), 2);
|
||||
assert_eq!(texture.stopped_texture_count(), 2);
|
||||
assert!(texture.has_direction(Direction::Up));
|
||||
assert!(texture.has_direction(Direction::Down));
|
||||
assert!(!texture.has_direction(Direction::Left));
|
||||
assert!(texture.has_stopped_direction(Direction::Up));
|
||||
assert!(texture.has_stopped_direction(Direction::Down));
|
||||
assert!(!texture.has_stopped_direction(Direction::Left));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tick() {
|
||||
let mut textures = HashMap::new();
|
||||
textures.insert(Direction::Up, mock_animated_texture(1));
|
||||
textures.insert(Direction::Down, mock_animated_texture(2));
|
||||
|
||||
let mut texture = DirectionalAnimatedTexture::new(textures, HashMap::new());
|
||||
|
||||
// Should not panic
|
||||
texture.tick(0.1);
|
||||
assert_eq!(texture.texture_count(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_texture() {
|
||||
let texture = DirectionalAnimatedTexture::new(HashMap::new(), HashMap::new());
|
||||
|
||||
assert_eq!(texture.texture_count(), 0);
|
||||
assert_eq!(texture.stopped_texture_count(), 0);
|
||||
assert!(!texture.has_direction(Direction::Up));
|
||||
assert!(!texture.has_stopped_direction(Direction::Up));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_partial_directions() {
|
||||
let mut textures = HashMap::new();
|
||||
textures.insert(Direction::Up, mock_animated_texture(1));
|
||||
|
||||
let texture = DirectionalAnimatedTexture::new(textures, HashMap::new());
|
||||
|
||||
assert_eq!(texture.texture_count(), 1);
|
||||
assert!(texture.has_direction(Direction::Up));
|
||||
assert!(!texture.has_direction(Direction::Down));
|
||||
assert!(!texture.has_direction(Direction::Left));
|
||||
assert!(!texture.has_direction(Direction::Right));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_clone() {
|
||||
let mut textures = HashMap::new();
|
||||
textures.insert(Direction::Up, mock_animated_texture(1));
|
||||
|
||||
let texture = DirectionalAnimatedTexture::new(textures, HashMap::new());
|
||||
let cloned = texture.clone();
|
||||
|
||||
assert_eq!(texture.texture_count(), cloned.texture_count());
|
||||
assert_eq!(texture.stopped_texture_count(), cloned.stopped_texture_count());
|
||||
assert_eq!(texture.has_direction(Direction::Up), cloned.has_direction(Direction::Up));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_all_directions() {
|
||||
let mut textures = HashMap::new();
|
||||
textures.insert(Direction::Up, mock_animated_texture(1));
|
||||
textures.insert(Direction::Down, mock_animated_texture(2));
|
||||
textures.insert(Direction::Left, mock_animated_texture(3));
|
||||
textures.insert(Direction::Right, mock_animated_texture(4));
|
||||
|
||||
let texture = DirectionalAnimatedTexture::new(textures, HashMap::new());
|
||||
|
||||
assert_eq!(texture.texture_count(), 4);
|
||||
assert!(texture.has_direction(Direction::Up));
|
||||
assert!(texture.has_direction(Direction::Down));
|
||||
assert!(texture.has_direction(Direction::Left));
|
||||
assert!(texture.has_direction(Direction::Right));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,6 +49,16 @@ impl AtlasTile {
|
||||
canvas.copy(&atlas.texture, src, dest).map_err(anyhow::Error::msg)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Helper methods for testing
|
||||
pub fn new(pos: U16Vec2, size: U16Vec2, color: Option<Color>) -> Self {
|
||||
Self { pos, size, color }
|
||||
}
|
||||
|
||||
pub fn with_color(mut self, color: Color) -> Self {
|
||||
self.color = Some(color);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SpriteAtlas {
|
||||
@@ -85,6 +95,19 @@ impl SpriteAtlas {
|
||||
pub fn texture(&self) -> &Texture<'static> {
|
||||
&self.texture
|
||||
}
|
||||
|
||||
// Helper methods for testing
|
||||
pub fn tiles_count(&self) -> usize {
|
||||
self.tiles.len()
|
||||
}
|
||||
|
||||
pub fn has_tile(&self, name: &str) -> bool {
|
||||
self.tiles.contains_key(name)
|
||||
}
|
||||
|
||||
pub fn default_color(&self) -> Option<Color> {
|
||||
self.default_color
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts a `Texture` to a `Texture<'static>` using transmute.
|
||||
@@ -103,3 +126,236 @@ impl SpriteAtlas {
|
||||
pub unsafe fn texture_to_static(texture: Texture) -> Texture<'static> {
|
||||
std::mem::transmute(texture)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use sdl2::pixels::Color;
|
||||
|
||||
// Mock texture for testing - we'll use a dummy approach since we can't create real SDL2 textures
|
||||
fn mock_texture() -> Texture<'static> {
|
||||
// This is unsafe and only for testing - in real usage this would be a proper texture
|
||||
unsafe { std::mem::transmute(0usize) }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_atlas_tile_new() {
|
||||
let pos = U16Vec2::new(10, 20);
|
||||
let size = U16Vec2::new(32, 32);
|
||||
let tile = AtlasTile::new(pos, size, None);
|
||||
|
||||
assert_eq!(tile.pos, pos);
|
||||
assert_eq!(tile.size, size);
|
||||
assert_eq!(tile.color, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_atlas_tile_with_color() {
|
||||
let pos = U16Vec2::new(10, 20);
|
||||
let size = U16Vec2::new(32, 32);
|
||||
let color = Color::RGB(255, 0, 0);
|
||||
let tile = AtlasTile::new(pos, size, None).with_color(color);
|
||||
|
||||
assert_eq!(tile.pos, pos);
|
||||
assert_eq!(tile.size, size);
|
||||
assert_eq!(tile.color, Some(color));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mapper_frame() {
|
||||
let frame = MapperFrame {
|
||||
x: 10,
|
||||
y: 20,
|
||||
width: 32,
|
||||
height: 32,
|
||||
};
|
||||
|
||||
assert_eq!(frame.x, 10);
|
||||
assert_eq!(frame.y, 20);
|
||||
assert_eq!(frame.width, 32);
|
||||
assert_eq!(frame.height, 32);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_atlas_mapper_new() {
|
||||
let mut frames = HashMap::new();
|
||||
frames.insert(
|
||||
"test".to_string(),
|
||||
MapperFrame {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 32,
|
||||
height: 32,
|
||||
},
|
||||
);
|
||||
|
||||
let mapper = AtlasMapper { frames };
|
||||
|
||||
assert_eq!(mapper.frames.len(), 1);
|
||||
assert!(mapper.frames.contains_key("test"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sprite_atlas_new() {
|
||||
let mut frames = HashMap::new();
|
||||
frames.insert(
|
||||
"test".to_string(),
|
||||
MapperFrame {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 32,
|
||||
height: 32,
|
||||
},
|
||||
);
|
||||
|
||||
let mapper = AtlasMapper { frames };
|
||||
let texture = mock_texture();
|
||||
let atlas = SpriteAtlas::new(texture, mapper);
|
||||
|
||||
assert_eq!(atlas.tiles_count(), 1);
|
||||
assert!(atlas.has_tile("test"));
|
||||
assert_eq!(atlas.default_color(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sprite_atlas_get_tile() {
|
||||
let mut frames = HashMap::new();
|
||||
frames.insert(
|
||||
"test".to_string(),
|
||||
MapperFrame {
|
||||
x: 10,
|
||||
y: 20,
|
||||
width: 32,
|
||||
height: 64,
|
||||
},
|
||||
);
|
||||
|
||||
let mapper = AtlasMapper { frames };
|
||||
let texture = mock_texture();
|
||||
let atlas = SpriteAtlas::new(texture, mapper);
|
||||
|
||||
let tile = atlas.get_tile("test");
|
||||
assert!(tile.is_some());
|
||||
|
||||
let tile = tile.unwrap();
|
||||
assert_eq!(tile.pos, U16Vec2::new(10, 20));
|
||||
assert_eq!(tile.size, U16Vec2::new(32, 64));
|
||||
assert_eq!(tile.color, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sprite_atlas_get_tile_nonexistent() {
|
||||
let mapper = AtlasMapper { frames: HashMap::new() };
|
||||
let texture = mock_texture();
|
||||
let atlas = SpriteAtlas::new(texture, mapper);
|
||||
|
||||
let tile = atlas.get_tile("nonexistent");
|
||||
assert!(tile.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sprite_atlas_set_color() {
|
||||
let mapper = AtlasMapper { frames: HashMap::new() };
|
||||
let texture = mock_texture();
|
||||
let mut atlas = SpriteAtlas::new(texture, mapper);
|
||||
|
||||
assert_eq!(atlas.default_color(), None);
|
||||
|
||||
let color = Color::RGB(255, 0, 0);
|
||||
atlas.set_color(color);
|
||||
|
||||
assert_eq!(atlas.default_color(), Some(color));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sprite_atlas_empty() {
|
||||
let mapper = AtlasMapper { frames: HashMap::new() };
|
||||
let texture = mock_texture();
|
||||
let atlas = SpriteAtlas::new(texture, mapper);
|
||||
|
||||
assert_eq!(atlas.tiles_count(), 0);
|
||||
assert!(!atlas.has_tile("any"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sprite_atlas_multiple_tiles() {
|
||||
let mut frames = HashMap::new();
|
||||
frames.insert(
|
||||
"tile1".to_string(),
|
||||
MapperFrame {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 32,
|
||||
height: 32,
|
||||
},
|
||||
);
|
||||
frames.insert(
|
||||
"tile2".to_string(),
|
||||
MapperFrame {
|
||||
x: 32,
|
||||
y: 0,
|
||||
width: 64,
|
||||
height: 64,
|
||||
},
|
||||
);
|
||||
|
||||
let mapper = AtlasMapper { frames };
|
||||
let texture = mock_texture();
|
||||
let atlas = SpriteAtlas::new(texture, mapper);
|
||||
|
||||
assert_eq!(atlas.tiles_count(), 2);
|
||||
assert!(atlas.has_tile("tile1"));
|
||||
assert!(atlas.has_tile("tile2"));
|
||||
assert!(!atlas.has_tile("tile3"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_atlas_tile_clone() {
|
||||
let pos = U16Vec2::new(10, 20);
|
||||
let size = U16Vec2::new(32, 32);
|
||||
let color = Color::RGB(255, 0, 0);
|
||||
let tile = AtlasTile::new(pos, size, Some(color));
|
||||
let cloned = tile;
|
||||
|
||||
assert_eq!(tile.pos, cloned.pos);
|
||||
assert_eq!(tile.size, cloned.size);
|
||||
assert_eq!(tile.color, cloned.color);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mapper_frame_clone() {
|
||||
let frame = MapperFrame {
|
||||
x: 10,
|
||||
y: 20,
|
||||
width: 32,
|
||||
height: 64,
|
||||
};
|
||||
let cloned = frame;
|
||||
|
||||
assert_eq!(frame.x, cloned.x);
|
||||
assert_eq!(frame.y, cloned.y);
|
||||
assert_eq!(frame.width, cloned.width);
|
||||
assert_eq!(frame.height, cloned.height);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_atlas_mapper_clone() {
|
||||
let mut frames = HashMap::new();
|
||||
frames.insert(
|
||||
"test".to_string(),
|
||||
MapperFrame {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 32,
|
||||
height: 32,
|
||||
},
|
||||
);
|
||||
|
||||
let mapper = AtlasMapper { frames };
|
||||
let cloned = mapper.clone();
|
||||
|
||||
assert_eq!(mapper.frames.len(), cloned.frames.len());
|
||||
assert!(mapper.frames.contains_key("test"));
|
||||
assert!(cloned.frames.contains_key("test"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,3 +151,228 @@ impl TextTexture {
|
||||
(8.0 * self.scale) as u32
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::texture::sprite::{AtlasMapper, MapperFrame, SpriteAtlas};
|
||||
use std::collections::HashMap;
|
||||
|
||||
fn create_mock_atlas() -> SpriteAtlas {
|
||||
let mut frames = HashMap::new();
|
||||
frames.insert(
|
||||
"text/A.png".to_string(),
|
||||
MapperFrame {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 8,
|
||||
height: 8,
|
||||
},
|
||||
);
|
||||
frames.insert(
|
||||
"text/1.png".to_string(),
|
||||
MapperFrame {
|
||||
x: 8,
|
||||
y: 0,
|
||||
width: 8,
|
||||
height: 8,
|
||||
},
|
||||
);
|
||||
frames.insert(
|
||||
"text/!.png".to_string(),
|
||||
MapperFrame {
|
||||
x: 16,
|
||||
y: 0,
|
||||
width: 8,
|
||||
height: 8,
|
||||
},
|
||||
);
|
||||
frames.insert(
|
||||
"text/-.png".to_string(),
|
||||
MapperFrame {
|
||||
x: 24,
|
||||
y: 0,
|
||||
width: 8,
|
||||
height: 8,
|
||||
},
|
||||
);
|
||||
frames.insert(
|
||||
"text/_double_quote.png".to_string(),
|
||||
MapperFrame {
|
||||
x: 32,
|
||||
y: 0,
|
||||
width: 8,
|
||||
height: 8,
|
||||
},
|
||||
);
|
||||
frames.insert(
|
||||
"text/_forward_slash.png".to_string(),
|
||||
MapperFrame {
|
||||
x: 40,
|
||||
y: 0,
|
||||
width: 8,
|
||||
height: 8,
|
||||
},
|
||||
);
|
||||
|
||||
let mapper = AtlasMapper { frames };
|
||||
// Note: In real tests, we'd need a proper texture, but for unit tests we can work around this
|
||||
unsafe { SpriteAtlas::new(std::mem::zeroed(), mapper) }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_text_texture_new() {
|
||||
let text_texture = TextTexture::new(1.0);
|
||||
assert_eq!(text_texture.scale(), 1.0);
|
||||
assert!(text_texture.char_map.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_text_texture_new_with_scale() {
|
||||
let text_texture = TextTexture::new(2.5);
|
||||
assert_eq!(text_texture.scale(), 2.5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_char_to_tile_name_letters() {
|
||||
let text_texture = TextTexture::new(1.0);
|
||||
|
||||
assert_eq!(text_texture.char_to_tile_name('A'), Some("text/A.png".to_string()));
|
||||
assert_eq!(text_texture.char_to_tile_name('Z'), Some("text/Z.png".to_string()));
|
||||
assert_eq!(text_texture.char_to_tile_name('a'), None); // lowercase not supported
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_char_to_tile_name_numbers() {
|
||||
let text_texture = TextTexture::new(1.0);
|
||||
|
||||
assert_eq!(text_texture.char_to_tile_name('0'), Some("text/0.png".to_string()));
|
||||
assert_eq!(text_texture.char_to_tile_name('9'), Some("text/9.png".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_char_to_tile_name_special_characters() {
|
||||
let text_texture = TextTexture::new(1.0);
|
||||
|
||||
assert_eq!(text_texture.char_to_tile_name('!'), Some("text/!.png".to_string()));
|
||||
assert_eq!(text_texture.char_to_tile_name('-'), Some("text/-.png".to_string()));
|
||||
assert_eq!(
|
||||
text_texture.char_to_tile_name('"'),
|
||||
Some("text/_double_quote.png".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
text_texture.char_to_tile_name('/'),
|
||||
Some("text/_forward_slash.png".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_char_to_tile_name_unsupported() {
|
||||
let text_texture = TextTexture::new(1.0);
|
||||
|
||||
assert_eq!(text_texture.char_to_tile_name(' '), None);
|
||||
assert_eq!(text_texture.char_to_tile_name('@'), None);
|
||||
assert_eq!(text_texture.char_to_tile_name('a'), None);
|
||||
assert_eq!(text_texture.char_to_tile_name('z'), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_set_scale() {
|
||||
let mut text_texture = TextTexture::new(1.0);
|
||||
assert_eq!(text_texture.scale(), 1.0);
|
||||
|
||||
text_texture.set_scale(3.0);
|
||||
assert_eq!(text_texture.scale(), 3.0);
|
||||
|
||||
text_texture.set_scale(0.5);
|
||||
assert_eq!(text_texture.scale(), 0.5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_text_width_empty_string() {
|
||||
let text_texture = TextTexture::new(1.0);
|
||||
assert_eq!(text_texture.text_width(""), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_text_width_single_character() {
|
||||
let text_texture = TextTexture::new(1.0);
|
||||
assert_eq!(text_texture.text_width("A"), 8); // 8 pixels per character at scale 1.0
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_text_width_multiple_characters() {
|
||||
let text_texture = TextTexture::new(1.0);
|
||||
assert_eq!(text_texture.text_width("ABC"), 24); // 3 * 8 = 24 pixels
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_text_width_with_scale() {
|
||||
let text_texture = TextTexture::new(2.0);
|
||||
assert_eq!(text_texture.text_width("A"), 16); // 8 * 2 = 16 pixels
|
||||
assert_eq!(text_texture.text_width("ABC"), 48); // 3 * 16 = 48 pixels
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_text_width_with_unsupported_characters() {
|
||||
let text_texture = TextTexture::new(1.0);
|
||||
// Only supported characters should be counted
|
||||
assert_eq!(text_texture.text_width("A B"), 16); // A and B only, space ignored
|
||||
assert_eq!(text_texture.text_width("A@B"), 16); // A and B only, @ ignored
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_text_height() {
|
||||
let text_texture = TextTexture::new(1.0);
|
||||
assert_eq!(text_texture.text_height(), 8); // 8 pixels per character at scale 1.0
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_text_height_with_scale() {
|
||||
let text_texture = TextTexture::new(2.0);
|
||||
assert_eq!(text_texture.text_height(), 16); // 8 * 2 = 16 pixels
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_text_height_with_fractional_scale() {
|
||||
let text_texture = TextTexture::new(1.5);
|
||||
assert_eq!(text_texture.text_height(), 12); // 8 * 1.5 = 12 pixels
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_char_tile_caching() {
|
||||
let mut text_texture = TextTexture::new(1.0);
|
||||
let atlas = create_mock_atlas();
|
||||
|
||||
// First call should cache the tile
|
||||
let tile1 = text_texture.get_char_tile(&atlas, 'A');
|
||||
assert!(tile1.is_some());
|
||||
|
||||
// Second call should use cached tile
|
||||
let tile2 = text_texture.get_char_tile(&atlas, 'A');
|
||||
assert!(tile2.is_some());
|
||||
|
||||
// Both should be the same tile
|
||||
assert_eq!(tile1.unwrap().pos, tile2.unwrap().pos);
|
||||
assert_eq!(tile1.unwrap().size, tile2.unwrap().size);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_char_tile_unsupported_character() {
|
||||
let mut text_texture = TextTexture::new(1.0);
|
||||
let atlas = create_mock_atlas();
|
||||
|
||||
let tile = text_texture.get_char_tile(&atlas, ' ');
|
||||
assert!(tile.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_char_tile_missing_from_atlas() {
|
||||
let mut text_texture = TextTexture::new(1.0);
|
||||
let atlas = create_mock_atlas();
|
||||
|
||||
// 'B' is not in our mock atlas
|
||||
let tile = text_texture.get_char_tile(&atlas, 'B');
|
||||
assert!(tile.is_none());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user