Skip to content

Commit d8552d1

Browse files
bors[bot]ltentrup
andauthored
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 <leander.tentrup@gmail.com>
2 parents bb0d460 + 65943c0 commit d8552d1

File tree

1 file changed

+27
-32
lines changed
  • crates/ra_ide/src/syntax_highlighting

1 file changed

+27
-32
lines changed

crates/ra_ide/src/syntax_highlighting/tests.rs

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,21 @@ use crate::{
77
FileRange, TextRange,
88
};
99

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+
1022
#[test]
1123
fn test_highlighting() {
12-
let (analysis, file_id) = single_file(
24+
check_highlighting(
1325
r#"
1426
#[derive(Clone, Debug)]
1527
struct Foo {
@@ -84,17 +96,14 @@ impl<T> Option<T> {
8496
}
8597
"#
8698
.trim(),
99+
"crates/ra_ide/src/snapshots/highlighting.html",
100+
false,
87101
);
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);
93102
}
94103

95104
#[test]
96105
fn test_rainbow_highlighting() {
97-
let (analysis, file_id) = single_file(
106+
check_highlighting(
98107
r#"
99108
fn main() {
100109
let hello = "hello";
@@ -110,12 +119,9 @@ fn bar() {
110119
}
111120
"#
112121
.trim(),
122+
"crates/ra_ide/src/snapshots/rainbow_highlighting.html",
123+
true,
113124
);
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);
119125
}
120126

121127
#[test]
@@ -153,7 +159,7 @@ fn test_ranges() {
153159

154160
#[test]
155161
fn test_flattening() {
156-
let (analysis, file_id) = single_file(
162+
check_highlighting(
157163
r##"
158164
fn fixture(ra_fixture: &str) {}
159165
@@ -167,13 +173,9 @@ fn main() {
167173
);
168174
}"##
169175
.trim(),
176+
"crates/ra_ide/src/snapshots/highlight_injection.html",
177+
false,
170178
);
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);
177179
}
178180

179181
#[test]
@@ -192,7 +194,7 @@ macro_rules! test {}
192194
fn test_string_highlighting() {
193195
// The format string detection is based on macro-expansion,
194196
// thus, we have to copy the macro definition from `std`
195-
let (analysis, file_id) = single_file(
197+
check_highlighting(
196198
r#"
197199
macro_rules! println {
198200
($($arg:tt)*) => ({
@@ -250,18 +252,14 @@ fn main() {
250252
println!("{ничоси}", ничоси = 92);
251253
}"#
252254
.trim(),
255+
"crates/ra_ide/src/snapshots/highlight_strings.html",
256+
false,
253257
);
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);
260258
}
261259

262260
#[test]
263261
fn test_unsafe_highlighting() {
264-
let (analysis, file_id) = single_file(
262+
check_highlighting(
265263
r#"
266264
unsafe fn unsafe_fn() {}
267265
@@ -282,10 +280,7 @@ fn main() {
282280
}
283281
"#
284282
.trim(),
283+
"crates/ra_ide/src/snapshots/highlight_unsafe.html",
284+
false,
285285
);
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);
291286
}

0 commit comments

Comments
 (0)