-
Notifications
You must be signed in to change notification settings - Fork 29
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 Builder for Snapshot #141
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using static Snapshooter.NUnit.Snapshot; | ||
|
||
namespace Snapshooter.NUnit | ||
{ | ||
internal class NUnitSnapshotBuilder : SnapshotBuilder | ||
{ | ||
public NUnitSnapshotBuilder(object target) | ||
: base(target.RemoveUnwantedWrappers()) | ||
{ | ||
} | ||
|
||
public override void Match() | ||
{ | ||
SnapshotFullName snapshotName = (SnapshotName, SnapshotNameExtension) switch | ||
{ | ||
({ }, null) => FullName(SnapshotName), | ||
(null, { }) => FullName(SnapshotNameExtension), | ||
({ }, { }) => FullName(SnapshotName, SnapshotNameExtension), | ||
_ => FullName() | ||
}; | ||
|
||
Snapshot.Match(Target, snapshotName, ConfigureOptions); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System; | ||
using static Snapshooter.NUnit.Snapshot; | ||
|
||
namespace Snapshooter.NUnit | ||
{ | ||
|
@@ -19,7 +20,7 @@ public static void MatchSnapshot( | |
Func<MatchOptions, MatchOptions> matchOptions = null) | ||
{ | ||
var cleanedObject = currentResult.RemoveUnwantedWrappers(); | ||
Snapshot.Match(cleanedObject, matchOptions); | ||
Match(cleanedObject, matchOptions); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -47,7 +48,7 @@ public static void MatchSnapshot( | |
Func<MatchOptions, MatchOptions> matchOptions = null) | ||
{ | ||
var cleanedObject = currentResult.RemoveUnwantedWrappers(); | ||
Snapshot.Match(cleanedObject, snapshotNameExtension, matchOptions); | ||
Match(cleanedObject, snapshotNameExtension, matchOptions); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -70,7 +71,7 @@ public static void MatchSnapshot( | |
Func<MatchOptions, MatchOptions> matchOptions = null) | ||
{ | ||
var cleanedObject = currentResult.RemoveUnwantedWrappers(); | ||
Snapshot.Match(cleanedObject, snapshotName, matchOptions); | ||
Match(cleanedObject, snapshotName, matchOptions); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -103,18 +104,18 @@ public static void MatchSnapshot( | |
Func<MatchOptions, MatchOptions> matchOptions = null) | ||
{ | ||
var cleanedObject = currentResult.RemoveUnwantedWrappers(); | ||
Snapshot.Match(cleanedObject, snapshotName, snapshotNameExtension, matchOptions); | ||
Match(cleanedObject, snapshotName, snapshotNameExtension, matchOptions); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a json snapshot of the given object and compares it with the | ||
/// already existing snapshot of the test. | ||
/// <summary> | ||
/// Creates a json snapshot of the given object and compares it with the | ||
/// already existing snapshot of the test. | ||
/// If no snapshot exists, a new snapshot will be created from the current result | ||
/// and saved under a certain file path, which will shown within the test message. | ||
/// </summary> | ||
/// <param name="currentResult">The object to match.</param> | ||
/// <param name="snapshotFullName"> | ||
/// The full name of a snapshot with folder and file name. | ||
/// The full name of a snapshot with folder and file name. | ||
/// To get a SnapshotFullName use Snapshot.FullName(). </param> | ||
/// <param name="matchOptions"> | ||
/// Additional compare actions, which can be applied during the snapshot comparison. | ||
|
@@ -125,7 +126,15 @@ public static void MatchSnapshot( | |
Func<MatchOptions, MatchOptions> matchOptions = null) | ||
{ | ||
var cleanedObject = currentResult.RemoveUnwantedWrappers(); | ||
Snapshot.Match(cleanedObject, snapshotFullName, matchOptions); | ||
Match(cleanedObject, snapshotFullName, matchOptions); | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a builder to configure the snapshot | ||
/// </summary> | ||
/// <param name="target"></param> | ||
/// <returns></returns> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returns not commented :) |
||
public static ISnapshotBuilder Snapshot(this object target) => | ||
new NUnitSnapshotBuilder(target); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using static Snapshooter.Xunit.Snapshot; | ||
|
||
namespace Snapshooter.Xunit | ||
{ | ||
|
@@ -20,7 +22,7 @@ public static void MatchSnapshot( | |
Func<MatchOptions, MatchOptions> matchOptions = null) | ||
{ | ||
var cleanedObject = currentResult.RemoveUnwantedWrappers(); | ||
Snapshot.Match(cleanedObject, matchOptions); | ||
Match(cleanedObject, matchOptions); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -48,7 +50,7 @@ public static void MatchSnapshot( | |
Func<MatchOptions, MatchOptions> matchOptions = null) | ||
{ | ||
var cleanedObject = currentResult.RemoveUnwantedWrappers(); | ||
Snapshot.Match(cleanedObject, snapshotNameExtension, matchOptions); | ||
Match(cleanedObject, snapshotNameExtension, matchOptions); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -71,7 +73,7 @@ public static void MatchSnapshot( | |
Func<MatchOptions, MatchOptions> matchOptions = null) | ||
{ | ||
var cleanedObject = currentResult.RemoveUnwantedWrappers(); | ||
Snapshot.Match(cleanedObject, snapshotName, matchOptions); | ||
Match(cleanedObject, snapshotName, matchOptions); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -104,18 +106,18 @@ public static void MatchSnapshot( | |
Func<MatchOptions, MatchOptions> matchOptions = null) | ||
{ | ||
var cleanedObject = currentResult.RemoveUnwantedWrappers(); | ||
Snapshot.Match(cleanedObject, snapshotName, snapshotNameExtension, matchOptions); | ||
Match(cleanedObject, snapshotName, snapshotNameExtension, matchOptions); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a json snapshot of the given object and compares it with the | ||
/// already existing snapshot of the test. | ||
/// <summary> | ||
/// Creates a json snapshot of the given object and compares it with the | ||
/// already existing snapshot of the test. | ||
/// If no snapshot exists, a new snapshot will be created from the current result | ||
/// and saved under a certain file path, which will shown within the test message. | ||
/// </summary> | ||
/// <param name="currentResult">The object to match.</param> | ||
/// <param name="snapshotFullName"> | ||
/// The full name of a snapshot with folder and file name. | ||
/// The full name of a snapshot with folder and file name. | ||
/// To get a SnapshotFullName use Snapshot.FullName(). </param> | ||
/// <param name="matchOptions"> | ||
/// Additional compare actions, which can be applied during the snapshot comparison. | ||
|
@@ -126,7 +128,15 @@ public static void MatchSnapshot( | |
Func<MatchOptions, MatchOptions> matchOptions = null) | ||
{ | ||
var cleanedObject = currentResult.RemoveUnwantedWrappers(); | ||
Snapshot.Match(cleanedObject, snapshotFullName, matchOptions); | ||
Match(cleanedObject, snapshotFullName, matchOptions); | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a builder to configure the snapshot | ||
/// </summary> | ||
/// <param name="target"></param> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Param not commented :) |
||
/// <returns></returns> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. returns not commented :) |
||
public static ISnapshotBuilder Snapshot(this object target) => | ||
new XUnitSnapshotBuilder(target); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using static Snapshooter.Xunit.Snapshot; | ||
|
||
namespace Snapshooter.Xunit | ||
{ | ||
internal class XUnitSnapshotBuilder : SnapshotBuilder | ||
{ | ||
public XUnitSnapshotBuilder(object target) | ||
: base(target.RemoveUnwantedWrappers()) | ||
{ | ||
} | ||
|
||
public override void Match() | ||
{ | ||
SnapshotFullName snapshotName = (SnapshotName, SnapshotNameExtension) switch | ||
{ | ||
({ }, null) => FullName(SnapshotName), | ||
(null, { }) => FullName(SnapshotNameExtension), | ||
({ }, { }) => FullName(SnapshotName, SnapshotNameExtension), | ||
_ => FullName() | ||
}; | ||
|
||
Snapshot.Match(Target, snapshotName, ConfigureOptions); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
|
||
namespace Snapshooter | ||
{ | ||
/// <summary> | ||
/// A builder to configure the snapshot | ||
/// </summary> | ||
public interface ISnapshotBuilder | ||
{ | ||
/// <summary> | ||
/// Configures the Match options of this snapshot | ||
/// </summary> | ||
/// <param name="configure"></param> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. param & returns empty |
||
/// <returns></returns> | ||
ISnapshotBuilder ConfigureOptions(Func<MatchOptions, MatchOptions> configure); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we could make this ConfigureOptions private (but we need a little refactoring then) |
||
|
||
/// <summary> | ||
/// Configures the name of this snapshot | ||
/// </summary> | ||
/// <param name="extensions"></param> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. param & returns empty |
||
/// <returns></returns> | ||
ISnapshotBuilder Name(string extensions); | ||
|
||
/// <summary> | ||
/// Configures the name extension | ||
/// </summary> | ||
/// <param name="extensions"></param> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. param & returns empty |
||
/// <returns></returns> | ||
ISnapshotBuilder NameExtension(SnapshotNameExtension extensions); | ||
|
||
/// <summary> | ||
/// Takes a snapshot of the Target and validates it | ||
/// </summary> | ||
void Match(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Snapshooter | ||
{ | ||
/// <summary> | ||
/// A builder to configure the snapshot | ||
/// </summary> | ||
public abstract class SnapshotBuilder | ||
: ISnapshotBuilder | ||
{ | ||
private readonly List<Func<MatchOptions, MatchOptions>> _optionsConfigurations = | ||
new List<Func<MatchOptions, MatchOptions>>(); | ||
|
||
public SnapshotBuilder(object target) | ||
{ | ||
Target = target; | ||
} | ||
|
||
/// <summary> | ||
/// The target that should be snapshoted | ||
/// </summary> | ||
protected object Target { get; } | ||
|
||
/// <summary> | ||
/// The name of the snapshot file | ||
/// </summary> | ||
protected string SnapshotName { get; private set; } | ||
|
||
/// <summary> | ||
/// The name extension of the snapshot | ||
/// </summary> | ||
protected SnapshotNameExtension SnapshotNameExtension { get; private set; } | ||
|
||
/// <summary> | ||
/// Configures the Match options of this snapshot | ||
/// </summary> | ||
/// <param name="configure"></param> | ||
/// <returns></returns> | ||
public ISnapshotBuilder ConfigureOptions(Func<MatchOptions, MatchOptions> configure) | ||
{ | ||
_optionsConfigurations.Add(configure); | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Configures the name of this snapshot | ||
/// </summary> | ||
/// <param name="extensions"></param> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename extensions to 'name' |
||
/// <returns></returns> | ||
public ISnapshotBuilder Name(string extensions) | ||
{ | ||
SnapshotName = extensions; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Configures the name extension | ||
/// </summary> | ||
/// <param name="extensions"></param> | ||
/// <returns></returns> | ||
public ISnapshotBuilder NameExtension(SnapshotNameExtension extensions) | ||
{ | ||
SnapshotNameExtension = extensions; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Takes a snapshot of the Target and validates it | ||
/// </summary> | ||
public abstract void Match(); | ||
|
||
/// <summary> | ||
/// Builds the MatchOptions based on the configuration | ||
/// </summary> | ||
/// <param name="options"></param> | ||
/// <returns></returns> | ||
protected MatchOptions ConfigureOptions(MatchOptions options) => | ||
_optionsConfigurations.Aggregate(options, (current, configure) => configure(current)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
params not commented :)