Skip to content

Commit 827c41c

Browse files
committed
moved formatting into renderers
They should be in control of how they want the data formatted. We still need to work on how to actually put the two together...
1 parent 945178e commit 827c41c

11 files changed

+50
-67
lines changed

TestStack.ConventionTests/Internal/ConventionContext.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using TestStack.ConventionTests.Conventions;
77
using TestStack.ConventionTests.Reporting;
88

9-
public class ConventionContext : IConventionResultContext
9+
public class ConventionContext : IConventionResultContext, IConventionFormatContext
1010
{
1111
readonly string dataDescription;
1212
readonly IList<IReportDataFormatter> formatters;
@@ -26,12 +26,25 @@ public ConventionResult[] ConventionResults
2626
get { return results.ToArray(); }
2727
}
2828

29+
ConventionReportFailure IConventionFormatContext.FormatData(object failingData)
30+
{
31+
var formatter = formatters.FirstOrDefault(f => f.CanFormat(failingData));
32+
if (formatter == null)
33+
{
34+
throw new NoDataFormatterFoundException(
35+
failingData.GetType().Name +
36+
" has no formatter, add one with `Convention.Formatters.Add(new MyDataFormatter());`");
37+
}
38+
39+
return formatter.Format(failingData);
40+
}
41+
2942
void IConventionResultContext.Is<T>(string resultTitle, IEnumerable<T> failingData)
3043
{
3144
// ReSharper disable PossibleMultipleEnumeration
3245
results.Add(new ConventionResult(resultTitle,
3346
dataDescription,
34-
failingData.Select(FormatData).ToArray()));
47+
failingData.ToObjectArray()));
3548
}
3649

3750
void IConventionResultContext.IsSymmetric<TResult>(
@@ -40,10 +53,10 @@ void IConventionResultContext.IsSymmetric<TResult>(
4053
{
4154
results.Add(new ConventionResult(firstSetFailureTitle,
4255
dataDescription,
43-
firstSetFailureData.Select(FormatData).ToArray()));
56+
firstSetFailureData.ToObjectArray()));
4457
results.Add(new ConventionResult(secondSetFailureTitle,
4558
dataDescription,
46-
secondSetFailureData.Select(FormatData).ToArray()));
59+
secondSetFailureData.ToObjectArray()));
4760
}
4861

4962
void IConventionResultContext.IsSymmetric<TResult>(
@@ -61,19 +74,6 @@ void IConventionResultContext.IsSymmetric<TResult>(
6174
secondSetFailureTitle, secondSetFailingData);
6275
}
6376

64-
ConventionReportFailure FormatData<T>(T failingData)
65-
{
66-
var formatter = formatters.FirstOrDefault(f => f.CanFormat(failingData));
67-
if (formatter == null)
68-
{
69-
throw new NoDataFormatterFoundException(
70-
typeof (T).Name +
71-
" has no formatter, add one with `Convention.Formatters.Add(new MyDataFormatter());`");
72-
}
73-
74-
return formatter.Format(failingData);
75-
}
76-
7777
public void Execute<TDataSource>(IConvention<TDataSource> convention, TDataSource data)
7878
where TDataSource : IConventionData
7979
{
@@ -83,7 +83,7 @@ public void Execute<TDataSource>(IConvention<TDataSource> convention, TDataSourc
8383

8484
foreach (var resultsProcessor in processors)
8585
{
86-
resultsProcessor.Process(ConventionResults);
86+
resultsProcessor.Process(this, ConventionResults);
8787
}
8888
}
8989
}

TestStack.ConventionTests/Internal/ConventionResult.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55

66
public class ConventionResult
77
{
8-
public ConventionResult(string conventionTitle, string dataDescription, ConventionReportFailure[] conventionFailures)
8+
public ConventionResult(string conventionTitle, string dataDescription, object[] data)
99
{
1010
ConventionTitle = conventionTitle;
1111
DataDescription = dataDescription;
12-
ConventionFailures = conventionFailures;
12+
Data = data;
1313
}
1414

1515
public TestResult Result
1616
{
1717
get
1818
{
19-
if (ConventionFailures.Any())
19+
if (Data.Any())
2020
{
2121
return TestResult.Failed;
2222
}
@@ -26,7 +26,6 @@ public TestResult Result
2626

2727
public string ConventionTitle { get; private set; }
2828
public string DataDescription { get; private set; }
29-
public ConventionReportFailure[] ConventionFailures { get; private set; }
30-
public string ApprovedException { get; private set; }
29+
public object[] Data { get; private set; }
3130
}
3231
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace TestStack.ConventionTests.Internal
2+
{
3+
public interface IConventionFormatContext
4+
{
5+
ConventionReportFailure FormatData(object failingData);
6+
}
7+
}

TestStack.ConventionTests/Internal/LinqExtensions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ public static bool None<T>(this IEnumerable<T> enumerable, Func<T, bool> predica
1616
return !enumerable.Any(predicate);
1717
}
1818

19-
2019
public static IEnumerable<T> Unless<T>(this IEnumerable<T> enumerable, Func<T, bool> predicate)
2120
{
2221
return enumerable.Where(i => predicate(i) == false);
2322
}
23+
24+
public static object[] ToObjectArray<T>(this IEnumerable<T> enumerable)
25+
{
26+
return enumerable.Select(x => (object) x).ToArray();
27+
}
2428
}
2529
}

