-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from tomasbruckner/exclude-properties-from-diff
#7 exclude properties from diff
Showing
12 changed files
with
318 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,55 @@ | ||
using JsonDiffPatchDotNet; | ||
using System; | ||
using System.Text.Json.JsonDiffPatch; | ||
using System.Text.Json.Nodes; | ||
using JestDotnet.Core.Settings; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace JestDotnet.Core | ||
{ | ||
internal static class SnapshotComparer | ||
{ | ||
internal static (bool IsValid, string Message) CompareSnapshots<T>(T expectedObject, T actualObject) | ||
internal static (bool IsValid, string Message) CompareSnapshots<T>( | ||
T expectedObject, | ||
T actualObject, | ||
JsonDiffOptions diffOptions = null | ||
) | ||
{ | ||
var serializedExpectedObject = JsonConvert.SerializeObject(expectedObject); | ||
var actualSerializedObject = JsonConvert.SerializeObject(actualObject); | ||
var isValid = serializedExpectedObject == actualSerializedObject; | ||
var message = isValid ? "" : "fail"; | ||
|
||
return (isValid, message); | ||
return Diff( | ||
JsonConvert.SerializeObject(expectedObject), | ||
JsonConvert.SerializeObject(actualObject), | ||
diffOptions | ||
); | ||
} | ||
|
||
internal static (bool IsValid, string Message) CompareSnapshots<T>( | ||
string serializedExpectedObject, | ||
T actualObject | ||
T actualObject, | ||
JsonDiffOptions diffOptions = null | ||
) | ||
{ | ||
var expectedToken = JToken.Parse(serializedExpectedObject); | ||
var actualToken = JToken.FromObject(actualObject); | ||
var isValid = JToken.DeepEquals(expectedToken, actualToken); | ||
var message = isValid ? "" : GetDiff(expectedToken, actualToken); | ||
|
||
return (isValid, message); | ||
return Diff( | ||
serializedExpectedObject, | ||
JsonConvert.SerializeObject(actualObject), | ||
diffOptions | ||
); | ||
} | ||
|
||
internal static string GetDiff(JToken expectedToken, JToken actualToken) | ||
internal static (bool IsValid, string Message) Diff( | ||
string expected, | ||
string actual, | ||
JsonDiffOptions diffOptions = null | ||
) | ||
{ | ||
var diff = new JsonDiffPatch(); | ||
var patch = diff.Diff(expectedToken, actualToken); | ||
var expectedNode = JsonNode.Parse(expected); | ||
var actualNode = JsonNode.Parse(actual); | ||
|
||
var diff = expectedNode.Diff( | ||
actualNode, | ||
diffOptions ?? SnapshotSettings.DefaultCreateDiffOptions() | ||
); | ||
|
||
return patch.ToString(); | ||
return (diff == null, diff?.ToJsonString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json.JsonDiffPatch; | ||
using JestDotnet; | ||
using Xunit; | ||
using XUnitTests.Helpers; | ||
|
||
namespace XUnitTests | ||
{ | ||
public class ExcludePathsTests | ||
{ | ||
[Fact] | ||
public void ShouldIgnoreIntValue() | ||
{ | ||
const int invalidValue = -1; | ||
|
||
var testObject = new ComplexObject | ||
{ | ||
BoolValue = false, | ||
IntValue = invalidValue, | ||
StringValue = string.Empty, | ||
ChildObject = new ChildObject | ||
{ | ||
IntValue = 33, | ||
StringValue = "child" | ||
}, | ||
DateTimeValue = DateTime.MaxValue, | ||
IntNullValue = null, | ||
Children = new Dictionary<string, DictionaryChildObject> | ||
{ | ||
{ | ||
"first", new DictionaryChildObject | ||
{ | ||
IntValue = 312, | ||
StringValue1 = "nested1", | ||
StringValue2 = null, | ||
IntNullValue = null, | ||
ReadOnlyDictionaryChildren = new Dictionary<string, bool> | ||
{ | ||
{ "key1", false }, | ||
{ "key2", true }, | ||
{ | ||
"very.very.very.very.very.very.very.very.very.very.very.very.very.long.key1", | ||
true | ||
}, | ||
{ "Рикроллинг", true }, | ||
{ | ||
"very.very.very.very.very.very.very.very.very.very.very.very.very.long.key3", | ||
true | ||
}, | ||
{ | ||
"very.very.very.very.very.very.very.very.very.very.very.very.very.long.key4", | ||
true | ||
}, | ||
{ "4", true } | ||
} | ||
} | ||
}, | ||
{ | ||
"second", new DictionaryChildObject | ||
{ | ||
IntValue = 312, | ||
StringValue1 = "nested2", | ||
StringValue2 = "x", | ||
IntNullValue = 4, | ||
ReadOnlyDictionaryChildren = new Dictionary<string, bool>() | ||
} | ||
}, | ||
{ | ||
"third", new DictionaryChildObject | ||
{ | ||
IntValue = invalidValue, | ||
StringValue1 = "nested2", | ||
StringValue2 = "x", | ||
IntNullValue = 4, | ||
ReadOnlyDictionaryChildren = null | ||
} | ||
} | ||
} | ||
}; | ||
|
||
JestAssert.ShouldMatchSnapshot( | ||
testObject, | ||
"", | ||
new JsonDiffOptions | ||
{ | ||
PropertyFilter = (s, _) => s != "IntValue", | ||
} | ||
); | ||
} | ||
|
||
[Fact] | ||
public void ShouldIgnoreSimpleIntValue() | ||
{ | ||
const int invalidValue = -1; | ||
const int validValue = 2; | ||
|
||
var testObject = new Dictionary<string, Dictionary<string, int>> | ||
{ | ||
{ | ||
"a", new Dictionary<string, int> | ||
{ | ||
{ "exclude", invalidValue }, | ||
{ "notExclude", 15} | ||
} | ||
}, | ||
{ | ||
"exclude", new Dictionary<string, int> | ||
{ | ||
{ "c", invalidValue } | ||
} | ||
} | ||
}; | ||
|
||
JestAssert.ShouldMatchSnapshot( | ||
testObject, | ||
"", | ||
new JsonDiffOptions | ||
{ | ||
JsonElementComparison = JsonElementComparison.Semantic, | ||
PropertyFilter = (s, _) => s != "exclude", | ||
} | ||
); | ||
} | ||
|
||
[Fact] | ||
public void ShouldIgnoreSimpleIntValue_Object() | ||
{ | ||
var actual = new Person | ||
{ | ||
Age = 13, | ||
DateOfBirth = new DateTime(2008, 7, 7), | ||
FirstName = "John", | ||
LastName = "Bam" | ||
}; | ||
|
||
var expected = new Person | ||
{ | ||
Age = 13, | ||
DateOfBirth = new DateTime(2008, 7, 7), | ||
FirstName = "John", | ||
LastName = "" | ||
}; | ||
|
||
JestAssert.ShouldMatchObject(actual,expected, new JsonDiffOptions | ||
{ | ||
PropertyFilter = (s, context) => s != "LastName" | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
JestDotnet/XUnitTests/__snapshots__/ExcludePathsTestsShouldIgnoreIntValue.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"Children": { | ||
"first": { | ||
"ReadOnlyDictionaryChildren": { | ||
"key1": false, | ||
"key2": true, | ||
"very.very.very.very.very.very.very.very.very.very.very.very.very.long.key1": true, | ||
"Рикроллинг": true, | ||
"very.very.very.very.very.very.very.very.very.very.very.very.very.long.key3": true, | ||
"very.very.very.very.very.very.very.very.very.very.very.very.very.long.key4": true, | ||
"4": true | ||
}, | ||
"IntValue": 312, | ||
"StringValue1": "nested1", | ||
"StringValue2": null, | ||
"IntNullValue": null | ||
}, | ||
"second": { | ||
"ReadOnlyDictionaryChildren": {}, | ||
"IntValue": 312, | ||
"StringValue1": "nested2", | ||
"StringValue2": "x", | ||
"IntNullValue": 4 | ||
}, | ||
"third": { | ||
"ReadOnlyDictionaryChildren": null, | ||
"IntValue": 312, | ||
"StringValue1": "nested2", | ||
"StringValue2": "x", | ||
"IntNullValue": 4 | ||
} | ||
}, | ||
"ChildObject": { | ||
"IntValue": 33, | ||
"StringValue": "child" | ||
}, | ||
"BoolValue": false, | ||
"IntValue": 123, | ||
"DateTimeValue": "9999-12-31T23:59:59.9999999", | ||
"StringValue": "", | ||
"IntNullValue": null | ||
} |
9 changes: 9 additions & 0 deletions
9
JestDotnet/XUnitTests/__snapshots__/ExcludePathsTestsShouldIgnoreSimpleIntValue.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"a": { | ||
"exclude": -1, | ||
"notExclude": 15 | ||
}, | ||
"exclude": { | ||
"c": -1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters