From 54a1a80d4f83e83bd2b35e4d90bb3b87c6324cdf Mon Sep 17 00:00:00 2001 From: cezary-stroczynski Date: Mon, 21 Oct 2024 12:36:36 +0200 Subject: [PATCH] ow_blob_codec - move tests to separate module --- ow_blob_codec/src/encoder.rs | 21 +++++++----- ow_blob_codec/src/lib.rs | 62 ++++++++++++++++++---------------- ow_blob_codec/src/validator.rs | 39 +++++++++++---------- 3 files changed, 68 insertions(+), 54 deletions(-) diff --git a/ow_blob_codec/src/encoder.rs b/ow_blob_codec/src/encoder.rs index fcd8090..5f535b6 100644 --- a/ow_blob_codec/src/encoder.rs +++ b/ow_blob_codec/src/encoder.rs @@ -35,14 +35,19 @@ pub fn file_to_vec(path: &Path) -> Result, Box> { Ok(encoded) } -#[test] -fn encoded_file_is_smaller() { - let path = Path::new("./tests/assets/valid_xml/ERN_example_1.xml"); - let mut file = File::open(path).unwrap(); - let mut file_buffer = Vec::new(); - file.read_to_end(&mut file_buffer).unwrap(); +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn encoded_file_is_smaller() { + let path = Path::new("./tests/assets/valid_xml/ERN_example_1.xml"); + let mut file = File::open(path).unwrap(); + let mut file_buffer = Vec::new(); + file.read_to_end(&mut file_buffer).unwrap(); - let encoded = file_to_vec(path).unwrap(); + let encoded = file_to_vec(path).unwrap(); - assert!(encoded.len() < file_buffer.len()); + assert!(encoded.len() < file_buffer.len()); + } } diff --git a/ow_blob_codec/src/lib.rs b/ow_blob_codec/src/lib.rs index 3e547eb..0dd4ab2 100644 --- a/ow_blob_codec/src/lib.rs +++ b/ow_blob_codec/src/lib.rs @@ -86,41 +86,45 @@ pub fn blob_from_file(path: &str) -> Result<[u8; BYTES_PER_BLOB], Box } Ok(kzg_blob) } +#[cfg(test)] +mod tests { + use super::*; -#[test] -#[should_panic] -fn panic_for_empty_dir() { - blob_from_dir("./tests/assets").unwrap(); -} + #[test] + #[should_panic] + fn panic_for_empty_dir() { + blob_from_dir("./tests/assets").unwrap(); + } -#[test] -#[should_panic] -fn panic_for_non_dir() { - blob_from_dir("./tests/assets/pierogi").unwrap(); -} + #[test] + #[should_panic] + fn panic_for_non_dir() { + blob_from_dir("./tests/assets/pierogi").unwrap(); + } -#[test] -#[should_panic] -fn panic_for_non_xml_file() { - blob_from_file("./tests/assets/test.txt").unwrap(); -} + #[test] + #[should_panic] + fn panic_for_non_xml_file() { + blob_from_file("./tests/assets/test.txt").unwrap(); + } -#[test] -#[should_panic] -fn panic_for_wrong_path() { - blob_from_file("./tests/kapusta").unwrap(); -} + #[test] + #[should_panic] + fn panic_for_wrong_path() { + blob_from_file("./tests/kapusta").unwrap(); + } -#[test] -fn encode_file_into_blob_from_valid_xml() { - let blob = blob_from_file("./tests/assets/valid_xml/ERN_example_1.xml").unwrap(); + #[test] + fn encode_file_into_blob_from_valid_xml() { + let blob = blob_from_file("./tests/assets/valid_xml/ERN_example_1.xml").unwrap(); - assert_ne!(blob, [0; BYTES_PER_BLOB]); -} + assert_ne!(blob, [0; BYTES_PER_BLOB]); + } -#[test] -fn encode_dir_files_into_blob() { - let blob = blob_from_dir("./tests/assets/valid_xml").unwrap(); + #[test] + fn encode_dir_files_into_blob() { + let blob = blob_from_dir("./tests/assets/valid_xml").unwrap(); - assert_ne!(blob, [0; BYTES_PER_BLOB]); + assert_ne!(blob, [0; BYTES_PER_BLOB]); + } } diff --git a/ow_blob_codec/src/validator.rs b/ow_blob_codec/src/validator.rs index 720f680..4180e54 100644 --- a/ow_blob_codec/src/validator.rs +++ b/ow_blob_codec/src/validator.rs @@ -35,23 +35,28 @@ pub fn validate_xml(path: &Path) -> Result<&Path, Box> { Ok(path) } -#[test] -#[should_panic] -fn panic_for_empty_file() { - let path = Path::new("./tests/assets/corrupted_xml/empty.xml"); - validate_xml(path).unwrap(); -} +#[cfg(test)] +mod tests { + use super::*; + + #[test] + #[should_panic] + fn panic_for_empty_file() { + let path = Path::new("./tests/assets/corrupted_xml/empty.xml"); + validate_xml(path).unwrap(); + } -#[test] -#[should_panic] -fn panic_for_corrupted_xml() { - let path = Path::new("./tests/assets/corrupted_xml/corrupted.xml"); - validate_xml(path).unwrap(); -} + #[test] + #[should_panic] + fn panic_for_corrupted_xml() { + let path = Path::new("./tests/assets/corrupted_xml/corrupted.xml"); + validate_xml(path).unwrap(); + } -#[test] -fn pass_for_valid_xml() { - validate_xml(Path::new("./tests/assets/valid_xml/ERN_example_1.xml")).unwrap(); - validate_xml(Path::new("./tests/assets/valid_xml/ERN_example_2.xml")).unwrap(); - validate_xml(Path::new("./tests/assets/valid_xml/ERN_example_3.xml")).unwrap(); + #[test] + fn pass_for_valid_xml() { + validate_xml(Path::new("./tests/assets/valid_xml/ERN_example_1.xml")).unwrap(); + validate_xml(Path::new("./tests/assets/valid_xml/ERN_example_2.xml")).unwrap(); + validate_xml(Path::new("./tests/assets/valid_xml/ERN_example_3.xml")).unwrap(); + } }