diff --git a/Cargo.toml b/Cargo.toml index 6f1200d..d76fcfc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "json_diff_ng" -version = "0.3.0" +version = "0.3.1" authors = ["ksceriath", "ChrisRega"] edition = "2021" license = "Unlicense" diff --git a/README.md b/README.md index ab54ce8..ec10cbf 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,8 @@ Usage Example: Option: -f : read input from json files -d : read input from command line +file : read input from json files +direct : read input from command line ### Installation diff --git a/src/process.rs b/src/process.rs index a83b5d7..4730be6 100644 --- a/src/process.rs +++ b/src/process.rs @@ -99,8 +99,18 @@ pub fn match_json(value1: &Value, value2: &Value) -> Mismatch { let mismatch: Vec<_> = replaced .into_iter() - .flat_map(|(o, ol, n, _nl)| { - (0..ol).map(move |i| (o + i, match_json(&a[o + i], &b[n + i]).keys_in_both)) + .flat_map(|(o, ol, n, nl)| { + let max_length = ol.max(nl); + (0..max_length).map(move |i| { + ( + o + i, + match_json( + a.get(o + i).unwrap_or(&Value::Null), + b.get(n + i).unwrap_or(&Value::Null), + ) + .keys_in_both, + ) + }) }) .collect(); @@ -277,6 +287,22 @@ mod tests { assert_eq!(diff.left_only_keys, KeyNode::Nil); } + #[test] + fn long_insertion_modification() { + let data1 = r#"["a","b","a"]"#; + let data2 = r#"["a","c","c","c","a"]"#; + let diff = compare_jsons(data1, data2).unwrap(); + let diffs = diff.keys_in_both.absolute_keys_to_vec(None); + // 1. is b!=c, second is a!=c, third is missing in data1 but c in data2 + assert_eq!(diffs.len(), 3); + assert_eq!( + diffs.last().unwrap().to_string(), + r#"[l: 3] -> { null != "c" }"# + ); + assert_eq!(diff.right_only_keys, KeyNode::Nil); + assert_eq!(diff.left_only_keys, KeyNode::Nil); + } + #[test] fn test_arrays_object_extra() { let data1 = r#"["a","b"]"#;