Skip to content

Commit

Permalink
[1.1.0] added JestAssert, improved readme (caveats - dynamic objects)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbruckner committed Mar 26, 2020
1 parent 4e0e0a8 commit 742c8b8
Show file tree
Hide file tree
Showing 19 changed files with 436 additions and 45 deletions.
6 changes: 4 additions & 2 deletions JestDotnet/JestDotnet/Core/Exceptions/SnapshotMismatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace JestDotnet.Core.Exceptions
{
public class SnapshotMismatch: Exception
public class SnapshotMismatch : Exception
{
public SnapshotMismatch(string message): base(message) {}
public SnapshotMismatch(string message) : base(message)
{
}
}
}
2 changes: 1 addition & 1 deletion JestDotnet/JestDotnet/Core/SnapshotComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ T actualObject
return (isValid, message);
}

private static string GetDiff(JToken expectedToken, JToken actualToken)
internal static string GetDiff(JToken expectedToken, JToken actualToken)
{
var diff = new JsonDiffPatch();
var patch = diff.Diff(expectedToken, actualToken);
Expand Down
9 changes: 9 additions & 0 deletions JestDotnet/JestDotnet/Core/SnapshotConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace JestDotnet.Core
{
internal static class SnapshotConstants
{
public const string SnapshotExtension = "snap";
public const string SnapshotDirectory = "__snapshots__";
public static readonly string SnapshotDotExtension = $".{SnapshotExtension}";
}
}
9 changes: 9 additions & 0 deletions JestDotnet/JestDotnet/Core/SnapshotResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,14 @@ internal static void StoreSnapshotData(string path, object actualObject)
Directory.CreateDirectory(Path.GetDirectoryName(path));
File.WriteAllText(path, serialized);
}

internal static string CreatePath(string sourceFilePath, string memberName, string hint)
{
var directoryName = $"{Path.GetDirectoryName(sourceFilePath)}/{SnapshotConstants.SnapshotDirectory}";
var fileName =
$"{Path.GetFileNameWithoutExtension(sourceFilePath)}{memberName}{hint}{SnapshotConstants.SnapshotDotExtension}";

return $"{directoryName}/{fileName}";
}
}
}
4 changes: 2 additions & 2 deletions JestDotnet/JestDotnet/Core/SnapshotUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ internal static void TryUpdateSnapshot<T>(string path, T actual, string message)

SnapshotResolver.StoreSnapshotData(path, actual);
}

