Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 6 additions & 71 deletions googletest/src/matcher_support/summarize_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@
use std::borrow::Cow;
use std::fmt::{Display, Write};

use nu_ansi_term::{Color, Style};

use crate::matcher_support::edit_distance;

/// Environment variable controlling the usage of ansi color in difference
/// summary.
const NO_COLOR_VAR: &str = "GTEST_RUST_NO_COLOR";

/// Returns a string describing how the expected and actual lines differ.
///
Expand Down Expand Up @@ -155,25 +150,24 @@ fn compress_common_lines(common_lines: Vec<&str>) -> String {
}

struct LineStyle {
ansi: Style,
header: char,
}

impl LineStyle {
fn extra_actual_style() -> Self {
Self { ansi: Style::new().fg(Color::Red).bold(), header: '-' }
Self { header: '-' }
}

fn extra_expected_style() -> Self {
Self { ansi: Style::new().fg(Color::Green).bold(), header: '+' }
Self { header: '+' }
}

fn comment_style() -> Self {
Self { ansi: Style::new().italic(), header: ' ' }
Self { header: ' ' }
}

fn unchanged_style() -> Self {
Self { ansi: Style::new(), header: ' ' }
Self { header: ' ' }
}

fn style(self, line: &str) -> StyledLine<'_> {
Expand All @@ -188,18 +182,7 @@ struct StyledLine<'a> {

impl<'a> Display for StyledLine<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if std::env::var(NO_COLOR_VAR).is_err() {
write!(
f,
"{}{}{}{}",
self.style.header,
self.style.ansi.prefix(),
self.line,
self.style.ansi.suffix()
)
} else {
write!(f, "{}{}", self.style.header, self.line)
}
write!(f, "{}{}", self.style.header, self.line)
}
}

