Progress in Lzma compression
[ouch.git] / src / test.rs
blob4e612172fb191f0291b5eb95541425d8bc006085
1 // TODO: remove tests of CompressionFormat::try_from since that's no longer used anywhere
3 #[cfg(test)]
4 mod cli {
6     use crate::cli::clap_app;
7     use crate::cli::Command;
8     use crate::cli::CommandKind::*;
9     use crate::error::OuchResult;
10     use crate::extension::CompressionFormat::*;
11     use crate::extension::Extension;
12     use crate::file::File;
13     use std::convert::TryFrom;
15     #[test]
16     fn decompress_files_into_folder() -> OuchResult<()> {
17         let matches = clap_app().get_matches_from(vec!["ouch", "-i", "file.zip", "-o", "folder/"]);
18         let command_from_matches = Command::try_from(matches)?;
20         assert_eq!(
21             command_from_matches,
22             Command {
23                 kind: Decompression(vec![
24                     File { 
25                         path: "file.zip".into(),
26                         contents_in_memory: None,
27                         extension: Some(Extension::from(Zip))
28                     }
29                 ]),
30                 output: Some(File {
31                     path: "folder".into(),
32                     contents_in_memory: None,
33                     extension: None
34                 }),
35             }
36         );
38         Ok(())
39     }
41     #[test]
42     fn decompress_files() -> OuchResult<()> {
43         let matches = clap_app().get_matches_from(vec!["ouch", "-i", "file.zip", "file.tar"]);
44         let command_from_matches = Command::try_from(matches)?;
46         assert_eq!(
47             command_from_matches,
48             Command {
49                 kind: Decompression(vec![
50                     File { 
51                         path: "file.zip".into(),
52                         contents_in_memory: None,
53                         extension: Some(Extension::from(Zip))
54                     },
55                     File { 
56                         path: "file.tar".into(),
57                         contents_in_memory: None,
58                         extension: Some(Extension::from(Tar))
59                     }
60                 ],),
61                 output: None,
62             }
63         );
65         Ok(())
66     }
68     #[test]
69     fn compress_files() -> OuchResult<()> {
70         let matches = clap_app().get_matches_from(vec![
71             "ouch",
72             "-i",
73             "file",
74             "file2.jpeg",
75             "file3.ok",
76             "-o",
77             "file.tar",
78         ]);
79         let command_from_matches = Command::try_from(matches)?;
81         assert_eq!(
82             command_from_matches,
83             Command {
84                 kind: Compression(vec![
85                     "file".into(),
86                     "file2.jpeg".into(),
87                     "file3.ok".into()
88                 ]),
89                 output: Some(
90                     File {
91                         path: "file.tar".into(),
92                         contents_in_memory: None,
93                         extension: Some(Extension::from(Tar))
94                     }
95                 ),
96             }
97         );
99         Ok(())
100     }
103 #[cfg(test)]
104 mod cli_errors {
106     use std::convert::TryFrom;
108     use crate::cli::clap_app;
109     use crate::cli::Command;
110     use crate::error::Error;
111     use crate::error::OuchResult;
113     #[test]
114     fn compress_files() -> OuchResult<()> {
115         let matches =
116             clap_app().get_matches_from(vec!["ouch", "-i", "a_file", "file2.jpeg", "file3.ok"]);
117         let res = Command::try_from(matches);
119         assert_eq!(
120             res,
121             Err(Error::InputsMustHaveBeenDecompressible("a_file".into()))
122         );
124         Ok(())
125     }
128 #[cfg(test)]
129 mod extension_extraction {
130     use crate::{error::OuchResult, extension::Extension}    ;
131     use crate::extension::CompressionFormat;
132     use std::{convert::TryFrom, path::PathBuf, str::FromStr};
134     #[test]
135     fn zip() -> OuchResult<()> {
136         let path = PathBuf::from_str("filename.tar.zip").unwrap();
137         assert_eq!(
138             CompressionFormat::try_from(&path)?,
139             CompressionFormat::Zip
140         );
142         Ok(())
143     }
144     
145     #[test]
146     fn tar_gz() -> OuchResult<()> {
147         let extension = Extension::new("folder.tar.gz")?;
149         assert_eq!(
150             extension,
151             Extension {
152                 first_ext: Some(CompressionFormat::Tar),
153                 second_ext: CompressionFormat::Gzip
154             }
155         );
157         Ok(())
158     }
160     #[test]
161     fn tar() -> OuchResult<()> {
162         let path = PathBuf::from_str("pictures.tar").unwrap();
163         assert_eq!(
164             CompressionFormat::try_from(&path)?,
165             CompressionFormat::Tar
166         );
168         Ok(())
169     }
171     #[test]
172     fn gz() -> OuchResult<()> {
173         let path = PathBuf::from_str("passwords.tar.gz").unwrap();
174         assert_eq!(
175             CompressionFormat::try_from(&path)?,
176             CompressionFormat::Gzip
177         );
179         Ok(())
180     }
182     #[test]
183     fn lzma() -> OuchResult<()> {
184         let path = PathBuf::from_str("mygame.tar.lzma").unwrap();
185         assert_eq!(
186             CompressionFormat::try_from(&path)?,
187             CompressionFormat::Lzma
188         );
190         Ok(())
191     }
193     #[test]
194     fn bz() -> OuchResult<()> {
195         let path = PathBuf::from_str("songs.tar.bz").unwrap();
196         assert_eq!(
197             CompressionFormat::try_from(&path)?,
198             CompressionFormat::Bzip
199         );
201         Ok(())
202     }
205 // #[cfg(test)]
206 // mod evaluator {
207 //     use crate::extension::Extension;
208 //     use crate::error::OuchResult;
209 //     use crate::file::File;
210 //     use crate::evaluator::Evaluator;
211 //     use crate::decompressors::{Decompressor, TarDecompressor, GzipDecompressor};
213 //     #[test]
214 //     fn test() -> OuchResult<()> {
215 //         let extension = Extension::new("folder.tar.gz")?;
217 //         let file = File {
218 //             path: "folder.tar.gz".into(),
219 //             contents_in_memory: None,
220 //             extension: Some(extension),
221 //         };
223 //         let (fst, snd) = Evaluator::get_decompressor(&file)?;
225 //         let fst = fst.unwrap();
227 //         assert_eq!(
228 //             fst,
229 //             Some(Box::new(TarDecompressor::{})
230 //         );
232 //         Ok(())
233 //     }
234 // }