Skip to content

Commit

Permalink
Merge pull request #358 from JakeGinnivan/fixDynamicShouldUnitTests
Browse files Browse the repository at this point in the history
Fix dynamic should unit tests
  • Loading branch information
JakeGinnivan committed Jan 30, 2016
2 parents e091e99 + 21b62a8 commit 54ae9c9
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 49 deletions.
2 changes: 2 additions & 0 deletions src/Shouldly.Shared/Internals/IShouldlyAssertionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ internal interface IShouldlyAssertionContext
{
string ShouldMethod { get; set; }
string CodePart { get; set; }
string FileName { get; set; }
int? LineNumber { get; set; }
object Key { get; set; }
object Expected { get; set; }
object Actual { get; set; }
Expand Down
4 changes: 4 additions & 0 deletions src/Shouldly.Shared/Internals/ShouldlyAssertionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ internal class ShouldlyAssertionContext : IShouldlyAssertionContext
{
public string ShouldMethod { get; set; }
public string CodePart { get; set; }
public string FileName { get; set; }
public int? LineNumber { get; set; }

public object Key { get; set; }
public object Expected { get; set; }
Expand Down Expand Up @@ -51,6 +53,8 @@ internal ShouldlyAssertionContext(string shouldlyMethod, object expected = null,
ShouldMethod = shouldlyMethod;

CodePart = actualCodeGetter.GetCodeText(actual, stackTrace);
FileName = actualCodeGetter.FileName;
LineNumber = actualCodeGetter.LineNumber;
}
#endif
}
Expand Down
3 changes: 1 addition & 2 deletions src/Shouldly.Shared/Internals/SourceCodeTextGetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ internal class ActualCodeTextGetter : ICodeTextGetter

public string GetCodeText(object actual, StackTrace stackTrace)
{
ParseStackTrace(stackTrace);

if (ShouldlyConfiguration.IsSourceDisabledInErrors())
return actual.ToStringAwesomely();
ParseStackTrace(stackTrace);
return GetCodePart();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,37 @@

namespace Shouldly.MessageGenerators
{
// internal class DynamicShouldMessageGenerator : ShouldlyMessageGenerator
// {
// static readonly Regex Validator = new Regex("HaveProperty", RegexOptions.Compiled);
// static readonly Regex DynamicObjectNameExtractor = new Regex(@"DynamicShould.HaveProperty\((?<dynamicObjectName>.*?),(?<propertyName>.*?)[\),]", RegexOptions.Compiled);
internal class DynamicShouldMessageGenerator : ShouldlyMessageGenerator
{
static readonly Regex Validator = new Regex("HaveProperty", RegexOptions.Compiled);
static readonly Regex DynamicObjectNameExtractor = new Regex(@"DynamicShould.HaveProperty\((?<dynamicObjectName>.*?),(?<propertyName>.*?)[\),]", RegexOptions.Compiled);

// public override bool CanProcess(IShouldlyAssertionContext context)
// {
// return Validator.IsMatch(context.ShouldMethod);
// }
public override bool CanProcess(IShouldlyAssertionContext context)
{
return Validator.IsMatch(context.ShouldMethod);
}

// public override string GenerateErrorMessage(IShouldlyAssertionContext context)
// {
// const string format =
//@"{0}
// should contain property
//{1}
// but does not.";
public override string GenerateErrorMessage(IShouldlyAssertionContext context)
{
var propertyName = context.Expected;

// var testFileName = context.OriginatingFrame.GetFileName();
// var assertionLineNumber = context.OriginatingFrame.GetFileLineNumber();
var testFileName = context.FileName;
var assertionLineNumber = context.LineNumber;

// var codeLine = string.Join("", File.ReadAllLines(testFileName).ToArray().Skip(assertionLineNumber - 1).Select(s => s.Trim()));
// var dynamicObjectName = DynamicObjectNameExtractor.Match(codeLine).Groups["dynamicObjectName"];
// var propertyName = DynamicObjectNameExtractor.Match(codeLine).Groups["propertyName"];
if (testFileName != null && assertionLineNumber != null)
{
var codeLine = string.Join("", File.ReadAllLines(testFileName).ToArray().Skip(assertionLineNumber.Value - 1).Select(s => s.Trim()));
var dynamicObjectName = DynamicObjectNameExtractor.Match(codeLine).Groups["dynamicObjectName"];

// return string.Format(format, dynamicObjectName, propertyName);
// }
// }
const string format = @"Dynamic object ""{0}"" should contain property ""{1}"" but does not.";
return string.Format(format, dynamicObjectName.ToString().Trim(), propertyName.ToString().Trim());
}
else
{
const string format = @"Dynamic object should contain property ""{0}"" but does not.";
return string.Format(format, propertyName.ToString().Trim());
}
}
}
}
#endif
2 changes: 0 additions & 2 deletions src/Shouldly.Shared/ShouldStaticClasses/DynamicShould.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#if net40
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
using JetBrains.Annotations;

namespace Shouldly
{
[DebuggerStepThrough]
[ShouldlyMethods]
public static class DynamicShould
{
Expand Down
2 changes: 1 addition & 1 deletion src/Shouldly.Shared/ShouldlyMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ internal abstract class ShouldlyMessage
new ShouldBeEmptyMessageGenerator(),
new ShouldAllBeMessageGenerator(),
#if net40
// new DynamicShouldMessageGenerator(), TODO Make worth without stacktraces
new DynamicShouldMessageGenerator(), //TODO Make worth without stacktraces
new ShouldCompleteInMessageGenerator(),
new ShouldBeNullOrWhiteSpaceMessageGenerator(),
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Foo
public string Bar { get; set; }
}

[Fact(Skip = "Dynamic object is broken if no stackframe is available")]
[Fact]
public void HavePropertyNonDynamicScenarioShouldFail()
{
dynamic testDynamicObject = new Foo();
Expand All @@ -23,13 +23,15 @@ public void HavePropertyNonDynamicScenarioShouldFail()

errorWithSource:
@"Dynamic object ""testDynamicObject"" should contain property ""foo"" but does not." + @"
Additional Info:
Some additional context",
Some additional context",

errorWithoutSource:
@"Dynamic object ""testDynamicObject"" should contain property ""foo"" but does not." + @"
@"Dynamic object should contain property ""foo"" but does not." + @"
Additional Info:
Some additional context");
Some additional context");
}

[Fact]
Expand Down
15 changes: 7 additions & 8 deletions src/Shouldly.Tests.Shared/DynamicShould/HavePropertyScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,24 @@ namespace Shouldly.Tests.DynamicShouldTests
{
public class HavePropertyScenario
{

[Fact(Skip = "DynamicShould is broken when stackframe is not available")]
[Fact]
public void HavePropertyScenarioShouldFail()
{
dynamic testDynamicObject = new ExpandoObject();
testDynamicObject.Bar = "BarPropertyValue";
Verify.ShouldFail(() =>
DynamicShould
.HaveProperty(testDynamicObject, "foo", "Some additional context"),
Verify.ShouldFail(() => DynamicShould.HaveProperty(testDynamicObject, "foo", "Some additional context"),

errorWithSource:
@"Dynamic object ""testDynamicObject"" should contain property ""foo"" but does not." + @"
Additional Info:
Some additional context",
Some additional context",

errorWithoutSource:
@"Dynamic object ""testDynamicObject"" should contain property ""foo"" but does not." + @"
@"Dynamic object should contain property ""foo"" but does not." + @"
Additional Info:
Some additional context");
Some additional context");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ namespace Shouldly.Tests.DynamicShouldTests
{
public class MultiLineHavePropertyScenario
{

[Fact(Skip = "DynamicShould is broken when stackframe is not available")]
[Fact]
public void MultiLineHavePropertyScenarioShouldFail()
{
dynamic testDynamicObject = new ExpandoObject();
Expand All @@ -19,13 +18,15 @@ public void MultiLineHavePropertyScenarioShouldFail()

errorWithSource:
@"Dynamic object ""testDynamicObject"" should contain property ""foo"" but does not." + @"
Additional Info:
Some additional context",
Some additional context",

errorWithoutSource:
@"Dynamic object ""testDynamicObject"" should contain property ""foo"" but does not." + @"
@"Dynamic object should contain property ""foo"" but does not." + @"
Additional Info:
Some additional context");
Some additional context");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Shouldly.Tests.DynamicShouldTests
public class MultiLineHavePropertyScenario_MaximumLines
{

[Fact(Skip = "DynamicShould is broken when stackframe is not available")]
[Fact]
public void MultiLineHavePropertyScenario_MaximumLinesShouldFail()
{
dynamic testDynamicObject = new ExpandoObject();
Expand All @@ -24,13 +24,15 @@ public void MultiLineHavePropertyScenario_MaximumLinesShouldFail()

errorWithSource:
@"Dynamic object ""testDynamicObject"" should contain property ""foo"" but does not." + @"
Additional Info:
Some additional context",
Some additional context",

errorWithoutSource:
@"Dynamic object ""testDynamicObject"" should contain property ""foo"" but does not." + @"
@"Dynamic object should contain property ""foo"" but does not." + @"
Additional Info:
Some additional context");
Some additional context");
}

[Fact]
Expand Down

0 comments on commit 54ae9c9

Please sign in to comment.