From 926076c6e94536d475172041799f4ea6f4e53a7b Mon Sep 17 00:00:00 2001 From: Elias Dalbeck Date: Sat, 9 Nov 2024 08:24:03 +0100 Subject: [PATCH 1/3] fixed wrong wordlist on tab completion --- src/core/wordlist.rs | 108 +++++++++++++++++++++++++++++-------------- 1 file changed, 74 insertions(+), 34 deletions(-) diff --git a/src/core/wordlist.rs b/src/core/wordlist.rs index aff8f870..ad5366f2 100644 --- a/src/core/wordlist.rs +++ b/src/core/wordlist.rs @@ -58,7 +58,8 @@ impl Wordlist { fn get_wordlist(&self, prefix: &str) -> &Vec { let count_dashes = prefix.matches('-').count(); - &self.words[count_dashes % self.words.len()] + let index = 1 - (count_dashes % 2); + &self.words[index] } #[cfg(feature = "fuzzy-complete")] @@ -165,6 +166,14 @@ mod test { assert_eq!(d.words[1][255], "zulu"); } + #[test] + fn test_get_wordlist() { + let list = Wordlist::default_wordlist(2); + assert_eq!(list.words.len(), 2); + assert_eq!(list.get_wordlist("22-"), &list.words[0]); + assert_eq!(list.get_wordlist("22-dictator-"), &list.words[1]); + } + fn vec_strs(all: &str) -> Vec<&str> { all.split_whitespace() .map(|s| if s == "." { "" } else { s }) @@ -185,9 +194,8 @@ mod test { let w = Wordlist::new(2, words); assert_eq!(w.get_completions(""), Vec::::new()); assert_eq!(w.get_completions("9"), Vec::::new()); - assert_eq!(w.get_completions("pur"), vec!["purple"]); - assert_eq!(w.get_completions("blu"), Vec::::new()); - assert_eq!(w.get_completions("purple-sa"), vec!["purple-sausages"]); + assert_eq!(w.get_completions("slet"), vec!["seltzer"]); + assert_eq!(w.get_completions("sausages-yll"), vec!["sausages-yellow"]); } #[test] @@ -202,6 +210,20 @@ mod test { assert_eq!(w.choose_words().as_ref(), "purple-sausages-purple-sausages"); } + #[test] + fn test_choose_words_matches_completion() { + let few_words: Vec> = vec![vec_strings("purple"), vec_strings("sausages")]; + + let w = Wordlist::new(2, few_words.clone()); + assert_eq!(w.choose_words().as_ref(), "purple-sausages"); + + // Check if odd and even wordlist are correctly selected + assert_eq!( + w.get_completions("1-purple-sausages").first().unwrap(), + &format!("1-{}", w.choose_words().as_ref()) + ); + } + #[test] fn test_choose_more_words() { let more_words = vec![vec_strings("purple yellow"), vec_strings("sausages")]; @@ -227,58 +249,76 @@ mod test { #[test] #[cfg(feature = "fuzzy-complete")] - fn test_wormhole_code_fuzzy_completions() { - let list = Wordlist::default_wordlist(2); - - assert_eq!(list.get_completions("22"), Vec::::new()); - assert_eq!(list.get_completions("22-"), Vec::::new()); - - // Invalid wormhole code check - assert_eq!(list.get_completions("trj"), Vec::::new()); + fn test_completion_fuzzy() { + let wl = Wordlist::default_wordlist(2); + let list = wl.get_wordlist("22-"); assert_eq!( - list.get_completions("22-chisel"), - ["22-chisel", "22-chairlift", "22-christmas"] + wl.fuzzy_complete("bzili", list).first().unwrap(), + "brazilian" ); - assert_eq!( - list.get_completions("22-chle"), - ["22-chisel", "22-chatter", "22-checkup"] + wl.fuzzy_complete("carvan", list).first().unwrap(), + "caravan" ); - - assert_eq!(list.get_completions("22-chisel-tba"), ["22-chisel-tobacco"]); + assert_ne!( + wl.fuzzy_complete("choking", list).first().unwrap(), + "choking" + ) } #[test] - #[cfg(feature = "fuzzy-complete")] - fn test_completion_fuzzy() { + fn test_completion_normal() { let wl = Wordlist::default_wordlist(2); let list = wl.get_wordlist("22-"); - 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"] + wl.normal_complete("braz", list).first().unwrap(), + "brazilian" ); + assert_ne!( + wl.fuzzy_complete("choking", list).first().unwrap(), + "choking" + ) } #[test] - fn test_completion_normal() { - let wl = Wordlist::default_wordlist(2); - let list = wl.get_wordlist("22-"); + fn test_wormhole_code_normal_completions() { + let list = Wordlist::default_wordlist(2); - assert_eq!(wl.normal_complete("che", list), ["checkup"]); + assert_eq!( + list.get_completions("22-cmpont").first().unwrap(), + "22-component" + ); + assert_eq!( + list.get_completions("22-component-chkup").first().unwrap(), + "22-component-checkup" + ); + assert_ne!(list.get_completions("22-troj"), ["trojan"]); } #[test] - fn test_full_wormhole_completion() { - let wl = Wordlist::default_wordlist(2); + #[cfg(feature = "fuzzy-complete")] + fn test_wormhole_code_fuzzy_completions() { + let list = Wordlist::default_wordlist(2); + + assert_eq!(list.get_completions("22"), Vec::::new()); + assert_eq!(list.get_completions("22-"), Vec::::new()); + assert_ne!(list.get_completions("22-trj"), ["trojan"]); + + assert_eq!( + list.get_completions("22-udau").first().unwrap(), + "22-undaunted" + ); + + assert_eq!( + list.get_completions("22-undua").first().unwrap(), + "22-undaunted" + ); - assert_eq!(wl.get_completions("22-chec").first().unwrap(), "22-checkup"); assert_eq!( - wl.get_completions("22-checkup-t").first().unwrap(), - "22-checkup-tobacco" + list.get_completions("22-undaunted-usht").first().unwrap(), + "22-undaunted-upshot" ); } } From cd4e41c6a28992b1cf49845fc85a480eaf5a33ed Mon Sep 17 00:00:00 2001 From: Elias Dalbeck Date: Sat, 9 Nov 2024 08:46:26 +0100 Subject: [PATCH 2/3] updated README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e7fb6c5b..a308448b 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,6 @@ This is a Rust port of the Python version at Date: Sat, 9 Nov 2024 16:39:24 +0100 Subject: [PATCH 3/3] removed invalid fuzzy string in completion tests --- src/core/wordlist.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/wordlist.rs b/src/core/wordlist.rs index ad5366f2..4fc0a02f 100644 --- a/src/core/wordlist.rs +++ b/src/core/wordlist.rs @@ -194,8 +194,8 @@ mod test { let w = Wordlist::new(2, words); assert_eq!(w.get_completions(""), Vec::::new()); assert_eq!(w.get_completions("9"), Vec::::new()); - assert_eq!(w.get_completions("slet"), vec!["seltzer"]); - assert_eq!(w.get_completions("sausages-yll"), vec!["sausages-yellow"]); + assert_eq!(w.get_completions("seltz"), vec!["seltzer"]); + assert_eq!(w.get_completions("sausages-yello"), vec!["sausages-yellow"]); } #[test]