aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide
diff options
context:
space:
mode:
authorAaron Loucks <[email protected]>2020-05-31 16:33:48 +0100
committerAaron Loucks <[email protected]>2020-06-03 11:46:07 +0100
commit5837acce532e0cd65a1c0cb8c03cc18a4c22f327 (patch)
treefa162f2dc57a53be9a6090fb3776cc9586332551 /crates/ra_ide
parent4c655c01f31ceffae4f8219f9706992e0e7f188a (diff)
Add basic hover and completion doc tests for macro generated items
Diffstat (limited to 'crates/ra_ide')
-rw-r--r--crates/ra_ide/src/completion.rs78
-rw-r--r--crates/ra_ide/src/hover.rs104
2 files changed, 181 insertions, 1 deletions
diff --git a/crates/ra_ide/src/completion.rs b/crates/ra_ide/src/completion.rs
index d890b69d2..a721e23c6 100644
--- a/crates/ra_ide/src/completion.rs
+++ b/crates/ra_ide/src/completion.rs
@@ -125,3 +125,81 @@ pub(crate) fn completions(
125 125
126 Some(acc) 126 Some(acc)
127} 127}
128
129#[cfg(test)]
130mod tests {
131 use crate::completion::completion_config::CompletionConfig;
132 use crate::mock_analysis::analysis_and_position;
133
134 struct DetailAndDocumentation<'a> {
135 detail: &'a str,
136 documentation: &'a str,
137 }
138
139 fn check_detail_and_documentation(fixture: &str, expected: DetailAndDocumentation) {
140 let (analysis, position) = analysis_and_position(fixture);
141 let config = CompletionConfig::default();
142 let completions = analysis.completions(&config, position).unwrap().unwrap();
143 for item in completions {
144 if item.detail() == Some(expected.detail) {
145 let opt = item.documentation();
146 let doc = opt.as_ref().map(|it| it.as_str());
147 assert_eq!(doc, Some(expected.documentation));
148 return;
149 }
150 }
151 panic!("completion detail not found: {}", expected.detail)
152 }
153
154 #[test]
155 fn test_completion_detail_from_macro_generated_struct_fn_doc_attr() {
156 check_detail_and_documentation(
157 r#"
158 //- /lib.rs
159 macro_rules! bar {
160 () => {
161 struct Bar;
162 impl Bar {
163 #[doc = "Do the foo"]
164 fn foo(&self) {}
165 }
166 }
167 }
168
169 bar!();
170
171 fn foo() {
172 let bar = Bar;
173 bar.fo<|>;
174 }
175 "#,
176 DetailAndDocumentation { detail: "fn foo(&self)", documentation: "Do the foo" },
177 );
178 }
179
180 #[test]
181 fn test_completion_detail_from_macro_generated_struct_fn_doc_comment() {
182 check_detail_and_documentation(
183 r#"
184 //- /lib.rs
185 macro_rules! bar {
186 () => {
187 struct Bar;
188 impl Bar {
189 /// Do the foo
190 fn foo(&self) {}
191 }
192 }
193 }
194
195 bar!();
196
197 fn foo() {
198 let bar = Bar;
199 bar.fo<|>;
200 }
201 "#,
202 DetailAndDocumentation { detail: "fn foo(&self)", documentation: " Do the foo" },
203 );
204 }
205}
diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs
index e25a7dacf..731fc3673 100644
--- a/crates/ra_ide/src/hover.rs
+++ b/crates/ra_ide/src/hover.rs
@@ -256,7 +256,7 @@ fn expand_doc_attrs(owner: &dyn ast::AttrsOwner) -> Option<String> {
256 if docs.is_empty() { 256 if docs.is_empty() {
257 None 257 None
258 } else { 258 } else {
259 Some(docs) 259 Some(docs.trim_end_matches("\n\n").to_owned())
260 } 260 }
261} 261}
262 262
@@ -996,4 +996,106 @@ fn func(foo: i32) { if true { <|>foo; }; }
996 &["mod my"], 996 &["mod my"],
997 ); 997 );
998 } 998 }
999
1000 #[test]
1001 fn test_hover_struct_doc_comment() {
1002 check_hover_result(
1003 r#"
1004 //- /lib.rs
1005 /// bar docs
1006 struct Bar;
1007
1008 fn foo() {
1009 let bar = Ba<|>r;
1010 }
1011 "#,
1012 &["struct Bar\n```\n___\n\nbar docs"],
1013 );
1014 }
1015
1016 #[test]
1017 fn test_hover_struct_doc_attr() {
1018 check_hover_result(
1019 r#"
1020 //- /lib.rs
1021 #[doc = "bar docs"]
1022 struct Bar;
1023
1024 fn foo() {
1025 let bar = Ba<|>r;
1026 }
1027 "#,
1028 &["struct Bar\n```\n___\n\nbar docs"],
1029 );
1030 }
1031
1032 #[test]
1033 fn test_hover_struct_doc_attr_multiple_and_mixed() {
1034 check_hover_result(
1035 r#"
1036 //- /lib.rs
1037 /// bar docs 0
1038 #[doc = "bar docs 1"]
1039 #[doc = "bar docs 2"]
1040 struct Bar;
1041
1042 fn foo() {
1043 let bar = Ba<|>r;
1044 }
1045 "#,
1046 &["struct Bar\n```\n___\n\nbar docs 0\n\nbar docs 1\n\nbar docs 2"],
1047 );
1048 }
1049
1050 #[test]
1051 fn test_hover_macro_generated_struct_fn_doc_comment() {
1052 check_hover_result(
1053 r#"
1054 //- /lib.rs
1055 macro_rules! bar {
1056 () => {
1057 struct Bar;
1058 impl Bar {
1059 /// Do the foo
1060 fn foo(&self) {}
1061 }
1062 }
1063 }
1064
1065 bar!();
1066
1067 fn foo() {
1068 let bar = Bar;
1069 bar.fo<|>o();
1070 }
1071 "#,
1072 &["Bar\n```\n\n```rust\nfn foo(&self)\n```\n___\n\n Do the foo"],
1073 );
1074 }
1075
1076 #[test]
1077 fn test_hover_macro_generated_struct_fn_doc_attr() {
1078 check_hover_result(
1079 r#"
1080 //- /lib.rs
1081 macro_rules! bar {
1082 () => {
1083 struct Bar;
1084 impl Bar {
1085 #[doc = "Do the foo"]
1086 fn foo(&self) {}
1087 }
1088 }
1089 }
1090
1091 bar!();
1092
1093 fn foo() {
1094 let bar = Bar;
1095 bar.fo<|>o();
1096 }
1097 "#,
1098 &["Bar\n```\n\n```rust\nfn foo(&self)\n```\n___\n\nDo the foo"],
1099 );
1100 }
999} 1101}