Skip to content

Commit

Permalink
Add diff module
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricereix committed May 25, 2024
1 parent b77fc74 commit 0ec511e
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/hurl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ xml-rs = { version = "0.8.20" }
lazy_static = "1.4.0"
# uuid features: lets you generate random UUIDs and use a faster (but still sufficiently random) RNG
uuid = { version = "1.8.0", features = ["v4" , "fast-rng"] }
similar = "2.5.0"

[target.'cfg(unix)'.dependencies]
termion = "4.0.0"
Expand Down
132 changes: 132 additions & 0 deletions packages/hurl/src/runner/diff.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Hurl (https://hurl.dev)
* Copyright (C) 2024 Orange
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
use colored::*;
use similar::{ChangeTag, TextDiff};

#[allow(dead_code)]
pub fn diff(expected: &str, actual: &str, color: bool) -> String {
let text_diff = TextDiff::from_lines(expected, actual);
let unified_diff = text_diff.unified_diff();

let mut s = String::new();
for hunk in unified_diff.iter_hunks() {
for change in hunk.iter_changes() {
let sign = match change.tag() {
ChangeTag::Delete => "-",
ChangeTag::Insert => "+",
ChangeTag::Equal => " ",
};

let mut line = format!("{}{}", sign, change);
if color {
line = match change.tag() {
ChangeTag::Delete => line.red().to_string(),
ChangeTag::Insert => line.green().to_string(),
ChangeTag::Equal => line.clone(),
};
}

s.push_str(line.as_str());
}
}

s.to_string()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_diff_json_strings() {
let old = r#"{
"first_name": "John",
"last_name": "Smith",
"is_alive": true,
"age": 27,
"address": {
"street_address": "21 2nd Street",
"city": "New York",
"state": "NY",
"postal_code": "10021-3100"
},
"phone_numbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [
"Catherine",
"Thomas",
"Trevor"
],
"spouse": null
}
"#;

let new = r#"{
"first_name": "John",
"last_name": "Smith",
"is_alive": true,
"age": 28,
"address": {
"street_address": "21 2nd Street",
"city": "New York",
"state": "NY",
"postal_code": "10021-3100"
},
"phone_numbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [
"Catherine",
"Thomas",
"Trevor"
],
"spouse": null
}
"#;

let diff_output = r#" "first_name": "John",
"last_name": "Smith",
"is_alive": true,
- "age": 27,
+ "age": 28,
"address": {
"street_address": "21 2nd Street",
"city": "New York",
"#;
assert_eq!(diff(old, new, false), diff_output);

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";
assert_eq!(diff(old, new, true), diff_colored_output);
}
}
1 change: 1 addition & 0 deletions packages/hurl/src/runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub use self::value::Value;
mod assert;
mod body;
mod capture;
mod diff;
mod entry;
mod error;
mod event;
Expand Down

0 comments on commit 0ec511e

Please sign in to comment.