From be17319993bdc4e5db754c51dd3a2830d64be17c Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 13 Oct 2024 16:51:32 +0200 Subject: [PATCH] add unit tests --- crates/servicepoint/src/command.rs | 26 +++++++++++++++++++++++++- crates/servicepoint/src/cp437.rs | 13 ++++++++++++- crates/servicepoint/src/grid.rs | 14 ++++---------- 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/crates/servicepoint/src/command.rs b/crates/servicepoint/src/command.rs index 8422971..210ab01 100644 --- a/crates/servicepoint/src/command.rs +++ b/crates/servicepoint/src/command.rs @@ -495,7 +495,8 @@ mod tests { command_code::CommandCode, origin::Pixels, packet::{Header, Packet}, - Brightness, Command, CompressionCode, Origin, PixelGrid, PrimitiveGrid, + Brightness, BrightnessGrid, Command, CompressionCode, Origin, + PixelGrid, PrimitiveGrid, }; fn round_trip(original: Command) { @@ -902,4 +903,27 @@ mod tests { Origin::new(1, 0) + Origin::new(3, 2) ); } + #[test] + fn packet_into_char_brightness_invalid() { + let grid = BrightnessGrid::new(2, 2); + let command = Command::CharBrightness(Origin::ZERO, grid); + let mut packet: Packet = command.into(); + let slot = packet.payload.get_mut(1).unwrap(); + *slot = 23; + assert_eq!( + Command::try_from(packet), + Err(TryFromPacketError::InvalidBrightness(23)) + ); + } + + #[test] + fn packet_into_brightness_invalid() { + let mut packet: Packet = Command::Brightness(Brightness::MAX).into(); + let slot = packet.payload.get_mut(0).unwrap(); + *slot = 42; + assert_eq!( + Command::try_from(packet), + Err(TryFromPacketError::InvalidBrightness(42)) + ); + } } diff --git a/crates/servicepoint/src/cp437.rs b/crates/servicepoint/src/cp437.rs index f779025..1a4b25d 100644 --- a/crates/servicepoint/src/cp437.rs +++ b/crates/servicepoint/src/cp437.rs @@ -14,7 +14,7 @@ pub type Cp437Grid = PrimitiveGrid; pub type CharGrid = PrimitiveGrid; /// Errors that can occur when loading CP-437. -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub enum Cp437LoadError { /// Invalid character in input prevented loading InvalidChar { @@ -232,6 +232,17 @@ mod tests { // line break will be added assert_eq!(actual, expected); } + + #[test] + fn load_ascii_invalid() { + assert_eq!( + Err(Cp437LoadError::InvalidChar { + char: '🥶', + index: 2 + }), + Cp437Grid::load_ascii("?#🥶42", 3, false) + ); + } } #[cfg(test)] diff --git a/crates/servicepoint/src/grid.rs b/crates/servicepoint/src/grid.rs index d367d98..68fe102 100644 --- a/crates/servicepoint/src/grid.rs +++ b/crates/servicepoint/src/grid.rs @@ -76,15 +76,9 @@ pub trait Grid { /// /// When the specified position is out of bounds for this grid. fn assert_in_bounds(&self, x: usize, y: usize) { - assert!( - x < self.width(), - "cannot access index [{x}, {y}] because x is outside of bounds 0..{}", - self.width() - 1 - ); - assert!( - y < self.height(), - "cannot access index [{x}, {y}] because y is outside of bounds 0..{}", - self.height() - 1 - ); + let width = self.width(); + assert!(x < width, "cannot access index [{x}, {y}] because x is outside of bounds [0..{width})"); + let height = self.height(); + assert!(y < height, "cannot access index [{x}, {y}] because x is outside of bounds [0..{height})"); } }