diff --git a/src/core/wordlist.rs b/src/core/wordlist.rs index 5a43ffd6..8ffb5f4a 100644 --- a/src/core/wordlist.rs +++ b/src/core/wordlist.rs @@ -37,14 +37,10 @@ impl Wordlist { let (prefix_without_last, last_partial) = prefix.rsplit_once('-').unwrap_or(("", prefix)); #[cfg(feature = "fuzzy-complete")] - let matches = self.fuzzy_complete(last_partial, words); + let matches: Vec = self.fuzzy_complete(last_partial, words); #[cfg(not(feature = "fuzzy-complete"))] - let matches = words - .iter() - .filter(|word| word.starts_with(last_partial)) - .cloned() - .collect(); + let matches: Vec = self.normal_complete(last_partial, words); matches .into_iter() @@ -68,7 +64,7 @@ impl Wordlist { /// Fuzzy wormhole code completion #[cfg(feature = "fuzzy-complete")] - fn fuzzy_complete(&self, partial: &str, words: &[String]) -> Vec { + pub fn fuzzy_complete(&self, partial: &str, words: &[String]) -> Vec { // We use Jaro-Winkler algorithm because it emphasizes the beginning of a word use fuzzt::algorithms::JaroWinkler; @@ -80,6 +76,14 @@ impl Wordlist { .collect() } + pub fn normal_complete(&self, partial: &str, words: &[String]) -> Vec { + words + .iter() + .filter(|word| word.starts_with(partial)) + .cloned() + .collect() + } + /// Choose wormhole code word pub fn choose_words(&self) -> String { let mut rng = OsRng; @@ -230,18 +234,35 @@ mod test { } #[test] - #[cfg(not(feature = "fuzzy-complete"))] - fn test_wormhole_code_normal_completions() { - let list = default_wordlist(2); + #[cfg(feature = "fuzzy-complete")] + fn test_completion_fuzzy() { + let wl = default_wordlist(2); + let list = wl.get_wordlist("22-"); - assert_eq!(list.get_completions("22"), Vec::::new()); - assert_eq!(list.get_completions("22-"), Vec::::new()); + assert_eq!(wl.fuzzy_complete("chck", list), ["checkup", "choking"]); + assert_eq!(wl.fuzzy_complete("checkp", list), ["checkup"]); + assert_eq!( + wl.fuzzy_complete("checkup", list), + ["checkup", "lockup", "cleanup"] + ); + } - // Invalid wormhole code check - assert_eq!(list.get_completions("tro"), Vec::::new()); + #[test] + fn test_completion_normal() { + let wl = default_wordlist(2); + let list = wl.get_wordlist("22-"); - assert_eq!(list.get_completions("22-chisel"), ["22-chisel"]); + assert_eq!(wl.normal_complete("che", list), ["checkup"]); + } + + #[test] + fn test_full_wormhole_completion() { + let wl = default_wordlist(2); - assert_eq!(list.get_completions("22-chisel-tob"), ["22-chisel-tobacco"]); + assert_eq!(wl.get_completions("22-chec").first().unwrap(), "22-checkup"); + assert_eq!( + wl.get_completions("22-checkup-t").first().unwrap(), + "22-checkup-tobacco" + ); } }