From 485749dc1cee5ddedfbce3817f8a953f3ccb8587 Mon Sep 17 00:00:00 2001 From: u5surf Date: Wed, 23 Jun 2021 00:08:30 +0900 Subject: [PATCH] Remove should_panic unit tests #307 --- sdk/core/src/errors.rs | 9 +++---- sdk/core/src/macros.rs | 5 ++-- sdk/core/src/request_options/ba512_range.rs | 18 +++++++------- sdk/core/src/request_options/content_range.rs | 12 +++++----- sdk/core/src/request_options/range.rs | 8 +++---- sdk/storage/src/core/into_azure_path.rs | 24 +++++++++---------- .../autorust/codegen/src/config_parser.rs | 3 +-- 7 files changed, 41 insertions(+), 38 deletions(-) diff --git a/sdk/core/src/errors.rs b/sdk/core/src/errors.rs index 1d2b0f37a4..a58099a881 100644 --- a/sdk/core/src/errors.rs +++ b/sdk/core/src/errors.rs @@ -1,4 +1,5 @@ use http::StatusCode; +use std::cmp::PartialEq; #[cfg(feature = "enable_hyper")] use hyper::{self, body, Body}; #[cfg(feature = "enable_hyper")] @@ -7,7 +8,7 @@ type HttpClientError = hyper::Error; type HttpClientError = reqwest::Error; #[non_exhaustive] -#[derive(Debug, thiserror::Error)] +#[derive(Debug, PartialEq, thiserror::Error)] pub enum ParsingError { #[error("Element not found: {0}")] ElementNotFound(String), @@ -25,7 +26,7 @@ pub enum ParseError { } #[non_exhaustive] -#[derive(Debug, thiserror::Error)] +#[derive(Debug, PartialEq, thiserror::Error)] pub enum AzurePathParseError { #[error("Path separator not found")] PathSeparatorNotFoundError, @@ -138,7 +139,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), @@ -157,7 +158,7 @@ pub enum PermissionError { }, } -#[derive(Debug, thiserror::Error)] +#[derive(Debug, PartialEq, thiserror::Error)] pub enum Parse512AlignedError { #[error("split not found")] SplitNotFound, diff --git a/sdk/core/src/macros.rs b/sdk/core/src/macros.rs index eebbf95f38..75383fed12 100644 --- a/sdk/core/src/macros.rs +++ b/sdk/core/src/macros.rs @@ -170,6 +170,7 @@ macro_rules! response_from_headers { mod test { create_enum!(Colors, (Black, "Black"), (White, "White"), (Red, "Red")); create_enum!(ColorsMonochrome, (Black, "Black"), (White, "White")); + use crate::ParsingError; struct Options { a: Option, @@ -203,9 +204,9 @@ mod test { } #[test] - #[should_panic(expected = "ElementNotFound(\"Red\")")] fn test_color_parse_err_1() { - "Red".parse::().unwrap(); + let err = "Red".parse::().unwrap_err(); + assert_eq!(err, ParsingError::ElementNotFound("Red".to_string())); } #[test] diff --git a/sdk/core/src/request_options/ba512_range.rs b/sdk/core/src/request_options/ba512_range.rs index acec348d58..4607e63643 100644 --- a/sdk/core/src/request_options/ba512_range.rs +++ b/sdk/core/src/request_options/ba512_range.rs @@ -92,6 +92,8 @@ impl fmt::Display for BA512Range { #[cfg(test)] mod test { use super::*; + use crate::Not512ByteAlignedError::StartRange; + use crate::Not512ByteAlignedError::EndRange; #[test] fn test_512range_parse() { @@ -102,27 +104,27 @@ mod test { } #[test] - #[should_panic(expected = "ParseIntError(ParseIntError { kind: InvalidDigit })")] fn test_512range_parse_panic_1() { - "abba/2000".parse::().unwrap(); + let err = "abba/2000".parse::().unwrap_err(); + assert_eq!(err.to_string(), "parse int error: invalid digit found in string".to_string()); } #[test] - #[should_panic(expected = "SplitNotFound")] fn test_512range_parse_panic_2() { - "1000-2000".parse::().unwrap(); + let err = "1000-2000".parse::().unwrap_err(); + assert_eq!(err, Parse512AlignedError::SplitNotFound); } #[test] - #[should_panic(expected = "Not512ByteAlignedError(StartRange(7))")] fn test_512range_invalid_start_range() { - "7/511".parse::().unwrap(); + let err ="7/511".parse::().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::().unwrap(); + let err = "0/100".parse::().unwrap_err(); + assert_eq!(err, Parse512AlignedError::Not512ByteAlignedError(EndRange(100))); } #[test] diff --git a/sdk/core/src/request_options/content_range.rs b/sdk/core/src/request_options/content_range.rs index 2375ea5b36..18d1f35974 100644 --- a/sdk/core/src/request_options/content_range.rs +++ b/sdk/core/src/request_options/content_range.rs @@ -95,21 +95,21 @@ mod test { } #[test] - #[should_panic(expected = "TokenNotFound(\"bytes \")")] fn test_parse_no_starting_token() { - "something else".parse::().unwrap(); + let err = "something else".parse::().unwrap_err(); + assert_eq!(err, ParseError::TokenNotFound("bytes ".to_string())); } #[test] - #[should_panic(expected = "SplitNotFound('-')")] fn test_parse_no_dash() { - "bytes 100".parse::().unwrap(); + let err = "bytes 100".parse::().unwrap_err(); + assert_eq!(err, ParseError::SplitNotFound('-')); } #[test] - #[should_panic(expected = "SplitNotFound('/')")] fn test_parse_no_slash() { - "bytes 100-500".parse::().unwrap(); + let err = "bytes 100-500".parse::().unwrap_err(); + assert_eq!(err, ParseError::SplitNotFound('/')); } #[test] diff --git a/sdk/core/src/request_options/range.rs b/sdk/core/src/request_options/range.rs index 3c23ecc718..074059777d 100644 --- a/sdk/core/src/request_options/range.rs +++ b/sdk/core/src/request_options/range.rs @@ -119,15 +119,15 @@ mod test { } #[test] - #[should_panic(expected = "ParseIntError(ParseIntError { kind: InvalidDigit })")] fn test_range_parse_panic_1() { - "abba/2000".parse::().unwrap(); + let err = "abba/2000".parse::().unwrap_err(); + assert_eq!(err.to_string(), "Parse int error invalid digit found in string".to_string()); } #[test] - #[should_panic(expected = "SplitNotFound")] fn test_range_parse_panic_2() { - "1000-2000".parse::().unwrap(); + let err = "1000-2000".parse::(); + assert_eq!(err, Err(ParseError::SplitNotFound('/'))); } #[test] diff --git a/sdk/storage/src/core/into_azure_path.rs b/sdk/storage/src/core/into_azure_path.rs index f974a0d514..c1bb152500 100644 --- a/sdk/storage/src/core/into_azure_path.rs +++ b/sdk/storage/src/core/into_azure_path.rs @@ -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 _e = AzurePathParseError::PathSeparatorNotFoundError; + assert_eq!(p.container_name().unwrap_err(), _e); + assert_eq!(p.blob_name().unwrap_err(), _e); } #[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 _e = AzurePathParseError::MultiplePathSeparatorsFoundError; + assert_eq!(p.container_name().unwrap_err(), _e); + assert_eq!(p.blob_name().unwrap_err(), _e); } #[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 _e = AzurePathParseError::MissingContainerError; + assert_eq!(p.container_name().unwrap_err(), _e); + assert_eq!(p.blob_name().unwrap_err(), _e); } #[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 _e = AzurePathParseError::MissingBlobError; + assert_eq!(p.container_name().unwrap_err(), _e); + assert_eq!(p.blob_name().unwrap_err(), _e); } } diff --git a/services/autorust/codegen/src/config_parser.rs b/services/autorust/codegen/src/config_parser.rs index 1e1296d9a6..424958ee41 100644 --- a/services/autorust/codegen/src/config_parser.rs +++ b/services/autorust/codegen/src/config_parser.rs @@ -302,7 +302,6 @@ input-file: } #[test] - #[should_panic] fn literate_config_should_fail_for_invalid_heading() { let invalid_input = " ## INVALID_HEADING @@ -316,7 +315,7 @@ input-file: - Microsoft.Storage/stable/2019-06-01/storage.json ``` "; - parse_configurations_from_cmark_config(invalid_input); + assert!(parse_configurations_from_cmark_config(invalid_input).is_empty()); } #[test]