@@ -7,9 +7,21 @@ use crate::{
7
7
FileRange , TextRange ,
8
8
} ;
9
9
10
+ /// Highlights the code given by the `ra_fixture` argument, renders the
11
+ /// result as HTML, and compares it with the HTML file given as `snapshot`.
12
+ /// Note that the `snapshot` file is overwritten by the rendered HTML.
13
+ fn check_highlighting ( ra_fixture : & str , snapshot : & str , rainbow : bool ) {
14
+ let ( analysis, file_id) = single_file ( ra_fixture) ;
15
+ let dst_file = project_dir ( ) . join ( snapshot) ;
16
+ let actual_html = & analysis. highlight_as_html ( file_id, rainbow) . unwrap ( ) ;
17
+ let expected_html = & read_text ( & dst_file) ;
18
+ fs:: write ( dst_file, & actual_html) . unwrap ( ) ;
19
+ assert_eq_text ! ( expected_html, actual_html) ;
20
+ }
21
+
10
22
#[ test]
11
23
fn test_highlighting ( ) {
12
- let ( analysis , file_id ) = single_file (
24
+ check_highlighting (
13
25
r#"
14
26
#[derive(Clone, Debug)]
15
27
struct Foo {
@@ -84,17 +96,14 @@ impl<T> Option<T> {
84
96
}
85
97
"#
86
98
. trim ( ) ,
99
+ "crates/ra_ide/src/snapshots/highlighting.html" ,
100
+ false ,
87
101
) ;
88
- let dst_file = project_dir ( ) . join ( "crates/ra_ide/src/snapshots/highlighting.html" ) ;
89
- let actual_html = & analysis. highlight_as_html ( file_id, false ) . unwrap ( ) ;
90
- let expected_html = & read_text ( & dst_file) ;
91
- fs:: write ( dst_file, & actual_html) . unwrap ( ) ;
92
- assert_eq_text ! ( expected_html, actual_html) ;
93
102
}
94
103
95
104
#[ test]
96
105
fn test_rainbow_highlighting ( ) {
97
- let ( analysis , file_id ) = single_file (
106
+ check_highlighting (
98
107
r#"
99
108
fn main() {
100
109
let hello = "hello";
@@ -110,12 +119,9 @@ fn bar() {
110
119
}
111
120
"#
112
121
. trim ( ) ,
122
+ "crates/ra_ide/src/snapshots/rainbow_highlighting.html" ,
123
+ true ,
113
124
) ;
114
- let dst_file = project_dir ( ) . join ( "crates/ra_ide/src/snapshots/rainbow_highlighting.html" ) ;
115
- let actual_html = & analysis. highlight_as_html ( file_id, true ) . unwrap ( ) ;
116
- let expected_html = & read_text ( & dst_file) ;
117
- fs:: write ( dst_file, & actual_html) . unwrap ( ) ;
118
- assert_eq_text ! ( expected_html, actual_html) ;
119
125
}
120
126
121
127
#[ test]
@@ -153,7 +159,7 @@ fn test_ranges() {
153
159
154
160
#[ test]
155
161
fn test_flattening ( ) {
156
- let ( analysis , file_id ) = single_file (
162
+ check_highlighting (
157
163
r##"
158
164
fn fixture(ra_fixture: &str) {}
159
165
@@ -167,13 +173,9 @@ fn main() {
167
173
);
168
174
}"##
169
175
. trim ( ) ,
176
+ "crates/ra_ide/src/snapshots/highlight_injection.html" ,
177
+ false ,
170
178
) ;
171
-
172
- let dst_file = project_dir ( ) . join ( "crates/ra_ide/src/snapshots/highlight_injection.html" ) ;
173
- let actual_html = & analysis. highlight_as_html ( file_id, false ) . unwrap ( ) ;
174
- let expected_html = & read_text ( & dst_file) ;
175
- fs:: write ( dst_file, & actual_html) . unwrap ( ) ;
176
- assert_eq_text ! ( expected_html, actual_html) ;
177
179
}
178
180
179
181
#[ test]
@@ -192,7 +194,7 @@ macro_rules! test {}
192
194
fn test_string_highlighting ( ) {
193
195
// The format string detection is based on macro-expansion,
194
196
// thus, we have to copy the macro definition from `std`
195
- let ( analysis , file_id ) = single_file (
197
+ check_highlighting (
196
198
r#"
197
199
macro_rules! println {
198
200
($($arg:tt)*) => ({
@@ -250,18 +252,14 @@ fn main() {
250
252
println!("{ничоси}", ничоси = 92);
251
253
}"#
252
254
. trim ( ) ,
255
+ "crates/ra_ide/src/snapshots/highlight_strings.html" ,
256
+ false ,
253
257
) ;
254
-
255
- let dst_file = project_dir ( ) . join ( "crates/ra_ide/src/snapshots/highlight_strings.html" ) ;
256
- let actual_html = & analysis. highlight_as_html ( file_id, false ) . unwrap ( ) ;
257
- let expected_html = & read_text ( & dst_file) ;
258
- fs:: write ( dst_file, & actual_html) . unwrap ( ) ;
259
- assert_eq_text ! ( expected_html, actual_html) ;
260
258
}
261
259
262
260
#[ test]
263
261
fn test_unsafe_highlighting ( ) {
264
- let ( analysis , file_id ) = single_file (
262
+ check_highlighting (
265
263
r#"
266
264
unsafe fn unsafe_fn() {}
267
265
@@ -282,10 +280,7 @@ fn main() {
282
280
}
283
281
"#
284
282
. trim ( ) ,
283
+ "crates/ra_ide/src/snapshots/highlight_unsafe.html" ,
284
+ false ,
285
285
) ;
286
- let dst_file = project_dir ( ) . join ( "crates/ra_ide/src/snapshots/highlight_unsafe.html" ) ;
287
- let actual_html = & analysis. highlight_as_html ( file_id, false ) . unwrap ( ) ;
288
- let expected_html = & read_text ( & dst_file) ;
289
- fs:: write ( dst_file, & actual_html) . unwrap ( ) ;
290
- assert_eq_text ! ( expected_html, actual_html) ;
291
286
}
0 commit comments