TestStack.ConventionTests/Reporting/ApproveResultsProcessor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
public class ApproveResultsProcessor : IResultsProcessor
99
{
10-
public void Process(params ConventionResult[] results)
10+
public void Process(IConventionFormatContext context, params ConventionResult[] results)
1111
{
1212
try
1313
{
1414
var conventionReportTextRenderer = new ConventionReportTextRenderer();
15-
conventionReportTextRenderer.Process(results);
15+
conventionReportTextRenderer.Process(context, results);
1616
Approvals.Verify(conventionReportTextRenderer.Output);
1717
}
1818
catch (ApprovalException ex)

TestStack.ConventionTests/Reporting/ConventionReportTextRenderer.cs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
public class ConventionReportTextRenderer : IResultsProcessor
77
{
8-
public void Process(params ConventionResult[] results)
8+
public void Process(IConventionFormatContext context, params ConventionResult[] results)
99
{
1010
var stringBuilder = new StringBuilder();
1111

@@ -17,14 +17,7 @@ public void Process(params ConventionResult[] results)
1717
stringBuilder.AppendLine(string.Empty.PadRight(title.Length, '-'));
1818
stringBuilder.AppendLine();
1919

20-
if (!string.IsNullOrEmpty(conventionReport.ApprovedException))
21-
{
22-
stringBuilder.AppendLine("With approved exceptions:");
23-
stringBuilder.AppendLine(conventionReport.ApprovedException);
24-
stringBuilder.AppendLine();
25-
}
26-
27-
RenderItems(conventionReport, stringBuilder);
20+
RenderItems(conventionReport, stringBuilder, context);
2821
stringBuilder.AppendLine();
2922
stringBuilder.AppendLine();
3023
}
@@ -34,19 +27,12 @@ public void Process(params ConventionResult[] results)
3427

3528
public string Output { get; private set; }
3629

37-
public void RenderItems(ConventionResult conventionResult)
38-
{
39-
var stringBuilder = new StringBuilder();
40-
RenderItems(conventionResult, stringBuilder);
41-
Output = stringBuilder.ToString();
42-
}
43-
44-
static void RenderItems(ConventionResult resultInfo, StringBuilder stringBuilder)
30+
static void RenderItems(ConventionResult resultInfo, StringBuilder stringBuilder, IConventionFormatContext context)
4531
{
46-
foreach (var conventionFailure in resultInfo.ConventionFailures)
32+
foreach (var conventionFailure in resultInfo.Data)
4733
{
4834
stringBuilder.Append("\t");
49-
stringBuilder.AppendLine(conventionFailure.ToString());
35+
stringBuilder.AppendLine(context.FormatData(conventionFailure).ToString());
5036
}
5137
}
5238
}

TestStack.ConventionTests/Reporting/ConventionReportTraceRenderer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
public class ConventionReportTraceRenderer : IResultsProcessor
77
{
8-
public void Process(params ConventionResult[] results)
8+
public void Process(IConventionFormatContext context, params ConventionResult[] results)
99
{
1010
var conventionReportTextRenderer = new ConventionReportTextRenderer();
11-
conventionReportTextRenderer.Process(results);
11+
conventionReportTextRenderer.Process(context, results);
1212
Trace.WriteLine(conventionReportTextRenderer.Output);
1313
}
1414
}

TestStack.ConventionTests/Reporting/HtmlReportRenderer.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public HtmlReportRenderer(string assemblyDirectory)
1818
file = Path.Combine(assemblyDirectory, "Conventions.htm");
1919
}
2020

21-
public void Process(params ConventionResult[] results)
21+
public void Process(IConventionFormatContext context, params ConventionResult[] results)
2222
{
2323
Reports.AddRange(results);
2424
var sb = new StringBuilder();
@@ -43,28 +43,14 @@ public void Process(params ConventionResult[] results)
4343
html.RenderEndTag();
4444
var title = String.Format("{0} for {1}", conventionReport.ConventionTitle, conventionReport.DataDescription);
4545
html.Write(title);
46-
if (!String.IsNullOrEmpty(conventionReport.ApprovedException))
47-
{
48-
html.RenderBeginTag(HtmlTextWriterTag.Div);
49-
html.RenderBeginTag(HtmlTextWriterTag.Strong);
50-
html.WriteLine("With approved exceptions:");
51-
html.RenderEndTag();
52-
html.RenderEndTag();
53-
}
5446

5547
html.RenderBeginTag(HtmlTextWriterTag.Ul);
5648

57-
if (!String.IsNullOrEmpty(conventionReport.ApprovedException))
58-
{
59-
html.RenderBeginTag(HtmlTextWriterTag.Li);
60-
html.WriteLine(conventionReport.ApprovedException);
61-
html.RenderEndTag();
62-
}
6349

64-
foreach (var conventionFailure in conventionReport.ConventionFailures)
50+
foreach (var conventionFailure in conventionReport.Data)
6551
{
6652
html.RenderBeginTag(HtmlTextWriterTag.Li);
67-
html.Write(conventionFailure.ToString());
53+
html.Write(context.FormatData(conventionFailure).ToString());
6854
html.RenderEndTag();
6955
}
7056

TestStack.ConventionTests/Reporting/IResultsProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
public interface IResultsProcessor
66
{
7-
void Process(params ConventionResult[] results);
7+
void Process(IConventionFormatContext context, params ConventionResult[] results);
88
}
99
}

TestStack.ConventionTests/Reporting/ThrowOnFailureResultsProcessor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
public class ThrowOnFailureResultsProcessor : IResultsProcessor
77
{
8-
public void Process(params ConventionResult[] results)
8+
public void Process(IConventionFormatContext context, params ConventionResult[] results)
99
{
1010
var conventionReportTextRenderer = new ConventionReportTextRenderer();
11-
conventionReportTextRenderer.Process(results);
11+
conventionReportTextRenderer.Process(context, results);
1212
if (results.Any(r => r.Result == TestResult.Failed))
1313
{
1414
throw new ConventionFailedException(conventionReportTextRenderer.Output);

0 commit comments

Comments
 (0)