Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Exclude all fields #203

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

54 changes: 50 additions & 4 deletions src/Snapshooter/MatchOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -469,10 +469,48 @@ public MatchOptions HashField(string fieldPath)
/// <param name="fieldPath">The json path to the field(s) to exclude.</param>
public MatchOptions ExcludeField(string fieldPath)
{
_matchOperators.Add(
new ExcludeMatchOperator(fieldPath));

return this;
return AddExcludeMatchOperator(fieldPath);
}

/// <summary>
/// The <see cref="ExcludeAllFields(string)"/> option excludes all available
/// fields by the given name. All fields with the given name will be excluded
/// during snapshot serialization.
/// </summary>
/// <example>
/// <code>
/// {
/// "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",
/// },
/// ]
/// }
/// </code>
/// <para>
/// <c>Snapshot.Match(userDavidWalton, matchOptions => matchOptions.ExcludeAllFields("UserId")</c>
///
/// 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 <see cref="ExcludeAllFields"/> option is allowed.
/// (No concatenated name strings)
/// </para>
/// </example>
/// <param name="name">The name of the field(s) to be excluded.</param>
public MatchOptions ExcludeAllFields(string name)
{
return AddExcludeMatchOperator(Wellknown.FindByNamePrefix + name);
}

/// <summary>
Expand Down Expand Up @@ -507,6 +545,14 @@ private MatchOptions AddIgnoreMatchOperator<T>(string fieldsPath)

return this;
}

private MatchOptions AddExcludeMatchOperator(string fieldsPath)
{
_matchOperators.Add(
new ExcludeMatchOperator(fieldsPath));

return this;
}

private MatchOptions Accept<T>(
string fieldsPath,
Expand Down
31 changes: 31 additions & 0 deletions test/Snapshooter.Xunit.Tests/SnapshotTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading