aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/tests
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis/tests')
-rw-r--r--crates/ra_analysis/tests/test/main.rs (renamed from crates/ra_analysis/tests/tests.rs)34
-rw-r--r--crates/ra_analysis/tests/test/runnables.rs109
-rw-r--r--crates/ra_analysis/tests/test/type_of.rs77
3 files changed, 213 insertions, 7 deletions
diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/test/main.rs
index a314fbc40..bfdf8aef2 100644
--- a/crates/ra_analysis/tests/tests.rs
+++ b/crates/ra_analysis/tests/test/main.rs
@@ -1,9 +1,12 @@
1mod runnables;
2mod type_of;
3
1use ra_syntax::TextRange; 4use ra_syntax::TextRange;
2use test_utils::{assert_eq_dbg, assert_eq_text}; 5use test_utils::{assert_eq_dbg, assert_eq_text};
3 6
4use ra_analysis::{ 7use ra_analysis::{
5 mock_analysis::{analysis_and_position, single_file, single_file_with_position, MockAnalysis}, 8 mock_analysis::{analysis_and_position, single_file, single_file_with_position, MockAnalysis},
6 AnalysisChange, CrateGraph, FileId, FnSignatureInfo, 9 AnalysisChange, CrateGraph, FileId, FnSignatureInfo, Query
7}; 10};
8 11
9fn get_signature(text: &str) -> (FnSignatureInfo, Option<usize>) { 12fn get_signature(text: &str) -> (FnSignatureInfo, Option<usize>) {
@@ -25,7 +28,7 @@ fn approximate_resolve_works_in_items() {
25 assert_eq_dbg( 28 assert_eq_dbg(
26 r#"ReferenceResolution { 29 r#"ReferenceResolution {
27 reference_range: [23; 26), 30 reference_range: [23; 26),
28 resolves_to: [(FileId(1), FileSymbol { name: "Foo", node_range: [0; 11), kind: STRUCT_DEF })] 31 resolves_to: [NavigationTarget { file_id: FileId(1), name: "Foo", kind: STRUCT_DEF, range: [0; 11), ptr: Some(LocalSyntaxPtr { range: [0; 11), kind: STRUCT_DEF }) }]
29 }"#, 32 }"#,
30 &symbols, 33 &symbols,
31 ); 34 );
@@ -46,7 +49,7 @@ fn test_resolve_module() {
46 assert_eq_dbg( 49 assert_eq_dbg(
47 r#"ReferenceResolution { 50 r#"ReferenceResolution {
48 reference_range: [4; 7), 51 reference_range: [4; 7),
49 resolves_to: [(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })] 52 resolves_to: [NavigationTarget { file_id: FileId(2), name: "foo", kind: MODULE, range: [0; 0), ptr: None }]
50 }"#, 53 }"#,
51 &symbols, 54 &symbols,
52 ); 55 );
@@ -64,7 +67,7 @@ fn test_resolve_module() {
64 assert_eq_dbg( 67 assert_eq_dbg(
65 r#"ReferenceResolution { 68 r#"ReferenceResolution {
66 reference_range: [4; 7), 69 reference_range: [4; 7),
67 resolves_to: [(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })] 70 resolves_to: [NavigationTarget { file_id: FileId(2), name: "foo", kind: MODULE, range: [0; 0), ptr: None }]
68 }"#, 71 }"#,
69 &symbols, 72 &symbols,
70 ); 73 );
@@ -107,7 +110,7 @@ fn test_resolve_parent_module() {
107 ); 110 );
108 let symbols = analysis.parent_module(pos).unwrap(); 111 let symbols = analysis.parent_module(pos).unwrap();
109 assert_eq_dbg( 112 assert_eq_dbg(
110 r#"[(FileId(1), FileSymbol { name: "foo", node_range: [4; 7), kind: MODULE })]"#, 113 r#"[NavigationTarget { file_id: FileId(1), name: "foo", kind: MODULE, range: [4; 7), ptr: None }]"#,
111 &symbols, 114 &symbols,
112 ); 115 );
113} 116}
@@ -126,7 +129,7 @@ fn test_resolve_parent_module_for_inline() {
126 ); 129 );
127 let symbols = analysis.parent_module(pos).unwrap(); 130 let symbols = analysis.parent_module(pos).unwrap();
128 assert_eq_dbg( 131 assert_eq_dbg(
129 r#"[(FileId(1), FileSymbol { name: "bar", node_range: [18; 21), kind: MODULE })]"#, 132 r#"[NavigationTarget { file_id: FileId(1), name: "bar", kind: MODULE, range: [18; 21), ptr: None }]"#,
130 &symbols, 133 &symbols,
131 ); 134 );
132} 135}
@@ -528,10 +531,11 @@ fn test_rename_for_mut_param() {
528 }"#, 531 }"#,
529 ); 532 );
530} 533}
534
531fn test_rename(text: &str, new_name: &str, expected: &str) { 535fn test_rename(text: &str, new_name: &str, expected: &str) {
532 let (analysis, position) = single_file_with_position(text); 536 let (analysis, position) = single_file_with_position(text);
533 let edits = analysis.rename(position, new_name).unwrap(); 537 let edits = analysis.rename(position, new_name).unwrap();
534 let mut text_edit_bulder = ra_text_edit::TextEditBuilder::new(); 538 let mut text_edit_bulder = ra_text_edit::TextEditBuilder::default();
535 let mut file_id: Option<FileId> = None; 539 let mut file_id: Option<FileId> = None;
536 for edit in edits { 540 for edit in edits {
537 file_id = Some(edit.file_id); 541 file_id = Some(edit.file_id);
@@ -544,3 +548,19 @@ fn test_rename(text: &str, new_name: &str, expected: &str) {
544 .apply(&*analysis.file_text(file_id.unwrap())); 548 .apply(&*analysis.file_text(file_id.unwrap()));
545 assert_eq_text!(expected, &*result); 549 assert_eq_text!(expected, &*result);
546} 550}
551
552#[test]
553fn world_symbols_include_stuff_from_macros() {
554 let (analysis, _) = single_file(
555 "
556salsa::query_group! {
557pub trait HirDatabase: SyntaxDatabase {}
558}
559 ",
560 );
561
562 let mut symbols = analysis.symbol_search(Query::new("Hir".into())).unwrap();
563 let s = symbols.pop().unwrap();
564 assert_eq!(s.name(), "HirDatabase");
565 assert_eq!(s.range(), TextRange::from_to(33.into(), 44.into()));
566}
diff --git a/crates/ra_analysis/tests/test/runnables.rs b/crates/ra_analysis/tests/test/runnables.rs
new file mode 100644
index 000000000..e6e0afbc3
--- /dev/null
+++ b/crates/ra_analysis/tests/test/runnables.rs
@@ -0,0 +1,109 @@
1use test_utils::assert_eq_dbg;
2
3use ra_analysis::mock_analysis::analysis_and_position;
4
5#[test]
6fn test_runnables() {
7 let (analysis, pos) = analysis_and_position(
8 r#"
9 //- /lib.rs
10 <|> //empty
11 fn main() {}
12
13 #[test]
14 fn test_foo() {}
15
16 #[test]
17 #[ignore]
18 fn test_foo() {}
19 "#,
20 );
21 let runnables = analysis.runnables(pos.file_id).unwrap();
22 assert_eq_dbg(
23 r#"[Runnable { range: [1; 21), kind: Bin },
24 Runnable { range: [22; 46), kind: Test { name: "test_foo" } },
25 Runnable { range: [47; 81), kind: Test { name: "test_foo" } }]"#,
26 &runnables,
27 )
28}
29
30#[test]
31fn test_runnables_module() {
32 let (analysis, pos) = analysis_and_position(
33 r#"
34 //- /lib.rs
35 <|> //empty
36 mod test_mod {
37 #[test]
38 fn test_foo1() {}
39 }
40 "#,
41 );
42 let runnables = analysis.runnables(pos.file_id).unwrap();
43 assert_eq_dbg(
44 r#"[Runnable { range: [1; 59), kind: TestMod { path: "test_mod" } },
45 Runnable { range: [28; 57), kind: Test { name: "test_foo1" } }]"#,
46 &runnables,
47 )
48}
49
50#[test]
51fn test_runnables_one_depth_layer_module() {
52 let (analysis, pos) = analysis_and_position(
53 r#"
54 //- /lib.rs
55 <|> //empty
56 mod foo {
57 mod test_mod {
58 #[test]
59 fn test_foo1() {}
60 }
61 }
62 "#,
63 );
64 let runnables = analysis.runnables(pos.file_id).unwrap();
65 assert_eq_dbg(
66 r#"[Runnable { range: [23; 85), kind: TestMod { path: "foo::test_mod" } },
67 Runnable { range: [46; 79), kind: Test { name: "test_foo1" } }]"#,
68 &runnables,
69 )
70}
71
72#[test]
73fn test_runnables_multiple_depth_module() {
74 let (analysis, pos) = analysis_and_position(
75 r#"
76 //- /lib.rs
77 <|> //empty
78 mod foo {
79 mod bar {
80 mod test_mod {
81 #[test]
82 fn test_foo1() {}
83 }
84 }
85 }
86 "#,
87 );
88 let runnables = analysis.runnables(pos.file_id).unwrap();
89 assert_eq_dbg(
90 r#"[Runnable { range: [41; 115), kind: TestMod { path: "foo::bar::test_mod" } },
91 Runnable { range: [68; 105), kind: Test { name: "test_foo1" } }]"#,
92 &runnables,
93 )
94}
95
96#[test]
97fn test_runnables_no_test_function_in_module() {
98 let (analysis, pos) = analysis_and_position(
99 r#"
100 //- /lib.rs
101 <|> //empty
102 mod test_mod {
103 fn foo1() {}
104 }
105 "#,
106 );
107 let runnables = analysis.runnables(pos.file_id).unwrap();
108 assert_eq_dbg(r#"[]"#, &runnables)
109}
diff --git a/crates/ra_analysis/tests/test/type_of.rs b/crates/ra_analysis/tests/test/type_of.rs
new file mode 100644
index 000000000..9d15b52a8
--- /dev/null
+++ b/crates/ra_analysis/tests/test/type_of.rs
@@ -0,0 +1,77 @@
1use ra_analysis::mock_analysis::single_file_with_range;
2
3#[test]
4fn test_type_of_for_function() {
5 let (analysis, range) = single_file_with_range(
6 "
7 pub fn foo() -> u32 { 1 };
8
9 fn main() {
10 let foo_test = <|>foo()<|>;
11 }
12 ",
13 );
14
15 let type_name = analysis.type_of(range).unwrap().unwrap();
16 assert_eq!("u32", &type_name);
17}
18
19// FIXME: improve type_of to make this work
20#[test]
21fn test_type_of_for_num() {
22 let (analysis, range) = single_file_with_range(
23 r#"
24 fn main() {
25 let foo_test = <|>"foo"<|>;
26 }
27 "#,
28 );
29
30 assert!(analysis.type_of(range).unwrap().is_none());
31}
32// FIXME: improve type_of to make this work
33#[test]
34fn test_type_of_for_binding() {
35 let (analysis, range) = single_file_with_range(
36 "
37 pub fn foo() -> u32 { 1 };
38
39 fn main() {
40 let <|>foo_test<|> = foo();
41 }
42 ",
43 );
44
45 assert!(analysis.type_of(range).unwrap().is_none());
46}
47
48// FIXME: improve type_of to make this work
49#[test]
50fn test_type_of_for_expr_1() {
51 let (analysis, range) = single_file_with_range(
52 "
53 fn main() {
54 let foo = <|>1 + foo_test<|>;
55 }
56 ",
57 );
58
59 let type_name = analysis.type_of(range).unwrap().unwrap();
60 assert_eq!("[unknown]", &type_name);
61}
62
63// FIXME: improve type_of to make this work
64#[test]
65fn test_type_of_for_expr_2() {
66 let (analysis, range) = single_file_with_range(
67 "
68 fn main() {
69 let foo: usize = 1;
70 let bar = <|>1 + foo_test<|>;
71 }
72 ",
73 );
74
75 let type_name = analysis.type_of(range).unwrap().unwrap();
76 assert_eq!("[unknown]", &type_name);
77}