aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion/src/tests.rs
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-06-16 20:45:02 +0100
committerLukas Wirth <[email protected]>2021-06-16 20:51:52 +0100
commitaa644b55859c6b5c6695a5d4fb35d1b6efbbebcc (patch)
tree47119538effd381ecd8e15d422103512f2b47406 /crates/ide_completion/src/tests.rs
parent11115ebad8d0cb367478a4f154abe08c0c25aa95 (diff)
Move test_utils into tests module
Diffstat (limited to 'crates/ide_completion/src/tests.rs')
-rw-r--r--crates/ide_completion/src/tests.rs158
1 files changed, 130 insertions, 28 deletions
diff --git a/crates/ide_completion/src/tests.rs b/crates/ide_completion/src/tests.rs
index 2205603fa..1495924ea 100644
--- a/crates/ide_completion/src/tests.rs
+++ b/crates/ide_completion/src/tests.rs
@@ -1,11 +1,34 @@
1mod item_list; 1mod item_list;
2 2
3use expect_test::Expect; 3use hir::{PrefixKind, Semantics};
4use stdx::format_to; 4use ide_db::{
5 base_db::{fixture::ChangeFixture, FileLoader, FilePosition},
6 helpers::{
7 insert_use::{ImportGranularity, InsertUseConfig},
8 SnippetCap,
9 },
10 RootDatabase,
11};
12use itertools::Itertools;
13use stdx::{format_to, trim_indent};
14use syntax::{AstNode, NodeOrToken, SyntaxElement};
15use test_utils::assert_eq_text;
16
17use crate::{item::CompletionKind, CompletionConfig, CompletionItem};
5 18
6use crate::{ 19pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
7 test_utils::{self, get_all_items, TEST_CONFIG}, 20 enable_postfix_completions: true,
8 CompletionConfig, CompletionItem, 21 enable_imports_on_the_fly: true,
22 enable_self_on_the_fly: true,
23 add_call_parenthesis: true,
24 add_call_argument_snippets: true,
25 snippet_cap: SnippetCap::new(true),
26 insert_use: InsertUseConfig {
27 granularity: ImportGranularity::Crate,
28 prefix_kind: PrefixKind::Plain,
29 enforce_granularity: true,
30 group: true,
31 },
9}; 32};
10 33
11fn completion_list(code: &str) -> String { 34fn completion_list(code: &str) -> String {
@@ -13,18 +36,56 @@ fn completion_list(code: &str) -> String {
13} 36}
14 37
15fn completion_list_with_config(config: CompletionConfig, code: &str) -> String { 38fn completion_list_with_config(config: CompletionConfig, code: &str) -> String {
39 render_completion_list(get_all_items(config, code))
40}
41
42/// Creates analysis from a multi-file fixture, returns positions marked with $0.
43pub(crate) fn position(ra_fixture: &str) -> (RootDatabase, FilePosition) {
44 let change_fixture = ChangeFixture::parse(ra_fixture);
45 let mut database = RootDatabase::default();
46 database.apply_change(change_fixture.change);
47 let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
48 let offset = range_or_offset.expect_offset();
49 (database, FilePosition { file_id, offset })
50}
51
52pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> {
53 do_completion_with_config(TEST_CONFIG, code, kind)
54}
55
56pub(crate) fn do_completion_with_config(
57 config: CompletionConfig,
58 code: &str,
59 kind: CompletionKind,
60) -> Vec<CompletionItem> {
61 get_all_items(config, code)
62 .into_iter()
63 .filter(|c| c.completion_kind == kind)
64 .sorted_by(|l, r| l.label().cmp(r.label()))
65 .collect()
66}
67
68pub(crate) fn filtered_completion_list(code: &str, kind: CompletionKind) -> String {
69 filtered_completion_list_with_config(TEST_CONFIG, code, kind)
70}
71
72pub(crate) fn filtered_completion_list_with_config(
73 config: CompletionConfig,
74 code: &str,
75 kind: CompletionKind,
76) -> String {
77 let kind_completions: Vec<CompletionItem> =
78 get_all_items(config, code).into_iter().filter(|c| c.completion_kind == kind).collect();
79 render_completion_list(kind_completions)
80}
81
82fn render_completion_list(completions: Vec<CompletionItem>) -> String {
16 fn monospace_width(s: &str) -> usize { 83 fn monospace_width(s: &str) -> usize {
17 s.chars().count() 84 s.chars().count()
18 } 85 }
19 86 let label_width =
20 let kind_completions: Vec<CompletionItem> = get_all_items(config, code).into_iter().collect(); 87 completions.iter().map(|it| monospace_width(it.label())).max().unwrap_or_default().min(16);
21 let label_width = kind_completions 88 completions
22 .iter()
23 .map(|it| monospace_width(it.label()))
24 .max()
25 .unwrap_or_default()
26 .min(16);
27 kind_completions
28 .into_iter() 89 .into_iter()
29 .map(|it| { 90 .map(|it| {
30 let tag = it.kind().unwrap().tag(); 91 let tag = it.kind().unwrap().tag();
@@ -43,23 +104,64 @@ fn completion_list_with_config(config: CompletionConfig, code: &str) -> String {
43 .collect() 104 .collect()
44} 105}
45 106
46fn check(ra_fixture: &str, expect: Expect) { 107pub(crate) fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
47 let base = r#"#[rustc_builtin_macro] 108 check_edit_with_config(TEST_CONFIG, what, ra_fixture_before, ra_fixture_after)
48pub macro Clone {} 109}
49enum Enum { Variant } 110
50struct Struct {} 111pub(crate) fn check_edit_with_config(
51#[macro_export] 112 config: CompletionConfig,
52macro_rules! foo {} 113 what: &str,
53mod bar {} 114 ra_fixture_before: &str,
54const CONST: () = (); 115 ra_fixture_after: &str,
55trait Trait {} 116) {
56"#; 117 let ra_fixture_after = trim_indent(ra_fixture_after);
57 let actual = completion_list(&format!("{}{}", base, ra_fixture)); 118 let (db, position) = position(ra_fixture_before);
58 expect.assert_eq(&actual) 119 let completions: Vec<CompletionItem> =
120 crate::completions(&db, &config, position).unwrap().into();
121 let (completion,) = completions
122 .iter()
123 .filter(|it| it.lookup() == what)
124 .collect_tuple()
125 .unwrap_or_else(|| panic!("can't find {:?} completion in {:#?}", what, completions));
126 let mut actual = db.file_text(position.file_id).to_string();
127
128 let mut combined_edit = completion.text_edit().to_owned();
129 if let Some(import_text_edit) =
130 completion.import_to_add().and_then(|edit| edit.to_text_edit(config.insert_use))
131 {
132 combined_edit.union(import_text_edit).expect(
133 "Failed to apply completion resolve changes: change ranges overlap, but should not",
134 )
135 }
136
137 combined_edit.apply(&mut actual);
138 assert_eq_text!(&ra_fixture_after, &actual)
139}
140
141pub(crate) fn check_pattern_is_applicable(code: &str, check: impl FnOnce(SyntaxElement) -> bool) {
142 let (db, pos) = position(code);
143
144 let sema = Semantics::new(&db);
145 let original_file = sema.parse(pos.file_id);
146 let token = original_file.syntax().token_at_offset(pos.offset).left_biased().unwrap();
147 assert!(check(NodeOrToken::Token(token)));
148}
149
150pub(crate) fn check_pattern_is_not_applicable(code: &str, check: fn(SyntaxElement) -> bool) {
151 let (db, pos) = position(code);
152 let sema = Semantics::new(&db);
153 let original_file = sema.parse(pos.file_id);
154 let token = original_file.syntax().token_at_offset(pos.offset).left_biased().unwrap();
155 assert!(!check(NodeOrToken::Token(token)));
156}
157
158pub(crate) fn get_all_items(config: CompletionConfig, code: &str) -> Vec<CompletionItem> {
159 let (db, position) = position(code);
160 crate::completions(&db, &config, position).unwrap().into()
59} 161}
60 162
61fn check_no_completion(ra_fixture: &str) { 163fn check_no_completion(ra_fixture: &str) {
62 let (db, position) = test_utils::position(ra_fixture); 164 let (db, position) = position(ra_fixture);
63 165
64 assert!( 166 assert!(
65 crate::completions(&db, &TEST_CONFIG, position).is_none(), 167 crate::completions(&db, &TEST_CONFIG, position).is_none(),