mirror of
https://github.com/Orange-OpenSource/hurl.git
synced 2024-11-30 00:37:52 +03:00
Return diff output as StyledString
This commit is contained in:
parent
4a4a172cf3
commit
07aab17edd
@ -15,15 +15,16 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
use colored::*;
|
|
||||||
|
use hurl_core::text::{Style, StyledString};
|
||||||
use similar::{ChangeTag, TextDiff};
|
use similar::{ChangeTag, TextDiff};
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn diff(expected: &str, actual: &str, color: bool) -> String {
|
pub fn diff(expected: &str, actual: &str) -> StyledString {
|
||||||
let text_diff = TextDiff::from_lines(expected, actual);
|
let text_diff = TextDiff::from_lines(expected, actual);
|
||||||
let unified_diff = text_diff.unified_diff();
|
let unified_diff = text_diff.unified_diff();
|
||||||
|
|
||||||
let mut s = String::new();
|
let mut s = StyledString::new();
|
||||||
for hunk in unified_diff.iter_hunks() {
|
for hunk in unified_diff.iter_hunks() {
|
||||||
for change in hunk.iter_changes() {
|
for change in hunk.iter_changes() {
|
||||||
let sign = match change.tag() {
|
let sign = match change.tag() {
|
||||||
@ -31,26 +32,22 @@ pub fn diff(expected: &str, actual: &str, color: bool) -> String {
|
|||||||
ChangeTag::Insert => "+",
|
ChangeTag::Insert => "+",
|
||||||
ChangeTag::Equal => " ",
|
ChangeTag::Equal => " ",
|
||||||
};
|
};
|
||||||
|
let line = format!("{}{}", sign, change);
|
||||||
let mut line = format!("{}{}", sign, change);
|
let style = match change.tag() {
|
||||||
if color {
|
ChangeTag::Delete => Style::new().red(),
|
||||||
line = match change.tag() {
|
ChangeTag::Insert => Style::new().green(),
|
||||||
ChangeTag::Delete => line.red().to_string(),
|
ChangeTag::Equal => Style::new(),
|
||||||
ChangeTag::Insert => line.green().to_string(),
|
};
|
||||||
ChangeTag::Equal => line.clone(),
|
s.push_with(&line, style);
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
s.push_str(line.as_str());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
s
|
||||||
s.to_string()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use hurl_core::text::{Format, Style, StyledString};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_diff_json_strings() {
|
fn test_diff_json_strings() {
|
||||||
@ -114,7 +111,33 @@ mod tests {
|
|||||||
}
|
}
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
let diff_output = r#" "first_name": "John",
|
let mut diff_output = StyledString::new();
|
||||||
|
diff_output.push(
|
||||||
|
r#" "first_name": "John",
|
||||||
|
"last_name": "Smith",
|
||||||
|
"is_alive": true,
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
diff_output.push_with(
|
||||||
|
r#"- "age": 27,
|
||||||
|
"#,
|
||||||
|
Style::new().red(),
|
||||||
|
);
|
||||||
|
diff_output.push_with(
|
||||||
|
r#"+ "age": 28,
|
||||||
|
"#,
|
||||||
|
Style::new().green(),
|
||||||
|
);
|
||||||
|
diff_output.push(
|
||||||
|
r#" "address": {
|
||||||
|
"street_address": "21 2nd Street",
|
||||||
|
"city": "New York",
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(diff(old, new), diff_output);
|
||||||
|
|
||||||
|
let diff_output_plain = r#" "first_name": "John",
|
||||||
"last_name": "Smith",
|
"last_name": "Smith",
|
||||||
"is_alive": true,
|
"is_alive": true,
|
||||||
- "age": 27,
|
- "age": 27,
|
||||||
@ -123,10 +146,10 @@ mod tests {
|
|||||||
"street_address": "21 2nd Street",
|
"street_address": "21 2nd Street",
|
||||||
"city": "New York",
|
"city": "New York",
|
||||||
"#;
|
"#;
|
||||||
assert_eq!(diff(old, new, false), diff_output);
|
assert_eq!(diff(old, new).to_string(Format::Plain), diff_output_plain);
|
||||||
|
|
||||||
control::set_override(true);
|
colored::control::set_override(true);
|
||||||
let diff_colored_output = " \"first_name\": \"John\",\n \"last_name\": \"Smith\",\n \"is_alive\": true,\n\u{1b}[31m- \"age\": 27,\n\u{1b}[0m\u{1b}[32m+ \"age\": 28,\n\u{1b}[0m \"address\": {\n \"street_address\": \"21 2nd Street\",\n \"city\": \"New York\",\n";
|
let diff_output_colored = " \"first_name\": \"John\",\n \"last_name\": \"Smith\",\n \"is_alive\": true,\n\u{1b}[31m- \"age\": 27,\n\u{1b}[0m\u{1b}[32m+ \"age\": 28,\n\u{1b}[0m \"address\": {\n \"street_address\": \"21 2nd Street\",\n \"city\": \"New York\",\n";
|
||||||
assert_eq!(diff(old, new, true), diff_colored_output);
|
assert_eq!(diff(old, new).to_string(Format::Ansi), diff_output_colored);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user