diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-05-22 00:18:19 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2021-05-22 00:18:19 +0100 |
commit | ae24651e445444d4ed4275a717ac10980f2957a4 (patch) | |
tree | 9ae9115957ab35a3bc7d2d446bce72031328af1a /crates/hir_def/src/item_tree/tests.rs | |
parent | 5b6c0c1af290996a407fb4be51e852317dfab7c2 (diff) | |
parent | 463ecefc64a48d80b2c4591fd4a1b82ae62b2897 (diff) |
Merge #8916
8916: ItemTree pretty-printing r=jonas-schievink a=jonas-schievink
This adds a printer for `ItemTree` contents, and a few tests to ensure that `ItemTree` lowering works like we expect it to. It also adds a new "Debug ItemTree" command that can be used to see the `ItemTree` of the currently open file. The pretty-printed output is usually close enough to Rust syntax that we can even use Rust syntax highlighting.
This is similar to the old `ItemTree` tests we had, but produces significantly more readable output, so these should actually carry their weight.
Co-authored-by: Jonas Schievink <[email protected]>
Diffstat (limited to 'crates/hir_def/src/item_tree/tests.rs')
-rw-r--r-- | crates/hir_def/src/item_tree/tests.rs | 244 |
1 files changed, 244 insertions, 0 deletions
diff --git a/crates/hir_def/src/item_tree/tests.rs b/crates/hir_def/src/item_tree/tests.rs new file mode 100644 index 000000000..100ae9b97 --- /dev/null +++ b/crates/hir_def/src/item_tree/tests.rs | |||
@@ -0,0 +1,244 @@ | |||
1 | use base_db::fixture::WithFixture; | ||
2 | use expect_test::{expect, Expect}; | ||
3 | |||
4 | use crate::{db::DefDatabase, test_db::TestDB}; | ||
5 | |||
6 | fn check(ra_fixture: &str, expect: Expect) { | ||
7 | let (db, file_id) = TestDB::with_single_file(ra_fixture); | ||
8 | let item_tree = db.file_item_tree(file_id.into()); | ||
9 | let pretty = item_tree.pretty_print(); | ||
10 | expect.assert_eq(&pretty); | ||
11 | } | ||
12 | |||
13 | #[test] | ||
14 | fn imports() { | ||
15 | check( | ||
16 | r#" | ||
17 | //! file comment | ||
18 | #![no_std] | ||
19 | //! another file comment | ||
20 | |||
21 | extern crate self as renamed; | ||
22 | pub(super) extern crate bli; | ||
23 | |||
24 | pub use crate::path::{nested, items as renamed, Trait as _}; | ||
25 | use globs::*; | ||
26 | |||
27 | /// docs on import | ||
28 | use crate::{A, B}; | ||
29 | "#, | ||
30 | expect![[r##" | ||
31 | #![doc = " file comment"] // AttrId { is_doc_comment: true, ast_index: 0 } | ||
32 | #![no_std] // AttrId { is_doc_comment: false, ast_index: 0 } | ||
33 | #![doc = " another file comment"] // AttrId { is_doc_comment: true, ast_index: 1 } | ||
34 | |||
35 | pub(self) extern crate self as renamed; | ||
36 | |||
37 | pub(super) extern crate bli; | ||
38 | |||
39 | pub use crate::path::nested; // 0 | ||
40 | |||
41 | pub use crate::path::items as renamed; // 1 | ||
42 | |||
43 | pub use crate::path::Trait as _; // 2 | ||
44 | |||
45 | pub(self) use globs::*; // 0 | ||
46 | |||
47 | #[doc = " docs on import"] // AttrId { is_doc_comment: true, ast_index: 0 } | ||
48 | pub(self) use crate::A; // 0 | ||
49 | |||
50 | #[doc = " docs on import"] // AttrId { is_doc_comment: true, ast_index: 0 } | ||
51 | pub(self) use crate::B; // 1 | ||
52 | "##]], | ||
53 | ); | ||
54 | } | ||
55 | |||
56 | #[test] | ||
57 | fn extern_blocks() { | ||
58 | check( | ||
59 | r#" | ||
60 | #[on_extern_block] | ||
61 | extern "C" { | ||
62 | #[on_extern_type] | ||
63 | type ExType; | ||
64 | |||
65 | #[on_extern_static] | ||
66 | static EX_STATIC: u8; | ||
67 | |||
68 | #[on_extern_fn] | ||
69 | fn ex_fn(); | ||
70 | } | ||
71 | "#, | ||
72 | expect![[r##" | ||
73 | #[on_extern_block] // AttrId { is_doc_comment: false, ast_index: 0 } | ||
74 | extern "C" { | ||
75 | #[on_extern_type] // AttrId { is_doc_comment: false, ast_index: 0 } | ||
76 | pub(self) type ExType; // extern | ||
77 | |||
78 | #[on_extern_static] // AttrId { is_doc_comment: false, ast_index: 0 } | ||
79 | pub(self) static EX_STATIC: u8 = _; // extern | ||
80 | |||
81 | #[on_extern_fn] // AttrId { is_doc_comment: false, ast_index: 0 } | ||
82 | // flags = 0x60 | ||
83 | pub(self) fn ex_fn() -> (); | ||
84 | } | ||
85 | "##]], | ||
86 | ); | ||
87 | } | ||
88 | |||
89 | #[test] | ||
90 | fn adts() { | ||
91 | check( | ||
92 | r#" | ||
93 | struct Unit; | ||
94 | |||
95 | #[derive(Debug)] | ||
96 | struct Struct { | ||
97 | /// fld docs | ||
98 | fld: (), | ||
99 | } | ||
100 | |||
101 | struct Tuple(#[attr] u8); | ||
102 | |||
103 | union Ize { | ||
104 | a: (), | ||
105 | b: (), | ||
106 | } | ||
107 | |||
108 | enum E { | ||
109 | /// comment on Unit | ||
110 | Unit, | ||
111 | /// comment on Tuple | ||
112 | Tuple(u8), | ||
113 | Struct { | ||
114 | /// comment on a: u8 | ||
115 | a: u8, | ||
116 | } | ||
117 | } | ||
118 | "#, | ||
119 | expect![[r##" | ||
120 | pub(self) struct Unit; | ||
121 | |||
122 | #[derive(Debug)] // AttrId { is_doc_comment: false, ast_index: 0 } | ||
123 | pub(self) struct Struct { | ||
124 | #[doc = " fld docs"] // AttrId { is_doc_comment: true, ast_index: 0 } | ||
125 | pub(self) fld: (), | ||
126 | } | ||
127 | |||
128 | pub(self) struct Tuple( | ||
129 | #[attr] // AttrId { is_doc_comment: false, ast_index: 0 } | ||
130 | pub(self) 0: u8, | ||
131 | ); | ||
132 | |||
133 | pub(self) union Ize { | ||
134 | pub(self) a: (), | ||
135 | pub(self) b: (), | ||
136 | } | ||
137 | |||
138 | pub(self) enum E { | ||
139 | #[doc = " comment on Unit"] // AttrId { is_doc_comment: true, ast_index: 0 } | ||
140 | Unit, | ||
141 | #[doc = " comment on Tuple"] // AttrId { is_doc_comment: true, ast_index: 0 } | ||
142 | Tuple( | ||
143 | pub(self) 0: u8, | ||
144 | ), | ||
145 | Struct { | ||
146 | #[doc = " comment on a: u8"] // AttrId { is_doc_comment: true, ast_index: 0 } | ||
147 | pub(self) a: u8, | ||
148 | }, | ||
149 | } | ||
150 | "##]], | ||
151 | ); | ||
152 | } | ||
153 | |||
154 | #[test] | ||
155 | fn misc() { | ||
156 | check( | ||
157 | r#" | ||
158 | pub static mut ST: () = (); | ||
159 | |||
160 | const _: Anon = (); | ||
161 | |||
162 | #[attr] | ||
163 | fn f(#[attr] arg: u8, _: ()) { | ||
164 | #![inner_attr_in_fn] | ||
165 | } | ||
166 | |||
167 | trait Tr: SuperTrait + 'lifetime { | ||
168 | type Assoc: AssocBound = Default; | ||
169 | fn method(&self); | ||
170 | } | ||
171 | "#, | ||
172 | expect![[r##" | ||
173 | pub static mut ST: () = _; | ||
174 | |||
175 | pub(self) const _: Anon = _; | ||
176 | |||
177 | #[attr] // AttrId { is_doc_comment: false, ast_index: 0 } | ||
178 | #[inner_attr_in_fn] // AttrId { is_doc_comment: false, ast_index: 1 } | ||
179 | // flags = 0x2 | ||
180 | pub(self) fn f( | ||
181 | #[attr] // AttrId { is_doc_comment: false, ast_index: 0 } | ||
182 | _: u8, | ||
183 | _: (), | ||
184 | ) -> (); | ||
185 | |||
186 | pub(self) trait Tr: SuperTrait + 'lifetime { | ||
187 | pub(self) type Assoc: AssocBound = Default; | ||
188 | |||
189 | // flags = 0x1 | ||
190 | pub(self) fn method( | ||
191 | _: &Self, | ||
192 | ) -> (); | ||
193 | } | ||
194 | "##]], | ||
195 | ); | ||
196 | } | ||
197 | |||
198 | #[test] | ||
199 | fn modules() { | ||
200 | check( | ||
201 | r#" | ||
202 | /// outer | ||
203 | mod inline { | ||
204 | //! inner | ||
205 | |||
206 | use super::*; | ||
207 | |||
208 | fn fn_in_module() {} | ||
209 | } | ||
210 | "#, | ||
211 | expect![[r##" | ||
212 | #[doc = " outer"] // AttrId { is_doc_comment: true, ast_index: 0 } | ||
213 | #[doc = " inner"] // AttrId { is_doc_comment: true, ast_index: 1 } | ||
214 | pub(self) mod inline { | ||
215 | pub(self) use super::*; // 0 | ||
216 | |||
217 | // flags = 0x2 | ||
218 | pub(self) fn fn_in_module() -> (); | ||
219 | } | ||
220 | "##]], | ||
221 | ); | ||
222 | } | ||
223 | |||
224 | #[test] | ||
225 | fn macros() { | ||
226 | check( | ||
227 | r#" | ||
228 | macro_rules! m { | ||
229 | () => {}; | ||
230 | } | ||
231 | |||
232 | pub macro m2() {} | ||
233 | |||
234 | m!(); | ||
235 | "#, | ||
236 | expect![[r#" | ||
237 | macro_rules! m { ... } | ||
238 | |||
239 | pub macro m2 { ... } | ||
240 | |||
241 | m!(...); | ||
242 | "#]], | ||
243 | ); | ||
244 | } | ||