]> git.lizzy.rs Git - rust.git/blob - crates/ra_ide/src/syntax_highlighting/tests.rs
Convert tests to text-size
[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::at(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 }
171
172 #[test]
173 fn test_string_highlighting() {
174     // The format string detection is based on macro-expansion,
175     // thus, we have to copy the macro definition from `std`
176     let (analysis, file_id) = single_file(
177         r#"
178 macro_rules! println {
179     ($($arg:tt)*) => ({
180         $crate::io::_print($crate::format_args_nl!($($arg)*));
181     })
182 }
183 #[rustc_builtin_macro]
184 macro_rules! format_args_nl {
185     ($fmt:expr) => {{ /* compiler built-in */ }};
186     ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
187 }
188
189 fn main() {
190     // from https://doc.rust-lang.org/std/fmt/index.html
191     println!("Hello");                 // => "Hello"
192     println!("Hello, {}!", "world");   // => "Hello, world!"
193     println!("The number is {}", 1);   // => "The number is 1"
194     println!("{:?}", (3, 4));          // => "(3, 4)"
195     println!("{value}", value=4);      // => "4"
196     println!("{} {}", 1, 2);           // => "1 2"
197     println!("{:04}", 42);             // => "0042" with leading zerosV
198     println!("{1} {} {0} {}", 1, 2);   // => "2 1 1 2"
199     println!("{argument}", argument = "test");   // => "test"
200     println!("{name} {}", 1, name = 2);          // => "2 1"
201     println!("{a} {c} {b}", a="a", b='b', c=3);  // => "a 3 b"
202     println!("Hello {:5}!", "x");
203     println!("Hello {:1$}!", "x", 5);
204     println!("Hello {1:0$}!", 5, "x");
205     println!("Hello {:width$}!", "x", width = 5);
206     println!("Hello {:<5}!", "x");
207     println!("Hello {:-<5}!", "x");
208     println!("Hello {:^5}!", "x");
209     println!("Hello {:>5}!", "x");
210     println!("Hello {:+}!", 5);
211     println!("{:#x}!", 27);
212     println!("Hello {:05}!", 5);
213     println!("Hello {:05}!", -5);
214     println!("{:#010x}!", 27);
215     println!("Hello {0} is {1:.5}", "x", 0.01);
216     println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
217     println!("Hello {0} is {2:.1$}", "x", 5, 0.01);
218     println!("Hello {} is {:.*}",    "x", 5, 0.01);
219     println!("Hello {} is {2:.*}",   "x", 5, 0.01);
220     println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01);
221     println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56);
222     println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56");
223     println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56");
224     println!("Hello {{}}");
225     println!("{{ Hello");
226
227     println!(r"Hello, {}!", "world");
228
229     println!("{\x41}", A = 92);
230     println!("{ничоси}", ничоси = 92);
231 }"#
232         .trim(),
233     );
234
235     let dst_file = project_dir().join("crates/ra_ide/src/snapshots/highlight_strings.html");
236     let actual_html = &analysis.highlight_as_html(file_id, false).unwrap();
237     let expected_html = &read_text(&dst_file);
238     fs::write(dst_file, &actual_html).unwrap();
239     assert_eq_text!(expected_html, actual_html);
240 }