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

Strict mode match options #197

Open
wants to merge 2 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
2 changes: 1 addition & 1 deletion src/Snapshooter.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmark", "benchmark", "{
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{F9DFF684-4ACF-45E4-B23E-E8928DE0C9FE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Snapshooter.Environment.Tests", "..\test\Snapshooter.Environment.Tests\Snapshooter.Environment.Tests.csproj", "{C5859230-A6A7-45E7-993C-79C23527CB73}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Snapshooter.StrictMode.Tests", "..\test\Snapshooter.StrictMode.Tests\Snapshooter.StrictMode.Tests.csproj", "{C5859230-A6A7-45E7-993C-79C23527CB73}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Snapshooter.Json.Tests", "..\test\Snapshooter.Json.Tests\Snapshooter.Json.Tests.csproj", "{C2727126-A53A-4B13-AB09-559260F294D7}"
EndProject
Expand Down
2 changes: 1 addition & 1 deletion src/Snapshooter/Core/ISnapshotFileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public interface ISnapshotFileHandler
/// <param name="snapshotFullName">The full name of the snapshot.</param>
/// <param name="snapshotData">The loaded snapshot data.</param>
/// <returns>True if the snapshot could be found.</returns>
bool TryReadSnapshot(SnapshotFullName snapshotFullName, out string snapshotData);
bool TryReadSnapshot(SnapshotFullName snapshotFullName, out string? snapshotData);

/// <summary>
/// Deletes the current snapshot if exists from the __snapshots__ folder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@

namespace Snapshooter.Core.Validators
{
internal static class EnvironmentValidator
internal static class StrictModeValidator
{
public static void CheckStrictMode(bool originalSnapshotExists)
public static void CheckStrictMode(bool originalSnapshotExists, MatchOptions matchOptions)
{
if (!originalSnapshotExists)
{
string value = Environment
.GetEnvironmentVariable("SNAPSHOOTER_STRICT_MODE");

if (string.Equals(value, "on", StringComparison.Ordinal)
|| (bool.TryParse(value, out bool b) && b))
if (IsStrictModeEnvironmentVariableActive() || matchOptions.UseStrictMode)
{
throw new SnapshotNotFoundException(
"Strict mode is enabled and no snapshot has been found " +
Expand All @@ -22,5 +18,11 @@ public static void CheckStrictMode(bool originalSnapshotExists)
}
}
}

private static bool IsStrictModeEnvironmentVariableActive()
{
var environmentVariableValue = Environment.GetEnvironmentVariable("SNAPSHOOTER_STRICT_MODE");
return string.Equals(environmentVariableValue, "on", StringComparison.Ordinal) || (bool.TryParse(environmentVariableValue, out bool b) && b);
}
}
}
27 changes: 27 additions & 0 deletions src/Snapshooter/MatchOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public class MatchOptions
/// </summary>
protected List<FieldMatchOperator> _matchOperators;

/// <summary>
/// Flag if strict mode should be used.
/// </summary>
protected bool _useStrictMode;

/// <summary>
/// Constructor of the <see cref="MatchOptions"/> class to create
/// a new instance.
Expand Down Expand Up @@ -500,6 +505,28 @@ public MatchOptions IncludeField(string fieldPath)
return this;
}

/// <summary>
/// The <see cref="SetStrictMode"/> method allows you to turn Strict Mode on or off.
/// If enabled the comparison will throw a <see cref="SnapshotNotFoundException"/> when no snapshot was found.
/// </summary>
/// <param name="useStrictMode">The flag that turns Strict Mode on or off.</param>
/// <returns></returns>
public MatchOptions SetStrictMode(bool useStrictMode = false)
{
_useStrictMode = useStrictMode;

return this;
}

/// <summary>
/// Flag if Strict Mode is on or off.
/// If Strict Mode is on the comparison will throw a <see cref="SnapshotNotFoundException"/> when no snapshot was found.
/// </summary>
public bool UseStrictMode
{
get { return _useStrictMode; }
}

private MatchOptions AddIgnoreMatchOperator<T>(string fieldsPath)
{
_matchOperators.Add(
Expand Down
23 changes: 11 additions & 12 deletions src/Snapshooter/SnapshotAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,37 +69,36 @@ public class SnapshotAssert : ISnapshotAssert
string actualSnapshotSerialized = _snapshotFormatter
.FormatSnapshot(objectSnapshotSerialized, matchOptions);

bool originalSnapshotExists = _snapshotFileHandler
.TryReadSnapshot(snapshotFullName, out string? originalSnapshotSerialized);
bool expectedSnapshotExists = _snapshotFileHandler
.TryReadSnapshot(snapshotFullName, out string? expectedSnapshotSerialized);

originalSnapshotSerialized ??= actualSnapshotSerialized;
expectedSnapshotSerialized ??= actualSnapshotSerialized;

ExecuteSnapshotComparison(
originalSnapshotExists,
expectedSnapshotExists,
expectedSnapshotSerialized,
actualSnapshotSerialized,
originalSnapshotSerialized,
snapshotFullName,
matchOptions);
}

private void ExecuteSnapshotComparison(
bool originalSnapshotExists,
bool expectedSnapshotExists,
string expectedSnapshotSerialized,
string actualSnapshotSerialized,
string originalSnapshotSerialized,
SnapshotFullName snapshotFullName,
MatchOptions matchOptions)
{
try
{
EnvironmentValidator.CheckStrictMode(
originalSnapshotExists);

StrictModeValidator.CheckStrictMode(expectedSnapshotExists, matchOptions);

_snapshotComparer.CompareSnapshots(
originalSnapshotSerialized,
expectedSnapshotSerialized,
actualSnapshotSerialized,
matchOptions);

if (!originalSnapshotExists)
if (!expectedSnapshotExists)
{
_snapshotFileHandler.SaveNewSnapshot(
snapshotFullName,
Expand Down
73 changes: 0 additions & 73 deletions test/Snapshooter.Environment.Tests/StrictModeTests.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using Newtonsoft.Json.Linq;

namespace Snapshooter.Environment.Tests.Helpers
namespace Snapshooter.StrictMode.Tests.Helpers
{
public class EnvironmentCleanupFixture : IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;

namespace Snapshooter.Environment.Tests.Helpers
namespace Snapshooter.StrictMode.Tests.Helpers
{
public static class CollectionFixtureNames
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<Import Project="$(CCTestProjectProps)" Condition="Exists('$(CCTestProjectProps)')" />

<PropertyGroup>
<AssemblyName>Snapshooter.Environment.Tests</AssemblyName>
<RootNamespace>Snapshooter.Environment.Tests</RootNamespace>
<AssemblyName>Snapshooter.StrictMode.Tests</AssemblyName>
<RootNamespace>Snapshooter.StrictMode.Tests</RootNamespace>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

Expand Down
132 changes: 132 additions & 0 deletions test/Snapshooter.StrictMode.Tests/StrictModeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System;
using System.IO;
using Snapshooter.Exceptions;
using Snapshooter.StrictMode.Tests.Helpers;
using Snapshooter.Tests.Data;
using Snapshooter.Xunit;
using Xunit;

namespace Snapshooter.StrictMode.Tests
{
[Collection(CollectionFixtureNames.SynchronExecutionFixture)]
public class StrictModeTests : IClassFixture<EnvironmentCleanupFixture>
{
[Theory]
[InlineData("on")]
[InlineData("true")]
public void Match_With_Environment_StrictMode_On_Snapshot_Missing(string value)
{
// arrange
System.Environment.SetEnvironmentVariable("SNAPSHOOTER_STRICT_MODE", value);
TestPerson testPerson = TestDataBuilder.TestPersonMarkWalton().Build();

// act
Action action = () => Snapshot.Match(testPerson);

//assert
Assert.Throws<SnapshotNotFoundException>(action);
}

[Theory]
[InlineData("on")]
[InlineData("true")]
public void Match_With_Environment_StrictMode_On_Snapshot_Exists(string value)
{
// arrange
System.Environment.SetEnvironmentVariable("SNAPSHOOTER_STRICT_MODE", value);
TestPerson testPerson = TestDataBuilder.TestPersonMarkWalton().Build();

// act & assert
Snapshot.Match(testPerson);
}

[Theory]
[InlineData("off")]
[InlineData("false")]
public void Match_With_Environment_StrictMode_Off_Snapshot_Not_Exists(string value)
{
// arrange
DeleteSnapshotFileIfExisting();
System.Environment.SetEnvironmentVariable("SNAPSHOOTER_STRICT_MODE", value);
TestPerson testPerson = TestDataBuilder.TestPersonMarkWalton().Build();

// act & assert
Snapshot.Match(testPerson);
DeleteSnapshotFileIfExisting();
}

[Fact]
public void Match_With_MatchOptions_StrictMode_On_Snapshot_Missing()
{
// arrange
TestPerson testPerson = TestDataBuilder.TestPersonMarkWalton().Build();

// act
Action action = () => Snapshot.Match(testPerson, options => options.SetStrictMode(true));

//assert
Assert.Throws<SnapshotNotFoundException>(action);
}

[Fact]
public void Match_With_MatchOptions_StrictMode_On_Snapshot_Exists()
{
// arrange
TestPerson testPerson = TestDataBuilder.TestPersonMarkWalton().Build();

// act & assert
Snapshot.Match(testPerson, options => options.SetStrictMode(true));
}

[Fact]
public void Match_With_MatchOptions_StrictMode_Off_Snapshot_Not_Exists()
{
// arrange
DeleteSnapshotFileIfExisting();
TestPerson testPerson = TestDataBuilder.TestPersonMarkWalton().Build();

// act & assert
Snapshot.Match(testPerson, options => options.SetStrictMode(false));
DeleteSnapshotFileIfExisting();
}

[Fact]
public void Match_With_Environment_StrictMode_Off_MatchOptions_StrictMode_On()
{
// arrange
TestPerson testPerson = TestDataBuilder.TestPersonMarkWalton().Build();

// act
Action action = () => Snapshot.Match(testPerson, options => options.SetStrictMode(true));

//assert
Assert.Throws<SnapshotNotFoundException>(action);
}

[Fact]
public void Match_With_Environment_StrictMode_On_MatchOptions_StrictMode_On()
{
// arrange
System.Environment.SetEnvironmentVariable("SNAPSHOOTER_STRICT_MODE", "on");
TestPerson testPerson = TestDataBuilder.TestPersonMarkWalton().Build();

// act
Action action = () => Snapshot.Match(testPerson, options => options.SetStrictMode(true));

//assert
Assert.Throws<SnapshotNotFoundException>(action);
}

private void DeleteSnapshotFileIfExisting()
{
var snapshotFullNameResolver = new SnapshotFullNameResolver(new XunitSnapshotFullNameReader());
SnapshotFullName snapshotFullName = snapshotFullNameResolver.ResolveSnapshotFullName();
var snapshotFileName = Path.Combine(snapshotFullName.FolderPath, FileNames.SnapshotFolderName, snapshotFullName.Filename);

if (File.Exists(snapshotFileName))
{
File.Delete(snapshotFileName);
}
}
}
}
Loading