Skip to content

Commit 71b2c7c

Browse files
committed
Use ANSI color code to represent difference line.
1 parent efc4996 commit 71b2c7c

File tree

5 files changed

+128
-122
lines changed

5 files changed

+128
-122
lines changed

googletest/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ googletest_macro = { path = "../googletest_macro", version = "0.8.0" }
3535
anyhow = { version = "1", optional = true }
3636
num-traits = "0.2.15"
3737
regex = "1.6.0"
38+
anstyle = "1.0.0"
3839

3940
[dev-dependencies]
4041
indoc = "2"

googletest/src/matchers/display_matcher.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ mod tests {
112112
"
113113
which displays as a string which isn't equal to \"123\\n345\"
114114
Difference:
115-
123
116-
+234
117-
-345
115+
123
116+
[1m[31m234[0m
117+
[1m[34m345[0m
118118
"
119119
))))
120120
)

googletest/src/matchers/eq_deref_of_matcher.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,12 @@ mod tests {
140140
Actual: Strukt { int: 123, string: "something" },
141141
which isn't equal to Strukt { int: 321, string: "someone" }
142142
Difference:
143-
Strukt {
144-
+ int: 123,
145-
- int: 321,
146-
+ string: "something",
147-
- string: "someone",
148-
}
143+
Strukt {
144+
[1m[31m int: 123,[0m
145+
[1m[34m int: 321,[0m
146+
[1m[31m string: "something",[0m
147+
[1m[34m string: "someone",[0m
148+
}
149149
"#})))
150150
)
151151
}

googletest/src/matchers/eq_matcher.rs

Lines changed: 80 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use anstyle::{Color, Style};
16+
1517
use crate::matcher::{Matcher, MatcherResult};
1618
use crate::matcher_support::edit_distance;
17-
use std::{borrow::Cow, fmt::Debug, marker::PhantomData};
19+
use std::{borrow::Cow, fmt::Debug, fmt::Write, marker::PhantomData};
1820

1921
/// Matches a value equal (in the sense of `==`) to `expected`.
2022
///
@@ -178,6 +180,12 @@ pub(super) fn create_diff_reversed(
178180
}
179181
}
180182

183+
const EXTRA_LEFT_STYLE: Style =
184+
Style::new().bold().fg_color(Some(Color::Ansi(anstyle::AnsiColor::Red)));
185+
const EXTRA_RIGHT_STYLE: Style =
186+
Style::new().bold().fg_color(Some(Color::Ansi(anstyle::AnsiColor::Blue)));
187+
const COMMENT_STYLE: Style = Style::new().italic();
188+
181189
fn edit_list_summary(edit_list: &[edit_distance::Edit<&str>]) -> String {
182190
let mut summary = String::new();
183191
// Use to collect common line and compress them.
@@ -188,15 +196,15 @@ fn edit_list_summary(edit_list: &[edit_distance::Edit<&str>]) -> String {
188196
common_line_buffer.push(*left);
189197
continue;
190198
}
191-
edit_distance::Edit::ExtraLeft(left) => ("+", *left),
192-
edit_distance::Edit::ExtraRight(right) => ("-", *right),
193-
edit_distance::Edit::AdditionalLeft => ("<---- remaining lines omitted ---->", ""),
199+
edit_distance::Edit::ExtraLeft(left) => (EXTRA_LEFT_STYLE, *left),
200+
edit_distance::Edit::ExtraRight(right) => (EXTRA_RIGHT_STYLE, *right),
201+
edit_distance::Edit::AdditionalLeft => {
202+
(COMMENT_STYLE, "<---- remaining lines omitted ---->")
203+
}
194204
};
195205
summary.push_str(&compress_common_lines(std::mem::take(&mut common_line_buffer)));
196206

197-
summary.push('\n');
198-
summary.push_str(start);
199-
summary.push_str(line);
207+
write!(&mut summary, "\n{}{line}{}", start.render(), start.render_reset()).unwrap();
200208
}
201209
summary.push_str(&compress_common_lines(common_line_buffer));
202210

@@ -211,7 +219,6 @@ fn compress_common_lines(common_lines: Vec<&str>) -> String {
211219
let mut all_lines = String::new();
212220
for line in common_lines {
213221
all_lines.push('\n');
214-
all_lines.push(' ');
215222
all_lines.push_str(line);
216223
}
217224
return all_lines;
@@ -221,18 +228,18 @@ fn compress_common_lines(common_lines: Vec<&str>) -> String {
221228

222229
for line in &common_lines[0..COMMON_LINES_CONTEXT_SIZE] {
223230
truncated_lines.push('\n');
224-
truncated_lines.push(' ');
225231
truncated_lines.push_str(line);
226232
}
227233

228234
truncated_lines.push_str(&format!(
229-
"\n<---- {} common lines omitted ---->",
230-
common_lines.len() - 2 * COMMON_LINES_CONTEXT_SIZE
235+
"\n{}<---- {} common lines omitted ---->{}",
236+
COMMENT_STYLE.render(),
237+
common_lines.len() - 2 * COMMON_LINES_CONTEXT_SIZE,
238+
COMMENT_STYLE.render_reset(),
231239
));
232240

233241
for line in &common_lines[common_lines.len() - COMMON_LINES_CONTEXT_SIZE..common_lines.len()] {
234242
truncated_lines.push('\n');
235-
truncated_lines.push(' ');
236243
truncated_lines.push_str(line);
237244
}
238245
truncated_lines
@@ -296,12 +303,12 @@ mod tests {
296303
Actual: Strukt { int: 123, string: "something" },
297304
which isn't equal to Strukt { int: 321, string: "someone" }
298305
Difference:
299-
Strukt {
300-
+ int: 123,
301-
- int: 321,
302-
+ string: "something",
303-
- string: "someone",
304-
}
306+
Strukt {
307+
[1m[31m int: 123,[0m
308+
[1m[34m int: 321,[0m
309+
[1m[31m string: "something",[0m
310+
[1m[34m string: "someone",[0m
311+
}
305312
"#})))
306313
)
307314
}
@@ -318,12 +325,12 @@ mod tests {
318325
Actual: [1, 2, 3],
319326
which isn't equal to [1, 3, 4]
320327
Difference:
321-
[
322-
1,
323-
+ 2,
324-
3,
325-
- 4,
326-
]
328+
[
329+
1,
330+
[1m[31m 2,[0m
331+
3,
332+
[1m[34m 4,[0m
333+
]
327334
"#})))
328335
)
329336
}
@@ -340,13 +347,13 @@ mod tests {
340347
Actual: [1, 2, 3, 4, 5],
341348
which isn't equal to [1, 3, 5]
342349
Difference:
343-
[
344-
1,
345-
+ 2,
346-
3,
347-
+ 4,
348-
5,
349-
]
350+
[
351+
1,
352+
[1m[31m 2,[0m
353+
3,
354+
[1m[31m 4,[0m
355+
5,
356+
]
350357
"#})))
351358
)
352359
}
@@ -359,17 +366,17 @@ mod tests {
359366
err(displays_as(contains_substring(indoc! {
360367
"
361368
Difference:
362-
[
363-
+ 1,
364-
+ 2,
365-
3,
366-
4,
367-
<---- 43 common lines omitted ---->
368-
48,
369-
49,
370-
- 50,
371-
- 51,
372-
]"})))
369+
[
370+
[1m[31m 1,[0m
371+
[1m[31m 2,[0m
372+
3,
373+
4,
374+
[3m<---- 43 common lines omitted ---->[0m
375+
48,
376+
49,
377+
[1m[34m 50,[0m
378+
[1m[34m 51,[0m
379+
]"})))
373380
)
374381
}
375382

@@ -381,17 +388,17 @@ mod tests {
381388
err(displays_as(contains_substring(indoc! {
382389
"
383390
Difference:
384-
[
385-
+ 1,
386-
+ 2,
387-
3,
388-
4,
389-
5,
390-
6,
391-
7,
392-
- 8,
393-
- 9,
394-
]"})))
391+
[
392+
[1m[31m 1,[0m
393+
[1m[31m 2,[0m
394+
3,
395+
4,
396+
5,
397+
6,
398+
7,
399+
[1m[34m 8,[0m
400+
[1m[34m 9,[0m
401+
]"})))
395402
)
396403
}
397404

@@ -403,14 +410,14 @@ mod tests {
403410
err(displays_as(contains_substring(indoc! {
404411
"
405412
Difference:
406-
[
407-
1,
408-
<---- 46 common lines omitted ---->
409-
48,
410-
49,
411-
- 50,
412-
- 51,
413-
]"})))
413+
[
414+
1,
415+
[3m<---- 46 common lines omitted ---->[0m
416+
48,
417+
49,
418+
[1m[34m 50,[0m
419+
[1m[34m 51,[0m
420+
]"})))
414421
)
415422
}
416423

@@ -422,14 +429,14 @@ mod tests {
422429
err(displays_as(contains_substring(indoc! {
423430
"
424431
Difference:
425-
[
426-
+ 1,
427-
+ 2,
428-
3,
429-
4,
430-
<---- 46 common lines omitted ---->
431-
51,
432-
]"})))
432+
[
433+
[1m[31m 1,[0m
434+
[1m[31m 2,[0m
435+
3,
436+
4,
437+
[3m<---- 46 common lines omitted ---->[0m
438+
51,
439+
]"})))
433440
)
434441
}
435442

@@ -473,10 +480,10 @@ mod tests {
473480
result,
474481
err(displays_as(contains_substring(indoc!(
475482
r#"
476-
First line
477-
+Second line
478-
-Second lines
479-
Third line
483+
First line
484+
[1m[31mSecond line[0m
485+
[1m[34mSecond lines[0m
486+
Third line
480487
"#
481488
))))
482489
)

0 commit comments

Comments
 (0)