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 Builder for Snapshot #141

Open
wants to merge 5 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
25 changes: 25 additions & 0 deletions src/Snapshooter.NUnit/NUnitSnapshotBuilder.cs
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);
}
}
}
27 changes: 18 additions & 9 deletions src/Snapshooter.NUnit/SnapshotExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using static Snapshooter.NUnit.Snapshot;

namespace Snapshooter.NUnit
{
Expand All @@ -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>
Expand Down Expand Up @@ -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>
Expand All @@ -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>
Expand Down Expand Up @@ -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.
Expand All @@ -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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

params not commented :)

/// <returns></returns>
Copy link
Member

Choose a reason for hiding this comment

The 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);
}
}
28 changes: 19 additions & 9 deletions src/Snapshooter.Xunit/SnapshotExtension.cs
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
{
Expand All @@ -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>
Expand Down Expand Up @@ -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>
Expand All @@ -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>
Expand Down Expand Up @@ -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.
Expand All @@ -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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Param not commented :)

/// <returns></returns>
Copy link
Member

Choose a reason for hiding this comment

The 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);
}
}
25 changes: 25 additions & 0 deletions src/Snapshooter.Xunit/XUnitSnapshotBuilder.cs
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);
}
}
}
36 changes: 36 additions & 0 deletions src/Snapshooter/ISnapshotBuilder.cs
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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

param & returns empty

/// <returns></returns>
ISnapshotBuilder ConfigureOptions(Func<MatchOptions, MatchOptions> configure);
Copy link
Member

Choose a reason for hiding this comment

The 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>
Copy link
Member

Choose a reason for hiding this comment

The 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>
Copy link
Member

Choose a reason for hiding this comment

The 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();
}
}
82 changes: 82 additions & 0 deletions src/Snapshooter/SnapshotBuilder.cs
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>
Copy link
Member

Choose a reason for hiding this comment

The 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));
}
}
Loading