Skip to content

Commit

Permalink
Add tests to ragnarok_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
vE5li committed Apr 18, 2024
1 parent 1dbd2bc commit 6c2d12a
Show file tree
Hide file tree
Showing 7 changed files with 388 additions and 377 deletions.
2 changes: 1 addition & 1 deletion korangar/src/graphics/renderers/picker/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl From<PickerTarget> for u32 {

#[cfg(test)]
#[allow(clippy::unusual_byte_groupings)]
mod test {
mod encoding {
use ragnarok_networking::EntityId;

use crate::graphics::PickerTarget;
Expand Down
2 changes: 1 addition & 1 deletion korangar/src/system/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl GameTimer {
}

#[cfg(test)]
mod test {
mod increment {
use super::*;

#[test]
Expand Down
176 changes: 106 additions & 70 deletions ragnarok_bytes/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,73 +62,109 @@ impl std::fmt::Debug for ConversionError {
}
}

// #[cfg(test)]
// mod instanciate {
// use super::{ConversionError, ConversionErrorType};
//
// #[test]
// fn from_error_type() {
// let error_type = ConversionErrorType::ByteStreamTooShort { type_name:
// "test" }; let error =
// ConversionError::from_error_type(error_type.clone());
//
// assert_eq!(error.error_type, error_type);
// assert!(error.stack.is_empty());
// }
//
// #[test]
// fn from_message() {
// let message = "test".to_owned();
// let error = ConversionError::from_message(message.clone());
//
// assert_eq!(error.error_type, ConversionErrorType::Specific { message
// }); assert!(error.stack.is_empty());
// }
// }
//
// #[cfg(test)]
// mod add_to_stack {
// use super::{ConversionError, ConversionErrorType};
//
// const FIRST: &str = "first";
// const SECOND: &str = "second";
// const THIRD: &str = "third";
//
// #[test]
// fn empty() {
// let error_type = ConversionErrorType::ByteStreamTooShort { type_name:
// "test" }; let mut error =
// ConversionError::from_error_type(error_type.clone());
//
// error.add_to_stack(FIRST);
//
// assert_eq!(error.stack, vec![FIRST]);
// }
//
// #[test]
// fn multiple() {
// let error_type = ConversionErrorType::ByteStreamTooShort { type_name:
// "test" }; let mut error =
// ConversionError::from_error_type(error_type.clone());
//
// error.add_to_stack(THIRD);
// error.add_to_stack(SECOND);
// error.add_to_stack(FIRST);
//
// assert_eq!(error.stack, vec![FIRST, SECOND, THIRD]);
// }
// }
//
// #[cfg(test)]
// mod type_check {
// use super::{ConversionError, ConversionErrorType};
//
// #[test]
// fn is_byte_stream_too_short() {
// let error_type = ConversionErrorType::ByteStreamTooShort { type_name:
// "test" }; let error =
// ConversionError::from_error_type(error_type.clone());
//
// assert!(error.is_byte_stream_too_short());
// }
// }
/// Result type returned by any conversion operation.
pub type ConversionResult<T> = Result<T, Box<ConversionError>>;

/// Trait providing stack track helpers to [`ConversionResult`]
pub trait ConversionResultExt {
/// Add a type name to the stack trace.
fn trace<CALLER>(self) -> Self;
}

impl<T> ConversionResultExt for ConversionResult<T> {
fn trace<CALLER>(self) -> Self {
self.map_err(|mut error| {
error.add_to_stack(std::any::type_name::<CALLER>());
error
})
}
}

#[cfg(test)]
mod conversion_result {
use super::{ConversionError, ConversionResult, ConversionResultExt};

struct Dummy {}

#[test]
fn trace_ok() {
let result = Ok(()).trace::<Dummy>();
assert!(result.is_ok());
}

#[test]
fn trace_err() {
let result: ConversionResult<()> = Err(ConversionError::from_message("test"));
let result = result.trace::<Dummy>();

assert!(result.is_err());
assert!(format!("{:?}", result.unwrap_err()).contains(std::any::type_name::<Dummy>()));
}
}

#[cfg(test)]
mod instanciate {
use super::{ConversionError, ConversionErrorType};

#[test]
fn from_error_type() {
let error_type = ConversionErrorType::ByteStreamTooShort { type_name: "test" };
let error = ConversionError::from_error_type(error_type.clone());

assert_eq!(error.error_type, error_type);
assert!(error.stack.is_empty());
}

#[test]
fn from_message() {
let message = "test".to_owned();
let error = ConversionError::from_message(message.clone());

assert_eq!(error.error_type, ConversionErrorType::Specific { message });
assert!(error.stack.is_empty());
}
}

#[cfg(test)]
mod add_to_stack {
use super::{ConversionError, ConversionErrorType};

const FIRST: &str = "first";
const SECOND: &str = "second";
const THIRD: &str = "third";

#[test]
fn empty() {
let error_type = ConversionErrorType::ByteStreamTooShort { type_name: "test" };
let mut error = ConversionError::from_error_type(error_type.clone());

error.add_to_stack(FIRST);

assert_eq!(error.stack, vec![FIRST]);
}

#[test]
fn multiple() {
let error_type = ConversionErrorType::ByteStreamTooShort { type_name: "test" };
let mut error = ConversionError::from_error_type(error_type.clone());

error.add_to_stack(THIRD);
error.add_to_stack(SECOND);
error.add_to_stack(FIRST);

assert_eq!(error.stack, vec![FIRST, SECOND, THIRD]);
}
}

#[cfg(test)]
mod type_check {
use super::{ConversionError, ConversionErrorType};

#[test]
fn is_byte_stream_too_short() {
let error_type = ConversionErrorType::ByteStreamTooShort { type_name: "test" };
let error = ConversionError::from_error_type(error_type.clone());

assert!(error.is_byte_stream_too_short());
}
}
61 changes: 61 additions & 0 deletions ragnarok_bytes/src/from_bytes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,64 @@ where
Ok(value)
}
}

#[cfg(test)]
mod from_n_bytes {
use super::FromBytes;
use crate::{ByteStream, FromBytesExt};

struct Test;

const TEST_BYTE_SIZE: usize = 4;

impl FromBytes for Test {
fn from_bytes<META>(byte_stream: &mut crate::ByteStream<META>) -> crate::ConversionResult<Self>
where
Self: Sized,
{
byte_stream.slice::<Self>(TEST_BYTE_SIZE).map(|_| Test)
}
}

#[test]
fn data_saturated() {
let mut byte_stream = ByteStream::<()>::without_metadata(&[0u8; TEST_BYTE_SIZE]);
let result = Test::from_n_bytes(&mut byte_stream, TEST_BYTE_SIZE);

assert!(result.is_ok());
assert!(byte_stream.is_empty());
}

#[test]
fn data_left() {
let mut byte_stream = ByteStream::<()>::without_metadata(&[0u8; TEST_BYTE_SIZE * 2]);
let result = Test::from_n_bytes(&mut byte_stream, TEST_BYTE_SIZE);

assert!(result.is_ok());
assert_eq!(byte_stream.remaining_bytes().len(), TEST_BYTE_SIZE);
}

#[test]
fn data_missing() {
let mut byte_stream = ByteStream::<()>::without_metadata(&[0u8; TEST_BYTE_SIZE]);
let result = Test::from_n_bytes(&mut byte_stream, TEST_BYTE_SIZE * 2);

assert!(result.is_err());

// NOTE: This assert is checking an implementation detail that might well change
// in the future.
assert_eq!(byte_stream.remaining_bytes().len(), TEST_BYTE_SIZE);
}

#[test]
fn incorrect_size() {
let mut byte_stream = ByteStream::<()>::without_metadata(&[0u8; TEST_BYTE_SIZE]);
let result = Test::from_n_bytes(&mut byte_stream, TEST_BYTE_SIZE / 2);

assert!(result.is_err());

// NOTE: This assert is checking an implementation detail that might well change
// in the future.
assert_eq!(byte_stream.remaining_bytes().len(), TEST_BYTE_SIZE / 2);
}
}
Loading

0 comments on commit 6c2d12a

Please sign in to comment.