Skip to content

Commit

Permalink
chore: Add tests for errors
Browse files Browse the repository at this point in the history
In order to better cover the expected bevahior of errors, this commit
adds a few tests to exercise the crate errors we use.
  • Loading branch information
endoze committed Nov 12, 2023
1 parent b551675 commit 495fb5e
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![deny(missing_docs)]
#[derive(thiserror::Error, Debug)]
#[repr(u8)]
/// Enum to define all of the possible errors that can occur during normal
/// use of Nysm.
pub enum NysmError {
Expand Down Expand Up @@ -35,6 +36,78 @@ pub enum NysmError {

impl PartialEq for NysmError {
fn eq(&self, other: &Self) -> bool {
println!("LEFT = {:?}", std::mem::discriminant(self));
println!("RIGHT = {:?}", std::mem::discriminant(other));
std::mem::discriminant(self) == std::mem::discriminant(other)
}
}

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

#[test]
fn test_serde_json_error() {
let error = NysmError::SerdeJson(serde_json::Error::custom("custom error"));
assert_eq!(error.to_string(), "Unable to parse data as json");
}

#[test]
fn test_serde_yaml_error() {
let error = NysmError::SerdeYaml(serde_yaml::Error::custom("custom error"));
assert_eq!(error.to_string(), "Unable to parse data as yaml");
}

#[test]
fn test_bat_print_error() {
let error = NysmError::BatPrint(Into::<bat::error::Error>::into("custom error"));
assert_eq!(error.to_string(), "Unable to pretty print data");
}

#[test]
fn test_io_error() {
let error = NysmError::IO(std::io::Error::new(
std::io::ErrorKind::Other,
"custom error",
));
assert_eq!(
error.to_string(),
"Unable to read/write file caused by: custom error"
);
}

#[test]
fn test_aws_list_secrets_no_list_error() {
let error = NysmError::AwsListSecretsNoList;
assert_eq!(
error.to_string(),
"Unable to retrieve list of secrets from aws response"
);
}

#[test]
fn test_aws_secret_value_no_value_string_error() {
let error = NysmError::AwsSecretValueNoValueString;
assert_eq!(
error.to_string(),
"Unable to retrieve string value from aws response"
);
}

#[test]
fn test_aws_secret_value_update_error() {
let error = NysmError::AwsSecretValueUpdate;
assert_eq!(error.to_string(), "Unable to update secret value");
}

#[test]
fn test_partial_eq() {
let error1 = NysmError::SerdeJson(serde_json::Error::custom("custom error"));
let error2 = NysmError::SerdeJson(serde_json::Error::custom("custom error"));
let error3 = NysmError::SerdeYaml(serde_yaml::Error::custom("different error"));

assert_eq!(error1, error2);
assert_ne!(error1, error3);
}
}

0 comments on commit 495fb5e

Please sign in to comment.