Skip to content

Commit

Permalink
Remove should_panic unit tests #307 (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
u5surf authored Jun 24, 2021
1 parent 718bbed commit 29d904b
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 42 deletions.
7 changes: 4 additions & 3 deletions sdk/core/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use http::StatusCode;
#[cfg(feature = "enable_hyper")]
use hyper::{self, body, Body};
use std::cmp::PartialEq;

#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -44,7 +45,7 @@ type HttpClientError = hyper::Error;
type HttpClientError = reqwest::Error;

#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
#[derive(Debug, PartialEq, thiserror::Error)]
pub enum ParsingError {
#[error("unknown variant of {item} found: \"{variant}\"")]
UnknownVariant { item: &'static str, variant: String },
Expand Down Expand Up @@ -167,7 +168,7 @@ impl HttpError {
}
}
}
#[derive(Debug, thiserror::Error)]
#[derive(Debug, PartialEq, thiserror::Error)]
pub enum Not512ByteAlignedError {
#[error("start range not 512-byte aligned: {0}")]
StartRange(u64),
Expand All @@ -186,7 +187,7 @@ pub enum PermissionError {
},
}

#[derive(Debug, thiserror::Error)]
#[derive(Debug, PartialEq, thiserror::Error)]
pub enum Parse512AlignedError {
#[error("split not found")]
SplitNotFound,
Expand Down
11 changes: 9 additions & 2 deletions sdk/core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ macro_rules! response_from_headers {

#[cfg(test)]
mod test {
use crate::ParsingError;
create_enum!(Colors, (Black, "Black"), (White, "White"), (Red, "Red"));
create_enum!(ColorsMonochrome, (Black, "Black"), (White, "White"));

Expand Down Expand Up @@ -206,9 +207,15 @@ mod test {
}

#[test]
#[should_panic(expected = "UnknownVariant { item: \"ColorsMonochrome\", variant: \"Red\" }")]
fn test_color_parse_err_1() {
"Red".parse::<ColorsMonochrome>().unwrap();
let err = "Red".parse::<ColorsMonochrome>().unwrap_err();
assert_eq!(
err,
ParsingError::UnknownVariant {
item: "ColorsMonochrome",
variant: "Red".to_string()
}
);
}

#[test]
Expand Down
23 changes: 15 additions & 8 deletions sdk/core/src/request_options/ba512_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ impl fmt::Display for BA512Range {
#[cfg(test)]
mod test {
use super::*;
use crate::Not512ByteAlignedError::{EndRange, StartRange};

#[test]
fn test_512range_parse() {
Expand All @@ -102,27 +103,33 @@ mod test {
}

#[test]
#[should_panic(expected = "ParseIntError(ParseIntError { kind: InvalidDigit })")]
fn test_512range_parse_panic_1() {
"abba/2000".parse::<BA512Range>().unwrap();
let err = "abba/2000".parse::<BA512Range>().unwrap_err();
assert!(matches!(err, Parse512AlignedError::ParseIntError(_)));
}

#[test]
#[should_panic(expected = "SplitNotFound")]
fn test_512range_parse_panic_2() {
"1000-2000".parse::<BA512Range>().unwrap();
let err = "1000-2000".parse::<BA512Range>().unwrap_err();
assert_eq!(err, Parse512AlignedError::SplitNotFound);
}

#[test]
#[should_panic(expected = "Not512ByteAlignedError(StartRange(7))")]
fn test_512range_invalid_start_range() {
"7/511".parse::<BA512Range>().unwrap();
let err = "7/511".parse::<BA512Range>().unwrap_err();
assert_eq!(
err,
Parse512AlignedError::Not512ByteAlignedError(StartRange(7))
);
}

#[test]
#[should_panic(expected = "Not512ByteAlignedError(EndRange(100))")]
fn test_512range_invalid_end_range() {
"0/100".parse::<BA512Range>().unwrap();
let err = "0/100".parse::<BA512Range>().unwrap_err();
assert_eq!(
err,
Parse512AlignedError::Not512ByteAlignedError(EndRange(100))
);
}

#[test]
Expand Down
39 changes: 27 additions & 12 deletions sdk/core/src/request_options/content_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,27 +107,42 @@ mod test {
}

#[test]
#[should_panic(
expected = "TokenNotFound { item: \"ContentRange\", token: \"bytes \", full: \"something else\" }"
)]
fn test_parse_no_starting_token() {
"something else".parse::<ContentRange>().unwrap();
let err = "something else".parse::<ContentRange>().unwrap_err();
assert_eq!(
err,
ParsingError::TokenNotFound {
item: "ContentRange",
token: "bytes ".to_string(),
full: "something else".to_string()
}
);
}

#[test]
#[should_panic(
expected = "TokenNotFound { item: \"ContentRange\", token: \"-\", full: \"bytes 100\""
)]
fn test_parse_no_dash() {
"bytes 100".parse::<ContentRange>().unwrap();
let err = "bytes 100".parse::<ContentRange>().unwrap_err();
assert_eq!(
err,
ParsingError::TokenNotFound {
item: "ContentRange",
token: "-".to_string(),
full: "bytes 100".to_string()
}
);
}

#[test]
#[should_panic(
expected = "TokenNotFound { item: \"ContentRange\", token: \"/\", full: \"bytes 100-500\""
)]
fn test_parse_no_slash() {
"bytes 100-500".parse::<ContentRange>().unwrap();
let err = "bytes 100-500".parse::<ContentRange>().unwrap_err();
assert_eq!(
err,
ParsingError::TokenNotFound {
item: "ContentRange",
token: "/".to_string(),
full: "bytes 100-500".to_string()
}
);
}

