-
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.
[1.1.0] added JestAssert, improved readme (caveats - dynamic objects)
- Loading branch information
1 parent
4e0e0a8
commit 742c8b8
Showing
19 changed files
with
436 additions
and
45 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
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 @@ | ||
namespace JestDotnet.Core | ||
{ | ||
internal static class SnapshotConstants | ||
{ | ||
public const string SnapshotExtension = "snap"; | ||
public const string SnapshotDirectory = "__snapshots__"; | ||
public static readonly string SnapshotDotExtension = $".{SnapshotExtension}"; | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} | ||
} |
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,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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.