|
1 | | -#![allow(unused_imports)] |
2 | | -// #![allow(dead_code)] |
3 | | -// #![allow(unused_variables)] |
4 | | - |
5 | | -// unit tests are part of the main crate |
6 | | - |
7 | 1 | #[cfg(test)] |
8 | 2 | use super::*; |
| 3 | +use std::str::FromStr; |
| 4 | + |
| 5 | +#[test] |
| 6 | +fn test_parse_hash_algorithm_valid() { |
| 7 | + assert_eq!( |
| 8 | + parse_hash_algorithm(Some("SHA3-256")).unwrap(), |
| 9 | + HashAlgorithm::SHA3_256 |
| 10 | + ); |
| 11 | + assert_eq!( |
| 12 | + parse_hash_algorithm(Some("MD5")).unwrap(), |
| 13 | + HashAlgorithm::MD5 |
| 14 | + ); |
| 15 | + assert_eq!( |
| 16 | + parse_hash_algorithm(Some("CRC32")).unwrap(), |
| 17 | + HashAlgorithm::CRC32 |
| 18 | + ); |
| 19 | + assert_eq!( |
| 20 | + parse_hash_algorithm(Some("WHIRLPOOL")).unwrap(), |
| 21 | + HashAlgorithm::Whirlpool |
| 22 | + ); |
| 23 | +} |
| 24 | + |
| 25 | +#[test] |
| 26 | +fn test_parse_hash_algorithm_default() { |
| 27 | + assert_eq!(parse_hash_algorithm(None).unwrap(), DEFAULT_HASH); |
| 28 | + assert_eq!(parse_hash_algorithm(Some("")).unwrap(), DEFAULT_HASH); |
| 29 | +} |
| 30 | + |
| 31 | +#[test] |
| 32 | +fn test_parse_hash_algorithm_invalid() { |
| 33 | + assert!(parse_hash_algorithm(Some("INVALID")).is_err()); |
| 34 | +} |
| 35 | + |
| 36 | +#[test] |
| 37 | +fn test_parse_hash_encoding_valid() { |
| 38 | + assert_eq!( |
| 39 | + parse_hash_encoding(Some("Hex")).unwrap(), |
| 40 | + OutputEncoding::Hex |
| 41 | + ); |
| 42 | + assert_eq!( |
| 43 | + parse_hash_encoding(Some("Base64")).unwrap(), |
| 44 | + OutputEncoding::Base64 |
| 45 | + ); |
| 46 | + assert_eq!( |
| 47 | + parse_hash_encoding(Some("Base32")).unwrap(), |
| 48 | + OutputEncoding::Base32 |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +#[test] |
| 53 | +fn test_parse_hash_encoding_default() { |
| 54 | + assert_eq!( |
| 55 | + parse_hash_encoding(None).unwrap(), |
| 56 | + OutputEncoding::Unspecified |
| 57 | + ); |
| 58 | + assert_eq!( |
| 59 | + parse_hash_encoding(Some("")).unwrap(), |
| 60 | + OutputEncoding::Unspecified |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +#[test] |
| 65 | +fn test_hash_algorithm_from_str() { |
| 66 | + assert_eq!( |
| 67 | + HashAlgorithm::from_str("sha3-256").unwrap(), |
| 68 | + HashAlgorithm::SHA3_256 |
| 69 | + ); |
| 70 | + assert_eq!( |
| 71 | + HashAlgorithm::from_str("SHA2").unwrap(), |
| 72 | + HashAlgorithm::SHA2_256 |
| 73 | + ); |
| 74 | + assert_eq!( |
| 75 | + HashAlgorithm::from_str("sha1").unwrap(), |
| 76 | + HashAlgorithm::SHA1 |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +#[test] |
| 81 | +fn test_config_settings_new() { |
| 82 | + let config = ConfigSettings::new( |
| 83 | + true, // debug_mode |
| 84 | + false, // exclude_fn |
| 85 | + false, // single_thread |
| 86 | + true, // case_sensitive |
| 87 | + HashAlgorithm::SHA3_256, |
| 88 | + OutputEncoding::Hex, |
| 89 | + Some(100), |
| 90 | + ); |
| 91 | + |
| 92 | + assert!(config.debug_mode); |
| 93 | + assert!(!config.exclude_fn); |
| 94 | + assert!(config.case_sensitive); |
| 95 | + assert_eq!(config.algorithm, HashAlgorithm::SHA3_256); |
| 96 | + assert_eq!(config.encoding, OutputEncoding::Hex); |
| 97 | + assert_eq!(config.limit_num, Some(100)); |
| 98 | + assert!(config.supplied_paths.is_empty()); |
| 99 | +} |
| 100 | + |
| 101 | +#[test] |
| 102 | +fn test_config_settings_set_paths() { |
| 103 | + let mut config = ConfigSettings::new( |
| 104 | + false, |
| 105 | + false, |
| 106 | + false, |
| 107 | + false, |
| 108 | + HashAlgorithm::MD5, |
| 109 | + OutputEncoding::Base64, |
| 110 | + None, |
| 111 | + ); |
| 112 | + |
| 113 | + let paths = vec!["file1.txt".to_string(), "file2.txt".to_string()]; |
| 114 | + config.set_supplied_paths(paths.clone()); |
| 115 | + |
| 116 | + assert_eq!(config.supplied_paths, paths); |
| 117 | +} |
9 | 118 |
|
10 | 119 | #[test] |
11 | | -fn unit_it_works() { |
12 | | - assert_eq!(2 + 2, 4); |
| 120 | +fn test_basic_hash_display() { |
| 121 | + let hash = BasicHash("abc123".to_string()); |
| 122 | + assert_eq!(format!("{hash}"), "abc123"); |
13 | 123 | } |
14 | 124 |
|
15 | 125 | #[test] |
16 | | -fn help_length() { |
17 | | - assert!(HELP.len() > 10); |
| 126 | +fn test_help_content() { |
| 127 | + assert!(HELP.contains("USAGE:")); |
| 128 | + assert!(HELP.contains("FLAGS:")); |
| 129 | + assert!(HELP.contains("OPTIONS:")); |
| 130 | + assert!(HELP.contains("Algorithm can be:")); |
| 131 | + assert!(HELP.len() > 100); |
18 | 132 | } |
0 commit comments