#[test]
Expand Down
15 changes: 11 additions & 4 deletions sdk/core/src/request_options/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,22 @@ mod test {
}

#[test]
#[should_panic(expected = "ParseIntError(ParseIntError { kind: InvalidDigit })")]
fn test_range_parse_panic_1() {
"abba/2000".parse::<Range>().unwrap();
let err = "abba/2000".parse::<Range>().unwrap_err();
assert!(matches!(err, ParsingError::ParseIntError(_)));
}

#[test]
#[should_panic(expected = "TokenNotFound")]
fn test_range_parse_panic_2() {
"1000-2000".parse::<Range>().unwrap();
let err = "1000-2000".parse::<Range>().unwrap_err();
assert_eq!(
err,
ParsingError::TokenNotFound {
item: "Range",
token: "/".to_string(),
full: "1000-2000".to_string()
}
);
}

#[test]
Expand Down
3 changes: 2 additions & 1 deletion sdk/storage/src/core/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cmp::PartialEq;
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum Error {
Expand Down Expand Up @@ -69,7 +70,7 @@ pub enum Error {
}

#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
#[derive(Debug, PartialEq, thiserror::Error)]
pub enum AzurePathParseError {
#[error("path separator not found")]
PathSeparatorNotFoundError,
Expand Down
24 changes: 12 additions & 12 deletions sdk/storage/src/core/into_azure_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,38 +72,38 @@ mod test {
}

#[test]
#[should_panic(expected = "PathSeparatorNotFoundError")]
fn no_slash() {
let path = "containerblob";
let p = &path;
assert!(p.container_name().unwrap() == "container");
assert!(path.blob_name().unwrap() == "blob");
let err = AzurePathParseError::PathSeparatorNotFoundError;
assert_eq!(p.container_name().unwrap_err(), err);
assert_eq!(path.blob_name().unwrap_err(), err);
}

#[test]
#[should_panic(expected = "MultiplePathSeparatorsFoundError")]
fn three_slashes() {
let path = "container/blob/extra";
let p = &path;
assert!(p.container_name().unwrap() == "container");
assert!(path.blob_name().unwrap() == "blob");
let err = AzurePathParseError::MultiplePathSeparatorsFoundError;
assert_eq!(p.container_name().unwrap_err(), err);
assert_eq!(path.blob_name().unwrap_err(), err);
}

#[test]
#[should_panic(expected = "MissingContainerError")]
fn missing_container() {
let path = "/blob";
let p = &path;
assert!(p.container_name().unwrap() == "container");
assert!(path.blob_name().unwrap() == "blob");
let err = AzurePathParseError::MissingContainerError;
assert_eq!(p.container_name().unwrap_err(), err);
assert_eq!(path.blob_name().unwrap_err(), err);
}

#[test]
#[should_panic(expected = "MissingBlobError")]
fn missing_blob() {
let path = "container/";
let p = &path;
assert!(p.container_name().unwrap() == "container");
assert!(path.blob_name().unwrap() == "blob");
let err = AzurePathParseError::MissingBlobError;
assert_eq!(p.container_name().unwrap_err(), err);
assert_eq!(path.blob_name().unwrap_err(), err);
}
}

0 comments on commit 29d904b

Please sign in to comment.