diff options
author | Guillaume Pasquet <dev@etenil.net> | 2019-11-17 02:37:21 +0000 |
---|---|---|
committer | Guillaume Pasquet <dev@etenil.net> | 2019-11-17 02:37:21 +0000 |
commit | 545a32a6830d722835ed3d739165f498b874b8d8 (patch) | |
tree | bfb75a4332bb10be7a06e749fc433abe90397a1a | |
parent | 1ffd583bea0a88b727cb0dacfbdcfbc31ed99ca5 (diff) |
Started writing unit tests.
-rw-r--r-- | src/entities.rs | 11 | ||||
-rw-r--r-- | src/world.rs | 88 |
2 files changed, 83 insertions, 16 deletions
diff --git a/src/entities.rs b/src/entities.rs index b0169c8..ab1c1a1 100644 --- a/src/entities.rs +++ b/src/entities.rs @@ -188,14 +188,3 @@ impl Player for Character { )
}
}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- fn test_attack() {
- let bob: Character = Enemy::new(String::from("Rogue"), 1, 4, 1, 4, (0, 0));
-
- assert_eq!(bob.attack(), 6);
- }
-}
diff --git a/src/world.rs b/src/world.rs index 328c15c..99e5ca9 100644 --- a/src/world.rs +++ b/src/world.rs @@ -1,11 +1,13 @@ use crate::entities::{Character, Enemy, Entity}; use crate::tiling::{TileGrid, TileType, Tileable}; use rand::Rng; -use std::cmp::min; +use std::cmp::{min, PartialEq}; +use std::fmt; pub type Point = (usize, usize); pub type Movement = (i8, i8); +#[derive(PartialEq)] enum CorridorType { Horizontal, Vertical, @@ -74,12 +76,29 @@ impl Tileable for Room { } } +#[derive(PartialEq)] struct Corridor { start: Point, length: usize, direction: CorridorType, } +impl fmt::Debug for Corridor { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{} corridor from ({},{}) of length {}", + match self.direction { + CorridorType::Horizontal => "Horizontal", + CorridorType::Vertical => "Vertical", + }, + self.start.0, + self.start.1, + self.length + ) + } +} + impl Corridor { fn new(start: Point, length: usize, direction: CorridorType) -> Corridor { Corridor { @@ -92,7 +111,7 @@ impl Corridor { pub fn make(start: Point, end: Point) -> Result<Corridor, String> { if start.0 != end.0 && start.1 != end.1 { return Err(String::from( - "Start and end points must be aligned to for a corridor", + "Start and end points must be aligned to make a corridor", )); } @@ -417,8 +436,67 @@ mod tests { use super::*; #[test] - fn test_generates_world() { - let mut level = Level::new(128, 128, 1, None); - level.generate(); + fn test_make_corridor_detects_horizontal() { + let start = (0, 0); + let end = (5, 0); + + let corridor = Corridor::make(start, end).unwrap(); + assert_eq!(corridor, Corridor::new(start, 5, CorridorType::Horizontal)); + } + + #[test] + fn test_make_corridor_detects_vertical() { + let start = (0, 0); + let end = (0, 5); + + let corridor = Corridor::make(start, end).unwrap(); + assert_eq!(corridor, Corridor::new(start, 5, CorridorType::Vertical)); + } + + #[test] + #[should_panic] + fn test_make_corridor_with_overlapping_points_should_panic() { + Corridor::make((0, 0), (0, 0)).unwrap(); + } + + #[test] + #[should_panic] + fn test_make_corridor_with_misaligned_points_should_panic() { + Corridor::make((3, 3), (5, 5)).unwrap(); + } + + #[test] + fn test_link_corridors_returns_a_vec_of_corridors() { + let cor = Corridor::link((0, 0), (5, 5)).unwrap(); + + let exp_horz = vec![ + Corridor::new((0, 0), 5, CorridorType::Horizontal), + Corridor::new((5, 0), 5, CorridorType::Vertical) + ]; + let exp_vert = vec![ + Corridor::new((0, 0), 5, CorridorType::Vertical), + Corridor::new((0, 5), 5, CorridorType::Horizontal) + ]; + + match cor[0].direction { + CorridorType::Horizontal => assert_eq!(cor, exp_horz), + CorridorType::Vertical => assert_eq!(cor, exp_vert) + }; + } + + #[test] + fn test_link_corridors_with_horizontal_aligned_points_returns_one_corridor() { + let cor = Corridor::link((0, 0), (5, 0)).unwrap(); + + assert_eq!(cor.len(), 1); + assert_eq!(cor[0], Corridor::new((0, 0), 5, CorridorType::Horizontal)); + } + + #[test] + fn test_link_corridors_with_vertical_aligned_points_returns_one_corridor() { + let cor = Corridor::link((0, 0), (0, 5)).unwrap(); + + assert_eq!(cor.len(), 1); + assert_eq!(cor[0], Corridor::new((0, 0), 5, CorridorType::Vertical)); } } |