From 2f7ee80d2e2f48153b549e404148acd571f78640 Mon Sep 17 00:00:00 2001 From: Ciaran Liedeman Date: Tue, 3 Dec 2024 13:09:55 +0200 Subject: [PATCH 1/2] Added Exclude all fields --- src/Snapshooter/MatchOptions.cs | 54 +++++++++++++++++-- test/Snapshooter.Xunit.Tests/SnapshotTests.cs | 31 +++++++++++ 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/src/Snapshooter/MatchOptions.cs b/src/Snapshooter/MatchOptions.cs index 1010739..2d998ce 100644 --- a/src/Snapshooter/MatchOptions.cs +++ b/src/Snapshooter/MatchOptions.cs @@ -469,10 +469,48 @@ public MatchOptions HashField(string fieldPath) /// The json path to the field(s) to exclude. public MatchOptions ExcludeField(string fieldPath) { - _matchOperators.Add( - new ExcludeMatchOperator(fieldPath)); - - return this; + return AddExcludeMatchOperator(fieldPath); + } + + /// + /// The option excludes all available + /// fields by the given name. All fields with the given name will be excluded + /// during snapshot serialization. + /// + /// + /// + /// { + /// "UserId": "0A332E69-FDDB-46B9-8E42-C411C3F633AC", + /// "Firstname": "David", + /// "Lastname": "Walton", + /// "Relatives": [ + /// { + /// "UserId": "E20EEEE6-39D1-4878-B8A9-621CECDDDA82", + /// "Firstname": "Mark", + /// "Lastname": "Walton", + /// }, + /// { + /// "UserId": "355910B4-6CD9-4FC3-962B-75D079C50415", + /// "Firstname": "Jenny", + /// "Lastname": "Walton", + /// }, + /// ] + /// } + /// + /// + /// Snapshot.Match(userDavidWalton, matchOptions => matchOptions.ExcludeAllFields("UserId") + /// + /// This configured match option has the effect, that all 'UserId' fields + /// will be excluded during snapshot serialization. (Therefore all 3 UserIds) + /// + /// Only one 'name' per option is allowed. + /// (No concatenated name strings) + /// + /// + /// The name of the field(s) to be excluded. + public MatchOptions ExcludeAllFields(string name) + { + return AddExcludeMatchOperator(Wellknown.FindByNamePrefix + name); } /// @@ -507,6 +545,14 @@ private MatchOptions AddIgnoreMatchOperator(string fieldsPath) return this; } + + private MatchOptions AddExcludeMatchOperator(string fieldsPath) + { + _matchOperators.Add( + new ExcludeMatchOperator(fieldsPath)); + + return this; + } private MatchOptions Accept( string fieldsPath, diff --git a/test/Snapshooter.Xunit.Tests/SnapshotTests.cs b/test/Snapshooter.Xunit.Tests/SnapshotTests.cs index 8180579..fc4923f 100644 --- a/test/Snapshooter.Xunit.Tests/SnapshotTests.cs +++ b/test/Snapshooter.Xunit.Tests/SnapshotTests.cs @@ -767,6 +767,37 @@ public void Match_CircularReference_SuccessfulMatch() Snapshot.Match(markWalton); } + #endregion + + #region Match Snapshots - Exclude Fields Tests + + [Fact] + public void Match_ExcludeScalarField_SuccessfulExcluded() + { + // arrange + TestPerson testPerson = TestDataBuilder + .TestPersonSandraSchneider() + .WithSize(1.5m) + .Build(); + + // act & assert + Snapshot.Match( + testPerson, matchOptions => matchOptions.ExcludeField("Size")); + } + + [Fact] + public void Match_ExcludeAllDateOfBirthFields_SuccessfulExcluded() + { + // arrange + TestPerson testPerson = TestDataBuilder + .TestPersonMarkWalton() + .Build(); + + // act & assert + Snapshot.Match( + testPerson, matchOptions => matchOptions.ExcludeAllFields("DateOfBirth")); + } + #endregion #region Match Snapshots - Scalar Types Tests From 5023a6945ce04a04feea4ee2139afdb7822deed2 Mon Sep 17 00:00:00 2001 From: PascalSenn Date: Thu, 19 Dec 2024 14:23:18 +0100 Subject: [PATCH 2/2] Trigger build --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4fa52af..fb97b41 100644 --- a/README.md +++ b/README.md @@ -298,3 +298,4 @@ In order to fail tests that are without a snapshot on your CI-build you can set This project has adopted the code of conduct defined by the [Contributor Covenant](https://contributor-covenant.org/) to clarify expected behavior in our community. For more information, see the [Swiss Life OSS Code of Conduct](https://swisslife-oss.github.io/coc). +