Skip to content

Commit

Permalink
Merge pull request #356 from JakeGinnivan/ShouldMatchApprovedShouldIn…
Browse files Browse the repository at this point in the history
…cludeClassName

Fixed approval defaults to include class name in filename.
  • Loading branch information
JakeGinnivan committed Jan 30, 2016
2 parents 900072b + ba204c7 commit ff544d0
Show file tree
Hide file tree
Showing 57 changed files with 55 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
To approve the changes run this command:
copy /Y "C:\PathToCode\shouldly\src\DocumentationExamples\ShouldMatchApprovedExamples.ApprovedFileDoesNotExist.received.txt" "C:\PathToCode\shouldly\src\DocumentationExamples\ShouldMatchApprovedExamples.ApprovedFileDoesNotExist.approved.txt"
----------------------------

Approval file C:\PathToCode\shouldly\src\DocumentationExamples\ShouldMatchApprovedExamples.ApprovedFileDoesNotExist.approved.txt
does not exist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
To approve the changes run this command:
copy /Y "C:\PathToCode\shouldly\src\DocumentationExamples\Document.Different.received.txt" "C:\PathToCode\shouldly\src\DocumentationExamples\ApprovedFileIsDifferent.approved.txt"
copy /Y "C:\PathToCode\shouldly\src\DocumentationExamples\ShouldMatchApprovedExamples.ApprovedFileIsDifferent.received.txt" "C:\PathToCode\shouldly\src\DocumentationExamples\ShouldMatchApprovedExamples.ApprovedFileIsDifferent.approved.txt"
----------------------------

simpsonsQuote
Expand Down

This file was deleted.

5 changes: 2 additions & 3 deletions src/DocumentationExamples/DocExampleWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public static void Document(Action shouldMethod, ITestOutputHelper testOutputHel
testOutputHelper.WriteLine("");
testOutputHelper.WriteLine(exceptionText);

var approvedFileFolder = $"CodeExamples/{callerMethod.DeclaringType.Name}";
Func<string, string> scrubber = v => Regex.Replace(v, @"\w:.+?shouldly\\src", "C:\\PathToCode\\shouldly\\src");
try
{
Expand All @@ -69,7 +68,7 @@ public static void Document(Action shouldMethod, ITestOutputHelper testOutputHel
configurationBuilder
.WithDescriminator("codeSample")
.UseCallerLocation()
.SubFolder(approvedFileFolder)
.SubFolder("CodeExamples")
.WithScrubber(scrubber);
additionConfig?.Invoke(configurationBuilder);
Expand All @@ -82,7 +81,7 @@ public static void Document(Action shouldMethod, ITestOutputHelper testOutputHel
configurationBuilder
.WithDescriminator("exceptionText")
.UseCallerLocation()
.SubFolder(approvedFileFolder)
.SubFolder("CodeExamples")
.WithScrubber(scrubber);
additionConfig?.Invoke(configurationBuilder);
Expand Down
9 changes: 5 additions & 4 deletions src/DocumentationExamples/ShouldMatchApprovedExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public void ApprovedFileDoesNotExist()
simpsonsQuote.ShouldMatchApproved(c => c.NoDiff());
}, _testOutputHelper, c =>
{
c.WithScrubber(s => s.Replace("Document.approved.txt", "ApprovedFileDoesNotExist.approved.txt"));
c.WithScrubber(s => s.Replace("DocExampleWriter.Document.approved.txt", "ShouldMatchApprovedExamples.ApprovedFileDoesNotExist.approved.txt"));
c.WithScrubber(s => s.Replace("DocExampleWriter.Document.received.txt", "ShouldMatchApprovedExamples.ApprovedFileDoesNotExist.received.txt"));
c.WithScrubber(s => s.Replace("c => c.NoDiff()", string.Empty));
});
}
Expand All @@ -37,9 +38,9 @@ public void ApprovedFileIsDifferent()
}, _testOutputHelper, c =>
{
// Scrubbing the generated docs is easier than altering the infrastructure to support this scenario
c.WithScrubber(s => s
.Replace("c => c.NoDiff().WithDescriminator(\"Different\")", string.Empty)
.Replace("Document.Different.approved.txt", "ApprovedFileIsDifferent.approved.txt"));
c.WithScrubber(s => s.Replace("c => c.NoDiff().WithDescriminator(\"Different\")", string.Empty));
c.WithScrubber(s => s.Replace("DocExampleWriter.Document.Different.approved.txt", "ShouldMatchApprovedExamples.ApprovedFileIsDifferent.approved.txt"));
c.WithScrubber(s => s.Replace("DocExampleWriter.Document.Different.received.txt", "ShouldMatchApprovedExamples.ApprovedFileIsDifferent.received.txt"));
});
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/Shouldly.Shared/Configuration/ShouldMatchConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System;

