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