aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion/src/tests.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_completion/src/tests.rs')
-rw-r--r--crates/ide_completion/src/tests.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/crates/ide_completion/src/tests.rs b/crates/ide_completion/src/tests.rs
new file mode 100644
index 000000000..4485a908e
--- /dev/null
+++ b/crates/ide_completion/src/tests.rs
@@ -0,0 +1,64 @@
1mod item_list;
2
3use expect_test::Expect;
4use stdx::format_to;
5
6use crate::{
7 test_utils::{self, get_all_items, TEST_CONFIG},
8 CompletionConfig, CompletionItem,
9};
10
11fn completion_list(code: &str) -> String {
12 completion_list_with_config(TEST_CONFIG, code)
13}
14
15fn completion_list_with_config(config: CompletionConfig, code: &str) -> String {
16 fn monospace_width(s: &str) -> usize {
17 s.chars().count()
18 }
19
20 let kind_completions: Vec<CompletionItem> = get_all_items(config, code).into_iter().collect();
21 let label_width = kind_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()
29 .map(|it| {
30 let tag = it.kind().unwrap().tag();
31 let var_name = format!("{} {}", tag, it.label());
32 let mut buf = var_name;
33 if let Some(detail) = it.detail() {
34 let width = label_width.saturating_sub(monospace_width(it.label()));
35 format_to!(buf, "{:width$} {}", "", detail, width = width);
36 }
37 if it.deprecated() {
38 format_to!(buf, " DEPRECATED");
39 }
40 format_to!(buf, "\n");
41 buf
42 })
43 .collect()
44}
45
46fn check(ra_fixture: &str, expect: Expect) {
47 let actual = completion_list(ra_fixture);
48 expect.assert_eq(&actual)
49}
50
51fn check_no_completion(ra_fixture: &str) {
52 let (db, position) = test_utils::position(ra_fixture);
53
54 assert!(
55 crate::completions(&db, &TEST_CONFIG, position).is_none(),
56 "Completions were generated, but weren't expected"
57 );
58}
59
60#[test]
61fn test_no_completions_required() {
62 cov_mark::check!(no_completion_required);
63 check_no_completion(r#"fn foo() { for i i$0 }"#);
64}