Skip to content

Commit

Permalink
Merge pull request #336 from sholland1/StringOptionsCleanUp
Browse files Browse the repository at this point in the history
String options clean up
  • Loading branch information
JakeGinnivan committed Nov 22, 2015
2 parents 7b8f15b + 63f9625 commit eab62cd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ public static IAssertion Create(string expected, string actual, StringCompareSho
return new StringShouldBeAssertion(
expected, actual,
StringComparer.InvariantCulture.Equals,
new ActualCodeTextGetter(),
new SourceCodeTextGetter(),
new StringDifferenceHighlighter(
"Case and Line Ending Sensitive Comparison",
Case.Sensitive));
case StringCompareShould.IgnoreCase:
return new StringShouldBeAssertion(
expected, actual,
StringComparer.InvariantCultureIgnoreCase.Equals,
new ActualCodeTextGetter(),
new SourceCodeTextGetter(),
new StringDifferenceHighlighter(
"Case Insensitive and Line Ending Sensitive Comparison",
Case.Insensitive));
Expand All @@ -31,7 +31,7 @@ public static IAssertion Create(string expected, string actual, StringCompareSho
expected, actual,
(a, e) => StringComparer.InvariantCulture.Equals(
a.NormalizeLineEndings(), e.NormalizeLineEndings()),
new ActualCodeTextGetter(),
new SourceCodeTextGetter(),
new StringDifferenceHighlighter(
"Case Sensitive and Line Ending Insensitive Comparison",
Case.Sensitive, s => s.NormalizeLineEndings()));
Expand All @@ -40,7 +40,7 @@ public static IAssertion Create(string expected, string actual, StringCompareSho
expected, actual,
(a, e) => StringComparer.InvariantCultureIgnoreCase.Equals(
a.NormalizeLineEndings(), e.NormalizeLineEndings()),
new ActualCodeTextGetter(),
new SourceCodeTextGetter(),
new StringDifferenceHighlighter(
"Case and Line Ending Insensitive Comparison",
Case.Insensitive, s => s.NormalizeLineEndings()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Shouldly.Internals
{
internal class ActualCodeTextGetter : ICodeTextGetter
internal class SourceCodeTextGetter : ICodeTextGetter
{
private bool DeterminedOriginatingFrame;
private string ShouldMethod;
Expand All @@ -17,11 +17,11 @@ internal class ActualCodeTextGetter : ICodeTextGetter

public string GetCodeText()
{
dostuff();
SetSourceCodeInfo();
return GetCodePart();
}

private void dostuff()
private void SetSourceCodeInfo()
{
var stackTrace = new StackTrace(true);
var i = 0;
Expand Down Expand Up @@ -54,12 +54,13 @@ private void dostuff()

var fileName = originatingFrame.GetFileName();

DeterminedOriginatingFrame = fileName != null && File.Exists(fileName);
ShouldMethod = shouldlyFrame.GetMethod().Name;
UnderlyingShouldMethod = shouldlyFrame.GetMethod();
FileName = fileName;
LineNumber = originatingFrame.GetFileLineNumber() - 1;
DeterminedOriginatingFrame = fileName != null && File.Exists(fileName);
UnderlyingShouldMethod = shouldlyFrame.GetMethod();
ShouldMethod = UnderlyingShouldMethod.Name;
FileName = fileName;
LineNumber = originatingFrame.GetFileLineNumber() - 1;
}

private bool IsShouldlyMethod(MethodBase method)
{
if (method.DeclaringType == null)
Expand All @@ -74,27 +75,22 @@ private string GetCodePart()
var codePart = "Shouldly uses your source code to generate it's great error messages, build your test project with full debug information to get better error messages" +
"\nThe provided expression";

if (DeterminedOriginatingFrame)
{
var codeLines = string.Join("\n", File.ReadAllLines(FileName).Skip(LineNumber).ToArray());
if (!DeterminedOriginatingFrame) return codePart;

var indexOf = codeLines.IndexOf(ShouldMethod);
if (indexOf > 0)
codePart = codeLines.Substring(0, indexOf - 1).Trim();
var codeLines = string.Join("\n", File.ReadAllLines(FileName).Skip(LineNumber).ToArray());

// When the static method is used instead of the extension method,
// the code part will be "Should".
// Using Endswith to cater for being inside a lambda
if (codePart.EndsWith("Should"))
{
codePart = GetCodePartFromParameter(indexOf, codeLines, codePart);
}
else
{
codePart = codePart.RemoveVariableAssignment().RemoveBlock();
}
var indexOf = codeLines.IndexOf(ShouldMethod);
if (indexOf > 0)
codePart = codeLines.Substring(0, indexOf - 1).Trim();

// When the static method is used instead of the extension method,
// the code part will be "Should".
// Using Endswith to cater for being inside a lambda
if (codePart.EndsWith("Should"))
{
return GetCodePartFromParameter(indexOf, codeLines, codePart);
}
return codePart;
return codePart.RemoveVariableAssignment().RemoveBlock();
}

private string GetCodePartFromParameter(int indexOfMethod, string codeLines, string codePart)
Expand Down
2 changes: 1 addition & 1 deletion src/Shouldly/Shouldly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<Compile Include="DifferenceHighlighting\FormattedDetailedDifferenceString.cs" />
<Compile Include="DifferenceHighlighting\DifferenceIndexConsolidator.cs" />
<Compile Include="DifferenceHighlighting\StringDifferenceHighlighter.cs" />
<Compile Include="Internals\ActualCodeTextGetter.cs" />
<Compile Include="Internals\SourceCodeTextGetter.cs" />
<Compile Include="Internals\AssertionFactories\StringShouldBeAssertionFactory.cs" />
<Compile Include="Internals\Assertions\IAssertion.cs" />
<Compile Include="Internals\Assertions\StringShouldBeAssertion.cs" />
Expand Down

0 comments on commit eab62cd

Please sign in to comment.