]> git.lizzy.rs Git - rust.git/blob - crates/ra_ide/src/syntax_highlighting/tests.rs
Merge #4023
[rust.git] / crates / ra_ide / src / syntax_highlighting / tests.rs
1 use std::fs;
2
3 use test_utils::{assert_eq_text, project_dir, read_text};
4
5 use crate::{
6     mock_analysis::{single_file, MockAnalysis},
7     FileRange, TextRange,
8 };
9
10 #[test]
11 fn test_highlighting() {
12     let (analysis, file_id) = single_file(
13         r#"
14 #[derive(Clone, Debug)]
15 struct Foo {
16     pub x: i32,
17     pub y: i32,
18 }
19
20 fn foo<'a, T>() -> T {
21     foo::<'a, i32>()
22 }
23
24 macro_rules! def_fn {
25     ($($tt:tt)*) => {$($tt)*}
26 }
27
28 def_fn! {
29     fn bar() -> u32 {
30         100
31     }
32 }
33
34 // comment
35 fn main() {
36     println!("Hello, {}!", 92);
37
38     let mut vec = Vec::new();
39     if true {
40         let x = 92;
41         vec.push(Foo { x, y: 1 });
42     }
43     unsafe { vec.set_len(0); }
44
45     let mut x = 42;
46     let y = &mut x;
47     let z = &y;
48
49     y;
50 }
51
52 enum Option<T> {
53     Some(T),
54     None,
55 }
56 use Option::*;
57
58 impl<T> Option<T> {
59     fn and<U>(self, other: Option<U>) -> Option<(T, U)> {
60         match other {
61             None => unimplemented!(),
62             Nope => Nope,
63         }
64     }
65 }
66 "#
67         .trim(),
68     );
69     let dst_file = project_dir().join("crates/ra_ide/src/snapshots/highlighting.html");
70     let actual_html = &analysis.highlight_as_html(file_id, false).unwrap();
71     let expected_html = &read_text(&dst_file);
72     fs::write(dst_file, &actual_html).unwrap();
73     assert_eq_text!(expected_html, actual_html);
74 }
75
76 #[test]
77 fn test_rainbow_highlighting() {
78     let (analysis, file_id) = single_file(
79         r#"
80 fn main() {
81     let hello = "hello";
82     let x = hello.to_string();
83     let y = hello.to_string();
84
85     let x = "other color please!";
86     let y = x.to_string();
87 }
88
89 fn bar() {
90     let mut hello = "hello";
91 }
92 "#
93         .trim(),
94     );
95     let dst_file = project_dir().join("crates/ra_ide/src/snapshots/rainbow_highlighting.html");
96     let actual_html = &analysis.highlight_as_html(file_id, true).unwrap();
97     let expected_html = &read_text(&dst_file);
98     fs::write(dst_file, &actual_html).unwrap();
99     assert_eq_text!(expected_html, actual_html);
100 }
101
102 #[test]
103 fn accidentally_quadratic() {
104     let file = project_dir().join("crates/ra_syntax/test_data/accidentally_quadratic");
105     let src = fs::read_to_string(file).unwrap();
106
107     let mut mock = MockAnalysis::new();
108     let file_id = mock.add_file("/main.rs", &src);
109     let host = mock.analysis_host();
110
111     // let t = std::time::Instant::now();
112     let _ = host.analysis().highlight(file_id).unwrap();
113     // eprintln!("elapsed: {:?}", t.elapsed());
114 }
115
116 #[test]
117 fn test_ranges() {
118     let (analysis, file_id) = single_file(
119         r#"
120             #[derive(Clone, Debug)]
121             struct Foo {
122                 pub x: i32,
123                 pub y: i32,
124             }"#,
125     );
126
127     // The "x"
128     let highlights = &analysis
129         .highlight_range(FileRange { file_id, range: TextRange::offset_len(82.into(), 1.into()) })
130         .unwrap();
131
132     assert_eq!(&highlights[0].highlight.to_string(), "field.declaration");
133 }
134
135 #[test]
136 fn test_flattening() {
137     let (analysis, file_id) = single_file(
138         r##"
139 fn fixture(ra_fixture: &str) {}
140
141 fn main() {
142     fixture(r#"
143         trait Foo {
144             fn foo() {
145                 println!("2 + 2 = {}", 4);
146             }
147         }"#
148     );
149 }"##
150         .trim(),
151     );
152
153     let dst_file = project_dir().join("crates/ra_ide/src/snapshots/highlight_injection.html");
154     let actual_html = &analysis.highlight_as_html(file_id, false).unwrap();
155     let expected_html = &read_text(&dst_file);
156     fs::write(dst_file, &actual_html).unwrap();
157     assert_eq_text!(expected_html, actual_html);
158 }
159
160 #[test]
161 fn ranges_sorted() {
162     let (analysis, file_id) = single_file(
163         r#"
164 #[foo(bar = "bar")]
165 macro_rules! test {}
166 }"#
167         .trim(),
168     );
169     let _ = analysis.highlight(file_id).unwrap();
170 }