mirror of
https://github.com/cccb/servicepoint.git
synced 2025-01-18 10:00:14 +01:00
add unit tests
This commit is contained in:
parent
ab82900414
commit
be17319993
|
@ -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))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ pub type Cp437Grid = PrimitiveGrid<u8>;
|
|||
pub type CharGrid = PrimitiveGrid<char>;
|
||||
|
||||
/// 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)]
|
||||
|
|
|
@ -76,15 +76,9 @@ pub trait Grid<T> {
|
|||
///
|
||||
/// 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})");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue