/
RsIncludeMacroResolveTest.kt
306 lines (277 loc) · 7.05 KB
/
RsIncludeMacroResolveTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/
package org.rust.lang.core.resolve
import org.intellij.lang.annotations.Language
import org.rust.ExpandMacros
class RsIncludeMacroResolveTest : RsResolveTestBase() {
fun `test resolve struct to included file`() = checkResolve("""
//- main.rs
include!("foo.rs");
fn main() {
println("{:?}", Foo);
//^ foo.rs
}
//- foo.rs
#[derive(Debug)]
struct Foo;
""")
fun `test resolve method to included file`() = checkResolve("""
//- main.rs
include!("foo.rs");
fn main() {
Foo.foo();
//^ foo.rs
}
//- foo.rs
struct Foo;
impl Foo {
fn foo(&self) {}
}
""")
fun `test resolve function from included file`() = checkResolve("""
//- lib.rs
include!("foo.rs");
fn bar() {}
//- foo.rs
pub fn foo() {
bar();
//^ lib.rs
}
""")
fun `test resolve method from included file`() = checkResolve("""
//- lib.rs
include!("foo.rs");
struct Bar;
impl Bar {
fn bar(&self) {}
}
//- foo.rs
pub fn foo() {
Bar.bar();
//^ lib.rs
}
""")
fun `test resolve to correct included file`() = checkResolve("""
//- main.rs
include!("foo/baz.rs");
fn foo(f: Foo) {}
//^ foo/baz.rs
//- lib.rs
include!("bar/baz.rs");
//- foo/baz.rs
struct Foo;
//- bar/baz.rs
struct Foo;
""")
fun `test include in inline module 1`() = checkResolve("""
//- lib.rs
mod foo {
include!("bar.rs");
}
fn foo(f: foo::Foo) {}
//^ bar.rs
//- foo/bar.rs
pub struct Foo;
//- bar.rs
pub struct Foo;
""")
fun `test include in inline module 2`() = checkResolve("""
//- lib.rs
mod foo {
struct Foo;
include!("bar.rs");
}
//- bar.rs
fn bar(x: Foo) {}
//^ lib.rs
""")
fun `test include in function local module`() = expect<IllegalStateException> {
checkResolve("""
//- lib.rs
fn foo() {
mod foo {
include!("bar.rs");
}
foo::Foo;
//^ bar.rs
}
//- bar.rs
struct Foo;
""")
}
fun `test include file in included file 1`() = checkResolve("""
//- lib.rs
include!("foo.rs");
fn foo(x: Foo) {}
//^ bar.rs
//- foo.rs
include!("bar.rs");
//- bar.rs
struct Foo;
""")
fun `test include file in included file 2`() = checkResolve("""
//- lib.rs
include!("foo.rs");
struct Foo;
//- foo.rs
include!("bar.rs");
//- bar.rs
fn foo(x: Foo) {}
//^ lib.rs
""")
@ExpandMacros
fun `test include macro in macro 1`() = checkResolve("""
//- lib.rs
macro_rules! generate_include {
($ package: tt) => {
include!($ package);
};
}
generate_include!("bar.rs");
pub struct Foo;
//- bar.rs
pub fn foo(x: Foo) {}
//^ lib.rs
""")
fun `test include macro in macro 2`() = checkResolve("""
//- lib.rs
macro_rules! generate_include {
($ package: tt) => {
include!($ package);
};
}
generate_include!("bar.rs");
pub fn foo(x: Foo) {}
//^ bar.rs
//- bar.rs
pub struct Foo;
""")
@ExpandMacros
fun `test include macro in macro 3`() = checkResolve("""
//- lib.rs
macro_rules! generate_include {
($ package: tt) => {
include!($ package);
};
}
macro_rules! generate_generate_include {
($ mod: ident, $ package: tt) => {
pub mod $ mod {
generate_include!($ package);
}
};
}
generate_generate_include!(bar, "bar.rs");
pub fn foo(x: bar::Foo) {}
//^ bar.rs
//- bar.rs
pub struct Foo;
""")
@ExpandMacros
fun `test macro call in included file 1`() = checkResolve("""
//- main.rs
macro_rules! foo {
() => {};
}
include!("foo.rs");
//- foo.rs
foo!();
//^ main.rs
""")
@ExpandMacros
fun `test macro call in included file 2`() = checkResolve("""
//- main.rs
macro_rules! gen_use {
() => { use inner::func; };
}
mod inner {
pub fn func() {}
} //X
include!("foo.rs");
fn main() {
func();
} //^ main.rs
//- foo.rs
gen_use!();
""")
fun `test macro def in included file`() = checkResolve("""
//- main.rs
include!("foo.rs");
mod other {
foo!();
} //^ foo.rs
//- foo.rs
macro_rules! foo { () => {} }
""")
fun `test concat in include 1`() = checkResolve("""
//- main.rs
include!(concat!("foo.rs"));
fn main() {
Foo.foo();
//^ foo.rs
}
//- foo.rs
struct Foo;
impl Foo {
fn foo(&self) {}
}
""")
fun `test concat in include 2`() = checkResolve("""
//- lib.rs
include!(concat!("bar/foo.rs"));
fn bar() {}
//- bar/foo.rs
fn foo() {
bar();
} //^ lib.rs
""")
fun `test concat in include 3`() = checkResolve("""
//- main.rs
include!(concat!("bar", "/foo.rs"));
fn main() {
Foo.foo();
//^ bar/foo.rs
}
//- bar/foo.rs
struct Foo;
impl Foo {
fn foo(&self) {}
}
""")
fun `test recursive concat in include`() = checkResolve("""
//- lib.rs
include!(concat!(concat!("bar"), "/foo.rs"));
fn bar() {}
//- bar/foo.rs
fn foo() {
bar();
} //^ lib.rs
""")
fun `test fqn include`() = checkResolve("""
//- main.rs
std::include!("foo.rs");
fn main() {
println("{:?}", Foo);
//^ foo.rs
}
//- foo.rs
#[derive(Debug)]
struct Foo;
""")
fun `test fqn include and concat`() = checkResolve("""
//- main.rs
std::include!(std::concat!("bar", "/foo.rs"));
fn main() {
println("{:?}", Foo);
//^ bar/foo.rs
}
//- bar/foo.rs
#[derive(Debug)]
struct Foo;
""")
private fun checkResolve(@Language("Rust") code: String) {
stubOnlyResolve(code) { element -> element.containingFile.virtualFile }
}
}