Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
max-heller committed Jul 30, 2023
1 parent 4bb7702 commit 91581be
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 56 deletions.
25 changes: 5 additions & 20 deletions crates/ide-completion/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,12 @@ impl Builder {
chars.next().is_some_and(unicode_ident::is_xid_start)
&& chars.all(unicode_ident::is_xid_continue)
})
.join(", ");
// Deliberately concatenated without separators as adding separators e.g.
// `alias1, alias2` results in LSP clients continuing to display the completion even
// after typing a comma or space.
.join("");
if !lookup_doc_aliases.is_empty() {
lookup = SmolStr::from(format!("{lookup} {lookup_doc_aliases}"));
lookup = SmolStr::from(format!("{lookup}{lookup_doc_aliases}"));
}
}
if let [import_edit] = &*self.imports_to_add {
Expand Down Expand Up @@ -567,12 +570,9 @@ impl Builder {

#[cfg(test)]
mod tests {
use ide_db::SymbolKind;
use itertools::Itertools;
use test_utils::assert_eq_text;

use crate::{CompletionItem, CompletionItemKind};

use super::{
CompletionRelevance, CompletionRelevancePostfixMatch, CompletionRelevanceTypeMatch,
};
Expand Down Expand Up @@ -647,19 +647,4 @@ mod tests {

check_relevance_score_ordered(expected_relevance_order);
}

#[test]
fn exclude_non_identifier_aliases_from_lookup() {
let mut item = CompletionItem::new(
CompletionItemKind::SymbolKind(SymbolKind::Trait),
Default::default(),
"PartialOrd",
);
let aliases = [">", "<", "<=", ">="];
item.doc_aliases(aliases.iter().map(|&alias| alias.into()).collect());
let item = item.build(&Default::default());
for alias in aliases {
assert!(!item.lookup().contains(alias));
}
}
}
28 changes: 4 additions & 24 deletions crates/ide-completion/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ mod type_pos;
mod use_tree;
mod visibility;

use std::ops::ControlFlow;

use expect_test::Expect;
use hir::PrefixKind;
use ide_db::{
Expand Down Expand Up @@ -187,29 +185,11 @@ pub(crate) fn check_edit_with_config(
let (db, position) = position(ra_fixture_before);
let completions: Vec<CompletionItem> =
crate::completions(&db, &config, position, None).unwrap();
let matching = completions
let (completion,) = completions
.iter()
// Match IDE behavior by considering completions as matching if `what` is a subsequence
// of the completion's lookup text.
.filter(|it| {
let mut lookup = it.lookup().chars();
what.chars().all(|c| lookup.contains(&c))
})
// Select the first exact match if one exists, or the first subsequence match if not
.try_fold(None, |first_match, completion| {
let exact_match = completion.lookup() == what;
if exact_match {
ControlFlow::Break(completion)
} else {
ControlFlow::Continue(first_match.or(Some(completion)))
}
});
let completion = match matching {
ControlFlow::Continue(first_match) => first_match
.unwrap_or_else(|| panic!("can't find {what:?} completion in {completions:#?}")),
ControlFlow::Break(exact_match) => exact_match,
};

.filter(|it| it.lookup() == what)
.collect_tuple()
.unwrap_or_else(|| panic!("can't find {what:?} completion in {completions:#?}"));
let mut actual = db.file_text(position.file_id).to_string();

let mut combined_edit = completion.text_edit.clone();
Expand Down
22 changes: 10 additions & 12 deletions crates/ide-completion/src/tests/special.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1282,26 +1282,24 @@ fn here_we_go() {
}

#[test]
fn completion_filtering_excludes_non_identifier_aliases() {
// Catch panic instead of using `#[should_panic]` as style check bans
// `#[should_panic]`. Making `check_edit` return a result would require
// a lot of test changes.
std::panic::catch_unwind(|| {
check_edit(
"Partial>",
r#"
fn completion_filtering_excludes_non_identifier_doc_aliases() {
check_edit(
"PartialOrdcmporder",
r#"
#[doc(alias = ">")]
#[doc(alias = "cmp")]
#[doc(alias = "order")]
trait PartialOrd {}
struct Foo<T: Partial$0
"#,
r#"
r#"
#[doc(alias = ">")]
#[doc(alias = "cmp")]
#[doc(alias = "order")]
trait PartialOrd {}
struct Foo<T: PartialOrd
"#,
)
})
.unwrap_err();
);
}

0 comments on commit 91581be

Please sign in to comment.