Skip to content

Commit

Permalink
Merge pull request #85 from EmbarkStudios/color-feature
Browse files Browse the repository at this point in the history
Move colored diff output to feature flag
  • Loading branch information
lipanski authored Sep 25, 2019
2 parents f1c5881 + 9a7f311 commit 33cb781
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
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

0 comments on commit 33cb781

Please sign in to comment.