|
1 | 1 | use colored::Colorize;
|
| 2 | +use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; |
2 | 3 | use std::process;
|
3 | 4 | use subprocess::Exec;
|
4 | 5 |
|
@@ -36,3 +37,82 @@ pub fn warning(text: &str, exit_on_warning: bool) {
|
36 | 37 | process::exit(1);
|
37 | 38 | }
|
38 | 39 | }
|
| 40 | + |
| 41 | +/// Returns vector of string that are similar by threshold to given string in given vector |
| 42 | +pub fn find_similar_string(string: String, strings: Vec<String>, threshold:f64) -> Vec<String> { |
| 43 | + strings.par_iter() |
| 44 | + .filter(|name| { |
| 45 | + similar_string::compare_similarity( |
| 46 | + string.to_lowercase(), |
| 47 | + name.to_lowercase(), |
| 48 | + ) > threshold |
| 49 | + }) |
| 50 | + .map(|name| name.to_string()) |
| 51 | + .collect::<Vec<_>>() |
| 52 | +} |
| 53 | + |
| 54 | +#[cfg(test)] |
| 55 | +mod tests { |
| 56 | + use super::*; |
| 57 | + |
| 58 | + #[test] |
| 59 | + fn test_validate_var_name_valid() { |
| 60 | + let result = validate_var_name("VALID_NAME"); |
| 61 | + assert!(result.is_ok()); |
| 62 | + } |
| 63 | + |
| 64 | + #[test] |
| 65 | + fn test_validate_var_name_with_space() { |
| 66 | + let result = validate_var_name("INVALID NAME"); |
| 67 | + assert!(result.is_err()); |
| 68 | + assert_eq!(result.unwrap_err(), "Variable name cannot contain spaces"); |
| 69 | + } |
| 70 | + |
| 71 | + #[test] |
| 72 | + fn test_find_similar_string_exact_match() { |
| 73 | + let strings = vec![ |
| 74 | + "PATH".to_string(), |
| 75 | + "HOME".to_string(), |
| 76 | + "USER".to_string() |
| 77 | + ]; |
| 78 | + let result = find_similar_string("PATH".to_string(), strings, 0.8); |
| 79 | + assert_eq!(result, vec!["PATH"]); |
| 80 | + } |
| 81 | + |
| 82 | + #[test] |
| 83 | + fn test_find_similar_string_case_insensitive() { |
| 84 | + let strings = vec![ |
| 85 | + "PATH".to_string(), |
| 86 | + "HOME".to_string(), |
| 87 | + "USER".to_string() |
| 88 | + ]; |
| 89 | + let result = find_similar_string("path".to_string(), strings, 0.8); |
| 90 | + assert_eq!(result, vec!["PATH"]); |
| 91 | + } |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn test_find_similar_string_no_match() { |
| 95 | + let strings = vec![ |
| 96 | + "PATH".to_string(), |
| 97 | + "HOME".to_string(), |
| 98 | + "USER".to_string() |
| 99 | + ]; |
| 100 | + let result = find_similar_string("XXXXXX".to_string(), strings, 0.8); |
| 101 | + assert!(result.is_empty()); |
| 102 | + } |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn test_find_similar_string_multiple_matches() { |
| 106 | + let strings = vec![ |
| 107 | + "TEST".to_string(), |
| 108 | + "TSET".to_string(), |
| 109 | + "TEXT".to_string(), |
| 110 | + "NONE".to_string() |
| 111 | + ]; |
| 112 | + let result = find_similar_string("TEST".to_string(), strings, 0.5); |
| 113 | + assert!(result.contains(&"TEST".to_string())); |
| 114 | + assert!(result.contains(&"TEXT".to_string())); |
| 115 | + assert!(result.contains(&"TSET".to_string())); |
| 116 | + assert!(!result.contains(&"NONE".to_string())); |
| 117 | + } |
| 118 | +} |
0 commit comments