diff options
Diffstat (limited to 'crates/ra_ide/src/syntax_highlighting/tests.rs')
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting/tests.rs | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/crates/ra_ide/src/syntax_highlighting/tests.rs b/crates/ra_ide/src/syntax_highlighting/tests.rs new file mode 100644 index 000000000..ff23d4ac5 --- /dev/null +++ b/crates/ra_ide/src/syntax_highlighting/tests.rs | |||
@@ -0,0 +1,127 @@ | |||
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<T>() -> T { | ||
21 | unimplemented!(); | ||
22 | foo::<i32>(); | ||
23 | } | ||
24 | |||
25 | macro_rules! def_fn { | ||
26 | ($($tt:tt)*) => {$($tt)*} | ||
27 | } | ||
28 | |||
29 | def_fn!{ | ||
30 | fn bar() -> u32 { | ||
31 | 100 | ||
32 | } | ||
33 | } | ||
34 | |||
35 | // comment | ||
36 | fn main() { | ||
37 | println!("Hello, {}!", 92); | ||
38 | |||
39 | let mut vec = Vec::new(); | ||
40 | if true { | ||
41 | let x = 92; | ||
42 | vec.push(Foo { x, y: 1 }); | ||
43 | } | ||
44 | unsafe { vec.set_len(0); } | ||
45 | |||
46 | let mut x = 42; | ||
47 | let y = &mut x; | ||
48 | let z = &y; | ||
49 | |||
50 | y; | ||
51 | } | ||
52 | |||
53 | enum E<X> { | ||
54 | V(X) | ||
55 | } | ||
56 | |||
57 | impl<X> E<X> { | ||
58 | fn new<T>() -> E<T> {} | ||
59 | } | ||
60 | "# | ||
61 | .trim(), | ||
62 | ); | ||
63 | let dst_file = project_dir().join("crates/ra_ide/src/snapshots/highlighting.html"); | ||
64 | let actual_html = &analysis.highlight_as_html(file_id, false).unwrap(); | ||
65 | let expected_html = &read_text(&dst_file); | ||
66 | fs::write(dst_file, &actual_html).unwrap(); | ||
67 | assert_eq_text!(expected_html, actual_html); | ||
68 | } | ||
69 | |||
70 | #[test] | ||
71 | fn test_rainbow_highlighting() { | ||
72 | let (analysis, file_id) = single_file( | ||
73 | r#" | ||
74 | fn main() { | ||
75 | let hello = "hello"; | ||
76 | let x = hello.to_string(); | ||
77 | let y = hello.to_string(); | ||
78 | |||
79 | let x = "other color please!"; | ||
80 | let y = x.to_string(); | ||
81 | } | ||
82 | |||
83 | fn bar() { | ||
84 | let mut hello = "hello"; | ||
85 | } | ||
86 | "# | ||
87 | .trim(), | ||
88 | ); | ||
89 | let dst_file = project_dir().join("crates/ra_ide/src/snapshots/rainbow_highlighting.html"); | ||
90 | let actual_html = &analysis.highlight_as_html(file_id, true).unwrap(); | ||
91 | let expected_html = &read_text(&dst_file); | ||
92 | fs::write(dst_file, &actual_html).unwrap(); | ||
93 | assert_eq_text!(expected_html, actual_html); | ||
94 | } | ||
95 | |||
96 | #[test] | ||
97 | fn accidentally_quadratic() { | ||
98 | let file = project_dir().join("crates/ra_syntax/test_data/accidentally_quadratic"); | ||
99 | let src = fs::read_to_string(file).unwrap(); | ||
100 | |||
101 | let mut mock = MockAnalysis::new(); | ||
102 | let file_id = mock.add_file("/main.rs", &src); | ||
103 | let host = mock.analysis_host(); | ||
104 | |||
105 | // let t = std::time::Instant::now(); | ||
106 | let _ = host.analysis().highlight(file_id).unwrap(); | ||
107 | // eprintln!("elapsed: {:?}", t.elapsed()); | ||
108 | } | ||
109 | |||
110 | #[test] | ||
111 | fn test_ranges() { | ||
112 | let (analysis, file_id) = single_file( | ||
113 | r#" | ||
114 | #[derive(Clone, Debug)] | ||
115 | struct Foo { | ||
116 | pub x: i32, | ||
117 | pub y: i32, | ||
118 | }"#, | ||
119 | ); | ||
120 | |||
121 | // The "x" | ||
122 | let highlights = &analysis | ||
123 | .highlight_range(FileRange { file_id, range: TextRange::offset_len(82.into(), 1.into()) }) | ||
124 | .unwrap(); | ||
125 | |||
126 | assert_eq!(&highlights[0].highlight.to_string(), "field"); | ||
127 | } | ||