Skip to content

Commit

Permalink
Add more trait impls to EnvFileBuilder
Browse files Browse the repository at this point in the history
- Clone
- PartialEq
- Eq

- PartialEq to String and &str
- PartialEq to Vec<u8> and &[u8]

- AsRef<[u8]>
  • Loading branch information
sonro committed Jul 31, 2024
1 parent ce6e00b commit 05c9818
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 17 deletions.
56 changes: 55 additions & 1 deletion test_util/src/env_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
}
Expand Down Expand Up @@ -159,3 +159,57 @@ impl From<String> for EnvFileBuilder {
}
}
}

impl AsRef<[u8]> for EnvFileBuilder {
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}

impl PartialEq<String> for EnvFileBuilder {
fn eq(&self, other: &String) -> bool {
self.as_bytes() == other.as_bytes()
}
}

impl PartialEq<str> for EnvFileBuilder {
fn eq(&self, other: &str) -> bool {
self.as_bytes() == other.as_bytes()
}
}

impl PartialEq<Vec<u8>> for EnvFileBuilder {
fn eq(&self, other: &Vec<u8>) -> bool {
self.as_bytes() == other
}
}

impl PartialEq<[u8]> for EnvFileBuilder {
fn eq(&self, other: &[u8]) -> bool {
self.as_bytes() == other
}
}

impl PartialEq<EnvFileBuilder> for String {
fn eq(&self, other: &EnvFileBuilder) -> bool {
self.as_bytes() == other.as_bytes()
}
}

impl PartialEq<EnvFileBuilder> for &str {
fn eq(&self, other: &EnvFileBuilder) -> bool {
self.as_bytes() == other.as_bytes()
}
}

impl PartialEq<EnvFileBuilder> for Vec<u8> {
fn eq(&self, other: &EnvFileBuilder) -> bool {
self == other.as_bytes()
}
}

impl PartialEq<EnvFileBuilder> for &[u8] {
fn eq(&self, other: &EnvFileBuilder) -> bool {
*self == other.as_bytes()
}
}
27 changes: 11 additions & 16 deletions test_util/src/tests/env_file_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ 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]
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]
Expand All @@ -37,58 +37,58 @@ 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]
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]
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<u8> = 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]
Expand All @@ -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);
}

0 comments on commit 05c9818

Please sign in to comment.