Skip to content

Commit ece1346

Browse files
author
Jake Ginnivan
committed
Pulled out data formatters
1 parent 6cb6ed4 commit ece1346

25 files changed

+263
-133
lines changed

TestStack.ConventionTests.Tests/ConventionAssertionClassTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ public void approval_mismatch()
2020
StringAssert.Contains("does not match approved file", ex.Message);
2121
}
2222

23-
public class FakeData : IConventionData, ICreateReportLineFor<string>
23+
public class FakeData : IConventionData
2424
{
2525
public string Description { get { return "Fake data"; } }
2626

2727
public void EnsureHasNonEmptySource()
2828
{
2929
}
3030

31-
public ConventionFailure CreateReportLine(string failingData)
31+
public ConventionReportFailure Format(string failingData)
3232
{
33-
return new ConventionFailure(failingData);
33+
return new ConventionReportFailure(failingData);
3434
}
3535
}
3636

TestStack.ConventionTests/Convention.cs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,36 @@
99
using ApprovalTests.Core.Exceptions;
1010
using TestStack.ConventionTests.Conventions;
1111
using TestStack.ConventionTests.Internal;
12+
using TestStack.ConventionTests.Reporting;
1213

1314
public static class Convention
1415
{
1516
static readonly HtmlReportRenderer HtmlRenderer = new HtmlReportRenderer(AssemblyDirectory);
16-
static readonly List<ConventionReport> Reports = new List<ConventionReport>();
17+
static readonly List<ResultInfo> Reports = new List<ResultInfo>();
1718

18-
public static IEnumerable<ConventionReport> ConventionReports { get { return Reports; } }
19+
static Convention()
20+
{
21+
Formatters = new List<IReportDataFormatter>
22+
{
23+
new TypeDataFormatter(),
24+
new ProjectReferenceFormatter(),
25+
new ProjectFileFormatter(),
26+
new MethodInfoDataFormatter(),
27+
new StringDataFormatter()
28+
};
29+
}
30+
31+
public static IEnumerable<ResultInfo> ConventionReports { get { return Reports; } }
32+
public static IList<IReportDataFormatter> Formatters { get; private set; }
1933

2034
public static void Is<TDataSource, TDataType>(IConvention<TDataSource, TDataType> convention, TDataSource data)
21-
where TDataSource : IConventionData, ICreateReportLineFor<TDataType>
35+
where TDataSource : IConventionData
2236
{
2337
Is(convention, data, new ConventionResultExceptionReporter());
2438
}
2539

2640
public static void Is<TDataSource, TDataType>(IConvention<TDataSource, TDataType> convention, TDataSource data, IConventionReportRenderer reporter)
27-
where TDataSource : IConventionData, ICreateReportLineFor<TDataType>
41+
where TDataSource : IConventionData
2842
{
2943
try
3044
{
@@ -42,7 +56,7 @@ public static void Is<TDataSource, TDataType>(IConvention<TDataSource, TDataType
4256
}
4357

4458
public static void IsWithApprovedExeptions<TDataSource, TDataType>(IConvention<TDataSource, TDataType> convention, TDataSource data)
45-
where TDataSource : IConventionData, ICreateReportLineFor<TDataType>
59+
where TDataSource : IConventionData
4660
{
4761
var conventionResult = GetConventionReport(convention.ConventionTitle, convention.GetFailingData(data).ToArray(), data);
4862
Reports.Add(conventionResult);
@@ -69,13 +83,13 @@ public static void IsWithApprovedExeptions<TDataSource, TDataType>(IConvention<T
6983
}
7084

7185
public static void Is<TDataSource, TDataType>(ISymmetricConvention<TDataSource, TDataType> convention, TDataSource data)
72-
where TDataSource : IConventionData, ICreateReportLineFor<TDataType>
86+
where TDataSource : IConventionData
7387
{
7488
Is(convention, data, new ConventionResultExceptionReporter());
7589
}
7690

7791
public static void Is<TDataSource, TDataType>(ISymmetricConvention<TDataSource, TDataType> convention, TDataSource data, IConventionReportRenderer reporter)
78-
where TDataSource : IConventionData, ICreateReportLineFor<TDataType>
92+
where TDataSource : IConventionData
7993
{
8094
try
8195
{
@@ -95,7 +109,7 @@ public static void Is<TDataSource, TDataType>(ISymmetricConvention<TDataSource,
95109
}
96110

97111
public static void IsWithApprovedExeptions<TDataSource, TDataType>(ISymmetricConvention<TDataSource, TDataType> convention, TDataSource data)
98-
where TDataSource : IConventionData, ICreateReportLineFor<TDataType>
112+
where TDataSource : IConventionData
99113
{
100114
var conventionResult = GetConventionReport(convention.ConventionTitle, convention.GetFailingData(data).ToArray(), data);
101115
var inverseConventionResult = GetConventionReport(convention.InverseTitle, convention.GetFailingInverseData(data).ToArray(), data);
@@ -130,20 +144,30 @@ public static void IsWithApprovedExeptions<TDataSource, TDataType>(ISymmetricCon
130144
}
131145
}
132146

133-
static ConventionReport GetConventionReport<TDataSource, TDataType>(string conventionTitle, TDataType[] failingData, TDataSource data)
134-
where TDataSource : IConventionData, ICreateReportLineFor<TDataType>
147+
static ResultInfo GetConventionReport<TDataSource, TDataType>(string conventionTitle, TDataType[] failingData, TDataSource data)
148+
where TDataSource : IConventionData
135149
{
136150
data.EnsureHasNonEmptySource();
137151
var passed = failingData.None();
138152

139-
var conventionResult = new ConventionReport(
140-
passed ? Result.Passed : Result.Failed,
153+
var conventionResult = new ResultInfo(
154+
passed ? TestResult.Passed : TestResult.Failed,
141155
conventionTitle,
142156
data.Description,
143-
failingData.Select(data.CreateReportLine));
157+
failingData.Select(FormatData).ToArray());
144158
return conventionResult;
145159
}
146160

161+
static ConventionReportFailure FormatData<T>(T failingData)
162+
{
163+
var formatter = Formatters.FirstOrDefault(f => f.CanFormat(failingData));
164+
165+
if (formatter == null)
166+
throw new NoDataFormatterFoundException(typeof(T).Name + " has no formatter, add one with `Convention.Formatters.Add(new MyDataFormatter());`");
167+
168+
return formatter.Format(failingData);
169+
}
170+
147171
// http://stackoverflow.com/questions/52797/c-how-do-i-get-the-path-of-the-assembly-the-code-is-in#answer-283917
148172
static string AssemblyDirectory
149173
{

TestStack.ConventionTests/ConventionData/ProjectFiles.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Xml.Linq;
66
using TestStack.ConventionTests.Internal;
77

8-
public class ProjectFiles : AbstractProjectData, ICreateReportLineFor<ProjectFile>
8+
public class ProjectFiles : AbstractProjectData
99
{
1010
public ProjectFiles(Assembly assembly, IProjectProvider projectProvider, IProjectLocator projectLocator)
1111
: base(assembly, projectProvider, projectLocator)
@@ -31,10 +31,5 @@ public ProjectFile[] Files
3131
.ToArray();
3232
}
3333
}
34-
35-
public ConventionFailure CreateReportLine(ProjectFile failingData)
36-
{
37-
return new ConventionFailure(failingData.FilePath);
38-
}
3934
}
4035
}

TestStack.ConventionTests/ConventionData/ProjectReferences.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
using System.Xml.Linq;
88
using TestStack.ConventionTests.Internal;
99

10-
public class ProjectReferences : AbstractProjectData, ICreateReportLineFor<ProjectReference>
10+
public class ProjectReferences : AbstractProjectData
1111
{
1212
public ProjectReferences(Assembly assembly, IProjectProvider projectProvider, IProjectLocator projectLocator)
1313
: base(assembly, projectProvider, projectLocator)
1414
{
15-
Items = PredicateHelpers.All<ProjectReference>();
1615
}
1716

1817
public ProjectReference[] References
@@ -25,7 +24,6 @@ public ProjectReference[] References
2524
{
2625
ReferencedPath = r
2726
})
28-
.Where(Items)
2927
.ToArray();
3028
}
3129
}
@@ -41,12 +39,5 @@ static IEnumerable<string> AllProjectReferences(XDocument projDefinition)
4139
.Select(refElem => refElem.Value);
4240
return references;
4341
}
44-
45-
public Func<ProjectReference, bool> Items { get; set; }
46-
47-
public ConventionFailure CreateReportLine(ProjectReference failingData)
48-
{
49-
return new ConventionFailure(failingData.ReferencedPath);
50-
}
5142
}
5243
}
Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
namespace TestStack.ConventionTests.ConventionData
22
{
33
using System;
4-
using System.Reflection;
54
using TestStack.ConventionTests.Conventions;
65
using TestStack.ConventionTests.Internal;
76

87
/// <summary>
98
/// This is where we set what our convention is all about.
109
/// </summary>
11-
public class Types : IConventionData, ICreateReportLineFor<Type>, ICreateReportLineFor<MethodInfo>
10+
public class Types : IConventionData
1211
{
1312
public Types(string descriptionOfTypes)
1413
{
@@ -24,15 +23,5 @@ public void EnsureHasNonEmptySource()
2423
if (TypesToVerify.None())
2524
throw new ConventionSourceInvalidException("You must supply types to verify");
2625
}
27-
28-
public ConventionFailure CreateReportLine(Type failingData)
29-
{
30-
return new ConventionFailure(failingData.FullName);
31-
}
32-
33-
public ConventionFailure CreateReportLine(MethodInfo failingData)
34-
{
35-
return new ConventionFailure(failingData.DeclaringType + "." + failingData.Name);
36-
}
3726
}
3827
}

