Skip to content

Commit

Permalink
test(ssg): ✅ improved test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed Sep 24, 2024
1 parent 4e4d5b1 commit ae85799
Showing 1 changed file with 113 additions and 8 deletions.
121 changes: 113 additions & 8 deletions ssg-rss/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,29 @@ pub enum RssError {
/// Result type for RSS operations.
pub type Result<T> = std::result::Result<T, RssError>;

impl RssError {
/// Creates a new `RssError::MissingField` error.
///
/// # Arguments
///
/// * `field_name` - The name of the missing field.
///
/// # Returns
///
/// Returns a new `RssError::MissingField` instance.
pub fn missing_field<S: Into<String>>(field_name: S) -> Self {
RssError::MissingField(field_name.into())
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::error::Error;

#[test]
fn test_rss_error_display() {
let error = RssError::MissingField("title".to_string());
let error = RssError::missing_field("title");
assert_eq!(error.to_string(), "Missing required field: title");
}

Expand All @@ -50,19 +66,108 @@ mod tests {

#[test]
fn test_utf8_error() {
let utf8_error =
String::from_utf8(vec![0, 159, 146, 150]).unwrap_err();
let utf8_error = String::from_utf8(vec![0, 159, 146, 150]).unwrap_err();
let error = RssError::Utf8Error(utf8_error);
assert!(error
.to_string()
.starts_with("UTF-8 conversion error:"));
assert!(error.to_string().starts_with("UTF-8 conversion error:"));
}

#[test]
fn test_io_error() {
let io_error =
io::Error::new(io::ErrorKind::NotFound, "File not found");
let io_error = io::Error::new(io::ErrorKind::NotFound, "File not found");
let error = RssError::IoError(io_error);
assert!(error.to_string().starts_with("I/O error:"));
}

#[test]
fn test_error_is_send_and_sync() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<RssError>();
}

#[test]
fn test_error_source() {
let io_error = io::Error::new(io::ErrorKind::NotFound, "File not found");
let error = RssError::IoError(io_error);
assert!(error.source().is_some());
}

// New tests start here

#[test]
fn test_missing_field_with_string() {
let error = RssError::missing_field(String::from("author"));
assert_eq!(error.to_string(), "Missing required field: author");
}

#[test]
fn test_missing_field_with_str() {
let error = RssError::missing_field("description");
assert_eq!(error.to_string(), "Missing required field: description");
}

#[test]
fn test_xml_write_error_details() {
let xml_error = quick_xml::Error::Io(std::sync::Arc::new(
io::Error::new(io::ErrorKind::PermissionDenied, "Permission denied"),
));
let error = RssError::XmlWriteError(xml_error);
assert!(error.to_string().contains("Permission denied"));
}

#[test]
fn test_utf8_error_details() {
let utf8_error = String::from_utf8(vec![0xFF, 0xFF]).unwrap_err();
let error = RssError::Utf8Error(utf8_error);
assert!(error.to_string().contains("invalid utf-8 sequence"));
}

#[test]
fn test_io_error_details() {
let io_error = io::Error::new(io::ErrorKind::AddrInUse, "Address already in use");
let error = RssError::IoError(io_error);
assert!(error.to_string().contains("Address already in use"));
}

#[test]
fn test_error_downcast() {
let error: Box<dyn Error> = Box::new(RssError::missing_field("category"));
let downcast_result = error.downcast::<RssError>();
assert!(downcast_result.is_ok());
}

#[test]
fn test_error_chain() {
let io_error = io::Error::new(io::ErrorKind::Other, "Underlying IO error");
let xml_error = quick_xml::Error::Io(std::sync::Arc::new(io_error));
let error = RssError::XmlWriteError(xml_error);

let mut error_chain = error.source();
assert!(error_chain.is_some());
error_chain = error_chain.unwrap().source();
assert!(error_chain.is_some());
assert_eq!(error_chain.unwrap().to_string(), "Underlying IO error");
}

#[test]
fn test_from_quick_xml_error() {
let xml_error = quick_xml::Error::Io(std::sync::Arc::new(
io::Error::new(io::ErrorKind::UnexpectedEof, "Unexpected EOF"),
));
let error: RssError = xml_error.into();
assert!(matches!(error, RssError::XmlWriteError(_)));
}

#[test]
fn test_from_utf8_error() {
let utf8_error = String::from_utf8(vec![0, 159, 146, 150]).unwrap_err();
let error: RssError = utf8_error.into();
assert!(matches!(error, RssError::Utf8Error(_)));
}

#[test]
fn test_from_io_error() {
let io_error = io::Error::new(io::ErrorKind::NotFound, "File not found");
let error: RssError = io_error.into();
assert!(matches!(error, RssError::IoError(_)));
}
}

0 comments on commit ae85799

Please sign in to comment.