diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-06-08 10:48:38 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-06-08 10:48:38 +0100 |
commit | d8552d114c3b2cedbc485ebe26d7b784c0845bd8 (patch) | |
tree | f269ccb60670a534294133a45fa2cfb66ce421ea /crates/ra_ide/src/syntax_highlighting/tests.rs | |
parent | bb0d4600fff40565ed679c0d879c7a9256570b61 (diff) | |
parent | 65943c058583f912175f2cfde64ff2a0d92809b6 (diff) |
Merge #4781
4781: Remove redundancy in syntax highlighting tests r=matklad a=ltentrup
Follow up from #4683. Improves syntax highlighting testing by introducing a function that contains the boilerplate comparison code. Keeps the `ra_fixture` argument in the first position, thus, the editor syntax highlighting injection still works.
Co-authored-by: Leander Tentrup <[email protected]>
Diffstat (limited to 'crates/ra_ide/src/syntax_highlighting/tests.rs')
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting/tests.rs | 59 |
1 files changed, 27 insertions, 32 deletions
diff --git a/crates/ra_ide/src/syntax_highlighting/tests.rs b/crates/ra_ide/src/syntax_highlighting/tests.rs index 36a1aa419..5e42c5b55 100644 --- a/crates/ra_ide/src/syntax_highlighting/tests.rs +++ b/crates/ra_ide/src/syntax_highlighting/tests.rs | |||
@@ -7,9 +7,21 @@ use crate::{ | |||
7 | FileRange, TextRange, | 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 | #[test] | 22 | #[test] |
11 | fn test_highlighting() { | 23 | fn test_highlighting() { |
12 | let (analysis, file_id) = single_file( | 24 | check_highlighting( |
13 | r#" | 25 | r#" |
14 | #[derive(Clone, Debug)] | 26 | #[derive(Clone, Debug)] |
15 | struct Foo { | 27 | struct Foo { |
@@ -84,17 +96,14 @@ impl<T> Option<T> { | |||
84 | } | 96 | } |
85 | "# | 97 | "# |
86 | .trim(), | 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 | #[test] | 104 | #[test] |
96 | fn test_rainbow_highlighting() { | 105 | fn test_rainbow_highlighting() { |
97 | let (analysis, file_id) = single_file( | 106 | check_highlighting( |
98 | r#" | 107 | r#" |
99 | fn main() { | 108 | fn main() { |
100 | let hello = "hello"; | 109 | let hello = "hello"; |
@@ -110,12 +119,9 @@ fn bar() { | |||
110 | } | 119 | } |
111 | "# | 120 | "# |
112 | .trim(), | 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 | #[test] | 127 | #[test] |
@@ -153,7 +159,7 @@ fn test_ranges() { | |||
153 | 159 | ||
154 | #[test] | 160 | #[test] |
155 | fn test_flattening() { | 161 | fn test_flattening() { |
156 | let (analysis, file_id) = single_file( | 162 | check_highlighting( |
157 | r##" | 163 | r##" |
158 | fn fixture(ra_fixture: &str) {} | 164 | fn fixture(ra_fixture: &str) {} |
159 | 165 | ||
@@ -167,13 +173,9 @@ fn main() { | |||
167 | ); | 173 | ); |
168 | }"## | 174 | }"## |
169 | .trim(), | 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 | #[test] | 181 | #[test] |
@@ -192,7 +194,7 @@ macro_rules! test {} | |||
192 | fn test_string_highlighting() { | 194 | fn test_string_highlighting() { |
193 | // The format string detection is based on macro-expansion, | 195 | // The format string detection is based on macro-expansion, |
194 | // thus, we have to copy the macro definition from `std` | 196 | // thus, we have to copy the macro definition from `std` |
195 | let (analysis, file_id) = single_file( | 197 | check_highlighting( |
196 | r#" | 198 | r#" |
197 | macro_rules! println { | 199 | macro_rules! println { |
198 | ($($arg:tt)*) => ({ | 200 | ($($arg:tt)*) => ({ |
@@ -250,18 +252,14 @@ fn main() { | |||
250 | println!("{ничоси}", ничоси = 92); | 252 | println!("{ничоси}", ничоси = 92); |
251 | }"# | 253 | }"# |
252 | .trim(), | 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 | #[test] | 260 | #[test] |
263 | fn test_unsafe_highlighting() { | 261 | fn test_unsafe_highlighting() { |
264 | let (analysis, file_id) = single_file( | 262 | check_highlighting( |
265 | r#" | 263 | r#" |
266 | unsafe fn unsafe_fn() {} | 264 | unsafe fn unsafe_fn() {} |
267 | 265 | ||
@@ -282,10 +280,7 @@ fn main() { | |||
282 | } | 280 | } |
283 | "# | 281 | "# |
284 | .trim(), | 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 | } |