aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/syntax_highlighting/tests.rs
diff options
context:
space:
mode:
authorDmitry <[email protected]>2020-08-14 19:32:05 +0100
committerDmitry <[email protected]>2020-08-14 19:32:05 +0100
commit178c3e135a2a249692f7784712492e7884ae0c00 (patch)
treeac6b769dbf7162150caa0c1624786a4dd79ff3be /crates/ra_ide/src/syntax_highlighting/tests.rs
parent06ff8e6c760ff05f10e868b5d1f9d79e42fbb49c (diff)
parentc2594daf2974dbd4ce3d9b7ec72481764abaceb5 (diff)
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'crates/ra_ide/src/syntax_highlighting/tests.rs')
-rw-r--r--crates/ra_ide/src/syntax_highlighting/tests.rs401
1 files changed, 0 insertions, 401 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 730efff0d..000000000
--- a/crates/ra_ide/src/syntax_highlighting/tests.rs
+++ /dev/null
@@ -1,401 +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
278union Union {
279 a: u32,
280 b: f32,
281}
282
283struct HasUnsafeFn;
284
285impl HasUnsafeFn {
286 unsafe fn unsafe_method(&self) {}
287}
288
289struct TypeForStaticMut {
290 a: u8
291}
292
293static mut global_mut: TypeForStaticMut = TypeForStaticMut { a: 0 };
294
295fn main() {
296 let x = &5 as *const usize;
297 let u = Union { b: 0 };
298 unsafe {
299 unsafe_fn();
300 let b = u.b;
301 match u {
302 Union { b: 0 } => (),
303 Union { a } => (),
304 }
305 HasUnsafeFn.unsafe_method();
306 let y = *(x);
307 let z = -x;
308 let a = global_mut.a;
309 }
310}
311"#
312 .trim(),
313 expect_file!["crates/ra_ide/test_data/highlight_unsafe.html"],
314 false,
315 );
316}
317
318#[test]
319fn test_highlight_doctest() {
320 check_highlighting(
321 r#"
322/// ```
323/// let _ = "early doctests should not go boom";
324/// ```
325struct Foo {
326 bar: bool,
327}
328
329impl Foo {
330 pub const bar: bool = true;
331
332 /// Constructs a new `Foo`.
333 ///
334 /// # Examples
335 ///
336 /// ```
337 /// # #![allow(unused_mut)]
338 /// let mut foo: Foo = Foo::new();
339 /// ```
340 pub const fn new() -> Foo {
341 Foo { bar: true }
342 }
343
344 /// `bar` method on `Foo`.
345 ///
346 /// # Examples
347 ///
348 /// ```
349 /// use x::y;
350 ///
351 /// let foo = Foo::new();
352 ///
353 /// // calls bar on foo
354 /// assert!(foo.bar());
355 ///
356 /// let bar = foo.bar || Foo::bar;
357 ///
358 /// /* multi-line
359 /// comment */
360 ///
361 /// let multi_line_string = "Foo
362 /// bar
363 /// ";
364 ///
365 /// ```
366 ///
367 /// ```rust,no_run
368 /// let foobar = Foo::new().bar();
369 /// ```
370 ///
371 /// ```sh
372 /// echo 1
373 /// ```
374 pub fn foo(&self) -> bool {
375 true
376 }
377}
378
379/// ```
380/// noop!(1);
381/// ```
382macro_rules! noop {
383 ($expr:expr) => {
384 $expr
385 }
386}
387"#
388 .trim(),
389 expect_file!["crates/ra_ide/test_data/highlight_doctest.html"],
390 false,
391 );
392}
393
394/// Highlights the code given by the `ra_fixture` argument, renders the
395/// result as HTML, and compares it with the HTML file given as `snapshot`.
396/// Note that the `snapshot` file is overwritten by the rendered HTML.
397fn check_highlighting(ra_fixture: &str, expect: ExpectFile, rainbow: bool) {
398 let (analysis, file_id) = single_file(ra_fixture);
399 let actual_html = &analysis.highlight_as_html(file_id, rainbow).unwrap();
400 expect.assert_eq(actual_html)
401}