boolean tests

This commit is contained in:
AF 2023-04-25 00:07:14 +00:00
parent 8d74cd6f3f
commit 77846511b2
2 changed files with 33 additions and 2 deletions

View File

@ -11,7 +11,7 @@ impl Serializable for bool {
} }
} }
#[derive(Debug)] #[derive(Debug, PartialEq)]
pub enum BooleanParseError { pub enum BooleanParseError {
OutOfBounds(u8), OutOfBounds(u8),
Eof, Eof,
@ -63,3 +63,34 @@ impl Atomic for bool {
impl ConstSizeAtomic for bool { impl ConstSizeAtomic for bool {
const SIZE: usize = 1; const SIZE: usize = 1;
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn can_parse_false() {
assert_eq!(bool::parse_slice(&[0]).unwrap(), false)
}
#[test]
fn can_parse_true() {
assert_eq!(bool::parse_slice(&[1]).unwrap(), true)
}
#[test]
fn cannot_parse_out_of_bound_value() {
assert_eq!(bool::parse_slice(&[2]).unwrap_err(), BooleanParseError::OutOfBounds(2));
assert_eq!(bool::parse_slice(&[2, 0]).unwrap_err(), BooleanParseError::OutOfBounds(2));
}
#[test]
fn cannot_parse_empty_slice() {
assert_eq!(bool::parse_slice(&[]).unwrap_err(), BooleanParseError::Eof);
}
#[test]
fn cannot_parse_extra_tail() {
assert_eq!(bool::parse_slice(&[0, 10, 20, 30]).unwrap_err(), BooleanParseError::ExtraData(3));
}
}

View File

@ -61,7 +61,7 @@ mod tests {
use crate::std::*; use crate::std::*;
#[test] #[test]
fn test_plain() -> Result<(), PlainParseError> { fn can_parse_plain_slice() -> Result<(), PlainParseError> {
let plain = Plain::parse_slice(b"slice")?; let plain = Plain::parse_slice(b"slice")?;
assert_eq!(plain.bytes().as_slice(), b"slice"); assert_eq!(plain.bytes().as_slice(), b"slice");
Ok(()) Ok(())