use std::io::{Read, Write}; use tempfile::NamedTempFile; use trevors_utilities::test_compression::TestCompression; #[test] fn smoke() { assert!(true) } #[test] fn compression_round_trip() { let to_compress = "The quick brown fox jumps over the lazy dog."; let decompressed_text = TestCompression::decompress_to_string(&TestCompression::compress_string(to_compress)); assert_eq!(to_compress, decompressed_text); } #[test] fn file_compression_round_trip() { // compress string to file... let string_to_compress = "The quick brown fox jumps over the lazy dog."; let compressed_string = TestCompression::compress_string(string_to_compress); let mut temp_target = NamedTempFile::new().unwrap(); let temp_reader = temp_target.reopen(); // ...write the compressed version to a file... temp_target .write_all(&compressed_string) .expect("Unable to write compressed file for test"); // // ...decompress from file... let mut compressed_read = Vec::new(); temp_reader .unwrap() .read_to_end(&mut compressed_read) .expect("Unable to read compressed data for test"); let decompresed = TestCompression::decompress_to_string(&compressed_read); // // ...verify its the same. assert_eq!(string_to_compress, decompresed); } #[test] fn file_compression_reader() { // Get the 'sample text' let to_compress = "The quick brown fox jumps over the lazy dog."; // ...write it to the temp file... let mut temp_file = NamedTempFile::new().expect("Unable to get temp file for test"); temp_file .write_all(&*TestCompression::compress_string(to_compress)) .expect("Unable to write compressed data to temp file for test"); let temp2_path = temp_file.path(); let uncompressed_text = TestCompression::load_compressed_file_to_string(temp2_path); assert_eq!(uncompressed_text, to_compress); } #[test] fn file_compression_writer() { let to_compress = "The quick brown fox jumps over the lazy dog."; let mut temp_file = NamedTempFile::new().expect("Unable to get temp file for test."); TestCompression::save_string_as_compressed_data(to_compress, temp_file.path()); // read back the compressed text let mut read_buffer = Vec::new(); temp_file .read_to_end(&mut read_buffer) .expect("Unable to read compressed data back."); let decompressed_text = TestCompression::decompress_to_string(&read_buffer); assert_eq!(decompressed_text, to_compress); } #[test] fn compress_file() { // create our test file. let test_text = "The quick brown fox jumps over the lazy dog."; let mut original_file = NamedTempFile::new().unwrap(); let compressed_file = NamedTempFile::new().unwrap(); original_file .write(test_text.as_bytes()) .expect("Unable to write original file"); // run the method being tested TestCompression::compress_file(original_file.path(), compressed_file.path()) .expect("Unable to compress temp file."); // verify the data in the new file matches what we expect let mut reader_file = compressed_file.reopen().unwrap(); let mut result_text = Vec::new(); reader_file .read_to_end(&mut result_text) .expect("Unable to read compressed data back to verify"); let decompressed_text = TestCompression::decompress_to_string(&result_text); assert_eq!(decompressed_text, test_text); } #[test] fn compress_string_to_file() { let to_compress = "The quick brown fox jumped over the lazy dog."; let compressed_text = TestCompression::compress_string(to_compress); let target_file = NamedTempFile::new().unwrap(); let mut duplicate = target_file.reopen().unwrap(); TestCompression::compress_string_to_file(to_compress, target_file.path()); let mut file_contents = Vec::new(); duplicate .read_to_end(&mut file_contents) .expect("Unable to read compressed file."); assert_eq!(file_contents, compressed_text); } #[test] fn decompress_file_to_string() { let to_compress = "The quick brown fox jumps over the lazy dog."; let temp = NamedTempFile::new().unwrap(); TestCompression::compress_string_to_file(to_compress, temp.path()); let result = TestCompression::decompress_file_to_string(temp.path()); assert_eq!(to_compress, result); } #[test] fn compress_file_to_array() { let to_compress = "The quick brown fox jumps over the lazy dog."; let mut temp_file = NamedTempFile::new().expect("Unable to get temp file for test"); temp_file.write_all(to_compress.as_bytes()).unwrap(); let result = TestCompression::compress_file_to_array(temp_file.path()); let decompressed_result = TestCompression::decompress_to_string(&result); assert_eq!(to_compress, decompressed_result); } #[test] fn decompress_to_array() { let original_text = "The quick brown fox jumps over the lazy dog."; let compressed_data = TestCompression::compress_string(original_text); // Write the compressed data to a temp file let mut temp_file = NamedTempFile::new().expect("Failed to create temp file"); temp_file .write_all(&compressed_data) .expect("Failed to write compressed data to file"); // Run the method under test let decompressed_data = TestCompression::decompress_file_to_array(temp_file.path()); // Convert to string to compare let decompressed_text = String::from_utf8(decompressed_data) .expect("Decompressed bytes were not valid UTF-8"); assert_eq!(decompressed_text, original_text); } #[test] #[should_panic(expected = "No such file or directory")] fn decompress_file_panics_on_missing_path() { let bad_path = std::path::Path::new("/definitely/invalid/path"); TestCompression::decompress_file_to_array(bad_path); }