From 05c98188d62411f252005fc9e0f52b2d67105cb5 Mon Sep 17 00:00:00 2001 From: Christopher Morton Date: Wed, 31 Jul 2024 11:16:17 +0100 Subject: [PATCH] Add more trait impls to EnvFileBuilder - Clone - PartialEq - Eq - PartialEq to String and &str - PartialEq to Vec and &[u8] - AsRef<[u8]> --- test_util/src/env_file.rs | 56 ++++++++++++++++++++++++- test_util/src/tests/env_file_builder.rs | 27 +++++------- 2 files changed, 66 insertions(+), 17 deletions(-) diff --git a/test_util/src/env_file.rs b/test_util/src/env_file.rs index d7753ff3..bd640f56 100644 --- a/test_util/src/env_file.rs +++ b/test_util/src/env_file.rs @@ -39,7 +39,7 @@ pub fn create_custom_env_file(env_vars: &[(&str, &str)]) -> String { /// Advanced test-env file constructor. /// /// Represented as bytes to allow for advanced manipulation and BOM testing. -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct EnvFileBuilder { contents: Vec, } @@ -159,3 +159,57 @@ impl From for EnvFileBuilder { } } } + +impl AsRef<[u8]> for EnvFileBuilder { + fn as_ref(&self) -> &[u8] { + self.as_bytes() + } +} + +impl PartialEq for EnvFileBuilder { + fn eq(&self, other: &String) -> bool { + self.as_bytes() == other.as_bytes() + } +} + +impl PartialEq for EnvFileBuilder { + fn eq(&self, other: &str) -> bool { + self.as_bytes() == other.as_bytes() + } +} + +impl PartialEq> for EnvFileBuilder { + fn eq(&self, other: &Vec) -> bool { + self.as_bytes() == other + } +} + +impl PartialEq<[u8]> for EnvFileBuilder { + fn eq(&self, other: &[u8]) -> bool { + self.as_bytes() == other + } +} + +impl PartialEq for String { + fn eq(&self, other: &EnvFileBuilder) -> bool { + self.as_bytes() == other.as_bytes() + } +} + +impl PartialEq for &str { + fn eq(&self, other: &EnvFileBuilder) -> bool { + self.as_bytes() == other.as_bytes() + } +} + +impl PartialEq for Vec { + fn eq(&self, other: &EnvFileBuilder) -> bool { + self == other.as_bytes() + } +} + +impl PartialEq for &[u8] { + fn eq(&self, other: &EnvFileBuilder) -> bool { + *self == other.as_bytes() + } +} diff --git a/test_util/src/tests/env_file_builder.rs b/test_util/src/tests/env_file_builder.rs index cd9b7c9d..9fbd0fae 100644 --- a/test_util/src/tests/env_file_builder.rs +++ b/test_util/src/tests/env_file_builder.rs @@ -17,7 +17,7 @@ fn add_key_empty_value() { let mut efb = EnvFileBuilder::new(); efb.add_key_value(DEFAULT_TEST_KEY, ""); let expected = format!("{DEFAULT_TEST_KEY}=\n"); - assert_contents_str(efb, &expected); + assert_eq!(expected, efb); } #[test] @@ -25,7 +25,7 @@ fn add_key_value() { let mut efb = EnvFileBuilder::new(); efb.add_key_value(DEFAULT_TEST_KEY, DEFAULT_TEST_VALUE); let expected = format!("{DEFAULT_TEST_KEY}={DEFAULT_TEST_VALUE}\n"); - assert_contents_str(efb, &expected); + assert_eq!(expected, efb); } #[test] @@ -37,7 +37,7 @@ fn add_multiple_key_values() { (DEFAULT_TEST_KEY, DEFAULT_TEST_VALUE), (DEFAULT_EXISTING_KEY, DEFAULT_OVERRIDING_VALUE), ]); - assert_contents_str(efb, &expected); + assert_eq!(expected, efb); } #[test] @@ -45,28 +45,28 @@ fn add_vars() { let mut efb = EnvFileBuilder::new(); efb.add_vars(CUSTOM_VARS); let expected = expected_env_file(CUSTOM_VARS); - assert_contents_str(efb, &expected); + assert_eq!(expected, efb); } #[test] fn add_str() { let mut efb = EnvFileBuilder::new(); efb.add_str("test"); - assert_contents_str(efb, "test"); + assert_eq!("test", efb); } #[test] fn add_bytes() { let mut efb = EnvFileBuilder::new(); efb.add_bytes(b"test"); - assert_contents_str(efb, "test"); + assert_eq!("test", efb); } #[test] fn add_byte() { let mut efb = EnvFileBuilder::new(); efb.add_byte(b't'); - assert_contents_str(efb, "t"); + assert_eq!("t", efb); } #[test] @@ -74,21 +74,21 @@ fn insert_utf8_bom() { let mut efb = EnvFileBuilder::new(); efb.add_str("test"); efb.insert_utf8_bom(); - assert_contents_str(efb, "\u{FEFF}test"); + assert_eq!("\u{FEFF}test", efb); } #[test] fn add_strln() { let mut efb = EnvFileBuilder::new(); efb.add_strln("test"); - assert_contents_str(efb, "test\n"); + assert_eq!("test\n", efb); } #[test] fn from_vec_u8() { let vec: Vec = Vec::from(create_default_env_file()); let efb = EnvFileBuilder::from(vec); - assert_contents_str(efb, &create_default_env_file()); + assert_eq!(create_default_env_file(), efb); } #[test] @@ -103,10 +103,5 @@ fn to_vec_u8() { #[test] fn from_string() { let efb = EnvFileBuilder::from(create_default_env_file()); - assert_contents_str(efb, &create_default_env_file()); -} - -fn assert_contents_str(efb: EnvFileBuilder, expected: &str) { - let contents = efb.into_owned_string(); - assert_eq!(expected, contents,); + assert_eq!(create_default_env_file(), efb); }