#if !PORTABLE

namespace Shouldly.Configuration
{
public delegate string FilenameGenerator(
TestMethodInfo testMethodInfo, string descriminator, string fileType, string fileExtension);

public class ShouldMatchConfiguration
{
public ShouldMatchConfiguration()
Expand All @@ -18,24 +22,30 @@ public ShouldMatchConfiguration(ShouldMatchConfiguration initialConfig)
TestMethodFinder = initialConfig.TestMethodFinder;
ApprovalFileSubFolder = initialConfig.ApprovalFileSubFolder;
Scrubber = initialConfig.Scrubber;
FilenameGenerator = initialConfig.FilenameGenerator;
}

public StringCompareShould StringCompareOptions { get; set; }
public string FilenameDescriminator { get; set; }
public bool PreventDiff { get; set; }

/// <summary>
/// File extension without the .
/// </summary>
public string FileExtension { get; set; }

public ITestMethodFinder TestMethodFinder { get; set; }
public string ApprovalFileSubFolder { get; set; }

/// <summary>
/// Scrubbers allow you to alter the received document before comparing it to approved.
///
/// This is useful for replacing dates or dynamic data with fixed data
/// </summary>
public Func<string, string> Scrubber { get; set; }

public FilenameGenerator FilenameGenerator { get; set; }
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public ShouldMatchConfigurationBuilder WithFileExtension(string fileExtension)
return Configure(c => c.FileExtension = fileExtension.TrimStart('.'));
}

public ShouldMatchConfigurationBuilder WithFilenameGenerator(FilenameGenerator filenameGenerator)
{
return Configure(c => c.FilenameGenerator = filenameGenerator);
}

/// <summary>
/// Default is to ignore line endings
/// </summary>
Expand Down
5 changes: 4 additions & 1 deletion src/Shouldly.Shared/Configuration/TestMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ public class TestMethodInfo
public TestMethodInfo(StackFrame callingFrame)
{
SourceFileDirectory = Path.GetDirectoryName(callingFrame.GetFileName());
MethodName = GetRealMethod(callingFrame.GetMethod()).Name;
var realMethod = GetRealMethod(callingFrame.GetMethod());
MethodName = realMethod.Name;
DeclaringTypeName = realMethod.DeclaringType?.Name;
}

static MethodBase GetRealMethod(MethodBase method)
Expand Down Expand Up @@ -58,6 +60,7 @@ static bool ContainsAttribute(object[] attributes, string attributeName)

public string SourceFileDirectory { get; private set; }
public string MethodName { get; private set; }
public string DeclaringTypeName { get; private set; }
}
}
#endif
4 changes: 3 additions & 1 deletion src/Shouldly.Shared/ShouldlyConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ static ShouldlyConfiguration()
{
StringCompareOptions = StringCompareShould.IgnoreLineEndings,
TestMethodFinder = new FirstNonShouldlyMethodFinder(),
FileExtension = "txt"
FileExtension = "txt",
FilenameGenerator = (testMethodInfo, descriminator, type, extension)
=> $"{testMethodInfo.DeclaringTypeName}.{testMethodInfo.MethodName}{descriminator}.{type}.{extension}"
});

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public static void ShouldMatchApproved(this string actual, Func<string> customMe
Directory.CreateDirectory(outputFolder);
}

