aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-02-09 15:13:11 +0000
committerAleksey Kladov <[email protected]>2021-02-09 15:13:11 +0000
commit2ac20b05f18895654ced1d243e99092441a94f86 (patch)
tree58793a5db718831aeca8c998d6496eb8da70c043 /crates/ide/src
parent2dc67c85b94a8e858706568c4116b3123348941b (diff)
Cleanup tests
Diffstat (limited to 'crates/ide/src')
-rw-r--r--crates/ide/src/fixture.rs16
-rw-r--r--crates/ide/src/goto_definition.rs12
-rw-r--r--crates/ide/src/parent_module.rs80
3 files changed, 53 insertions, 55 deletions
diff --git a/crates/ide/src/fixture.rs b/crates/ide/src/fixture.rs
index cc8218885..cc6641ba1 100644
--- a/crates/ide/src/fixture.rs
+++ b/crates/ide/src/fixture.rs
@@ -1,5 +1,6 @@
1//! Utilities for creating `Analysis` instances for tests. 1//! Utilities for creating `Analysis` instances for tests.
2use ide_db::base_db::fixture::ChangeFixture; 2use ide_db::base_db::fixture::ChangeFixture;
3use syntax::{TextRange, TextSize};
3use test_utils::{extract_annotations, RangeOrOffset}; 4use test_utils::{extract_annotations, RangeOrOffset};
4 5
5use crate::{Analysis, AnalysisHost, FileId, FilePosition, FileRange}; 6use crate::{Analysis, AnalysisHost, FileId, FilePosition, FileRange};
@@ -68,3 +69,18 @@ pub(crate) fn annotations(ra_fixture: &str) -> (Analysis, FilePosition, Vec<(Fil
68 .collect(); 69 .collect();
69 (host.analysis(), FilePosition { file_id, offset }, annotations) 70 (host.analysis(), FilePosition { file_id, offset }, annotations)
70} 71}
72
73pub(crate) fn nav_target_annotation(ra_fixture: &str) -> (Analysis, FilePosition, FileRange) {
74 let (analysis, position, mut annotations) = annotations(ra_fixture);
75 let (mut expected, data) = annotations.pop().unwrap();
76 assert!(annotations.is_empty());
77 match data.as_str() {
78 "" => (),
79 "file" => {
80 expected.range =
81 TextRange::up_to(TextSize::of(&*analysis.file_text(expected.file_id).unwrap()))
82 }
83 data => panic!("bad data: {}", data),
84 }
85 (analysis, position, expected)
86}
diff --git a/crates/ide/src/goto_definition.rs b/crates/ide/src/goto_definition.rs
index 1a997fa40..c91eb1283 100644
--- a/crates/ide/src/goto_definition.rs
+++ b/crates/ide/src/goto_definition.rs
@@ -136,17 +136,7 @@ mod tests {
136 use crate::fixture; 136 use crate::fixture;
137 137
138 fn check(ra_fixture: &str) { 138 fn check(ra_fixture: &str) {
139 let (analysis, position, mut annotations) = fixture::annotations(ra_fixture); 139 let (analysis, position, expected) = fixture::nav_target_annotation(ra_fixture);
140 let (mut expected, data) = annotations.pop().unwrap();
141 match data.as_str() {
142 "" => (),
143 "file" => {
144 expected.range =
145 TextRange::up_to(TextSize::of(&*analysis.file_text(expected.file_id).unwrap()))
146 }
147 data => panic!("bad data: {}", data),
148 }
149
150 let mut navs = 140 let mut navs =
151 analysis.goto_definition(position).unwrap().expect("no definition found").info; 141 analysis.goto_definition(position).unwrap().expect("no definition found").info;
152 if navs.len() == 0 { 142 if navs.len() == 0 {
diff --git a/crates/ide/src/parent_module.rs b/crates/ide/src/parent_module.rs
index d343638fb..e5515ef2c 100644
--- a/crates/ide/src/parent_module.rs
+++ b/crates/ide/src/parent_module.rs
@@ -63,69 +63,61 @@ pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
63 63
64#[cfg(test)] 64#[cfg(test)]
65mod tests { 65mod tests {
66 use ide_db::base_db::FileRange;
66 use test_utils::mark; 67 use test_utils::mark;
67 68
68 use crate::fixture::{self}; 69 use crate::fixture;
70
71 fn check(ra_fixture: &str) {
72 let (analysis, position, expected) = fixture::nav_target_annotation(ra_fixture);
73 let mut navs = analysis.parent_module(position).unwrap();
74 assert_eq!(navs.len(), 1);
75 let nav = navs.pop().unwrap();
76 assert_eq!(expected, FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() });
77 }
69 78
70 #[test] 79 #[test]
71 fn test_resolve_parent_module() { 80 fn test_resolve_parent_module() {
72 let (analysis, pos) = fixture::position( 81 check(
73 " 82 r#"
74 //- /lib.rs 83//- /lib.rs
75 mod foo; 84 mod foo;
76 //- /foo.rs 85//^^^^^^^^
77 $0// empty 86
78 ", 87//- /foo.rs
88$0// empty
89"#,
79 ); 90 );
80 let nav = analysis.parent_module(pos).unwrap().pop().unwrap();
81 nav.assert_match("foo Module FileId(0) 0..8");
82 } 91 }
83 92
84 #[test] 93 #[test]
85 fn test_resolve_parent_module_on_module_decl() { 94 fn test_resolve_parent_module_on_module_decl() {
86 mark::check!(test_resolve_parent_module_on_module_decl); 95 mark::check!(test_resolve_parent_module_on_module_decl);
87 let (analysis, pos) = fixture::position( 96 check(
88 " 97 r#"
89 //- /lib.rs 98//- /lib.rs
90 mod foo; 99 mod foo;
91 100//^^^^^^^^
92 //- /foo.rs 101//- /foo.rs
93 mod $0bar; 102mod $0bar;
94 103
95 //- /foo/bar.rs 104//- /foo/bar.rs
96 // empty 105// empty
97 ", 106"#,
98 ); 107 );
99 let nav = analysis.parent_module(pos).unwrap().pop().unwrap();
100 nav.assert_match("foo Module FileId(0) 0..8");
101 } 108 }
102 109
103 #[test] 110 #[test]
104 fn test_resolve_parent_module_for_inline() { 111 fn test_resolve_parent_module_for_inline() {
105 let (analysis, pos) = fixture::position( 112 check(
106 "
107 //- /lib.rs
108 mod foo {
109 mod bar {
110 mod baz { $0 }
111 }
112 }
113 ",
114 );
115 let nav = analysis.parent_module(pos).unwrap().pop().unwrap();
116 nav.assert_match("baz Module FileId(0) 32..44");
117 }
118
119 #[test]
120 fn test_resolve_crate_root() {
121 let (analysis, file_id) = fixture::file(
122 r#" 113 r#"
123//- /main.rs 114//- /lib.rs
124mod foo; 115mod foo {
125//- /foo.rs 116 mod bar {
126$0 117 mod baz { $0 }
118 } //^^^^^^^^^^^^
119}
127"#, 120"#,
128 ); 121 );
129 assert_eq!(analysis.crate_for(file_id).unwrap().len(), 1);
130 } 122 }
131} 123}