Expand All @@ -208,31 +191,6 @@ mod tests {
use super::*;
use crate::{matcher_support::edit_distance::Mode, prelude::*};
use indoc::indoc;
use serial_test::serial;

#[must_use]
fn remove_var() -> TempVar {
let old_value = std::env::var(NO_COLOR_VAR);
std::env::remove_var(NO_COLOR_VAR);
TempVar(old_value.ok())
}

#[must_use]
fn set_var(var: &str) -> TempVar {
let old_value = std::env::var(NO_COLOR_VAR);
std::env::set_var(NO_COLOR_VAR, var);
TempVar(old_value.ok())
}
struct TempVar(Option<String>);

impl Drop for TempVar {
fn drop(&mut self) {
match &self.0 {
Some(old_var) => std::env::set_var(NO_COLOR_VAR, old_var),
None => std::env::remove_var(NO_COLOR_VAR),
}
}
}

// Make a long text with each element of the iterator on one line.
// `collection` must contains at least one element.
Expand Down Expand Up @@ -272,32 +230,9 @@ mod tests {
verify_that!(create_diff(&build_text(1..500), &build_text(501..1000), Mode::Exact), eq(""))
}

#[test]
#[serial]
fn create_diff_exact_small_difference() -> Result<()> {
let _cleanup = remove_var();

verify_that!(
create_diff(&build_text(1..50), &build_text(1..51), Mode::Exact),
eq(indoc! {
"

Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m):
1
2
\x1B[3m<---- 45 common lines omitted ---->\x1B[0m
48
49
+\x1B[1;32m50\x1B[0m"
})
)
}

#[test]
#[serial]
fn create_diff_exact_small_difference_no_color() -> Result<()> {
let _cleanup = set_var("NO_COLOR");

fn create_diff_exact_small_difference() -> Result<()> {
verify_that!(
create_diff(&build_text(1..50), &build_text(1..51), Mode::Exact),
eq(indoc! {
Expand Down
6 changes: 3 additions & 3 deletions googletest/src/matchers/display_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ mod tests {
err(displays_as(contains_substring(indoc!(
"
which displays as a string which isn't equal to \"123\\n345\"
Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m):
Difference(-actual / +expected):
123
-\x1B[1;31m234\x1B[0m
+\x1B[1;32m345\x1B[0m
-234
+345
"
))))
)
Expand Down
10 changes: 5 additions & 5 deletions googletest/src/matchers/eq_deref_of_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ mod tests {
"
Actual: Strukt { int: 123, string: \"something\" },
which isn't equal to Strukt { int: 321, string: \"someone\" }
Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m):
Difference(-actual / +expected):
Strukt {
-\x1B[1;31m int: 123,\x1B[0m
+\x1B[1;32m int: 321,\x1B[0m
-\x1B[1;31m string: \"something\",\x1B[0m
+\x1B[1;32m string: \"someone\",\x1B[0m
- int: 123,
+ int: 321,
- string: \"something\",
+ string: \"someone\",
}
"})))
)
Expand Down
68 changes: 34 additions & 34 deletions googletest/src/matchers/eq_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ mod tests {
"
Actual: Strukt { int: 123, string: \"something\" },
which isn't equal to Strukt { int: 321, string: \"someone\" }
Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m):
Difference(-actual / +expected):
Strukt {
-\x1B[1;31m int: 123,\x1B[0m
+\x1B[1;32m int: 321,\x1B[0m
-\x1B[1;31m string: \"something\",\x1B[0m
+\x1B[1;32m string: \"someone\",\x1B[0m
- int: 123,
+ int: 321,
- string: \"something\",
+ string: \"someone\",
}
"})))
)
Expand All @@ -200,12 +200,12 @@ mod tests {
Expected: is equal to [1, 3, 4]
Actual: [1, 2, 3],
which isn't equal to [1, 3, 4]
Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m):
Difference(-actual / +expected):
[
1,
-\x1B[1;31m 2,\x1B[0m
- 2,
3,
+\x1B[1;32m 4,\x1B[0m
+ 4,
]
"})))
)
Expand All @@ -222,12 +222,12 @@ mod tests {
Expected: is equal to [1, 3, 5]
Actual: [1, 2, 3, 4, 5],
which isn't equal to [1, 3, 5]
Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m):
Difference(-actual / +expected):
[
1,
-\x1B[1;31m 2,\x1B[0m
- 2,
3,
-\x1B[1;31m 4,\x1B[0m
- 4,
5,
]
"})))
Expand All @@ -241,17 +241,17 @@ mod tests {
result,
err(displays_as(contains_substring(indoc! {
"
Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m):
Difference(-actual / +expected):
[
-\x1B[1;31m 1,\x1B[0m
-\x1B[1;31m 2,\x1B[0m
- 1,
- 2,
3,
4,
\x1B[3m<---- 43 common lines omitted ---->\x1B[0m
<---- 43 common lines omitted ---->
48,
49,
+\x1B[1;32m 50,\x1B[0m
+\x1B[1;32m 51,\x1B[0m
+ 50,
+ 51,
]"})))
)
}
Expand All @@ -263,17 +263,17 @@ mod tests {
result,
err(displays_as(contains_substring(indoc! {
"
Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m):
Difference(-actual / +expected):
[
-\x1B[1;31m 1,\x1B[0m
-\x1B[1;31m 2,\x1B[0m
- 1,
- 2,
3,
4,
5,
6,
7,
+\x1B[1;32m 8,\x1B[0m
+\x1B[1;32m 9,\x1B[0m
+ 8,
+ 9,
]"})))
)
}
Expand All @@ -285,14 +285,14 @@ mod tests {
result,
err(displays_as(contains_substring(indoc! {
"
Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m):
Difference(-actual / +expected):
[
1,
\x1B[3m<---- 46 common lines omitted ---->\x1B[0m
<---- 46 common lines omitted ---->
48,
49,
+\x1B[1;32m 50,\x1B[0m
+\x1B[1;32m 51,\x1B[0m
+ 50,
+ 51,
]"})))
)
}
Expand All @@ -304,13 +304,13 @@ mod tests {
result,
err(displays_as(contains_substring(indoc! {
"
Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m):
Difference(-actual / +expected):
[
-\x1B[1;31m 1,\x1B[0m
-\x1B[1;31m 2,\x1B[0m
- 1,
- 2,
3,
4,
\x1B[3m<---- 46 common lines omitted ---->\x1B[0m
<---- 46 common lines omitted ---->
51,
]"})))
)
Expand Down Expand Up @@ -357,8 +357,8 @@ mod tests {
err(displays_as(contains_substring(indoc!(
"
First line
-\x1B[1;31mSecond line\x1B[0m
+\x1B[1;32mSecond lines\x1B[0m
-Second line
+Second lines
Third line
"
))))
Expand All @@ -381,7 +381,7 @@ mod tests {
verify_that!(
result,
err(displays_as(not(contains_substring(
"Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m):"
"Difference(-actual / +expected):"
))))
)
}
Expand All @@ -402,7 +402,7 @@ mod tests {
verify_that!(
result,
err(displays_as(not(contains_substring(
"Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m):"
"Difference(-actual / +expected):"
))))
)
}
Expand Down
Loading