3 use expect_test::{expect_file, ExpectFile};
4 use test_utils::project_dir;
6 use crate::{mock_analysis::single_file, FileRange, TextRange};
9 fn test_highlighting() {
12 use inner::{self as inner_mod};
15 // Needed for function consuming vs normal
32 fn bar(&self) -> i32 {
38 fn baz(mut self, f: Foo) -> i32 {
46 fn quop(&self) -> i32 {
57 fn baz(self, f: FooCopy) -> u32 {
65 fn quop(&self) -> u32 {
70 static mut STATIC_MUT: i32 = 0;
72 fn foo<'a, T>() -> T {
77 ($($tt:tt)*) => {$($tt)*}
94 println!("Hello, {}!", 92);
96 let mut vec = Vec::new();
99 vec.push(Foo { x, y: 1 });
116 let Foo { x: z, y } = Foo { x: z, y };
120 let mut foo = Foo { x, y: x };
121 let foo2 = Foo { x, y: x };
126 let mut copy = FooCopy { x };
139 fn and<U>(self, other: Option<U>) -> Option<(T, U)> {
141 None => unimplemented!(),
148 expect_file!["./test_data/highlighting.html"],
154 fn test_rainbow_highlighting() {
159 let x = hello.to_string();
160 let y = hello.to_string();
162 let x = "other color please!";
163 let y = x.to_string();
167 let mut hello = "hello";
171 expect_file!["./test_data/rainbow_highlighting.html"],
177 fn accidentally_quadratic() {
178 let file = project_dir().join("crates/syntax/test_data/accidentally_quadratic");
179 let src = fs::read_to_string(file).unwrap();
181 let (analysis, file_id) = single_file(&src);
183 // let t = std::time::Instant::now();
184 let _ = analysis.highlight(file_id).unwrap();
185 // eprintln!("elapsed: {:?}", t.elapsed());
190 let (analysis, file_id) = single_file(
192 #[derive(Clone, Debug)]
201 let highlights = &analysis
202 .highlight_range(FileRange { file_id, range: TextRange::at(45.into(), 1.into()) })
205 assert_eq!(&highlights[0].highlight.to_string(), "field.declaration");
209 fn test_flattening() {
212 fn fixture(ra_fixture: &str) {}
218 println!("2 + 2 = {}", 4);
224 expect_file!["./test_data/highlight_injection.html"],
231 let (analysis, file_id) = single_file(
238 let _ = analysis.highlight(file_id).unwrap();
242 fn test_string_highlighting() {
243 // The format string detection is based on macro-expansion,
244 // thus, we have to copy the macro definition from `std`
247 macro_rules! println {
249 $crate::io::_print($crate::format_args_nl!($($arg)*));
252 #[rustc_builtin_macro]
253 macro_rules! format_args_nl {
254 ($fmt:expr) => {{ /* compiler built-in */ }};
255 ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
259 // from https://doc.rust-lang.org/std/fmt/index.html
260 println!("Hello"); // => "Hello"
261 println!("Hello, {}!", "world"); // => "Hello, world!"
262 println!("The number is {}", 1); // => "The number is 1"
263 println!("{:?}", (3, 4)); // => "(3, 4)"
264 println!("{value}", value=4); // => "4"
265 println!("{} {}", 1, 2); // => "1 2"
266 println!("{:04}", 42); // => "0042" with leading zerosV
267 println!("{1} {} {0} {}", 1, 2); // => "2 1 1 2"
268 println!("{argument}", argument = "test"); // => "test"
269 println!("{name} {}", 1, name = 2); // => "2 1"
270 println!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b"
271 println!("{{{}}}", 2); // => "{2}"
272 println!("Hello {:5}!", "x");
273 println!("Hello {:1$}!", "x", 5);
274 println!("Hello {1:0$}!", 5, "x");
275 println!("Hello {:width$}!", "x", width = 5);
276 println!("Hello {:<5}!", "x");
277 println!("Hello {:-<5}!", "x");
278 println!("Hello {:^5}!", "x");
279 println!("Hello {:>5}!", "x");
280 println!("Hello {:+}!", 5);
281 println!("{:#x}!", 27);
282 println!("Hello {:05}!", 5);
283 println!("Hello {:05}!", -5);
284 println!("{:#010x}!", 27);
285 println!("Hello {0} is {1:.5}", "x", 0.01);
286 println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
287 println!("Hello {0} is {2:.1$}", "x", 5, 0.01);
288 println!("Hello {} is {:.*}", "x", 5, 0.01);
289 println!("Hello {} is {2:.*}", "x", 5, 0.01);
290 println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01);
291 println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56);
292 println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56");
293 println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56");
294 println!("Hello {{}}");
295 println!("{{ Hello");
297 println!(r"Hello, {}!", "world");
300 println!("Hello\nWorld");
301 println!("\u{48}\x65\x6C\x6C\x6F World");
303 println!("{\x41}", A = 92);
304 println!("{ничоси}", ничоси = 92);
307 expect_file!["./test_data/highlight_strings.html"],
313 fn test_unsafe_highlighting() {
316 unsafe fn unsafe_fn() {}
326 unsafe fn unsafe_method(&self) {}
329 struct TypeForStaticMut {
333 static mut global_mut: TypeForStaticMut = TypeForStaticMut { a: 0 };
341 fn calls_autoref(&self);
344 impl DoTheAutoref for u16 {
345 fn calls_autoref(&self) {}
349 let x = &5 as *const _ as *const usize;
350 let u = Union { b: 0 };
352 // unsafe fn and method calls
356 Union { b: 0 } => (),
359 HasUnsafeFn.unsafe_method();
364 // unsafe access to a static mut
365 let a = global_mut.a;
367 // unsafe ref of packed fields
368 let packed = Packed { a: 0 };
370 let ref a = packed.a;
371 let Packed { ref a } = packed;
372 let Packed { a: ref _a } = packed;
374 // unsafe auto ref of packed field
375 packed.a.calls_autoref();
380 expect_file!["./test_data/highlight_unsafe.html"],
386 fn test_highlight_doctest() {
390 /// let _ = "early doctests should not go boom";
397 pub const bar: bool = true;
399 /// Constructs a new `Foo`.
404 /// # #![allow(unused_mut)]
405 /// let mut foo: Foo = Foo::new();
407 pub const fn new() -> Foo {
411 /// `bar` method on `Foo`.
418 /// let foo = Foo::new();
420 /// // calls bar on foo
421 /// assert!(foo.bar());
423 /// let bar = foo.bar || Foo::bar;
428 /// let multi_line_string = "Foo
435 /// let foobar = Foo::new().bar();
441 pub fn foo(&self) -> bool {
456 expect_file!["./test_data/highlight_doctest.html"],
462 fn test_extern_crate() {
465 //- /main.rs crate:main deps:std,alloc
467 extern crate alloc as abc;
468 //- /std/lib.rs crate:std
470 //- /alloc/lib.rs crate:alloc
473 expect_file!["./test_data/highlight_extern_crate.html"],
478 /// Highlights the code given by the `ra_fixture` argument, renders the
479 /// result as HTML, and compares it with the HTML file given as `snapshot`.
480 /// Note that the `snapshot` file is overwritten by the rendered HTML.
481 fn check_highlighting(ra_fixture: &str, expect: ExpectFile, rainbow: bool) {
482 let (analysis, file_id) = single_file(ra_fixture);
483 let actual_html = &analysis.highlight_as_html(file_id, rainbow).unwrap();
484 expect.assert_eq(actual_html)