Fix menu::menu_functions::find_common_string (#363)

* Fix `menu::menu_functions::find_common_string`

* Add test for `DefaultCompleter`

* Add test strings
This commit is contained in:
Tomoki Aonuma 2022-03-27 19:46:57 +09:00 committed by GitHub
parent e982abf7e2
commit 7c594b16ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 77 additions and 4 deletions

View File

@ -348,3 +348,40 @@ impl CompletionNode {
completions
}
}
#[cfg(test)]
mod tests {
#[test]
fn default_completer_with_non_ansi() {
use super::*;
let mut completions = DefaultCompleter::default();
completions.insert(
vec!["", "", ""]
.iter()
.map(|s| s.to_string())
.collect(),
);
assert_eq!(
completions.complete("", 3),
vec![
Suggestion {
value: "".into(),
description: None,
span: Span { start: 0, end: 3 },
},
Suggestion {
value: "".into(),
description: None,
span: Span { start: 0, end: 3 },
},
Suggestion {
value: "".into(),
description: None,
span: Span { start: 0, end: 3 },
},
]
);
}
}

View File

@ -61,6 +61,8 @@ fn main() -> Result<()> {
"abaaac".into(),
"abaaaxyc".into(),
"abaaarabc".into(),
"こんにちは世界".into(),
"こんばんは世界".into(),
];
let completer = Box::new(DefaultCompleter::new_with_wordlen(commands.clone(), 2));

View File

@ -138,15 +138,15 @@ pub fn find_common_string(values: &[Suggestion]) -> (Option<&Suggestion>, Option
} else {
first
.value
.chars()
.zip(suggestion.value.chars())
.position(|(mut lhs, mut rhs)| {
.char_indices()
.zip(suggestion.value.char_indices())
.find(|((_, mut lhs), (_, mut rhs))| {
lhs.make_ascii_lowercase();
rhs.make_ascii_lowercase();
lhs != rhs
})
.map(|new_index| match index {
.map(|((new_index, _), _)| match index {
Some(index) => {
if index <= new_index {
index
@ -403,4 +403,38 @@ mod tests {
let res = string_difference(new_string, old_string);
assert_eq!(res, (4, "b = ñ "));
}
#[test]
fn find_common_string_with_ansi() {
use crate::Span;
let input: Vec<_> = ["nushell", "null"]
.into_iter()
.map(|s| Suggestion {
value: s.into(),
description: None,
span: Span::new(0, s.len()),
})
.collect();
let res = find_common_string(&input);
assert!(matches!(res, (Some(elem), Some(2)) if elem == &input[0]));
}
#[test]
fn find_common_string_with_non_ansi() {
use crate::Span;
let input: Vec<_> = ["", ""]
.into_iter()
.map(|s| Suggestion {
value: s.into(),
description: None,
span: Span::new(0, s.len()),
})
.collect();
let res = find_common_string(&input);
assert!(matches!(res, (Some(elem), Some(6)) if elem == &input[0]));
}
}