Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move colored diff output to feature flag #85

Merged
merged 2 commits into from
Sep 25, 2019
Merged
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
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ regex = "^1.0.5"
lazy_static = "^1.1.0"
serde_json = "^1.0.17"
difference = "^2.0"
colored = "^1.6"
colored = { version = "^1.6", optional = true }
log = "0.4.6"
percent-encoding = "^1.0.1"
assert-json-diff = "^1.0.0"

[features]
default = ["color"]
color = ["colored"]
17 changes: 17 additions & 0 deletions src/diff.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use difference::{Difference, Changeset};
#[cfg(feature = "color")]
use colored::*;

pub fn compare(expected: &str, actual: &str) -> String {
Expand All @@ -21,24 +22,40 @@ pub fn compare(expected: &str, actual: &str) -> String {
for (i, change) in diffs.iter().enumerate() {
match change {
Difference::Same(ref z) => {
#[cfg(feature = "color")]
result.push_str(&z.green().to_string());
#[cfg(not(feature = "color"))]
result.push_str(&z);

if i < diffs.len() - 1 { result.push(' '); }
}
Difference::Add(ref z) => {
#[cfg(feature = "color")]
result.push_str(&z.white().on_green().to_string());
#[cfg(not(feature = "color"))]
result.push_str(&z);

if i < diffs.len() - 1 { result.push(' '); }
}
_ => (),
}
}
result.push('\n');
} else {
#[cfg(feature = "color")]
result.push_str(&x.bright_green().to_string());
#[cfg(not(feature = "color"))]
result.push_str(&x);

result.push('\n');
}
},
Difference::Rem(ref x) => {
#[cfg(feature = "color")]
result.push_str(&x.red().to_string());
#[cfg(not(feature = "color"))]
result.push_str(&x);

result.push('\n');
},
}
Expand Down
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@
//!
//! The errors produced by the `assert` method contain information about the tested mock, but also about the
//! **last unmatched request**, which can be very useful to track down an error in your implementation or
//! a missing or incomplete mock. A coloured diff is also displayed.
//! a missing or incomplete mock. A colored diff is also displayed.
//!
//! Color output is enabled by default, but can be toggled with the `color` feature flag.
//!
//! Here's an example of how a `Mock#assert` error looks like:
//!
Expand All @@ -155,7 +157,7 @@
//!
//! > Difference:
//!
//! # A coloured diff
//! # A colored diff
//!
//! ```
//!
Expand Down Expand Up @@ -470,6 +472,7 @@ extern crate regex;
#[macro_use] extern crate log;
extern crate serde_json;
extern crate difference;
#[cfg(feature = "color")]
extern crate colored;
extern crate percent_encoding;
extern crate assert_json_diff;
Expand Down