1 //! Tests and test utilities for completions.
3 //! Most tests live in this module or its submodules. The tests in these submodules are "location"
4 //! oriented, that is they try to check completions for something like type position, param position
6 //! Tests that are more orientated towards specific completion types like visibility checks of path
7 //! completions or `check_edit` tests usually live in their respective completion modules instead.
8 //! This gives this test module and its submodules here the main purpose of giving the developer an
9 //! overview of whats being completed where, not how.
26 use hir::{PrefixKind, Semantics};
28 base_db::{fixture::ChangeFixture, FileLoader, FilePosition},
30 insert_use::{ImportGranularity, InsertUseConfig},
35 use itertools::Itertools;
36 use stdx::{format_to, trim_indent};
37 use syntax::{AstNode, NodeOrToken, SyntaxElement};
38 use test_utils::assert_eq_text;
40 use crate::{item::CompletionKind, CompletionConfig, CompletionItem};
42 /// Lots of basic item definitions
43 const BASE_ITEMS_FIXTURE: &str = r#"
44 enum Enum { TupleV(u32), RecordV { field: u32 }, UnitV }
45 use self::Enum::TupleV;
49 static STATIC: Unit = Unit;
50 const CONST: Unit = Unit;
51 struct Record { field: u32 }
56 #[rustc_builtin_macro]
59 union Union { field: i32 }
62 pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
63 enable_postfix_completions: true,
64 enable_imports_on_the_fly: true,
65 enable_self_on_the_fly: true,
66 add_call_parenthesis: true,
67 add_call_argument_snippets: true,
68 snippet_cap: SnippetCap::new(true),
69 insert_use: InsertUseConfig {
70 granularity: ImportGranularity::Crate,
71 prefix_kind: PrefixKind::Plain,
72 enforce_granularity: true,
74 skip_glob_imports: true,
78 pub(crate) fn completion_list(code: &str) -> String {
79 completion_list_with_config(TEST_CONFIG, code)
82 fn completion_list_with_config(config: CompletionConfig, code: &str) -> String {
83 // filter out all but one builtintype completion for smaller test outputs
84 let items = get_all_items(config, code);
85 let mut bt_seen = false;
89 it.completion_kind != CompletionKind::BuiltinType || !mem::replace(&mut bt_seen, true)
92 render_completion_list(items)
95 /// Creates analysis from a multi-file fixture, returns positions marked with $0.
96 pub(crate) fn position(ra_fixture: &str) -> (RootDatabase, FilePosition) {
97 let change_fixture = ChangeFixture::parse(ra_fixture);
98 let mut database = RootDatabase::default();
99 database.apply_change(change_fixture.change);
100 let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
101 let offset = range_or_offset.expect_offset();
102 (database, FilePosition { file_id, offset })
105 pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> {
106 do_completion_with_config(TEST_CONFIG, code, kind)
109 pub(crate) fn do_completion_with_config(
110 config: CompletionConfig,
112 kind: CompletionKind,
113 ) -> Vec<CompletionItem> {
114 get_all_items(config, code)
116 .filter(|c| c.completion_kind == kind)
117 .sorted_by(|l, r| l.label().cmp(r.label()))
121 pub(crate) fn filtered_completion_list(code: &str, kind: CompletionKind) -> String {
122 filtered_completion_list_with_config(TEST_CONFIG, code, kind)
125 pub(crate) fn filtered_completion_list_with_config(
126 config: CompletionConfig,
128 kind: CompletionKind,
130 let kind_completions: Vec<CompletionItem> =
131 get_all_items(config, code).into_iter().filter(|c| c.completion_kind == kind).collect();
132 render_completion_list(kind_completions)
135 fn render_completion_list(completions: Vec<CompletionItem>) -> String {
136 fn monospace_width(s: &str) -> usize {
140 completions.iter().map(|it| monospace_width(it.label())).max().unwrap_or_default().min(22);
144 let tag = it.kind().unwrap().tag();
145 let var_name = format!("{} {}", tag, it.label());
146 let mut buf = var_name;
147 if let Some(detail) = it.detail() {
148 let width = label_width.saturating_sub(monospace_width(it.label()));
149 format_to!(buf, "{:width$} {}", "", detail, width = width);
152 format_to!(buf, " DEPRECATED");
154 format_to!(buf, "\n");
160 pub(crate) fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
161 check_edit_with_config(TEST_CONFIG, what, ra_fixture_before, ra_fixture_after)
164 pub(crate) fn check_edit_with_config(
165 config: CompletionConfig,
167 ra_fixture_before: &str,
168 ra_fixture_after: &str,
170 let ra_fixture_after = trim_indent(ra_fixture_after);
171 let (db, position) = position(ra_fixture_before);
172 let completions: Vec<CompletionItem> =
173 crate::completions(&db, &config, position).unwrap().into();
174 let (completion,) = completions
176 .filter(|it| it.lookup() == what)
178 .unwrap_or_else(|| panic!("can't find {:?} completion in {:#?}", what, completions));
179 let mut actual = db.file_text(position.file_id).to_string();
181 let mut combined_edit = completion.text_edit().to_owned();
182 if let Some(import_text_edit) =
183 completion.import_to_add().and_then(|edit| edit.to_text_edit(config.insert_use))
185 combined_edit.union(import_text_edit).expect(
186 "Failed to apply completion resolve changes: change ranges overlap, but should not",
190 combined_edit.apply(&mut actual);
191 assert_eq_text!(&ra_fixture_after, &actual)
194 pub(crate) fn check_pattern_is_applicable(code: &str, check: impl FnOnce(SyntaxElement) -> bool) {
195 let (db, pos) = position(code);
197 let sema = Semantics::new(&db);
198 let original_file = sema.parse(pos.file_id);
199 let token = original_file.syntax().token_at_offset(pos.offset).left_biased().unwrap();
200 assert!(check(NodeOrToken::Token(token)));
203 pub(crate) fn check_pattern_is_not_applicable(code: &str, check: fn(SyntaxElement) -> bool) {
204 let (db, pos) = position(code);
205 let sema = Semantics::new(&db);
206 let original_file = sema.parse(pos.file_id);
207 let token = original_file.syntax().token_at_offset(pos.offset).left_biased().unwrap();
208 assert!(!check(NodeOrToken::Token(token)));
211 pub(crate) fn get_all_items(config: CompletionConfig, code: &str) -> Vec<CompletionItem> {
212 let (db, position) = position(code);
213 crate::completions(&db, &config, position).map_or_else(Vec::default, Into::into)
216 fn check_no_completion(ra_fixture: &str) {
217 let (db, position) = position(ra_fixture);
220 crate::completions(&db, &TEST_CONFIG, position).is_none(),
221 "Completions were generated, but weren't expected"
226 fn test_no_completions_required() {
227 cov_mark::check!(no_completion_required);
228 check_no_completion(r#"fn foo() { for i i$0 }"#);