Skip to content

Commit

Permalink
Merge pull request #68 from originalworks/ow-blob-codec-move-test-to-…
Browse files Browse the repository at this point in the history
…module

ow_blob_codec - move tests to separate module
  • Loading branch information
cezary-stroczynski authored Oct 21, 2024
2 parents 916dccc + 54a1a80 commit 7552b55
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 54 deletions.
21 changes: 13 additions & 8 deletions ow_blob_codec/src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,19 @@ pub fn file_to_vec(path: &Path) -> Result<Vec<u8>, Box<dyn Error>> {
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());
}
}
62 changes: 33 additions & 29 deletions ow_blob_codec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,41 +86,45 @@ pub fn blob_from_file(path: &str) -> Result<[u8; BYTES_PER_BLOB], Box<dyn Error>
}
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]);
}
}
39 changes: 22 additions & 17 deletions ow_blob_codec/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,28 @@ pub fn validate_xml(path: &Path) -> Result<&Path, Box<dyn Error>> {
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();
}
}

0 comments on commit 7552b55

Please sign in to comment.