aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src/ui/fuzzy_match
diff options
context:
space:
mode:
authorPascal Kuthe2022-09-25 21:43:24 +0000
committerBlaž Hrastnik2022-10-11 03:25:28 +0000
commit7af599e0af964d1ea9d3e454782ac5d977f1621f (patch)
tree4aea547e7f7cd6f1beba0927101071a396ca9c57 /helix-term/src/ui/fuzzy_match
parentc388e16e09b36c665c4f92db4f7a071e7c1d9761 (diff)
Treat space as a seperator instead of a character in fuzzy picker
Diffstat (limited to 'helix-term/src/ui/fuzzy_match')
-rw-r--r--helix-term/src/ui/fuzzy_match/test.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/helix-term/src/ui/fuzzy_match/test.rs b/helix-term/src/ui/fuzzy_match/test.rs
new file mode 100644
index 00000000..3f90ef68
--- /dev/null
+++ b/helix-term/src/ui/fuzzy_match/test.rs
@@ -0,0 +1,47 @@
+use crate::ui::fuzzy_match::FuzzyQuery;
+use crate::ui::fuzzy_match::Matcher;
+
+fn run_test<'a>(query: &str, items: &'a [&'a str]) -> Vec<String> {
+ let query = FuzzyQuery::new(query);
+ let matcher = Matcher::default();
+ items
+ .iter()
+ .filter_map(|item| {
+ let (_, indicies) = query.fuzzy_indicies(item, &matcher)?;
+ let matched_string = indicies
+ .iter()
+ .map(|&pos| item.chars().nth(pos).unwrap())
+ .collect();
+ Some(matched_string)
+ })
+ .collect()
+}
+
+#[test]
+fn match_single_value() {
+ let matches = run_test("foo", &["foobar", "foo", "bar"]);
+ assert_eq!(matches, &["foo", "foo"])
+}
+
+#[test]
+fn match_multiple_values() {
+ let matches = run_test(
+ "foo bar",
+ &["foo bar", "foo bar", "bar foo", "bar", "foo"],
+ );
+ assert_eq!(matches, &["foobar", "foobar", "barfoo"])
+}
+
+#[test]
+fn space_escape() {
+ let matches = run_test(r"foo\ bar", &["bar foo", "foo bar", "foobar"]);
+ assert_eq!(matches, &["foo bar"])
+}
+
+#[test]
+fn trim() {
+ let matches = run_test(r" foo bar ", &["bar foo", "foo bar", "foobar"]);
+ assert_eq!(matches, &["barfoo", "foobar", "foobar"]);
+ let matches = run_test(r" foo bar\ ", &["bar foo", "foo bar", "foobar"]);
+ assert_eq!(matches, &["bar foo"])
+}