From bf2a05f48893e9ef16cd50efefdeee9add1b93ee Mon Sep 17 00:00:00 2001 From: Cain <75994858+cainthebest@users.noreply.github.com> Date: Fri, 10 Mar 2023 21:27:30 +0100 Subject: [PATCH] [Tests] result, display, trait and cloning (#19) --- src/errors.rs | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index afefef1..a647123 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,13 +1,12 @@ - //! The library's possible errors. use std::fmt; -use std::{fmt::Formatter, error::Error}; +use std::{error::Error, fmt::Formatter}; /// Result of Type and GDError. pub type GDResult = Result; /// GameDig Error. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum GDError { /// The received packet was bigger than the buffer size. PacketOverflow, @@ -48,3 +47,44 @@ impl fmt::Display for GDError { write!(f, "{:?}", self) } } + +#[cfg(test)] +mod tests { + use super::*; + + // Testing Ok variant of the GDResult type + #[test] + fn test_gdresult_ok() { + let result: GDResult = Ok(42); + assert_eq!(result.unwrap(), 42); + } + + // Testing Err variant of the GDResult type + #[test] + fn test_gdresult_err() { + let result: GDResult = Err(GDError::InvalidInput); + assert_eq!(result.is_err(), true); + } + + // Testing the Display trait for the GDError type + #[test] + fn test_display() { + let error = GDError::PacketOverflow; + assert_eq!(format!("{}", error), "PacketOverflow"); + } + + // Testing the Error trait for the GDError type + #[test] + fn test_error_trait() { + let error = GDError::PacketBad; + assert!(error.source().is_none()); + } + + // Testing cloning the GDError type + #[test] + fn test_cloning() { + let error = GDError::BadGame(String::from("MyGame")); + let cloned_error = error.clone(); + assert_eq!(error, cloned_error); + } +}