internal static void TryUpdateMissingSnapshot<T>(string path, T actual)
{
if (!ShouldUpdateIfEmpty())
{
throw new SnapshotDoesNotExist($"Snapshot does not exist");
throw new SnapshotDoesNotExist("Snapshot does not exist");
}

SnapshotResolver.StoreSnapshotData(path, actual);
Expand Down
52 changes: 52 additions & 0 deletions JestDotnet/JestDotnet/JestAssert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Runtime.CompilerServices;
using JestDotnet.Core;
using JestDotnet.Core.Exceptions;

namespace JestDotnet
{
public static class JestAssert
{
public static void ShouldMatchSnapshot(
object actual,
string hint = "",
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = ""
)
{
var path = SnapshotResolver.CreatePath(sourceFilePath, memberName, hint);
var snapshot = SnapshotResolver.GetSnapshotData(path);

if (string.IsNullOrEmpty(snapshot))
{
SnapshotUpdater.TryUpdateMissingSnapshot(path, actual);
return;
}

var (isValid, message) = (ValueTuple<bool, string>) SnapshotComparer.CompareSnapshots(snapshot, actual);
if (!isValid)
{
SnapshotUpdater.TryUpdateSnapshot(path, actual, message);
}
}

public static void ShouldMatchInlineSnapshot(dynamic actual, string inlineSnapshot)
{
var (isValid, message) =
(ValueTuple<bool, string>) SnapshotComparer.CompareSnapshots(inlineSnapshot, actual);
if (!isValid)
{
throw new SnapshotMismatch(message);
}
}

public static void ShouldMatchObject(dynamic actual, dynamic expected)
{
var (isValid, message) = (ValueTuple<bool, string>) SnapshotComparer.CompareSnapshots(expected, actual);
if (!isValid)
{
throw new SnapshotMismatch(message);
}
}
}
}
6 changes: 3 additions & 3 deletions JestDotnet/JestDotnet/JestDotnet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>1.0.0</Version>
<Version>1.1.0</Version>
<Authors>Tomas Bruckner</Authors>
<RepositoryUrl>https://github.com/tomasbruckner/jest-snapshot-dotnet</RepositoryUrl>
<RepositoryUrl>https://github.com/tomasbruckner/jest-dotnet</RepositoryUrl>
<Description>Snapshot testing in C#. See Project Site for more details</Description>
<PackageProjectUrl>https://github.com/tomasbruckner/jest-snapshot-dotnet</PackageProjectUrl>
<PackageProjectUrl>https://github.com/tomasbruckner/jest-dotnet</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>testing, test, snapshot, jest, json, xunit</PackageTags>
<LangVersion>latest</LangVersion>
Expand Down
24 changes: 4 additions & 20 deletions JestDotnet/JestDotnet/JestDotnetExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
using System;
using System.IO;
using System.Runtime.CompilerServices;
using JestDotnet.Core;
using JestDotnet.Core.Exceptions;

namespace JestDotnet
{
public static class JestDotnetExtensions
{
private const string SnapshotExtension = "snap";
private const string SnapshotDirectory = "__snapshots__";
private static readonly string SnapshotDotExtension = $".{SnapshotExtension}";

public static void ShouldMatchSnapshot(
this object actual,
string hint = "",
[System.Runtime.CompilerServices.CallerMemberName]
string memberName = "",
[System.Runtime.CompilerServices.CallerFilePath]
string sourceFilePath = ""
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = ""
)
{
var path = CreatePath(sourceFilePath, memberName, hint);
var path = SnapshotResolver.CreatePath(sourceFilePath, memberName, hint);
var snapshot = SnapshotResolver.GetSnapshotData(path);

if (string.IsNullOrEmpty(snapshot))
Expand Down Expand Up @@ -53,14 +46,5 @@ public static void ShouldMatchObject(this object actual, object expected)
throw new SnapshotMismatch(message);
}
}

private static string CreatePath(string sourceFilePath, string memberName, string hint)
{
var directoryName = $"{Path.GetDirectoryName(sourceFilePath)}/{SnapshotDirectory}";
var fileName =
$"{Path.GetFileNameWithoutExtension(sourceFilePath)}{memberName}{hint}{SnapshotDotExtension}";

return $"{directoryName}/{fileName}";
}
}
}
106 changes: 106 additions & 0 deletions JestDotnet/XUnitTests/ExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using JestDotnet;
using JestDotnet.Core.Exceptions;
using Xunit;
using XUnitTests.Helpers;

namespace XUnitTests
{
public class ExtensionTests
{
[Fact]
public void ShouldMatchInlineSnapshot()
{
var person = new Person
{
Age = 13,
DateOfBirth = new DateTime(2008, 7, 7),
FirstName = "John",
LastName = "Bam"
};

person.ShouldMatchInlineSnapshot(
@"
{
""FirstName"": ""John"",
""LastName"": ""Bam"",
""DateOfBirth"": ""2008-07-07T00:00:00"",
""Age"": 13,
}"
);
}

[Fact]
public void ShouldMatchObject()
{
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 = "Bam"
};

actual.ShouldMatchObject(expected);
}

[Fact]
public void ShouldMatchSnapshot()
{
var person = new Person
{
Age = 13,
DateOfBirth = new DateTime(2008, 7, 7),
FirstName = "John",
LastName = "Bam"
};

person.ShouldMatchSnapshot();
}

[Fact]
public void ShouldMatchSnapshotMismatch()
{
var person = new Person
{
Age = 13,
DateOfBirth = new DateTime(2008, 7, 7),
FirstName = "John",
LastName = "Bam"
};

Assert.Throws<SnapshotMismatch>(
() => { person.ShouldMatchSnapshot(); }
);
}

[Fact]
public void ShouldMatchSnapshotRecursion()
{
var person = new PersonRecursion
{
Age = 13,
DateOfBirth = new DateTime(2008, 7, 7),
FirstName = "John",
LastName = "Bam",
Parent = new PersonRecursion
{
Age = 43,
DateOfBirth = new DateTime(1978, 7, 7),
FirstName = "James",
LastName = "Bam"
}
};

person.ShouldMatchSnapshot();
}
}
}
Loading

0 comments on commit 742c8b8

Please sign in to comment.