TestStack.ConventionTests/ConventionReport.cs

Lines changed: 0 additions & 30 deletions
This file was deleted.

TestStack.ConventionTests/IConventionReportRenderer.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
namespace TestStack.ConventionTests
2-
{
3-
public class ConventionFailure
4-
{
5-
public string Failure { get; set; }
6-
7-
public ConventionFailure(string failure)
8-
{
9-
Failure = failure;
10-
}
11-
12-
public override string ToString()
13-
{
14-
return Failure;
15-
}
16-
}
1+
namespace TestStack.ConventionTests.Internal
2+
{
3+
public class ConventionReportFailure
4+
{
5+
public string Failure { get; set; }
6+
7+
public ConventionReportFailure(string failure)
8+
{
9+
Failure = failure;
10+
}
11+
12+
public override string ToString()
13+
{
14+
return Failure;
15+
}
16+
}
1717
}

TestStack.ConventionTests/Internal/ICreateReportLineFor.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace TestStack.ConventionTests.Internal
2+
{
3+
using System;
4+
using System.Runtime.Serialization;
5+
6+
[Serializable]
7+
public class NoDataFormatterFoundException : Exception
8+
{
9+
public NoDataFormatterFoundException()
10+
{
11+
}
12+
13+
public NoDataFormatterFoundException(string message) : base(message)
14+
{
15+
}
16+
17+
public NoDataFormatterFoundException(string message, Exception inner) : base(message, inner)
18+
{
19+
}
20+
21+
protected NoDataFormatterFoundException(
22+
SerializationInfo info,
23+
StreamingContext context) : base(info, context)
24+
{
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)