diff options
Diffstat (limited to 'crates/ra_ide/src/syntax_highlighting/tests.rs')
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting/tests.rs | 380 |
1 files changed, 0 insertions, 380 deletions
diff --git a/crates/ra_ide/src/syntax_highlighting/tests.rs b/crates/ra_ide/src/syntax_highlighting/tests.rs deleted file mode 100644 index 87a6e2523..000000000 --- a/crates/ra_ide/src/syntax_highlighting/tests.rs +++ /dev/null | |||
@@ -1,380 +0,0 @@ | |||
1 | use std::fs; | ||
2 | |||
3 | use expect::{expect_file, ExpectFile}; | ||
4 | use test_utils::project_dir; | ||
5 | |||
6 | use crate::{mock_analysis::single_file, FileRange, TextRange}; | ||
7 | |||
8 | #[test] | ||
9 | fn test_highlighting() { | ||
10 | check_highlighting( | ||
11 | r#" | ||
12 | #[derive(Clone, Debug)] | ||
13 | struct Foo { | ||
14 | pub x: i32, | ||
15 | pub y: i32, | ||
16 | } | ||
17 | |||
18 | trait Bar { | ||
19 | fn bar(&self) -> i32; | ||
20 | } | ||
21 | |||
22 | impl Bar for Foo { | ||
23 | fn bar(&self) -> i32 { | ||
24 | self.x | ||
25 | } | ||
26 | } | ||
27 | |||
28 | impl Foo { | ||
29 | fn baz(mut self) -> i32 { | ||
30 | self.x | ||
31 | } | ||
32 | |||
33 | fn qux(&mut self) { | ||
34 | self.x = 0; | ||
35 | } | ||
36 | } | ||
37 | |||
38 | static mut STATIC_MUT: i32 = 0; | ||
39 | |||
40 | fn foo<'a, T>() -> T { | ||
41 | foo::<'a, i32>() | ||
42 | } | ||
43 | |||
44 | macro_rules! def_fn { | ||
45 | ($($tt:tt)*) => {$($tt)*} | ||
46 | } | ||
47 | |||
48 | def_fn! { | ||
49 | fn bar() -> u32 { | ||
50 | 100 | ||
51 | } | ||
52 | } | ||
53 | |||
54 | macro_rules! noop { | ||
55 | ($expr:expr) => { | ||
56 | $expr | ||
57 | } | ||
58 | } | ||
59 | |||
60 | // comment | ||
61 | fn main() { | ||
62 | println!("Hello, {}!", 92); | ||
63 | |||
64 | let mut vec = Vec::new(); | ||
65 | if true { | ||
66 | let x = 92; | ||
67 | vec.push(Foo { x, y: 1 }); | ||
68 | } | ||
69 | unsafe { | ||
70 | vec.set_len(0); | ||
71 | STATIC_MUT = 1; | ||
72 | } | ||
73 | |||
74 | for e in vec { | ||
75 | // Do nothing | ||
76 | } | ||
77 | |||
78 | noop!(noop!(1)); | ||
79 | |||
80 | let mut x = 42; | ||
81 | let y = &mut x; | ||
82 | let z = &y; | ||
83 | |||
84 | let Foo { x: z, y } = Foo { x: z, y }; | ||
85 | |||
86 | y; | ||
87 | } | ||
88 | |||
89 | enum Option<T> { | ||
90 | Some(T), | ||
91 | None, | ||
92 | } | ||
93 | use Option::*; | ||
94 | |||
95 | impl<T> Option<T> { | ||
96 | fn and<U>(self, other: Option<U>) -> Option<(T, U)> { | ||
97 | match other { | ||
98 | None => unimplemented!(), | ||
99 | Nope => Nope, | ||
100 | } | ||
101 | } | ||
102 | } | ||
103 | "# | ||
104 | .trim(), | ||
105 | expect_file!["crates/ra_ide/test_data/highlighting.html"], | ||
106 | false, | ||
107 | ); | ||
108 | } | ||
109 | |||
110 | #[test] | ||
111 | fn test_rainbow_highlighting() { | ||
112 | check_highlighting( | ||
113 | r#" | ||
114 | fn main() { | ||
115 | let hello = "hello"; | ||
116 | let x = hello.to_string(); | ||
117 | let y = hello.to_string(); | ||
118 | |||
119 | let x = "other color please!"; | ||
120 | let y = x.to_string(); | ||
121 | } | ||
122 | |||
123 | fn bar() { | ||
124 | let mut hello = "hello"; | ||
125 | } | ||
126 | "# | ||
127 | .trim(), | ||
128 | expect_file!["crates/ra_ide/test_data/rainbow_highlighting.html"], | ||
129 | true, | ||
130 | ); | ||
131 | } | ||
132 | |||
133 | #[test] | ||
134 | fn accidentally_quadratic() { | ||
135 | let file = project_dir().join("crates/ra_syntax/test_data/accidentally_quadratic"); | ||
136 | let src = fs::read_to_string(file).unwrap(); | ||
137 | |||
138 | let (analysis, file_id) = single_file(&src); | ||
139 | |||
140 | // let t = std::time::Instant::now(); | ||
141 | let _ = analysis.highlight(file_id).unwrap(); | ||
142 | // eprintln!("elapsed: {:?}", t.elapsed()); | ||
143 | } | ||
144 | |||
145 | #[test] | ||
146 | fn test_ranges() { | ||
147 | let (analysis, file_id) = single_file( | ||
148 | r#" | ||
149 | #[derive(Clone, Debug)] | ||
150 | struct Foo { | ||
151 | pub x: i32, | ||
152 | pub y: i32, | ||
153 | } | ||
154 | "#, | ||
155 | ); | ||
156 | |||
157 | // The "x" | ||
158 | let highlights = &analysis | ||
159 | .highlight_range(FileRange { file_id, range: TextRange::at(45.into(), 1.into()) }) | ||
160 | .unwrap(); | ||
161 | |||
162 | assert_eq!(&highlights[0].highlight.to_string(), "field.declaration"); | ||
163 | } | ||
164 | |||
165 | #[test] | ||
166 | fn test_flattening() { | ||
167 | check_highlighting( | ||
168 | r##" | ||
169 | fn fixture(ra_fixture: &str) {} | ||
170 | |||
171 | fn main() { | ||
172 | fixture(r#" | ||
173 | trait Foo { | ||
174 | fn foo() { | ||
175 | println!("2 + 2 = {}", 4); | ||
176 | } | ||
177 | }"# | ||
178 | ); | ||
179 | }"## | ||
180 | .trim(), | ||
181 | expect_file!["crates/ra_ide/test_data/highlight_injection.html"], | ||
182 | false, | ||
183 | ); | ||
184 | } | ||
185 | |||
186 | #[test] | ||
187 | fn ranges_sorted() { | ||
188 | let (analysis, file_id) = single_file( | ||
189 | r#" | ||
190 | #[foo(bar = "bar")] | ||
191 | macro_rules! test {} | ||
192 | }"# | ||
193 | .trim(), | ||
194 | ); | ||
195 | let _ = analysis.highlight(file_id).unwrap(); | ||
196 | } | ||
197 | |||
198 | #[test] | ||
199 | fn test_string_highlighting() { | ||
200 | // The format string detection is based on macro-expansion, | ||
201 | // thus, we have to copy the macro definition from `std` | ||
202 | check_highlighting( | ||
203 | r#" | ||
204 | macro_rules! println { | ||
205 | ($($arg:tt)*) => ({ | ||
206 | $crate::io::_print($crate::format_args_nl!($($arg)*)); | ||
207 | }) | ||
208 | } | ||
209 | #[rustc_builtin_macro] | ||
210 | macro_rules! format_args_nl { | ||
211 | ($fmt:expr) => {{ /* compiler built-in */ }}; | ||
212 | ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }}; | ||
213 | } | ||
214 | |||
215 | fn main() { | ||
216 | // from https://doc.rust-lang.org/std/fmt/index.html | ||
217 | println!("Hello"); // => "Hello" | ||
218 | println!("Hello, {}!", "world"); // => "Hello, world!" | ||
219 | println!("The number is {}", 1); // => "The number is 1" | ||
220 | println!("{:?}", (3, 4)); // => "(3, 4)" | ||
221 | println!("{value}", value=4); // => "4" | ||
222 | println!("{} {}", 1, 2); // => "1 2" | ||
223 | println!("{:04}", 42); // => "0042" with leading zerosV | ||
224 | println!("{1} {} {0} {}", 1, 2); // => "2 1 1 2" | ||
225 | println!("{argument}", argument = "test"); // => "test" | ||
226 | println!("{name} {}", 1, name = 2); // => "2 1" | ||
227 | println!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b" | ||
228 | println!("{{{}}}", 2); // => "{2}" | ||
229 | println!("Hello {:5}!", "x"); | ||
230 | println!("Hello {:1$}!", "x", 5); | ||
231 | println!("Hello {1:0$}!", 5, "x"); | ||
232 | println!("Hello {:width$}!", "x", width = 5); | ||
233 | println!("Hello {:<5}!", "x"); | ||
234 | println!("Hello {:-<5}!", "x"); | ||
235 | println!("Hello {:^5}!", "x"); | ||
236 | println!("Hello {:>5}!", "x"); | ||
237 | println!("Hello {:+}!", 5); | ||
238 | println!("{:#x}!", 27); | ||
239 | println!("Hello {:05}!", 5); | ||
240 | println!("Hello {:05}!", -5); | ||
241 | println!("{:#010x}!", 27); | ||
242 | println!("Hello {0} is {1:.5}", "x", 0.01); | ||
243 | println!("Hello {1} is {2:.0$}", 5, "x", 0.01); | ||
244 | println!("Hello {0} is {2:.1$}", "x", 5, 0.01); | ||
245 | println!("Hello {} is {:.*}", "x", 5, 0.01); | ||
246 | println!("Hello {} is {2:.*}", "x", 5, 0.01); | ||
247 | println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01); | ||
248 | println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56); | ||
249 | println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56"); | ||
250 | println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56"); | ||
251 | println!("Hello {{}}"); | ||
252 | println!("{{ Hello"); | ||
253 | |||
254 | println!(r"Hello, {}!", "world"); | ||
255 | |||
256 | // escape sequences | ||
257 | println!("Hello\nWorld"); | ||
258 | println!("\u{48}\x65\x6C\x6C\x6F World"); | ||
259 | |||
260 | println!("{\x41}", A = 92); | ||
261 | println!("{ничоси}", ничоси = 92); | ||
262 | }"# | ||
263 | .trim(), | ||
264 | expect_file!["crates/ra_ide/test_data/highlight_strings.html"], | ||
265 | false, | ||
266 | ); | ||
267 | } | ||
268 | |||
269 | #[test] | ||
270 | fn test_unsafe_highlighting() { | ||
271 | check_highlighting( | ||
272 | r#" | ||
273 | unsafe fn unsafe_fn() {} | ||
274 | |||
275 | struct HasUnsafeFn; | ||
276 | |||
277 | impl HasUnsafeFn { | ||
278 | unsafe fn unsafe_method(&self) {} | ||
279 | } | ||
280 | |||
281 | fn main() { | ||
282 | let x = &5 as *const usize; | ||
283 | unsafe { | ||
284 | unsafe_fn(); | ||
285 | HasUnsafeFn.unsafe_method(); | ||
286 | let y = *(x); | ||
287 | let z = -x; | ||
288 | } | ||
289 | } | ||
290 | "# | ||
291 | .trim(), | ||
292 | expect_file!["crates/ra_ide/test_data/highlight_unsafe.html"], | ||
293 | false, | ||
294 | ); | ||
295 | } | ||
296 | |||
297 | #[test] | ||
298 | fn test_highlight_doctest() { | ||
299 | check_highlighting( | ||
300 | r#" | ||
301 | /// ``` | ||
302 | /// let _ = "early doctests should not go boom"; | ||
303 | /// ``` | ||
304 | struct Foo { | ||
305 | bar: bool, | ||
306 | } | ||
307 | |||
308 | impl Foo { | ||
309 | pub const bar: bool = true; | ||
310 | |||
311 | /// Constructs a new `Foo`. | ||
312 | /// | ||
313 | /// # Examples | ||
314 | /// | ||
315 | /// ``` | ||
316 | /// # #![allow(unused_mut)] | ||
317 | /// let mut foo: Foo = Foo::new(); | ||
318 | /// ``` | ||
319 | pub const fn new() -> Foo { | ||
320 | Foo { bar: true } | ||
321 | } | ||
322 | |||
323 | /// `bar` method on `Foo`. | ||
324 | /// | ||
325 | /// # Examples | ||
326 | /// | ||
327 | /// ``` | ||
328 | /// use x::y; | ||
329 | /// | ||
330 | /// let foo = Foo::new(); | ||
331 | /// | ||
332 | /// // calls bar on foo | ||
333 | /// assert!(foo.bar()); | ||
334 | /// | ||
335 | /// let bar = foo.bar || Foo::bar; | ||
336 | /// | ||
337 | /// /* multi-line | ||
338 | /// comment */ | ||
339 | /// | ||
340 | /// let multi_line_string = "Foo | ||
341 | /// bar | ||
342 | /// "; | ||
343 | /// | ||
344 | /// ``` | ||
345 | /// | ||
346 | /// ```rust,no_run | ||
347 | /// let foobar = Foo::new().bar(); | ||
348 | /// ``` | ||
349 | /// | ||
350 | /// ```sh | ||
351 | /// echo 1 | ||
352 | /// ``` | ||
353 | pub fn foo(&self) -> bool { | ||
354 | true | ||
355 | } | ||
356 | } | ||
357 | |||
358 | /// ``` | ||
359 | /// noop!(1); | ||
360 | /// ``` | ||
361 | macro_rules! noop { | ||
362 | ($expr:expr) => { | ||
363 | $expr | ||
364 | } | ||
365 | } | ||
366 | "# | ||
367 | .trim(), | ||
368 | expect_file!["crates/ra_ide/test_data/highlight_doctest.html"], | ||
369 | false, | ||
370 | ); | ||
371 | } | ||
372 | |||
373 | /// Highlights the code given by the `ra_fixture` argument, renders the | ||
374 | /// result as HTML, and compares it with the HTML file given as `snapshot`. | ||
375 | /// Note that the `snapshot` file is overwritten by the rendered HTML. | ||
376 | fn check_highlighting(ra_fixture: &str, expect: ExpectFile, rainbow: bool) { | ||
377 | let (analysis, file_id) = single_file(ra_fixture); | ||
378 | let actual_html = &analysis.highlight_as_html(file_id, rainbow).unwrap(); | ||
379 | expect.assert_eq(actual_html) | ||
380 | } | ||