var approvedFile = Path.Combine(outputFolder, $"{testMethodInfo.MethodName}{descriminator}.approved.{config.FileExtension}");
var receivedFile = Path.Combine(outputFolder, $"{testMethodInfo.MethodName}{descriminator}.received.{config.FileExtension}");
var approvedFile = Path.Combine(outputFolder, config.FilenameGenerator(testMethodInfo, descriminator, "approved", config.FileExtension));
var receivedFile = Path.Combine(outputFolder, config.FilenameGenerator(testMethodInfo, descriminator, "received", config.FileExtension));
File.WriteAllText(receivedFile, actual);

if (!File.Exists(approvedFile))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public void Simple()
public void MissingApprovedFile()
{
var errorMsg = $@"To approve the changes run this command:
copy /Y ""C:\PathToCode\shouldly\src\Shouldly.Tests.Shared\ShouldMatchApproved\MissingApprovedFile.{targetDescriminator}.received.txt"" ""C:\PathToCode\shouldly\src\Shouldly.Tests.Shared\ShouldMatchApproved\MissingApprovedFile.{targetDescriminator}.approved.txt""
copy /Y ""C:\PathToCode\shouldly\src\Shouldly.Tests.Shared\ShouldMatchApproved\ShouldMatchApprovedScenarios.MissingApprovedFile.{targetDescriminator}.received.txt"" ""C:\PathToCode\shouldly\src\Shouldly.Tests.Shared\ShouldMatchApproved\ShouldMatchApprovedScenarios.MissingApprovedFile.{targetDescriminator}.approved.txt""
----------------------------
Approval file C:\PathToCode\shouldly\src\Shouldly.Tests.Shared\ShouldMatchApproved\MissingApprovedFile.{targetDescriminator}.approved.txt
Approval file C:\PathToCode\shouldly\src\Shouldly.Tests.Shared\ShouldMatchApproved\ShouldMatchApprovedScenarios.MissingApprovedFile.{targetDescriminator}.approved.txt
does not exist";
Verify.ShouldFail(() =>
"Bar".ShouldMatchApproved(c => c.WithDescriminator(targetDescriminator).NoDiff()),
Expand All @@ -59,7 +59,7 @@ public void DifferencesUseShouldlyMessages()

errorWithSource:
$@"To approve the changes run this command:
copy /Y ""C:\PathToCode\shouldly\src\Shouldly.Tests.Shared\ShouldMatchApproved\DifferencesUseShouldlyMessages.{targetDescriminator}.received.txt"" ""C:\PathToCode\shouldly\src\Shouldly.Tests.Shared\ShouldMatchApproved\DifferencesUseShouldlyMessages.{targetDescriminator}.approved.txt""
copy /Y ""C:\PathToCode\shouldly\src\Shouldly.Tests.Shared\ShouldMatchApproved\ShouldMatchApprovedScenarios.DifferencesUseShouldlyMessages.{targetDescriminator}.received.txt"" ""C:\PathToCode\shouldly\src\Shouldly.Tests.Shared\ShouldMatchApproved\ShouldMatchApprovedScenarios.DifferencesUseShouldlyMessages.{targetDescriminator}.approved.txt""
----------------------------
str
Expand All @@ -78,7 +78,7 @@ but was

errorWithoutSource:
$@"To approve the changes run this command:
copy /Y ""C:\PathToCode\shouldly\src\Shouldly.Tests.Shared\ShouldMatchApproved\DifferencesUseShouldlyMessages.{targetDescriminator}.received.txt"" ""C:\PathToCode\shouldly\src\Shouldly.Tests.Shared\ShouldMatchApproved\DifferencesUseShouldlyMessages.{targetDescriminator}.approved.txt""
copy /Y ""C:\PathToCode\shouldly\src\Shouldly.Tests.Shared\ShouldMatchApproved\ShouldMatchApprovedScenarios.DifferencesUseShouldlyMessages.{targetDescriminator}.received.txt"" ""C:\PathToCode\shouldly\src\Shouldly.Tests.Shared\ShouldMatchApproved\ShouldMatchApprovedScenarios.DifferencesUseShouldlyMessages.{targetDescriminator}.approved.txt""
----------------------------
""Foo""
Expand Down Expand Up @@ -125,12 +125,17 @@ public void IgnoresLineEndingsByDefault()
{
var stacktrace = new StackTrace(true);
var sourceFileDir = Path.GetDirectoryName(stacktrace.GetFrame(0).GetFileName());
var approved = Path.Combine(sourceFileDir, $"IgnoresLineEndingsByDefault.{targetDescriminator}.approved.txt");
var approved = Path.Combine(sourceFileDir, $"ShouldMatchApprovedScenarios.IgnoresLineEndingsByDefault.{targetDescriminator}.approved.txt");
File.WriteAllText(approved, "Different\nStyle\nLine\nBreaks");

"Different\r\nStyle\r\nLine\r\nBreaks".ShouldMatchApproved(c => c.WithDescriminator(targetDescriminator));

File.Delete(approved);
try
{
"Different\r\nStyle\r\nLine\r\nBreaks".ShouldMatchApproved(c => c.WithDescriminator(targetDescriminator));
}
finally
{
File.Delete(approved);
}
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ public class DoNotLaunchWhenEnvVariableIsPresent : Shouldly.Configuration.IShoul
public DoNotLaunchWhenEnvVariableIsPresent(string environmentalVariable) { }
public bool ShouldNotLaunch() { }
}
public delegate string FilenameGenerator(Shouldly.Configuration.TestMethodInfo testMethodInfo, string descriminator, string fileType, string fileExtension);
public class FirstNonShouldlyMethodFinder : Shouldly.Configuration.ITestMethodFinder
{
public FirstNonShouldlyMethodFinder() { }
Expand Down Expand Up @@ -624,6 +625,7 @@ public class ShouldMatchConfiguration
public string ApprovalFileSubFolder { get; set; }
public string FileExtension { get; set; }
public string FilenameDescriminator { get; set; }
public Shouldly.Configuration.FilenameGenerator FilenameGenerator { get; set; }
public bool PreventDiff { get; set; }
public System.Func<string, string> Scrubber { get; set; }
public Shouldly.StringCompareShould StringCompareOptions { get; set; }
Expand All @@ -640,12 +642,14 @@ public class ShouldMatchConfigurationBuilder
public Shouldly.Configuration.ShouldMatchConfigurationBuilder UseCallerLocation() { }
public Shouldly.Configuration.ShouldMatchConfigurationBuilder WithDescriminator(string fileDescriminator) { }
public Shouldly.Configuration.ShouldMatchConfigurationBuilder WithFileExtension(string fileExtension) { }
public Shouldly.Configuration.ShouldMatchConfigurationBuilder WithFilenameGenerator(Shouldly.Configuration.FilenameGenerator filenameGenerator) { }
public Shouldly.Configuration.ShouldMatchConfigurationBuilder WithScrubber(System.Func<string, string> scrubber) { }
public Shouldly.Configuration.ShouldMatchConfigurationBuilder WithStringCompareOptions(Shouldly.StringCompareShould stringCompareOptions) { }
}
public class TestMethodInfo
{
public TestMethodInfo(System.Diagnostics.StackFrame callingFrame) { }
public string DeclaringTypeName { get; }
public string MethodName { get; }
public string SourceFileDirectory { get; }
}
Expand Down

0 comments on commit ff544d0

Please sign in to comment.