Skip to content

Commit 7766214

Browse files
author
Jake Ginnivan
committed
Pulled out a executor class to simplify the ConventionClass
1 parent ccc8237 commit 7766214

File tree

3 files changed

+52
-45
lines changed

3 files changed

+52
-45
lines changed

TestStack.ConventionTests/Convention.cs

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static Convention()
2929
}
3030

3131
public static IEnumerable<ResultInfo> ConventionReports { get { return Reports; } }
32-
public static IList<IReportDataFormatter> Formatters { get; private set; }
32+
public static IList<IReportDataFormatter> Formatters { get; set; }
3333

3434
public static void Is<TDataSource>(IConvention<TDataSource> convention, TDataSource data)
3535
where TDataSource : IConventionData
@@ -42,8 +42,7 @@ public static void Is<TDataSource>(IConvention<TDataSource> convention, TDataSou
4242
{
4343
try
4444
{
45-
var conventionResult = GetConventionReport(convention.ConventionTitle, convention.GetFailingData(data).ToArray(), data);
46-
45+
var conventionResult = Executor.GetConventionReport(convention.ConventionTitle, convention.GetFailingData(data).ToArray(), data);
4746
Reports.Add(conventionResult);
4847

4948
new ConventionReportTraceRenderer().Render(conventionResult);
@@ -58,15 +57,12 @@ public static void Is<TDataSource>(IConvention<TDataSource> convention, TDataSou
5857
public static void IsWithApprovedExeptions<TDataSource>(IConvention<TDataSource> convention, TDataSource data)
5958
where TDataSource : IConventionData
6059
{
61-
var conventionResult = GetConventionReport(convention.ConventionTitle, convention.GetFailingData(data).ToArray(), data);
60+
var conventionResult = Executor.GetConventionReportWithApprovedExeptions(convention.ConventionTitle, convention.GetFailingData(data).ToArray(), data);
6261
Reports.Add(conventionResult);
6362

6463
try
6564
{
6665
var conventionReportTextRenderer = new ConventionReportTextRenderer();
67-
conventionReportTextRenderer.RenderItems(conventionResult);
68-
conventionResult.WithApprovedException(conventionReportTextRenderer.Output);
69-
7066
conventionReportTextRenderer.Render(conventionResult);
7167
Approvals.Verify(conventionReportTextRenderer.Output);
7268

@@ -93,8 +89,8 @@ public static void Is<TDataSource>(ISymmetricConvention<TDataSource> convention,
9389
{
9490
try
9591
{
96-
var conventionResult = GetConventionReport(convention.ConventionTitle, convention.GetFailingData(data).ToArray(), data);
97-
var inverseConventionResult = GetConventionReport(convention.InverseTitle, convention.GetFailingInverseData(data).ToArray(), data);
92+
var conventionResult = Executor.GetConventionReport(convention.ConventionTitle, convention.GetFailingData(data).ToArray(), data);
93+
var inverseConventionResult = Executor.GetConventionReport(convention.InverseTitle, convention.GetFailingInverseData(data).ToArray(), data);
9894

9995
Reports.Add(conventionResult);
10096
Reports.Add(inverseConventionResult);
@@ -111,26 +107,18 @@ public static void Is<TDataSource>(ISymmetricConvention<TDataSource> convention,
111107
public static void IsWithApprovedExeptions<TDataSource>(ISymmetricConvention<TDataSource> convention, TDataSource data)
112108
where TDataSource : IConventionData
113109
{
114-
var conventionResult = GetConventionReport(convention.ConventionTitle, convention.GetFailingData(data).ToArray(), data);
115-
var inverseConventionResult = GetConventionReport(convention.InverseTitle, convention.GetFailingInverseData(data).ToArray(), data);
110+
var conventionResult = Executor.GetConventionReportWithApprovedExeptions(convention.ConventionTitle, convention.GetFailingData(data).ToArray(), data);
111+
var inverseConventionResult = Executor.GetConventionReportWithApprovedExeptions(convention.InverseTitle, convention.GetFailingInverseData(data).ToArray(), data);
116112
Reports.Add(conventionResult);
117113
Reports.Add(inverseConventionResult);
118114

119115
try
120116
{
121-
var conventionReportTextRenderer = new ConventionReportTextRenderer();
122-
// Add approved exceptions to report
123-
conventionReportTextRenderer.RenderItems(conventionResult);
124-
conventionResult.WithApprovedException(conventionReportTextRenderer.Output);
125-
126-
// Add approved exceptions to inverse report
127-
conventionReportTextRenderer.RenderItems(inverseConventionResult);
128-
inverseConventionResult.WithApprovedException(conventionReportTextRenderer.Output);
129-
130117
//Render both, with approved exceptions included
118+
var conventionReportTextRenderer = new ConventionReportTextRenderer();
131119
conventionReportTextRenderer.Render(conventionResult, inverseConventionResult);
132120
Approvals.Verify(conventionReportTextRenderer.Output);
133-
121+
134122
// Trace on success
135123
new ConventionReportTraceRenderer().Render(conventionResult, inverseConventionResult);
136124
}
@@ -144,30 +132,6 @@ public static void IsWithApprovedExeptions<TDataSource>(ISymmetricConvention<TDa
144132
}
145133
}
146134

147-
static ResultInfo GetConventionReport<TDataSource, TDataType>(string conventionTitle, TDataType[] failingData, TDataSource data)
148-
where TDataSource : IConventionData
149-
{
150-
data.EnsureHasNonEmptySource();
151-
var passed = failingData.None();
152-
153-
var conventionResult = new ResultInfo(
154-
passed ? TestResult.Passed : TestResult.Failed,
155-
conventionTitle,
156-
data.Description,
157-
failingData.Select(FormatData).ToArray());
158-
return conventionResult;
159-
}
160-
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-
171135
// http://stackoverflow.com/questions/52797/c-how-do-i-get-the-path-of-the-assembly-the-code-is-in#answer-283917
172136
static string AssemblyDirectory
173137
{
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace TestStack.ConventionTests.Internal
2+
{
3+
using System.Linq;
4+
using TestStack.ConventionTests.Reporting;
5+
6+
public static class Executor
7+
{
8+
public static ResultInfo GetConventionReport(string conventionTitle, object[] failingData, IConventionData data)
9+
{
10+
data.EnsureHasNonEmptySource();
11+
var passed = failingData.None();
12+
13+
var conventionResult = new ResultInfo(
14+
passed ? TestResult.Passed : TestResult.Failed,
15+
conventionTitle,
16+
data.Description,
17+
failingData.Select(FormatData).ToArray());
18+
return conventionResult;
19+
}
20+
21+
public static ResultInfo GetConventionReportWithApprovedExeptions(string conventionTitle, object[] failingData, IConventionData data)
22+
{
23+
var conventionResult = Executor.GetConventionReport(conventionTitle, failingData, data);
24+
var conventionReportTextRenderer = new ConventionReportTextRenderer();
25+
// Add approved exceptions to report
26+
conventionReportTextRenderer.RenderItems(conventionResult);
27+
conventionResult.WithApprovedException(conventionReportTextRenderer.Output);
28+
29+
return conventionResult;
30+
}
31+
32+
static ConventionReportFailure FormatData<T>(T failingData)
33+
{
34+
var formatter = Convention.Formatters.FirstOrDefault(f => f.CanFormat(failingData));
35+
36+
if (formatter == null)
37+
throw new NoDataFormatterFoundException(typeof(T).Name + " has no formatter, add one with `Convention.Formatters.Add(new MyDataFormatter());`");
38+
39+
return formatter.Format(failingData);
40+
}
41+
}
42+
}

TestStack.ConventionTests/TestStack.ConventionTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<ItemGroup>
6363
<Compile Include="ConventionFailedException.cs" />
6464
<Compile Include="Internal\ConventionReportFailure.cs" />
65+
<Compile Include="Internal\Executor.cs" />
6566
<Compile Include="Internal\ResultInfo.cs" />
6667
<Compile Include="Internal\NoDataFormatterFoundException.cs" />
6768
<Compile Include="Reporting\HtmlReportRenderer.cs" />

0 commit comments

Comments
 (0)