From dc714217c141404604ae954c5d7b858e7fc45e33 Mon Sep 17 00:00:00 2001 From: Teajey <21069848+Teajey@users.noreply.github.com> Date: Sun, 13 Aug 2023 10:21:04 +0100 Subject: [PATCH] fix: array diff variant conditions --- src/lib.rs | 20 ++++++++++++-------- tests/snapshots/diff__kitchen_sink.snap | 6 ++++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 314ff49..ddb1c6f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ use serde::{ser::SerializeMap, Serialize}; pub enum EntryDifference { /// An entry from `target` that `source` is missing Missing { value: serde_json::Value }, - /// An extraneous entry that `source` has, and `target` doesn't + /// An entry that `source` has, and `target` doesn't Extra, /// The entry exists in both JSONs, but the values are different Value { value_diff: Difference }, @@ -31,19 +31,23 @@ impl Serialize for DumbMap { #[derive(Debug, Serialize)] #[serde(tag = "array_difference", rename_all = "snake_case")] pub enum ArrayDifference { - /// `source` and `target` are the same length, but some values in the same indices are different + /// `source` and `target` are the same length, but some values of the same indices are different PairsOnly { different_pairs: DumbMap, }, /// `source` is shorter than `target` Shorter { + /// differing pairs that appear in the overlapping indices of `source` and `target` different_pairs: Option>, - extra_elements: Vec, + /// elements missing in `source` that appear in `target` + missing_elements: Vec, }, /// `source` is longer than `target` Longer { + /// differing pairs that appear in the overlapping indices of `source` and `target` different_pairs: Option>, - missing_elements: usize, + /// The amount of extra elements `source` has that `target` does not + extra_length: usize, }, } @@ -118,16 +122,16 @@ pub fn arrays( let missing_elements = target_iter.collect::>(); if !extra_elements.is_empty() { - return Some(ArrayDifference::Shorter { + return Some(ArrayDifference::Longer { different_pairs, - extra_elements, + extra_length: extra_elements.len(), }); } if !missing_elements.is_empty() { - return Some(ArrayDifference::Longer { + return Some(ArrayDifference::Shorter { different_pairs, - missing_elements: missing_elements.len(), + missing_elements, }); } diff --git a/tests/snapshots/diff__kitchen_sink.snap b/tests/snapshots/diff__kitchen_sink.snap index 7b8d20b..d96c242 100644 --- a/tests/snapshots/diff__kitchen_sink.snap +++ b/tests/snapshots/diff__kitchen_sink.snap @@ -27,9 +27,11 @@ expression: "serde_json::to_string_pretty(&diff).expect(\"couldn't pretty\")" "entry_difference": "value", "value_diff": { "difference_of": "array", - "array_difference": "longer", + "array_difference": "shorter", "different_pairs": null, - "missing_elements": 1 + "missing_elements": [ + true + ] } }, "G": {