add unit tests

This commit is contained in:
Vinzenz Schroeter 2024-10-13 16:51:32 +02:00
parent ab82900414
commit be17319993
3 changed files with 41 additions and 12 deletions

View file

@ -495,7 +495,8 @@ mod tests {
command_code::CommandCode, command_code::CommandCode,
origin::Pixels, origin::Pixels,
packet::{Header, Packet}, packet::{Header, Packet},
Brightness, Command, CompressionCode, Origin, PixelGrid, PrimitiveGrid, Brightness, BrightnessGrid, Command, CompressionCode, Origin,
PixelGrid, PrimitiveGrid,
}; };
fn round_trip(original: Command) { fn round_trip(original: Command) {
@ -902,4 +903,27 @@ mod tests {
Origin::new(1, 0) + Origin::new(3, 2) 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))
);
}
} }

View file

@ -14,7 +14,7 @@ pub type Cp437Grid = PrimitiveGrid<u8>;
pub type CharGrid = PrimitiveGrid<char>; pub type CharGrid = PrimitiveGrid<char>;
/// Errors that can occur when loading CP-437. /// Errors that can occur when loading CP-437.
#[derive(Debug)] #[derive(Debug, PartialEq)]
pub enum Cp437LoadError { pub enum Cp437LoadError {
/// Invalid character in input prevented loading /// Invalid character in input prevented loading
InvalidChar { InvalidChar {
@ -232,6 +232,17 @@ mod tests {
// line break will be added // line break will be added
assert_eq!(actual, expected); 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)] #[cfg(test)]

View file

@ -76,15 +76,9 @@ pub trait Grid<T> {
/// ///
/// When the specified position is out of bounds for this grid. /// When the specified position is out of bounds for this grid.
fn assert_in_bounds(&self, x: usize, y: usize) { fn assert_in_bounds(&self, x: usize, y: usize) {
assert!( let width = self.width();
x < self.width(), assert!(x < width, "cannot access index [{x}, {y}] because x is outside of bounds [0..{width})");
"cannot access index [{x}, {y}] because x is outside of bounds 0..{}", let height = self.height();
self.width() - 1 assert!(y < height, "cannot access index [{x}, {y}] because x is outside of bounds [0..{height})");
);
assert!(
y < self.height(),
"cannot access index [{x}, {y}] because y is outside of bounds 0..{}",
self.height() - 1
);
} }
} }