From 8d7e25aca6ce7f2bac94a91edf62c4aaa3d8048f Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Sun, 21 May 2023 17:20:01 +0200 Subject: [PATCH] Complete unit testing --- src/lib.rs | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fd84005..035d134 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,7 +28,7 @@ pub trait IMbid { fn mbid(&self) -> &str; } -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug)] enum UrlType { MusicBrainz, MusicButler, @@ -36,12 +36,17 @@ enum UrlType { Qobuz, } -#[derive(Debug, PartialEq, Eq)] struct InvalidUrlError { url_type: UrlType, url: String, } +impl fmt::Display for InvalidUrlError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "invalid url of type {:?}: {}", self.url_type, self.url) + } +} + /// MusicBrainz reference. #[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)] pub struct MusicBrainz(Url); @@ -422,7 +427,7 @@ impl From for Error { impl From for Error { fn from(err: InvalidUrlError) -> Error { - Error::InvalidUrlError(format!("{:?} - {}", err.url_type, err.url)) + Error::InvalidUrlError(err.to_string()) } } @@ -620,11 +625,27 @@ mod tests { assert_eq!(url, mb.url()); assert_eq!(uuid, mb.mbid()); + let url = format!("not a url at all"); + let expected_error: Error = url::ParseError::RelativeUrlWithoutBase.into(); + let actual_error = MusicBrainz::new(&url).unwrap_err(); + assert_eq!(actual_error, expected_error); + assert_eq!(actual_error.to_string(), expected_error.to_string()); + let url = format!("https://musicbrainz.org/artist/i-am-not-a-uuid"); - assert!(MusicBrainz::new(&url).is_err()); + let expected_error: Error = Uuid::try_parse("i-am-not-a-uuid").unwrap_err().into(); + let actual_error = MusicBrainz::new(&url).unwrap_err(); + assert_eq!(actual_error, expected_error); + assert_eq!(actual_error.to_string(), expected_error.to_string()); let url = format!("https://musicbrainz.org/artist"); - assert!(MusicBrainz::new(&url).is_err()); + let expected_error: Error = InvalidUrlError { + url_type: UrlType::MusicBrainz, + url: url.clone(), + } + .into(); + let actual_error = MusicBrainz::new(&url).unwrap_err(); + assert_eq!(actual_error, expected_error); + assert_eq!(actual_error.to_string(), expected_error.to_string()); } #[test]