From 0d8c73aae39b04dcfdd91d5622abe743e54256d3 Mon Sep 17 00:00:00 2001 From: Krzysztof Kozmic Date: Sun, 18 Aug 2013 11:30:35 +1000 Subject: [PATCH 01/11] added abstraction for handling results --- TestStack.ConventionTests/Internal/ConventionContext.cs | 1 - TestStack.ConventionTests/Internal/IResultsProcessor.cs | 7 +++++++ TestStack.ConventionTests/TestStack.ConventionTests.csproj | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 TestStack.ConventionTests/Internal/IResultsProcessor.cs diff --git a/TestStack.ConventionTests/Internal/ConventionContext.cs b/TestStack.ConventionTests/Internal/ConventionContext.cs index 028daec..4245308 100644 --- a/TestStack.ConventionTests/Internal/ConventionContext.cs +++ b/TestStack.ConventionTests/Internal/ConventionContext.cs @@ -83,7 +83,6 @@ public ConventionResult[] GetConventionResults(IConvention + From 2c19bf97ffadac144edcf237969c31d22a2890dd Mon Sep 17 00:00:00 2001 From: Krzysztof Kozmic Date: Sun, 18 Aug 2013 11:37:02 +1000 Subject: [PATCH 02/11] converted IConventionReportRenderer to IResultsProcessor This seems like a better abstraction, especially that some of them actually not render anything but approve/validate the results. This seems like a better abstraction not imposing any particular way the result should be handled --- TestStack.ConventionTests/Convention.cs | 14 +++++++------- .../Internal/IResultsProcessor.cs | 7 ------- .../Reporting/ConventionReportTextRenderer.cs | 4 ++-- .../Reporting/ConventionReportTraceRenderer.cs | 6 +++--- .../Reporting/ConventionResultExceptionReporter.cs | 6 +++--- .../Reporting/HtmlReportRenderer.cs | 4 ++-- .../Reporting/IConventionReportRenderer.cs | 9 --------- .../Reporting/IResultsProcessor.cs | 9 +++++++++ .../TestStack.ConventionTests.csproj | 3 +-- 9 files changed, 27 insertions(+), 35 deletions(-) delete mode 100644 TestStack.ConventionTests/Internal/IResultsProcessor.cs delete mode 100644 TestStack.ConventionTests/Reporting/IConventionReportRenderer.cs create mode 100644 TestStack.ConventionTests/Reporting/IResultsProcessor.cs diff --git a/TestStack.ConventionTests/Convention.cs b/TestStack.ConventionTests/Convention.cs index 86a85f9..ad8bd8b 100644 --- a/TestStack.ConventionTests/Convention.cs +++ b/TestStack.ConventionTests/Convention.cs @@ -35,7 +35,7 @@ public static void Is(IConvention convention, TDataSou Is(convention, data, new ConventionResultExceptionReporter()); } - public static void Is(IConvention convention, TDataSource data, IConventionReportRenderer reporter) + public static void Is(IConvention convention, TDataSource data, IResultsProcessor reporter) where TDataSource : IConventionData { try @@ -44,12 +44,12 @@ public static void Is(IConvention convention, TDataSou var conventionResult = context.GetConventionResults(convention, data); Reports.AddRange(conventionResult); - new ConventionReportTraceRenderer().Render(conventionResult); - reporter.Render(conventionResult); + new ConventionReportTraceRenderer().Process(conventionResult); + reporter.Process(conventionResult); } finally { - HtmlRenderer.Render(Reports.ToArray()); + HtmlRenderer.Process(Reports.ToArray()); } } @@ -63,10 +63,10 @@ public static void IsWithApprovedExeptions(IConvention try { var conventionReportTextRenderer = new ConventionReportTextRenderer(); - conventionReportTextRenderer.Render(conventionResult); + conventionReportTextRenderer.Process(conventionResult); Approvals.Verify(conventionReportTextRenderer.Output); - new ConventionReportTraceRenderer().Render(conventionResult); + new ConventionReportTraceRenderer().Process(conventionResult); } catch (ApprovalException ex) { @@ -74,7 +74,7 @@ public static void IsWithApprovedExeptions(IConvention } finally { - HtmlRenderer.Render(Reports.ToArray()); + HtmlRenderer.Process(Reports.ToArray()); } } diff --git a/TestStack.ConventionTests/Internal/IResultsProcessor.cs b/TestStack.ConventionTests/Internal/IResultsProcessor.cs deleted file mode 100644 index 815707e..0000000 --- a/TestStack.ConventionTests/Internal/IResultsProcessor.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace TestStack.ConventionTests.Internal -{ - public interface IResultsProcessor - { - void Process(ConventionResult result); - } -} \ No newline at end of file diff --git a/TestStack.ConventionTests/Reporting/ConventionReportTextRenderer.cs b/TestStack.ConventionTests/Reporting/ConventionReportTextRenderer.cs index dc7f72e..538c3f0 100644 --- a/TestStack.ConventionTests/Reporting/ConventionReportTextRenderer.cs +++ b/TestStack.ConventionTests/Reporting/ConventionReportTextRenderer.cs @@ -3,9 +3,9 @@ using System.Text; using TestStack.ConventionTests.Internal; - public class ConventionReportTextRenderer : IConventionReportRenderer + public class ConventionReportTextRenderer : IResultsProcessor { - public void Render(params ConventionResult[] conventionResult) + public void Process(params ConventionResult[] conventionResult) { var stringBuilder = new StringBuilder(); diff --git a/TestStack.ConventionTests/Reporting/ConventionReportTraceRenderer.cs b/TestStack.ConventionTests/Reporting/ConventionReportTraceRenderer.cs index d283562..91817fc 100644 --- a/TestStack.ConventionTests/Reporting/ConventionReportTraceRenderer.cs +++ b/TestStack.ConventionTests/Reporting/ConventionReportTraceRenderer.cs @@ -3,12 +3,12 @@ using System.Diagnostics; using TestStack.ConventionTests.Internal; - public class ConventionReportTraceRenderer : IConventionReportRenderer + public class ConventionReportTraceRenderer : IResultsProcessor { - public void Render(params ConventionResult[] conventionResult) + public void Process(params ConventionResult[] conventionResult) { var conventionReportTextRenderer = new ConventionReportTextRenderer(); - conventionReportTextRenderer.Render(conventionResult); + conventionReportTextRenderer.Process(conventionResult); Trace.WriteLine(conventionReportTextRenderer.Output); } } diff --git a/TestStack.ConventionTests/Reporting/ConventionResultExceptionReporter.cs b/TestStack.ConventionTests/Reporting/ConventionResultExceptionReporter.cs index e2d788b..2f4324a 100644 --- a/TestStack.ConventionTests/Reporting/ConventionResultExceptionReporter.cs +++ b/TestStack.ConventionTests/Reporting/ConventionResultExceptionReporter.cs @@ -3,12 +3,12 @@ using System.Linq; using TestStack.ConventionTests.Internal; - public class ConventionResultExceptionReporter : IConventionReportRenderer + public class ConventionResultExceptionReporter : IResultsProcessor { - public void Render(params ConventionResult[] conventionResult) + public void Process(params ConventionResult[] conventionResult) { var conventionReportTextRenderer = new ConventionReportTextRenderer(); - conventionReportTextRenderer.Render(conventionResult); + conventionReportTextRenderer.Process(conventionResult); if (conventionResult.Any(r => r.Result == TestResult.Failed)) { throw new ConventionFailedException(conventionReportTextRenderer.Output); diff --git a/TestStack.ConventionTests/Reporting/HtmlReportRenderer.cs b/TestStack.ConventionTests/Reporting/HtmlReportRenderer.cs index 79ae3b9..9d6bdee 100644 --- a/TestStack.ConventionTests/Reporting/HtmlReportRenderer.cs +++ b/TestStack.ConventionTests/Reporting/HtmlReportRenderer.cs @@ -5,7 +5,7 @@ using System.Web.UI; using TestStack.ConventionTests.Internal; - public class HtmlReportRenderer : IConventionReportRenderer + public class HtmlReportRenderer : IResultsProcessor { readonly string file; @@ -14,7 +14,7 @@ public HtmlReportRenderer(string assemblyDirectory) file = Path.Combine(assemblyDirectory, "Conventions.htm"); } - public void Render(params ConventionResult[] conventionResult) + public void Process(params ConventionResult[] conventionResult) { var sb = new StringBuilder(); var html = new HtmlTextWriter(new StringWriter(sb)); diff --git a/TestStack.ConventionTests/Reporting/IConventionReportRenderer.cs b/TestStack.ConventionTests/Reporting/IConventionReportRenderer.cs deleted file mode 100644 index c472036..0000000 --- a/TestStack.ConventionTests/Reporting/IConventionReportRenderer.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace TestStack.ConventionTests.Reporting -{ - using TestStack.ConventionTests.Internal; - - public interface IConventionReportRenderer - { - void Render(params ConventionResult[] conventionResult); - } -} \ No newline at end of file diff --git a/TestStack.ConventionTests/Reporting/IResultsProcessor.cs b/TestStack.ConventionTests/Reporting/IResultsProcessor.cs new file mode 100644 index 0000000..2d6f4db --- /dev/null +++ b/TestStack.ConventionTests/Reporting/IResultsProcessor.cs @@ -0,0 +1,9 @@ +namespace TestStack.ConventionTests.Reporting +{ + using TestStack.ConventionTests.Internal; + + public interface IResultsProcessor + { + void Process(params ConventionResult[] conventionResult); + } +} \ No newline at end of file diff --git a/TestStack.ConventionTests/TestStack.ConventionTests.csproj b/TestStack.ConventionTests/TestStack.ConventionTests.csproj index 9a8072e..9b29d1b 100644 --- a/TestStack.ConventionTests/TestStack.ConventionTests.csproj +++ b/TestStack.ConventionTests/TestStack.ConventionTests.csproj @@ -63,7 +63,6 @@ - @@ -81,7 +80,7 @@ - + From c4664982a893197fab7ce8984d8f877016a43f2f Mon Sep 17 00:00:00 2001 From: Krzysztof Kozmic Date: Sun, 18 Aug 2013 11:45:56 +1000 Subject: [PATCH 03/11] added ApproveResultsProcessor + some renames --- TestStack.ConventionTests/Convention.cs | 2 +- .../Reporting/ApproveResultsProcessor.cs | 24 +++++++++++++++++++ .../Reporting/ConventionReportTextRenderer.cs | 4 ++-- .../ConventionReportTraceRenderer.cs | 4 ++-- .../Reporting/HtmlReportRenderer.cs | 4 ++-- .../Reporting/IResultsProcessor.cs | 2 +- ...r.cs => ThrowOnFailureResultsProcessor.cs} | 8 +++---- .../TestStack.ConventionTests.csproj | 3 ++- 8 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 TestStack.ConventionTests/Reporting/ApproveResultsProcessor.cs rename TestStack.ConventionTests/Reporting/{ConventionResultExceptionReporter.cs => ThrowOnFailureResultsProcessor.cs} (54%) diff --git a/TestStack.ConventionTests/Convention.cs b/TestStack.ConventionTests/Convention.cs index ad8bd8b..d3a5bc7 100644 --- a/TestStack.ConventionTests/Convention.cs +++ b/TestStack.ConventionTests/Convention.cs @@ -32,7 +32,7 @@ static Convention() public static void Is(IConvention convention, TDataSource data) where TDataSource : IConventionData { - Is(convention, data, new ConventionResultExceptionReporter()); + Is(convention, data, new ThrowOnFailureResultsProcessor()); } public static void Is(IConvention convention, TDataSource data, IResultsProcessor reporter) diff --git a/TestStack.ConventionTests/Reporting/ApproveResultsProcessor.cs b/TestStack.ConventionTests/Reporting/ApproveResultsProcessor.cs new file mode 100644 index 0000000..c4b8832 --- /dev/null +++ b/TestStack.ConventionTests/Reporting/ApproveResultsProcessor.cs @@ -0,0 +1,24 @@ +namespace TestStack.ConventionTests.Reporting +{ + using ApprovalTests; + using ApprovalTests.Core.Exceptions; + using TestStack.ConventionTests.Internal; + + public class ApproveResultsProcessor : IResultsProcessor + { + public void Process(params ConventionResult[] results) + { + try + { + var conventionReportTextRenderer = new ConventionReportTextRenderer(); + conventionReportTextRenderer.Process(results); + Approvals.Verify(conventionReportTextRenderer.Output); + } + catch (ApprovalException ex) + { + throw new ConventionFailedException("Approved exceptions for convention differs\r\n\r\n" + ex.Message, + ex); + } + } + } +} \ No newline at end of file diff --git a/TestStack.ConventionTests/Reporting/ConventionReportTextRenderer.cs b/TestStack.ConventionTests/Reporting/ConventionReportTextRenderer.cs index 538c3f0..6519082 100644 --- a/TestStack.ConventionTests/Reporting/ConventionReportTextRenderer.cs +++ b/TestStack.ConventionTests/Reporting/ConventionReportTextRenderer.cs @@ -5,11 +5,11 @@ public class ConventionReportTextRenderer : IResultsProcessor { - public void Process(params ConventionResult[] conventionResult) + public void Process(params ConventionResult[] results) { var stringBuilder = new StringBuilder(); - foreach (var conventionReport in conventionResult) + foreach (var conventionReport in results) { var title = string.Format("{0}: '{1}' for '{2}'", conventionReport.Result, conventionReport.ConventionTitle, conventionReport.DataDescription); diff --git a/TestStack.ConventionTests/Reporting/ConventionReportTraceRenderer.cs b/TestStack.ConventionTests/Reporting/ConventionReportTraceRenderer.cs index 91817fc..f17fbf0 100644 --- a/TestStack.ConventionTests/Reporting/ConventionReportTraceRenderer.cs +++ b/TestStack.ConventionTests/Reporting/ConventionReportTraceRenderer.cs @@ -5,10 +5,10 @@ public class ConventionReportTraceRenderer : IResultsProcessor { - public void Process(params ConventionResult[] conventionResult) + public void Process(params ConventionResult[] results) { var conventionReportTextRenderer = new ConventionReportTextRenderer(); - conventionReportTextRenderer.Process(conventionResult); + conventionReportTextRenderer.Process(results); Trace.WriteLine(conventionReportTextRenderer.Output); } } diff --git a/TestStack.ConventionTests/Reporting/HtmlReportRenderer.cs b/TestStack.ConventionTests/Reporting/HtmlReportRenderer.cs index 9d6bdee..f1e1f6b 100644 --- a/TestStack.ConventionTests/Reporting/HtmlReportRenderer.cs +++ b/TestStack.ConventionTests/Reporting/HtmlReportRenderer.cs @@ -14,7 +14,7 @@ public HtmlReportRenderer(string assemblyDirectory) file = Path.Combine(assemblyDirectory, "Conventions.htm"); } - public void Process(params ConventionResult[] conventionResult) + public void Process(params ConventionResult[] results) { var sb = new StringBuilder(); var html = new HtmlTextWriter(new StringWriter(sb)); @@ -29,7 +29,7 @@ public void Process(params ConventionResult[] conventionResult) html.Write("Project Conventions"); html.RenderEndTag(); - foreach (var conventionReport in conventionResult) + foreach (var conventionReport in results) { html.RenderBeginTag(HtmlTextWriterTag.P); html.RenderBeginTag(HtmlTextWriterTag.Div); diff --git a/TestStack.ConventionTests/Reporting/IResultsProcessor.cs b/TestStack.ConventionTests/Reporting/IResultsProcessor.cs index 2d6f4db..22feedc 100644 --- a/TestStack.ConventionTests/Reporting/IResultsProcessor.cs +++ b/TestStack.ConventionTests/Reporting/IResultsProcessor.cs @@ -4,6 +4,6 @@ public interface IResultsProcessor { - void Process(params ConventionResult[] conventionResult); + void Process(params ConventionResult[] results); } } \ No newline at end of file diff --git a/TestStack.ConventionTests/Reporting/ConventionResultExceptionReporter.cs b/TestStack.ConventionTests/Reporting/ThrowOnFailureResultsProcessor.cs similarity index 54% rename from TestStack.ConventionTests/Reporting/ConventionResultExceptionReporter.cs rename to TestStack.ConventionTests/Reporting/ThrowOnFailureResultsProcessor.cs index 2f4324a..9847811 100644 --- a/TestStack.ConventionTests/Reporting/ConventionResultExceptionReporter.cs +++ b/TestStack.ConventionTests/Reporting/ThrowOnFailureResultsProcessor.cs @@ -3,13 +3,13 @@ using System.Linq; using TestStack.ConventionTests.Internal; - public class ConventionResultExceptionReporter : IResultsProcessor + public class ThrowOnFailureResultsProcessor : IResultsProcessor { - public void Process(params ConventionResult[] conventionResult) + public void Process(params ConventionResult[] results) { var conventionReportTextRenderer = new ConventionReportTextRenderer(); - conventionReportTextRenderer.Process(conventionResult); - if (conventionResult.Any(r => r.Result == TestResult.Failed)) + conventionReportTextRenderer.Process(results); + if (results.Any(r => r.Result == TestResult.Failed)) { throw new ConventionFailedException(conventionReportTextRenderer.Output); } diff --git a/TestStack.ConventionTests/TestStack.ConventionTests.csproj b/TestStack.ConventionTests/TestStack.ConventionTests.csproj index 9b29d1b..73a3e18 100644 --- a/TestStack.ConventionTests/TestStack.ConventionTests.csproj +++ b/TestStack.ConventionTests/TestStack.ConventionTests.csproj @@ -64,12 +64,13 @@ + - + From ce23eb2b78f026539cd7a0569a6384cdd2e86cb0 Mon Sep 17 00:00:00 2001 From: Krzysztof Kozmic Date: Sun, 18 Aug 2013 11:48:45 +1000 Subject: [PATCH 04/11] moved knowledge of approved exceptions out of ConventionContext --- TestStack.ConventionTests/Convention.cs | 4 ++-- .../Internal/ConventionContext.cs | 21 +------------------ 2 files changed, 3 insertions(+), 22 deletions(-) diff --git a/TestStack.ConventionTests/Convention.cs b/TestStack.ConventionTests/Convention.cs index d3a5bc7..cc2f744 100644 --- a/TestStack.ConventionTests/Convention.cs +++ b/TestStack.ConventionTests/Convention.cs @@ -41,7 +41,7 @@ public static void Is(IConvention convention, TDataSou try { var context = new ConventionContext(data.Description, Formatters); - var conventionResult = context.GetConventionResults(convention, data); + var conventionResult = context.Execute(convention, data); Reports.AddRange(conventionResult); new ConventionReportTraceRenderer().Process(conventionResult); @@ -57,7 +57,7 @@ public static void IsWithApprovedExeptions(IConvention where TDataSource : IConventionData { var context = new ConventionContext(data.Description, Formatters); - var conventionResult = context.GetConventionResultsWithApprovedExeptions(convention, data); + var conventionResult = context.Execute(convention, data); Reports.AddRange(conventionResult); try diff --git a/TestStack.ConventionTests/Internal/ConventionContext.cs b/TestStack.ConventionTests/Internal/ConventionContext.cs index 4245308..98cd527 100644 --- a/TestStack.ConventionTests/Internal/ConventionContext.cs +++ b/TestStack.ConventionTests/Internal/ConventionContext.cs @@ -77,7 +77,7 @@ ConventionReportFailure FormatData(T failingData) return formatter.Format(failingData); } - public ConventionResult[] GetConventionResults(IConvention convention, + public ConventionResult[] Execute(IConvention convention, TDataSource data) where TDataSource : IConventionData { @@ -87,24 +87,5 @@ public ConventionResult[] GetConventionResults(IConvention( - IConvention convention, TDataSource data) - where TDataSource : IConventionData - { - var conventionReportTextRenderer = new ConventionReportTextRenderer(); - // Add approved exceptions to report - if (!data.HasData) - throw new ConventionSourceInvalidException(String.Format("{0} has no data", data.Description)); - - convention.Execute(data, this); - foreach (var conventionResult in ConventionResults) - { - conventionReportTextRenderer.RenderItems(conventionResult); - conventionResult.WithApprovedException(conventionReportTextRenderer.Output); - } - - return ConventionResults; - } } } \ No newline at end of file From 216ac1e00d24dc3cfc92c3e7c908d8dbbe280ee7 Mon Sep 17 00:00:00 2001 From: Krzysztof Kozmic Date: Sun, 18 Aug 2013 11:59:07 +1000 Subject: [PATCH 05/11] using ApproveResultsProcessor --- TestStack.ConventionTests/Convention.cs | 28 +++---------------- .../Reporting/ApproveResultsProcessor.cs | 6 +++- 2 files changed, 9 insertions(+), 25 deletions(-) diff --git a/TestStack.ConventionTests/Convention.cs b/TestStack.ConventionTests/Convention.cs index cc2f744..41ee045 100644 --- a/TestStack.ConventionTests/Convention.cs +++ b/TestStack.ConventionTests/Convention.cs @@ -4,8 +4,6 @@ using System.Collections.Generic; using System.IO; using System.Reflection; - using ApprovalTests; - using ApprovalTests.Core.Exceptions; using TestStack.ConventionTests.Internal; using TestStack.ConventionTests.Reporting; @@ -35,7 +33,8 @@ public static void Is(IConvention convention, TDataSou Is(convention, data, new ThrowOnFailureResultsProcessor()); } - public static void Is(IConvention convention, TDataSource data, IResultsProcessor reporter) + public static void Is(IConvention convention, TDataSource data, + IResultsProcessor processor) where TDataSource : IConventionData { try @@ -45,7 +44,7 @@ public static void Is(IConvention convention, TDataSou Reports.AddRange(conventionResult); new ConventionReportTraceRenderer().Process(conventionResult); - reporter.Process(conventionResult); + processor.Process(conventionResult); } finally { @@ -56,26 +55,7 @@ public static void Is(IConvention convention, TDataSou public static void IsWithApprovedExeptions(IConvention convention, TDataSource data) where TDataSource : IConventionData { - var context = new ConventionContext(data.Description, Formatters); - var conventionResult = context.Execute(convention, data); - Reports.AddRange(conventionResult); - - try - { - var conventionReportTextRenderer = new ConventionReportTextRenderer(); - conventionReportTextRenderer.Process(conventionResult); - Approvals.Verify(conventionReportTextRenderer.Output); - - new ConventionReportTraceRenderer().Process(conventionResult); - } - catch (ApprovalException ex) - { - throw new ConventionFailedException("Approved exceptions for convention differs\r\n\r\n"+ex.Message, ex); - } - finally - { - HtmlRenderer.Process(Reports.ToArray()); - } + Is(convention, data, new ApproveResultsProcessor()); } // http://stackoverflow.com/questions/52797/c-how-do-i-get-the-path-of-the-assembly-the-code-is-in#answer-283917 diff --git a/TestStack.ConventionTests/Reporting/ApproveResultsProcessor.cs b/TestStack.ConventionTests/Reporting/ApproveResultsProcessor.cs index c4b8832..da2e885 100644 --- a/TestStack.ConventionTests/Reporting/ApproveResultsProcessor.cs +++ b/TestStack.ConventionTests/Reporting/ApproveResultsProcessor.cs @@ -1,5 +1,6 @@ namespace TestStack.ConventionTests.Reporting { + using System; using ApprovalTests; using ApprovalTests.Core.Exceptions; using TestStack.ConventionTests.Internal; @@ -16,7 +17,10 @@ public void Process(params ConventionResult[] results) } catch (ApprovalException ex) { - throw new ConventionFailedException("Approved exceptions for convention differs\r\n\r\n" + ex.Message, + throw new ConventionFailedException("Approved exceptions for convention differs" + + Environment.NewLine + + Environment.NewLine + + ex.Message, ex); } } From cb3d9a5faa61f1cbd6335951ad678b0b99b0dd34 Mon Sep 17 00:00:00 2001 From: Krzysztof Kozmic Date: Sun, 18 Aug 2013 12:06:06 +1000 Subject: [PATCH 06/11] folded HtmlReportRenderer to be treated just like any other processor --- TestStack.ConventionTests/Convention.cs | 34 ++++++++++--------- .../Reporting/HtmlReportRenderer.cs | 13 ++++--- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/TestStack.ConventionTests/Convention.cs b/TestStack.ConventionTests/Convention.cs index 41ee045..8d61d2a 100644 --- a/TestStack.ConventionTests/Convention.cs +++ b/TestStack.ConventionTests/Convention.cs @@ -10,7 +10,6 @@ public static class Convention { static readonly HtmlReportRenderer HtmlRenderer = new HtmlReportRenderer(AssemblyDirectory); - static readonly List Reports = new List(); static Convention() { @@ -24,38 +23,41 @@ static Convention() }; } - public static IEnumerable ConventionReports { get { return Reports; } } - public static IList Formatters { get; set; } + public static IList Formatters { get; set; } public static void Is(IConvention convention, TDataSource data) where TDataSource : IConventionData { - Is(convention, data, new ThrowOnFailureResultsProcessor()); + Is(convention, data, new IResultsProcessor[] + { + HtmlRenderer, + new ConventionReportTraceRenderer(), + new ThrowOnFailureResultsProcessor() + }); } public static void Is(IConvention convention, TDataSource data, - IResultsProcessor processor) + IResultsProcessor[] processors) where TDataSource : IConventionData { - try - { - var context = new ConventionContext(data.Description, Formatters); - var conventionResult = context.Execute(convention, data); - Reports.AddRange(conventionResult); + var context = new ConventionContext(data.Description, Formatters); + var conventionResult = context.Execute(convention, data); - new ConventionReportTraceRenderer().Process(conventionResult); - processor.Process(conventionResult); - } - finally + foreach (var processor in processors) { - HtmlRenderer.Process(Reports.ToArray()); + processor.Process(conventionResult); } } public static void IsWithApprovedExeptions(IConvention convention, TDataSource data) where TDataSource : IConventionData { - Is(convention, data, new ApproveResultsProcessor()); + Is(convention, data, new IResultsProcessor[] + { + HtmlRenderer, + new ConventionReportTraceRenderer(), + new ApproveResultsProcessor() + }); } // http://stackoverflow.com/questions/52797/c-how-do-i-get-the-path-of-the-assembly-the-code-is-in#answer-283917 diff --git a/TestStack.ConventionTests/Reporting/HtmlReportRenderer.cs b/TestStack.ConventionTests/Reporting/HtmlReportRenderer.cs index f1e1f6b..4ee1730 100644 --- a/TestStack.ConventionTests/Reporting/HtmlReportRenderer.cs +++ b/TestStack.ConventionTests/Reporting/HtmlReportRenderer.cs @@ -1,5 +1,7 @@ namespace TestStack.ConventionTests.Reporting { + using System; + using System.Collections.Generic; using System.IO; using System.Text; using System.Web.UI; @@ -8,6 +10,8 @@ public class HtmlReportRenderer : IResultsProcessor { readonly string file; + static readonly List Reports = new List(); + public static IEnumerable ConventionReports { get { return Reports; } } public HtmlReportRenderer(string assemblyDirectory) { @@ -16,6 +20,7 @@ public HtmlReportRenderer(string assemblyDirectory) public void Process(params ConventionResult[] results) { + Reports.AddRange(results); var sb = new StringBuilder(); var html = new HtmlTextWriter(new StringWriter(sb)); html.WriteLine(""); @@ -29,16 +34,16 @@ public void Process(params ConventionResult[] results) html.Write("Project Conventions"); html.RenderEndTag(); - foreach (var conventionReport in results) + foreach (var conventionReport in Reports) { html.RenderBeginTag(HtmlTextWriterTag.P); html.RenderBeginTag(HtmlTextWriterTag.Div); html.RenderBeginTag(HtmlTextWriterTag.Strong); html.Write(conventionReport.Result+": "); html.RenderEndTag(); - var title = string.Format("{0} for {1}", conventionReport.ConventionTitle, conventionReport.DataDescription); + var title = String.Format("{0} for {1}", conventionReport.ConventionTitle, conventionReport.DataDescription); html.Write(title); - if (!string.IsNullOrEmpty(conventionReport.ApprovedException)) + if (!String.IsNullOrEmpty(conventionReport.ApprovedException)) { html.RenderBeginTag(HtmlTextWriterTag.Div); html.RenderBeginTag(HtmlTextWriterTag.Strong); @@ -49,7 +54,7 @@ public void Process(params ConventionResult[] results) html.RenderBeginTag(HtmlTextWriterTag.Ul); - if (!string.IsNullOrEmpty(conventionReport.ApprovedException)) + if (!String.IsNullOrEmpty(conventionReport.ApprovedException)) { html.RenderBeginTag(HtmlTextWriterTag.Li); html.WriteLine(conventionReport.ApprovedException); From 767bcfde115a68fc61c8d2a1c9a94f4dd4196d69 Mon Sep 17 00:00:00 2001 From: Krzysztof Kozmic Date: Sun, 18 Aug 2013 12:15:06 +1000 Subject: [PATCH 07/11] evaluating success/failure in ConvenionResult --- .../Internal/ConventionContext.cs | 12 +--- .../Internal/ConventionResult.cs | 58 ++++++++++--------- 2 files changed, 34 insertions(+), 36 deletions(-) diff --git a/TestStack.ConventionTests/Internal/ConventionContext.cs b/TestStack.ConventionTests/Internal/ConventionContext.cs index 98cd527..247642c 100644 --- a/TestStack.ConventionTests/Internal/ConventionContext.cs +++ b/TestStack.ConventionTests/Internal/ConventionContext.cs @@ -26,9 +26,7 @@ public ConventionResult[] ConventionResults void IConventionResultContext.Is(string resultTitle, IEnumerable failingData) { // ReSharper disable PossibleMultipleEnumeration - results.Add(new ConventionResult( - failingData.None() ? TestResult.Passed : TestResult.Failed, - resultTitle, + results.Add(new ConventionResult(resultTitle, dataDescription, failingData.Select(FormatData).ToArray())); } @@ -37,14 +35,10 @@ void IConventionResultContext.IsSymmetric( string firstSetFailureTitle, IEnumerable firstSetFailureData, string secondSetFailureTitle, IEnumerable secondSetFailureData) { - results.Add(new ConventionResult( - firstSetFailureData.None() ? TestResult.Passed : TestResult.Failed, - firstSetFailureTitle, + results.Add(new ConventionResult(firstSetFailureTitle, dataDescription, firstSetFailureData.Select(FormatData).ToArray())); - results.Add(new ConventionResult( - secondSetFailureData.None() ? TestResult.Passed : TestResult.Failed, - secondSetFailureTitle, + results.Add(new ConventionResult(secondSetFailureTitle, dataDescription, secondSetFailureData.Select(FormatData).ToArray())); } diff --git a/TestStack.ConventionTests/Internal/ConventionResult.cs b/TestStack.ConventionTests/Internal/ConventionResult.cs index a9fc654..ec896bb 100644 --- a/TestStack.ConventionTests/Internal/ConventionResult.cs +++ b/TestStack.ConventionTests/Internal/ConventionResult.cs @@ -1,28 +1,32 @@ -namespace TestStack.ConventionTests.Internal -{ - using TestStack.ConventionTests.Reporting; - - public class ConventionResult - { - public TestResult Result { get; private set; } - public string ConventionTitle { get; private set; } - public string DataDescription { get; private set; } - public ConventionReportFailure[] ConventionFailures { get; private set; } - public string ApprovedException { get; private set; } - - public ConventionResult(TestResult result, string conventionTitle, string dataDescription, ConventionReportFailure[] conventionFailures) - { - Result = result; - ConventionTitle = conventionTitle; - DataDescription = dataDescription; - ConventionFailures = conventionFailures; - } - - public void WithApprovedException(string output) - { - ApprovedException = output; - Result = TestResult.Passed; - ConventionFailures = new ConventionReportFailure[0]; - } - } +namespace TestStack.ConventionTests.Internal +{ + using System.Linq; + using TestStack.ConventionTests.Reporting; + + public class ConventionResult + { + public ConventionResult(string conventionTitle, string dataDescription, ConventionReportFailure[] conventionFailures) + { + ConventionTitle = conventionTitle; + DataDescription = dataDescription; + ConventionFailures = conventionFailures; + } + + public TestResult Result + { + get + { + if (ConventionFailures.Any()) + { + return TestResult.Failed; + } + return TestResult.Passed; + } + } + + public string ConventionTitle { get; private set; } + public string DataDescription { get; private set; } + public ConventionReportFailure[] ConventionFailures { get; private set; } + public string ApprovedException { get; private set; } + } } \ No newline at end of file From 945178e339abf94409705a54868b44b485957194 Mon Sep 17 00:00:00 2001 From: Krzysztof Kozmic Date: Sun, 18 Aug 2013 12:17:40 +1000 Subject: [PATCH 08/11] moved logic out of the static class and into ConventionContext --- TestStack.ConventionTests/Convention.cs | 9 ++------- .../Internal/ConventionContext.cs | 13 +++++++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/TestStack.ConventionTests/Convention.cs b/TestStack.ConventionTests/Convention.cs index 8d61d2a..9d908f4 100644 --- a/TestStack.ConventionTests/Convention.cs +++ b/TestStack.ConventionTests/Convention.cs @@ -40,13 +40,8 @@ public static void Is(IConvention convention, TDataSou IResultsProcessor[] processors) where TDataSource : IConventionData { - var context = new ConventionContext(data.Description, Formatters); - var conventionResult = context.Execute(convention, data); - - foreach (var processor in processors) - { - processor.Process(conventionResult); - } + var context = new ConventionContext(data.Description, Formatters, processors); + context.Execute(convention, data); } public static void IsWithApprovedExeptions(IConvention convention, TDataSource data) diff --git a/TestStack.ConventionTests/Internal/ConventionContext.cs b/TestStack.ConventionTests/Internal/ConventionContext.cs index 247642c..da56569 100644 --- a/TestStack.ConventionTests/Internal/ConventionContext.cs +++ b/TestStack.ConventionTests/Internal/ConventionContext.cs @@ -10,11 +10,14 @@ public class ConventionContext : IConventionResultContext { readonly string dataDescription; readonly IList formatters; + readonly IList processors; readonly IList results = new List(); - public ConventionContext(string dataDescription, IList formatters) + public ConventionContext(string dataDescription, IList formatters, + IList processors) { this.formatters = formatters; + this.processors = processors; this.dataDescription = dataDescription; } @@ -71,15 +74,17 @@ ConventionReportFailure FormatData(T failingData) return formatter.Format(failingData); } - public ConventionResult[] Execute(IConvention convention, - TDataSource data) + public void Execute(IConvention convention, TDataSource data) where TDataSource : IConventionData { if (!data.HasData) throw new ConventionSourceInvalidException(String.Format("{0} has no data", data.Description)); convention.Execute(data, this); - return ConventionResults; + foreach (var resultsProcessor in processors) + { + resultsProcessor.Process(ConventionResults); + } } } } \ No newline at end of file From 827c41c73bc197052f9139443204bd4436209908 Mon Sep 17 00:00:00 2001 From: Krzysztof Kozmic Date: Sun, 18 Aug 2013 12:54:56 +1000 Subject: [PATCH 09/11] 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... --- .../Internal/ConventionContext.cs | 36 +++++++++---------- .../Internal/ConventionResult.cs | 9 +++-- .../Internal/IConventionFormatContext.cs | 7 ++++ .../Internal/LinqExtensions.cs | 6 +++- .../Reporting/ApproveResultsProcessor.cs | 4 +-- .../Reporting/ConventionReportTextRenderer.cs | 24 +++---------- .../ConventionReportTraceRenderer.cs | 4 +-- .../Reporting/HtmlReportRenderer.cs | 20 ++--------- .../Reporting/IResultsProcessor.cs | 2 +- .../ThrowOnFailureResultsProcessor.cs | 4 +-- .../TestStack.ConventionTests.csproj | 1 + 11 files changed, 50 insertions(+), 67 deletions(-) create mode 100644 TestStack.ConventionTests/Internal/IConventionFormatContext.cs diff --git a/TestStack.ConventionTests/Internal/ConventionContext.cs b/TestStack.ConventionTests/Internal/ConventionContext.cs index da56569..1fc6502 100644 --- a/TestStack.ConventionTests/Internal/ConventionContext.cs +++ b/TestStack.ConventionTests/Internal/ConventionContext.cs @@ -6,7 +6,7 @@ using TestStack.ConventionTests.Conventions; using TestStack.ConventionTests.Reporting; - public class ConventionContext : IConventionResultContext + public class ConventionContext : IConventionResultContext, IConventionFormatContext { readonly string dataDescription; readonly IList formatters; @@ -26,12 +26,25 @@ public ConventionResult[] ConventionResults get { return results.ToArray(); } } + ConventionReportFailure IConventionFormatContext.FormatData(object failingData) + { + var formatter = formatters.FirstOrDefault(f => f.CanFormat(failingData)); + if (formatter == null) + { + throw new NoDataFormatterFoundException( + failingData.GetType().Name + + " has no formatter, add one with `Convention.Formatters.Add(new MyDataFormatter());`"); + } + + return formatter.Format(failingData); + } + void IConventionResultContext.Is(string resultTitle, IEnumerable failingData) { // ReSharper disable PossibleMultipleEnumeration results.Add(new ConventionResult(resultTitle, dataDescription, - failingData.Select(FormatData).ToArray())); + failingData.ToObjectArray())); } void IConventionResultContext.IsSymmetric( @@ -40,10 +53,10 @@ void IConventionResultContext.IsSymmetric( { results.Add(new ConventionResult(firstSetFailureTitle, dataDescription, - firstSetFailureData.Select(FormatData).ToArray())); + firstSetFailureData.ToObjectArray())); results.Add(new ConventionResult(secondSetFailureTitle, dataDescription, - secondSetFailureData.Select(FormatData).ToArray())); + secondSetFailureData.ToObjectArray())); } void IConventionResultContext.IsSymmetric( @@ -61,19 +74,6 @@ void IConventionResultContext.IsSymmetric( secondSetFailureTitle, secondSetFailingData); } - ConventionReportFailure FormatData(T failingData) - { - var formatter = formatters.FirstOrDefault(f => f.CanFormat(failingData)); - if (formatter == null) - { - throw new NoDataFormatterFoundException( - typeof (T).Name + - " has no formatter, add one with `Convention.Formatters.Add(new MyDataFormatter());`"); - } - - return formatter.Format(failingData); - } - public void Execute(IConvention convention, TDataSource data) where TDataSource : IConventionData { @@ -83,7 +83,7 @@ public void Execute(IConvention convention, TDataSourc foreach (var resultsProcessor in processors) { - resultsProcessor.Process(ConventionResults); + resultsProcessor.Process(this, ConventionResults); } } } diff --git a/TestStack.ConventionTests/Internal/ConventionResult.cs b/TestStack.ConventionTests/Internal/ConventionResult.cs index ec896bb..14c807e 100644 --- a/TestStack.ConventionTests/Internal/ConventionResult.cs +++ b/TestStack.ConventionTests/Internal/ConventionResult.cs @@ -5,18 +5,18 @@ public class ConventionResult { - public ConventionResult(string conventionTitle, string dataDescription, ConventionReportFailure[] conventionFailures) + public ConventionResult(string conventionTitle, string dataDescription, object[] data) { ConventionTitle = conventionTitle; DataDescription = dataDescription; - ConventionFailures = conventionFailures; + Data = data; } public TestResult Result { get { - if (ConventionFailures.Any()) + if (Data.Any()) { return TestResult.Failed; } @@ -26,7 +26,6 @@ public TestResult Result public string ConventionTitle { get; private set; } public string DataDescription { get; private set; } - public ConventionReportFailure[] ConventionFailures { get; private set; } - public string ApprovedException { get; private set; } + public object[] Data { get; private set; } } } \ No newline at end of file diff --git a/TestStack.ConventionTests/Internal/IConventionFormatContext.cs b/TestStack.ConventionTests/Internal/IConventionFormatContext.cs new file mode 100644 index 0000000..e3d024d --- /dev/null +++ b/TestStack.ConventionTests/Internal/IConventionFormatContext.cs @@ -0,0 +1,7 @@ +namespace TestStack.ConventionTests.Internal +{ + public interface IConventionFormatContext + { + ConventionReportFailure FormatData(object failingData); + } +} \ No newline at end of file diff --git a/TestStack.ConventionTests/Internal/LinqExtensions.cs b/TestStack.ConventionTests/Internal/LinqExtensions.cs index b2f7ee6..cb8ad12 100644 --- a/TestStack.ConventionTests/Internal/LinqExtensions.cs +++ b/TestStack.ConventionTests/Internal/LinqExtensions.cs @@ -16,10 +16,14 @@ public static bool None(this IEnumerable enumerable, Func predica return !enumerable.Any(predicate); } - public static IEnumerable Unless(this IEnumerable enumerable, Func predicate) { return enumerable.Where(i => predicate(i) == false); } + + public static object[] ToObjectArray(this IEnumerable enumerable) + { + return enumerable.Select(x => (object) x).ToArray(); + } } } \ No newline at end of file diff --git a/TestStack.ConventionTests/Reporting/ApproveResultsProcessor.cs b/TestStack.ConventionTests/Reporting/ApproveResultsProcessor.cs index da2e885..b020ed7 100644 --- a/TestStack.ConventionTests/Reporting/ApproveResultsProcessor.cs +++ b/TestStack.ConventionTests/Reporting/ApproveResultsProcessor.cs @@ -7,12 +7,12 @@ public class ApproveResultsProcessor : IResultsProcessor { - public void Process(params ConventionResult[] results) + public void Process(IConventionFormatContext context, params ConventionResult[] results) { try { var conventionReportTextRenderer = new ConventionReportTextRenderer(); - conventionReportTextRenderer.Process(results); + conventionReportTextRenderer.Process(context, results); Approvals.Verify(conventionReportTextRenderer.Output); } catch (ApprovalException ex) diff --git a/TestStack.ConventionTests/Reporting/ConventionReportTextRenderer.cs b/TestStack.ConventionTests/Reporting/ConventionReportTextRenderer.cs index 6519082..725e601 100644 --- a/TestStack.ConventionTests/Reporting/ConventionReportTextRenderer.cs +++ b/TestStack.ConventionTests/Reporting/ConventionReportTextRenderer.cs @@ -5,7 +5,7 @@ public class ConventionReportTextRenderer : IResultsProcessor { - public void Process(params ConventionResult[] results) + public void Process(IConventionFormatContext context, params ConventionResult[] results) { var stringBuilder = new StringBuilder(); @@ -17,14 +17,7 @@ public void Process(params ConventionResult[] results) stringBuilder.AppendLine(string.Empty.PadRight(title.Length, '-')); stringBuilder.AppendLine(); - if (!string.IsNullOrEmpty(conventionReport.ApprovedException)) - { - stringBuilder.AppendLine("With approved exceptions:"); - stringBuilder.AppendLine(conventionReport.ApprovedException); - stringBuilder.AppendLine(); - } - - RenderItems(conventionReport, stringBuilder); + RenderItems(conventionReport, stringBuilder, context); stringBuilder.AppendLine(); stringBuilder.AppendLine(); } @@ -34,19 +27,12 @@ public void Process(params ConventionResult[] results) public string Output { get; private set; } - public void RenderItems(ConventionResult conventionResult) - { - var stringBuilder = new StringBuilder(); - RenderItems(conventionResult, stringBuilder); - Output = stringBuilder.ToString(); - } - - static void RenderItems(ConventionResult resultInfo, StringBuilder stringBuilder) + static void RenderItems(ConventionResult resultInfo, StringBuilder stringBuilder, IConventionFormatContext context) { - foreach (var conventionFailure in resultInfo.ConventionFailures) + foreach (var conventionFailure in resultInfo.Data) { stringBuilder.Append("\t"); - stringBuilder.AppendLine(conventionFailure.ToString()); + stringBuilder.AppendLine(context.FormatData(conventionFailure).ToString()); } } } diff --git a/TestStack.ConventionTests/Reporting/ConventionReportTraceRenderer.cs b/TestStack.ConventionTests/Reporting/ConventionReportTraceRenderer.cs index f17fbf0..09d8446 100644 --- a/TestStack.ConventionTests/Reporting/ConventionReportTraceRenderer.cs +++ b/TestStack.ConventionTests/Reporting/ConventionReportTraceRenderer.cs @@ -5,10 +5,10 @@ public class ConventionReportTraceRenderer : IResultsProcessor { - public void Process(params ConventionResult[] results) + public void Process(IConventionFormatContext context, params ConventionResult[] results) { var conventionReportTextRenderer = new ConventionReportTextRenderer(); - conventionReportTextRenderer.Process(results); + conventionReportTextRenderer.Process(context, results); Trace.WriteLine(conventionReportTextRenderer.Output); } } diff --git a/TestStack.ConventionTests/Reporting/HtmlReportRenderer.cs b/TestStack.ConventionTests/Reporting/HtmlReportRenderer.cs index 4ee1730..9f4553c 100644 --- a/TestStack.ConventionTests/Reporting/HtmlReportRenderer.cs +++ b/TestStack.ConventionTests/Reporting/HtmlReportRenderer.cs @@ -18,7 +18,7 @@ public HtmlReportRenderer(string assemblyDirectory) file = Path.Combine(assemblyDirectory, "Conventions.htm"); } - public void Process(params ConventionResult[] results) + public void Process(IConventionFormatContext context, params ConventionResult[] results) { Reports.AddRange(results); var sb = new StringBuilder(); @@ -43,28 +43,14 @@ public void Process(params ConventionResult[] results) html.RenderEndTag(); var title = String.Format("{0} for {1}", conventionReport.ConventionTitle, conventionReport.DataDescription); html.Write(title); - if (!String.IsNullOrEmpty(conventionReport.ApprovedException)) - { - html.RenderBeginTag(HtmlTextWriterTag.Div); - html.RenderBeginTag(HtmlTextWriterTag.Strong); - html.WriteLine("With approved exceptions:"); - html.RenderEndTag(); - html.RenderEndTag(); - } html.RenderBeginTag(HtmlTextWriterTag.Ul); - if (!String.IsNullOrEmpty(conventionReport.ApprovedException)) - { - html.RenderBeginTag(HtmlTextWriterTag.Li); - html.WriteLine(conventionReport.ApprovedException); - html.RenderEndTag(); - } - foreach (var conventionFailure in conventionReport.ConventionFailures) + foreach (var conventionFailure in conventionReport.Data) { html.RenderBeginTag(HtmlTextWriterTag.Li); - html.Write(conventionFailure.ToString()); + html.Write(context.FormatData(conventionFailure).ToString()); html.RenderEndTag(); } diff --git a/TestStack.ConventionTests/Reporting/IResultsProcessor.cs b/TestStack.ConventionTests/Reporting/IResultsProcessor.cs index 22feedc..ab73623 100644 --- a/TestStack.ConventionTests/Reporting/IResultsProcessor.cs +++ b/TestStack.ConventionTests/Reporting/IResultsProcessor.cs @@ -4,6 +4,6 @@ public interface IResultsProcessor { - void Process(params ConventionResult[] results); + void Process(IConventionFormatContext context, params ConventionResult[] results); } } \ No newline at end of file diff --git a/TestStack.ConventionTests/Reporting/ThrowOnFailureResultsProcessor.cs b/TestStack.ConventionTests/Reporting/ThrowOnFailureResultsProcessor.cs index 9847811..fc2ea2f 100644 --- a/TestStack.ConventionTests/Reporting/ThrowOnFailureResultsProcessor.cs +++ b/TestStack.ConventionTests/Reporting/ThrowOnFailureResultsProcessor.cs @@ -5,10 +5,10 @@ public class ThrowOnFailureResultsProcessor : IResultsProcessor { - public void Process(params ConventionResult[] results) + public void Process(IConventionFormatContext context, params ConventionResult[] results) { var conventionReportTextRenderer = new ConventionReportTextRenderer(); - conventionReportTextRenderer.Process(results); + conventionReportTextRenderer.Process(context, results); if (results.Any(r => r.Result == TestResult.Failed)) { throw new ConventionFailedException(conventionReportTextRenderer.Output); diff --git a/TestStack.ConventionTests/TestStack.ConventionTests.csproj b/TestStack.ConventionTests/TestStack.ConventionTests.csproj index 73a3e18..3cc1b3a 100644 --- a/TestStack.ConventionTests/TestStack.ConventionTests.csproj +++ b/TestStack.ConventionTests/TestStack.ConventionTests.csproj @@ -63,6 +63,7 @@ + From 1347d877e03a2b6426e4efbcd4cf60eaf67b4e80 Mon Sep 17 00:00:00 2001 From: Krzysztof Kozmic Date: Sun, 18 Aug 2013 13:00:37 +1000 Subject: [PATCH 10/11] it's not up to the formatter to decide pass/fail --- ...onClassTests.approval_mismatch.approved.txt | 5 ++--- ...assemblies_referencing_bin_obj.approved.txt | 4 ++-- ...n_obj_with_approved_exceptions.approved.txt | 5 ++--- ...scripts_not_embedded_resources.approved.txt | 4 ++-- ...urces_with_approved_exceptions.approved.txt | 5 ++--- ...asses_have_default_constructor.approved.txt | 4 ++-- ...ructor_wth_approved_exceptions.approved.txt | 5 ++--- ...ntions.all_methods_are_virtual.approved.txt | 4 ++-- ...irtual_wth_approved_exceptions.approved.txt | 5 ++--- ...s.dtos_exists_in_dto_namespace.approved.txt | 8 ++++---- ...espace_wth_approved_exceptions.approved.txt | 12 ++++-------- .../Internal/ConventionResult.cs | 18 +++++------------- .../Reporting/ConventionReportTextRenderer.cs | 2 +- .../Reporting/HtmlReportRenderer.cs | 1 - .../ThrowOnFailureResultsProcessor.cs | 2 +- 15 files changed, 33 insertions(+), 51 deletions(-) diff --git a/TestStack.ConventionTests.Tests/ConventionAssertionClassTests.approval_mismatch.approved.txt b/TestStack.ConventionTests.Tests/ConventionAssertionClassTests.approval_mismatch.approved.txt index 11906c4..d965a0a 100644 --- a/TestStack.ConventionTests.Tests/ConventionAssertionClassTests.approval_mismatch.approved.txt +++ b/TestStack.ConventionTests.Tests/ConventionAssertionClassTests.approval_mismatch.approved.txt @@ -1,5 +1,4 @@ -Passed: 'Header' for 'Fake data' --------------------------------- +'Header' for 'Fake data' +------------------------ -With approved exceptions: Approved Exception \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/ProjectBasedConventions.assemblies_referencing_bin_obj.approved.txt b/TestStack.ConventionTests.Tests/ProjectBasedConventions.assemblies_referencing_bin_obj.approved.txt index c1f4909..237bbdb 100644 --- a/TestStack.ConventionTests.Tests/ProjectBasedConventions.assemblies_referencing_bin_obj.approved.txt +++ b/TestStack.ConventionTests.Tests/ProjectBasedConventions.assemblies_referencing_bin_obj.approved.txt @@ -1,4 +1,4 @@ -Failed: 'Project must not reference dlls from bin or obj directories' for 'TestStack.ConventionTests.Tests' ------------------------------------------------------------------------------------------------------------ +'Project must not reference dlls from bin or obj directories' for 'TestStack.ConventionTests.Tests' +--------------------------------------------------------------------------------------------------- bin\Debug\ApprovalTests.dll \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/ProjectBasedConventions.assemblies_referencing_bin_obj_with_approved_exceptions.approved.txt b/TestStack.ConventionTests.Tests/ProjectBasedConventions.assemblies_referencing_bin_obj_with_approved_exceptions.approved.txt index 1b176e9..237bbdb 100644 --- a/TestStack.ConventionTests.Tests/ProjectBasedConventions.assemblies_referencing_bin_obj_with_approved_exceptions.approved.txt +++ b/TestStack.ConventionTests.Tests/ProjectBasedConventions.assemblies_referencing_bin_obj_with_approved_exceptions.approved.txt @@ -1,5 +1,4 @@ -Passed: 'Project must not reference dlls from bin or obj directories' for 'TestStack.ConventionTests.Tests' ------------------------------------------------------------------------------------------------------------ +'Project must not reference dlls from bin or obj directories' for 'TestStack.ConventionTests.Tests' +--------------------------------------------------------------------------------------------------- -With approved exceptions: bin\Debug\ApprovalTests.dll \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/ProjectBasedConventions.scripts_not_embedded_resources.approved.txt b/TestStack.ConventionTests.Tests/ProjectBasedConventions.scripts_not_embedded_resources.approved.txt index 4ce889e..b6e30f9 100644 --- a/TestStack.ConventionTests.Tests/ProjectBasedConventions.scripts_not_embedded_resources.approved.txt +++ b/TestStack.ConventionTests.Tests/ProjectBasedConventions.scripts_not_embedded_resources.approved.txt @@ -1,4 +1,4 @@ -Failed: '.sql Files must be embedded resources' for 'TestStack.ConventionTests.Tests' -------------------------------------------------------------------------------------- +'.sql Files must be embedded resources' for 'TestStack.ConventionTests.Tests' +----------------------------------------------------------------------------- Scripts\Script2.sql \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/ProjectBasedConventions.scripts_not_embedded_resources_with_approved_exceptions.approved.txt b/TestStack.ConventionTests.Tests/ProjectBasedConventions.scripts_not_embedded_resources_with_approved_exceptions.approved.txt index 0217788..b6e30f9 100644 --- a/TestStack.ConventionTests.Tests/ProjectBasedConventions.scripts_not_embedded_resources_with_approved_exceptions.approved.txt +++ b/TestStack.ConventionTests.Tests/ProjectBasedConventions.scripts_not_embedded_resources_with_approved_exceptions.approved.txt @@ -1,5 +1,4 @@ -Passed: '.sql Files must be embedded resources' for 'TestStack.ConventionTests.Tests' -------------------------------------------------------------------------------------- +'.sql Files must be embedded resources' for 'TestStack.ConventionTests.Tests' +----------------------------------------------------------------------------- -With approved exceptions: Scripts\Script2.sql \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/TypeBasedConventions.all_classes_have_default_constructor.approved.txt b/TestStack.ConventionTests.Tests/TypeBasedConventions.all_classes_have_default_constructor.approved.txt index 173a5a4..b19146c 100644 --- a/TestStack.ConventionTests.Tests/TypeBasedConventions.all_classes_have_default_constructor.approved.txt +++ b/TestStack.ConventionTests.Tests/TypeBasedConventions.all_classes_have_default_constructor.approved.txt @@ -1,5 +1,5 @@ -Failed: 'Types must have a default constructor' for 'nHibernate Entitites' --------------------------------------------------------------------------- +'Types must have a default constructor' for 'nHibernate Entitites' +------------------------------------------------------------------ TestAssembly.ClassWithNoDefaultCtor TestAssembly.ClassWithPrivateDefaultCtor \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/TypeBasedConventions.all_classes_have_default_constructor_wth_approved_exceptions.approved.txt b/TestStack.ConventionTests.Tests/TypeBasedConventions.all_classes_have_default_constructor_wth_approved_exceptions.approved.txt index 3cad8d9..b19146c 100644 --- a/TestStack.ConventionTests.Tests/TypeBasedConventions.all_classes_have_default_constructor_wth_approved_exceptions.approved.txt +++ b/TestStack.ConventionTests.Tests/TypeBasedConventions.all_classes_have_default_constructor_wth_approved_exceptions.approved.txt @@ -1,6 +1,5 @@ -Passed: 'Types must have a default constructor' for 'nHibernate Entitites' --------------------------------------------------------------------------- +'Types must have a default constructor' for 'nHibernate Entitites' +------------------------------------------------------------------ -With approved exceptions: TestAssembly.ClassWithNoDefaultCtor TestAssembly.ClassWithPrivateDefaultCtor \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/TypeBasedConventions.all_methods_are_virtual.approved.txt b/TestStack.ConventionTests.Tests/TypeBasedConventions.all_methods_are_virtual.approved.txt index ec20755..7259361 100644 --- a/TestStack.ConventionTests.Tests/TypeBasedConventions.all_methods_are_virtual.approved.txt +++ b/TestStack.ConventionTests.Tests/TypeBasedConventions.all_methods_are_virtual.approved.txt @@ -1,4 +1,4 @@ -Failed: 'Methods must be virtual' for 'nHibernate Entitites' ------------------------------------------------------------- +'Methods must be virtual' for 'nHibernate Entitites' +---------------------------------------------------- TestAssembly.SampleDomainClass.TestNonVirtual \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/TypeBasedConventions.all_methods_are_virtual_wth_approved_exceptions.approved.txt b/TestStack.ConventionTests.Tests/TypeBasedConventions.all_methods_are_virtual_wth_approved_exceptions.approved.txt index c1902ad..7259361 100644 --- a/TestStack.ConventionTests.Tests/TypeBasedConventions.all_methods_are_virtual_wth_approved_exceptions.approved.txt +++ b/TestStack.ConventionTests.Tests/TypeBasedConventions.all_methods_are_virtual_wth_approved_exceptions.approved.txt @@ -1,5 +1,4 @@ -Passed: 'Methods must be virtual' for 'nHibernate Entitites' ------------------------------------------------------------- +'Methods must be virtual' for 'nHibernate Entitites' +---------------------------------------------------- -With approved exceptions: TestAssembly.SampleDomainClass.TestNonVirtual \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace.approved.txt b/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace.approved.txt index b864151..b222199 100644 --- a/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace.approved.txt +++ b/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace.approved.txt @@ -1,10 +1,10 @@ -Failed: 'Dtos must be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly types' ---------------------------------------------------------------------------------------- +'Dtos must be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly types' +------------------------------------------------------------------------------- TestAssembly.SomeDto -Failed: 'Non-Dtos must not be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly types' ------------------------------------------------------------------------------------------------ +'Non-Dtos must not be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly types' +--------------------------------------------------------------------------------------- TestAssembly.Dtos.AnotherClass \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace_wth_approved_exceptions.approved.txt b/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace_wth_approved_exceptions.approved.txt index 6d0ccc9..b222199 100644 --- a/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace_wth_approved_exceptions.approved.txt +++ b/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace_wth_approved_exceptions.approved.txt @@ -1,14 +1,10 @@ -Passed: 'Dtos must be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly types' ---------------------------------------------------------------------------------------- +'Dtos must be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly types' +------------------------------------------------------------------------------- -With approved exceptions: TestAssembly.SomeDto +'Non-Dtos must not be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly types' +--------------------------------------------------------------------------------------- - -Passed: 'Non-Dtos must not be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly types' ------------------------------------------------------------------------------------------------ - -With approved exceptions: TestAssembly.Dtos.AnotherClass \ No newline at end of file diff --git a/TestStack.ConventionTests/Internal/ConventionResult.cs b/TestStack.ConventionTests/Internal/ConventionResult.cs index 14c807e..8150cb7 100644 --- a/TestStack.ConventionTests/Internal/ConventionResult.cs +++ b/TestStack.ConventionTests/Internal/ConventionResult.cs @@ -1,7 +1,6 @@ namespace TestStack.ConventionTests.Internal { using System.Linq; - using TestStack.ConventionTests.Reporting; public class ConventionResult { @@ -12,20 +11,13 @@ public ConventionResult(string conventionTitle, string dataDescription, object[] Data = data; } - public TestResult Result - { - get - { - if (Data.Any()) - { - return TestResult.Failed; - } - return TestResult.Passed; - } - } - public string ConventionTitle { get; private set; } public string DataDescription { get; private set; } public object[] Data { get; private set; } + + public bool HasData + { + get { return Data.Any(); } + } } } \ No newline at end of file diff --git a/TestStack.ConventionTests/Reporting/ConventionReportTextRenderer.cs b/TestStack.ConventionTests/Reporting/ConventionReportTextRenderer.cs index 725e601..e507314 100644 --- a/TestStack.ConventionTests/Reporting/ConventionReportTextRenderer.cs +++ b/TestStack.ConventionTests/Reporting/ConventionReportTextRenderer.cs @@ -11,7 +11,7 @@ public void Process(IConventionFormatContext context, params ConventionResult[] foreach (var conventionReport in results) { - var title = string.Format("{0}: '{1}' for '{2}'", conventionReport.Result, conventionReport.ConventionTitle, + var title = string.Format("'{0}' for '{1}'", conventionReport.ConventionTitle, conventionReport.DataDescription); stringBuilder.AppendLine(title); stringBuilder.AppendLine(string.Empty.PadRight(title.Length, '-')); diff --git a/TestStack.ConventionTests/Reporting/HtmlReportRenderer.cs b/TestStack.ConventionTests/Reporting/HtmlReportRenderer.cs index 9f4553c..23dbaca 100644 --- a/TestStack.ConventionTests/Reporting/HtmlReportRenderer.cs +++ b/TestStack.ConventionTests/Reporting/HtmlReportRenderer.cs @@ -39,7 +39,6 @@ public void Process(IConventionFormatContext context, params ConventionResult[] html.RenderBeginTag(HtmlTextWriterTag.P); html.RenderBeginTag(HtmlTextWriterTag.Div); html.RenderBeginTag(HtmlTextWriterTag.Strong); - html.Write(conventionReport.Result+": "); html.RenderEndTag(); var title = String.Format("{0} for {1}", conventionReport.ConventionTitle, conventionReport.DataDescription); html.Write(title); diff --git a/TestStack.ConventionTests/Reporting/ThrowOnFailureResultsProcessor.cs b/TestStack.ConventionTests/Reporting/ThrowOnFailureResultsProcessor.cs index fc2ea2f..5bec062 100644 --- a/TestStack.ConventionTests/Reporting/ThrowOnFailureResultsProcessor.cs +++ b/TestStack.ConventionTests/Reporting/ThrowOnFailureResultsProcessor.cs @@ -9,7 +9,7 @@ public void Process(IConventionFormatContext context, params ConventionResult[] { var conventionReportTextRenderer = new ConventionReportTextRenderer(); conventionReportTextRenderer.Process(context, results); - if (results.Any(r => r.Result == TestResult.Failed)) + if (results.Any(r => r.HasData)) { throw new ConventionFailedException(conventionReportTextRenderer.Output); } From ab75360f18e3f745b1c8f3e9c9001c1485fa4c17 Mon Sep 17 00:00:00 2001 From: Krzysztof Kozmic Date: Sun, 18 Aug 2013 15:46:40 +1000 Subject: [PATCH 11/11] overhauled reporting ConventionResult now holds formatted result and recommended file extension. This will save us from having to have a copy of text renderer in almost any other renderer. Also this gives us flexibility to either use existing result, or overwrite it (or ignore and render your own, but not save it back for validation, like HTML renderer does --- ...nvention_with_simple_reporter.approved.csv | 5 ++ .../CsvReportTests.cs | 4 +- ...ssemblies_referencing_bin_obj.approved.txt | 2 +- ..._obj_with_approved_exceptions.approved.txt | 2 +- ...cripts_not_embedded_resources.approved.txt | 2 +- ...rces_with_approved_exceptions.approved.txt | 2 +- ...sses_have_default_constructor.approved.txt | 2 +- ...uctor_wth_approved_exceptions.approved.txt | 2 +- ...tions.all_methods_are_virtual.approved.txt | 2 +- ...rtual_wth_approved_exceptions.approved.txt | 2 +- ....dtos_exists_in_dto_namespace.approved.txt | 3 +- ...dtos_exists_in_dto_namespace1.approved.txt | 4 ++ ...space_wth_approved_exceptions.approved.txt | 6 --- ...pace_wth_approved_exceptions1.approved.txt | 4 ++ TestStack.ConventionTests/Convention.cs | 48 +++++++++++-------- .../Internal/ConventionContext.cs | 12 +++-- .../Internal/ConventionResult.cs | 14 +++++- .../ConventionTestsApprovalTextWriter.cs | 35 ++++++++++++++ .../Reporting/ApproveResultsProcessor.cs | 33 ++++++++++--- .../Reporting/ConventionReportTextRenderer.cs | 28 +++++------ .../ConventionReportTraceRenderer.cs | 7 +-- .../Reporting/ConvertibleFormatter.cs | 22 +++++++++ .../Reporting/CsvReporter.cs | 19 ++++++-- .../Reporting/DefaultFormatter.cs | 7 ++- .../Reporting/FallbackFormatter.cs | 18 +++++++ .../Reporting/MethodInfoDataFormatter.cs | 10 ++-- .../ThrowOnFailureResultsProcessor.cs | 9 ++-- .../TestStack.ConventionTests.csproj | 3 ++ 28 files changed, 222 insertions(+), 85 deletions(-) create mode 100644 TestStack.ConventionTests.Tests/CsvReportTests.Can_run_convention_with_simple_reporter.approved.csv create mode 100644 TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace1.approved.txt create mode 100644 TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace_wth_approved_exceptions1.approved.txt create mode 100644 TestStack.ConventionTests/Internal/ConventionTestsApprovalTextWriter.cs create mode 100644 TestStack.ConventionTests/Reporting/ConvertibleFormatter.cs create mode 100644 TestStack.ConventionTests/Reporting/FallbackFormatter.cs diff --git a/TestStack.ConventionTests.Tests/CsvReportTests.Can_run_convention_with_simple_reporter.approved.csv b/TestStack.ConventionTests.Tests/CsvReportTests.Can_run_convention_with_simple_reporter.approved.csv new file mode 100644 index 0000000..07a98d3 --- /dev/null +++ b/TestStack.ConventionTests.Tests/CsvReportTests.Can_run_convention_with_simple_reporter.approved.csv @@ -0,0 +1,5 @@ +collection,item,can add,can remove +TestAssembly.Collections.Branch,TestAssembly.Collections.Leaf,False,False +TestAssembly.Collections.Forest,TestAssembly.Collections.Tree,True,True +TestAssembly.Collections.Tree,TestAssembly.Collections.Branch,True,False +TestAssembly.Collections.Tree,TestAssembly.Collections.Leaf,False,False diff --git a/TestStack.ConventionTests.Tests/CsvReportTests.cs b/TestStack.ConventionTests.Tests/CsvReportTests.cs index 729e1ee..e653fec 100644 --- a/TestStack.ConventionTests.Tests/CsvReportTests.cs +++ b/TestStack.ConventionTests.Tests/CsvReportTests.cs @@ -5,13 +5,13 @@ using NUnit.Framework; using TestAssembly.Collections; using TestStack.ConventionTests.ConventionData; + using TestStack.ConventionTests.Reporting; using TestStack.ConventionTests.Tests.TestConventions; [UseReporter(typeof(DiffReporter))] public class CsvReportTests { [Test] - [Explicit("This is work in progress so ignoring for now")] public void Can_run_convention_with_simple_reporter() { Convention.IsWithApprovedExeptions(new CollectionsRelationsConvention(), new Types("Entities") @@ -19,7 +19,7 @@ public void Can_run_convention_with_simple_reporter() TypesToVerify = typeof (Leaf).Assembly.GetExportedTypes() .Where(t => t.Namespace == typeof (Leaf).Namespace).ToArray() - }); + }, new CsvReporter()); } } } \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/ProjectBasedConventions.assemblies_referencing_bin_obj.approved.txt b/TestStack.ConventionTests.Tests/ProjectBasedConventions.assemblies_referencing_bin_obj.approved.txt index 237bbdb..5e98f99 100644 --- a/TestStack.ConventionTests.Tests/ProjectBasedConventions.assemblies_referencing_bin_obj.approved.txt +++ b/TestStack.ConventionTests.Tests/ProjectBasedConventions.assemblies_referencing_bin_obj.approved.txt @@ -1,4 +1,4 @@ 'Project must not reference dlls from bin or obj directories' for 'TestStack.ConventionTests.Tests' --------------------------------------------------------------------------------------------------- - bin\Debug\ApprovalTests.dll \ No newline at end of file + bin\Debug\ApprovalTests.dll diff --git a/TestStack.ConventionTests.Tests/ProjectBasedConventions.assemblies_referencing_bin_obj_with_approved_exceptions.approved.txt b/TestStack.ConventionTests.Tests/ProjectBasedConventions.assemblies_referencing_bin_obj_with_approved_exceptions.approved.txt index 237bbdb..5e98f99 100644 --- a/TestStack.ConventionTests.Tests/ProjectBasedConventions.assemblies_referencing_bin_obj_with_approved_exceptions.approved.txt +++ b/TestStack.ConventionTests.Tests/ProjectBasedConventions.assemblies_referencing_bin_obj_with_approved_exceptions.approved.txt @@ -1,4 +1,4 @@ 'Project must not reference dlls from bin or obj directories' for 'TestStack.ConventionTests.Tests' --------------------------------------------------------------------------------------------------- - bin\Debug\ApprovalTests.dll \ No newline at end of file + bin\Debug\ApprovalTests.dll diff --git a/TestStack.ConventionTests.Tests/ProjectBasedConventions.scripts_not_embedded_resources.approved.txt b/TestStack.ConventionTests.Tests/ProjectBasedConventions.scripts_not_embedded_resources.approved.txt index b6e30f9..ef39646 100644 --- a/TestStack.ConventionTests.Tests/ProjectBasedConventions.scripts_not_embedded_resources.approved.txt +++ b/TestStack.ConventionTests.Tests/ProjectBasedConventions.scripts_not_embedded_resources.approved.txt @@ -1,4 +1,4 @@ '.sql Files must be embedded resources' for 'TestStack.ConventionTests.Tests' ----------------------------------------------------------------------------- - Scripts\Script2.sql \ No newline at end of file + Scripts\Script2.sql diff --git a/TestStack.ConventionTests.Tests/ProjectBasedConventions.scripts_not_embedded_resources_with_approved_exceptions.approved.txt b/TestStack.ConventionTests.Tests/ProjectBasedConventions.scripts_not_embedded_resources_with_approved_exceptions.approved.txt index b6e30f9..ef39646 100644 --- a/TestStack.ConventionTests.Tests/ProjectBasedConventions.scripts_not_embedded_resources_with_approved_exceptions.approved.txt +++ b/TestStack.ConventionTests.Tests/ProjectBasedConventions.scripts_not_embedded_resources_with_approved_exceptions.approved.txt @@ -1,4 +1,4 @@ '.sql Files must be embedded resources' for 'TestStack.ConventionTests.Tests' ----------------------------------------------------------------------------- - Scripts\Script2.sql \ No newline at end of file + Scripts\Script2.sql diff --git a/TestStack.ConventionTests.Tests/TypeBasedConventions.all_classes_have_default_constructor.approved.txt b/TestStack.ConventionTests.Tests/TypeBasedConventions.all_classes_have_default_constructor.approved.txt index b19146c..a0fc6e2 100644 --- a/TestStack.ConventionTests.Tests/TypeBasedConventions.all_classes_have_default_constructor.approved.txt +++ b/TestStack.ConventionTests.Tests/TypeBasedConventions.all_classes_have_default_constructor.approved.txt @@ -2,4 +2,4 @@ ------------------------------------------------------------------ TestAssembly.ClassWithNoDefaultCtor - TestAssembly.ClassWithPrivateDefaultCtor \ No newline at end of file + TestAssembly.ClassWithPrivateDefaultCtor diff --git a/TestStack.ConventionTests.Tests/TypeBasedConventions.all_classes_have_default_constructor_wth_approved_exceptions.approved.txt b/TestStack.ConventionTests.Tests/TypeBasedConventions.all_classes_have_default_constructor_wth_approved_exceptions.approved.txt index b19146c..a0fc6e2 100644 --- a/TestStack.ConventionTests.Tests/TypeBasedConventions.all_classes_have_default_constructor_wth_approved_exceptions.approved.txt +++ b/TestStack.ConventionTests.Tests/TypeBasedConventions.all_classes_have_default_constructor_wth_approved_exceptions.approved.txt @@ -2,4 +2,4 @@ ------------------------------------------------------------------ TestAssembly.ClassWithNoDefaultCtor - TestAssembly.ClassWithPrivateDefaultCtor \ No newline at end of file + TestAssembly.ClassWithPrivateDefaultCtor diff --git a/TestStack.ConventionTests.Tests/TypeBasedConventions.all_methods_are_virtual.approved.txt b/TestStack.ConventionTests.Tests/TypeBasedConventions.all_methods_are_virtual.approved.txt index 7259361..ec621b2 100644 --- a/TestStack.ConventionTests.Tests/TypeBasedConventions.all_methods_are_virtual.approved.txt +++ b/TestStack.ConventionTests.Tests/TypeBasedConventions.all_methods_are_virtual.approved.txt @@ -1,4 +1,4 @@ 'Methods must be virtual' for 'nHibernate Entitites' ---------------------------------------------------- - TestAssembly.SampleDomainClass.TestNonVirtual \ No newline at end of file + TestAssembly.SampleDomainClass.TestNonVirtual diff --git a/TestStack.ConventionTests.Tests/TypeBasedConventions.all_methods_are_virtual_wth_approved_exceptions.approved.txt b/TestStack.ConventionTests.Tests/TypeBasedConventions.all_methods_are_virtual_wth_approved_exceptions.approved.txt index 7259361..ec621b2 100644 --- a/TestStack.ConventionTests.Tests/TypeBasedConventions.all_methods_are_virtual_wth_approved_exceptions.approved.txt +++ b/TestStack.ConventionTests.Tests/TypeBasedConventions.all_methods_are_virtual_wth_approved_exceptions.approved.txt @@ -1,4 +1,4 @@ 'Methods must be virtual' for 'nHibernate Entitites' ---------------------------------------------------- - TestAssembly.SampleDomainClass.TestNonVirtual \ No newline at end of file + TestAssembly.SampleDomainClass.TestNonVirtual diff --git a/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace.approved.txt b/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace.approved.txt index b222199..fc2936a 100644 --- a/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace.approved.txt +++ b/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace.approved.txt @@ -3,8 +3,7 @@ TestAssembly.SomeDto - 'Non-Dtos must not be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly types' --------------------------------------------------------------------------------------- - TestAssembly.Dtos.AnotherClass \ No newline at end of file + TestAssembly.Dtos.AnotherClass diff --git a/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace1.approved.txt b/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace1.approved.txt new file mode 100644 index 0000000..5a005e3 --- /dev/null +++ b/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace1.approved.txt @@ -0,0 +1,4 @@ +'Non-Dtos must not be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly types' +--------------------------------------------------------------------------------------- + + TestAssembly.Dtos.AnotherClass diff --git a/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace_wth_approved_exceptions.approved.txt b/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace_wth_approved_exceptions.approved.txt index b222199..6281d35 100644 --- a/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace_wth_approved_exceptions.approved.txt +++ b/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace_wth_approved_exceptions.approved.txt @@ -2,9 +2,3 @@ ------------------------------------------------------------------------------- TestAssembly.SomeDto - - -'Non-Dtos must not be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly types' ---------------------------------------------------------------------------------------- - - TestAssembly.Dtos.AnotherClass \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace_wth_approved_exceptions1.approved.txt b/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace_wth_approved_exceptions1.approved.txt new file mode 100644 index 0000000..5a005e3 --- /dev/null +++ b/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace_wth_approved_exceptions1.approved.txt @@ -0,0 +1,4 @@ +'Non-Dtos must not be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly types' +--------------------------------------------------------------------------------------- + + TestAssembly.Dtos.AnotherClass diff --git a/TestStack.ConventionTests/Convention.cs b/TestStack.ConventionTests/Convention.cs index 9d908f4..447d520 100644 --- a/TestStack.ConventionTests/Convention.cs +++ b/TestStack.ConventionTests/Convention.cs @@ -19,24 +19,41 @@ static Convention() new ProjectReferenceFormatter(), new ProjectFileFormatter(), new MethodInfoDataFormatter(), - new StringDataFormatter() + new StringDataFormatter(), + new ConvertibleFormatter(), + new FallbackFormatter() }; } public static IList Formatters { get; set; } - public static void Is(IConvention convention, TDataSource data) + static string AssemblyDirectory + { + get + { + // http://stackoverflow.com/questions/52797/c-how-do-i-get-the-path-of-the-assembly-the-code-is-in#answer-283917 + var codeBase = Assembly.GetExecutingAssembly().CodeBase; + var uri = new UriBuilder(codeBase); + var path = Uri.UnescapeDataString(uri.Path); + return Path.GetDirectoryName(path); + } + } + + public static void Is(IConvention convention, TDataSource data, + params IResultsProcessor[] extraResultProcessors) where TDataSource : IConventionData { - Is(convention, data, new IResultsProcessor[] + var processors = new List(extraResultProcessors) { + new ConventionReportTextRenderer(), HtmlRenderer, new ConventionReportTraceRenderer(), new ThrowOnFailureResultsProcessor() - }); + }; + Execute(convention, data, processors.ToArray()); } - public static void Is(IConvention convention, TDataSource data, + static void Execute(IConvention convention, TDataSource data, IResultsProcessor[] processors) where TDataSource : IConventionData { @@ -44,27 +61,18 @@ public static void Is(IConvention convention, TDataSou context.Execute(convention, data); } - public static void IsWithApprovedExeptions(IConvention convention, TDataSource data) + public static void IsWithApprovedExeptions(IConvention convention, TDataSource data, + params IResultsProcessor[] extraResultProcessors) where TDataSource : IConventionData { - Is(convention, data, new IResultsProcessor[] + var processors = new List(extraResultProcessors) { + new ConventionReportTextRenderer(), HtmlRenderer, new ConventionReportTraceRenderer(), new ApproveResultsProcessor() - }); - } - - // http://stackoverflow.com/questions/52797/c-how-do-i-get-the-path-of-the-assembly-the-code-is-in#answer-283917 - static string AssemblyDirectory - { - get - { - string codeBase = Assembly.GetExecutingAssembly().CodeBase; - var uri = new UriBuilder(codeBase); - string path = Uri.UnescapeDataString(uri.Path); - return Path.GetDirectoryName(path); - } + }; + Execute(convention, data, processors.ToArray()); } } } \ No newline at end of file diff --git a/TestStack.ConventionTests/Internal/ConventionContext.cs b/TestStack.ConventionTests/Internal/ConventionContext.cs index 1fc6502..2fdba19 100644 --- a/TestStack.ConventionTests/Internal/ConventionContext.cs +++ b/TestStack.ConventionTests/Internal/ConventionContext.cs @@ -39,10 +39,12 @@ ConventionReportFailure IConventionFormatContext.FormatData(object failingData) return formatter.Format(failingData); } - void IConventionResultContext.Is(string resultTitle, IEnumerable failingData) + void IConventionResultContext.Is(string resultTitle, IEnumerable failingData) { // ReSharper disable PossibleMultipleEnumeration - results.Add(new ConventionResult(resultTitle, + results.Add(new ConventionResult( + typeof(TResult), + resultTitle, dataDescription, failingData.ToObjectArray())); } @@ -51,10 +53,12 @@ void IConventionResultContext.IsSymmetric( string firstSetFailureTitle, IEnumerable firstSetFailureData, string secondSetFailureTitle, IEnumerable secondSetFailureData) { - results.Add(new ConventionResult(firstSetFailureTitle, + results.Add(new ConventionResult( + typeof(TResult), firstSetFailureTitle, dataDescription, firstSetFailureData.ToObjectArray())); - results.Add(new ConventionResult(secondSetFailureTitle, + results.Add(new ConventionResult( + typeof(TResult), secondSetFailureTitle, dataDescription, secondSetFailureData.ToObjectArray())); } diff --git a/TestStack.ConventionTests/Internal/ConventionResult.cs b/TestStack.ConventionTests/Internal/ConventionResult.cs index 8150cb7..91c635e 100644 --- a/TestStack.ConventionTests/Internal/ConventionResult.cs +++ b/TestStack.ConventionTests/Internal/ConventionResult.cs @@ -1,16 +1,20 @@ namespace TestStack.ConventionTests.Internal { + using System; using System.Linq; public class ConventionResult { - public ConventionResult(string conventionTitle, string dataDescription, object[] data) + public ConventionResult(Type dataType, string conventionTitle, string dataDescription, object[] data) { + DataType = dataType; ConventionTitle = conventionTitle; DataDescription = dataDescription; Data = data; } + public string RecommendedFileExtension { get; private set; } + public Type DataType { get; private set; } public string ConventionTitle { get; private set; } public string DataDescription { get; private set; } public object[] Data { get; private set; } @@ -19,5 +23,13 @@ public bool HasData { get { return Data.Any(); } } + + public string FormattedResult { get; private set; } + + public void WithFormattedResult(string formattedResult, string recommendedFileExtension = "txt") + { + FormattedResult = formattedResult; + RecommendedFileExtension = recommendedFileExtension; + } } } \ No newline at end of file diff --git a/TestStack.ConventionTests/Internal/ConventionTestsApprovalTextWriter.cs b/TestStack.ConventionTests/Internal/ConventionTestsApprovalTextWriter.cs new file mode 100644 index 0000000..d2aa7be --- /dev/null +++ b/TestStack.ConventionTests/Internal/ConventionTestsApprovalTextWriter.cs @@ -0,0 +1,35 @@ +namespace TestStack.ConventionTests.Internal +{ + using ApprovalTests; + using ApprovalTests.Core; + + public class ConventionTestsApprovalTextWriter : ApprovalTextWriter, IApprovalWriter + { + readonly int count; + + + public ConventionTestsApprovalTextWriter(string formattedResult, int count, string extensionWithoutDot) + : base(formattedResult, extensionWithoutDot) + { + this.count = count; + } + + string IApprovalWriter.GetApprovalFilename(string basename) + { + if (count == 0) + { + return GetApprovalFilename(basename); + } + return GetApprovalFilename(basename + count); + } + + string IApprovalWriter.GetReceivedFilename(string basename) + { + if (count == 0) + { + return GetReceivedFilename(basename); + } + return GetReceivedFilename(basename + count); + } + } +} \ No newline at end of file diff --git a/TestStack.ConventionTests/Reporting/ApproveResultsProcessor.cs b/TestStack.ConventionTests/Reporting/ApproveResultsProcessor.cs index b020ed7..43d53e8 100644 --- a/TestStack.ConventionTests/Reporting/ApproveResultsProcessor.cs +++ b/TestStack.ConventionTests/Reporting/ApproveResultsProcessor.cs @@ -1,6 +1,8 @@ namespace TestStack.ConventionTests.Reporting { using System; + using System.Collections.Generic; + using System.Linq; using ApprovalTests; using ApprovalTests.Core.Exceptions; using TestStack.ConventionTests.Internal; @@ -9,20 +11,37 @@ public class ApproveResultsProcessor : IResultsProcessor { public void Process(IConventionFormatContext context, params ConventionResult[] results) { - try + var failedApprovals = new List(); + for (var count = 0; count < results.Length; count++) { - var conventionReportTextRenderer = new ConventionReportTextRenderer(); - conventionReportTextRenderer.Process(context, results); - Approvals.Verify(conventionReportTextRenderer.Output); + var result = results[count]; + try + { + Approvals.Verify(new ConventionTestsApprovalTextWriter(result.FormattedResult, count,result.RecommendedFileExtension)); + } + catch (ApprovalException ex) + { + failedApprovals.Add(ex); + } } - catch (ApprovalException ex) + if (failedApprovals.Count == 0) { + return; + } + if (failedApprovals.Count == 1) + { + var ex = failedApprovals[0]; throw new ConventionFailedException("Approved exceptions for convention differs" + Environment.NewLine + Environment.NewLine + - ex.Message, - ex); + ex.Message, ex); } + throw new ConventionFailedException("Approved exceptions for convention differs" + + Environment.NewLine + + Environment.NewLine + + string.Join(Environment.NewLine, + failedApprovals.Select(x => x.Message)), + new AggregateException(failedApprovals.ToArray())); } } } \ No newline at end of file diff --git a/TestStack.ConventionTests/Reporting/ConventionReportTextRenderer.cs b/TestStack.ConventionTests/Reporting/ConventionReportTextRenderer.cs index e507314..89197aa 100644 --- a/TestStack.ConventionTests/Reporting/ConventionReportTextRenderer.cs +++ b/TestStack.ConventionTests/Reporting/ConventionReportTextRenderer.cs @@ -7,26 +7,24 @@ public class ConventionReportTextRenderer : IResultsProcessor { public void Process(IConventionFormatContext context, params ConventionResult[] results) { - var stringBuilder = new StringBuilder(); - - foreach (var conventionReport in results) + foreach (var result in results) { - var title = string.Format("'{0}' for '{1}'", conventionReport.ConventionTitle, - conventionReport.DataDescription); - stringBuilder.AppendLine(title); - stringBuilder.AppendLine(string.Empty.PadRight(title.Length, '-')); - stringBuilder.AppendLine(); + if (result.FormattedResult != null) + { + continue; + } + var description = new StringBuilder(); + var title = string.Format("'{0}' for '{1}'", result.ConventionTitle, + result.DataDescription); + description.AppendLine(title); + description.AppendLine(string.Empty.PadRight(title.Length, '-')); + description.AppendLine(); - RenderItems(conventionReport, stringBuilder, context); - stringBuilder.AppendLine(); - stringBuilder.AppendLine(); + RenderItems(result, description, context); + result.WithFormattedResult(description.ToString()); } - - Output = stringBuilder.ToString().TrimEnd(); } - public string Output { get; private set; } - static void RenderItems(ConventionResult resultInfo, StringBuilder stringBuilder, IConventionFormatContext context) { foreach (var conventionFailure in resultInfo.Data) diff --git a/TestStack.ConventionTests/Reporting/ConventionReportTraceRenderer.cs b/TestStack.ConventionTests/Reporting/ConventionReportTraceRenderer.cs index 09d8446..4d5b2fa 100644 --- a/TestStack.ConventionTests/Reporting/ConventionReportTraceRenderer.cs +++ b/TestStack.ConventionTests/Reporting/ConventionReportTraceRenderer.cs @@ -7,9 +7,10 @@ public class ConventionReportTraceRenderer : IResultsProcessor { public void Process(IConventionFormatContext context, params ConventionResult[] results) { - var conventionReportTextRenderer = new ConventionReportTextRenderer(); - conventionReportTextRenderer.Process(context, results); - Trace.WriteLine(conventionReportTextRenderer.Output); + foreach (var conventionResult in results) + { + Trace.WriteLine(conventionResult.FormattedResult); + } } } } \ No newline at end of file diff --git a/TestStack.ConventionTests/Reporting/ConvertibleFormatter.cs b/TestStack.ConventionTests/Reporting/ConvertibleFormatter.cs new file mode 100644 index 0000000..94a71a2 --- /dev/null +++ b/TestStack.ConventionTests/Reporting/ConvertibleFormatter.cs @@ -0,0 +1,22 @@ +namespace TestStack.ConventionTests.Reporting +{ + using System; + using System.Diagnostics; + using System.Globalization; + using TestStack.ConventionTests.Internal; + + public class ConvertibleFormatter : IReportDataFormatter + { + public bool CanFormat(object failingData) + { + return failingData is IConvertible; + } + + public ConventionReportFailure Format(object failingData) + { + var convertible = failingData as IConvertible; + Debug.Assert(convertible != null, "convertible != null"); + return new ConventionReportFailure(convertible.ToString(CultureInfo.InvariantCulture)); + } + } +} \ No newline at end of file diff --git a/TestStack.ConventionTests/Reporting/CsvReporter.cs b/TestStack.ConventionTests/Reporting/CsvReporter.cs index a8f57d7..7dc529e 100644 --- a/TestStack.ConventionTests/Reporting/CsvReporter.cs +++ b/TestStack.ConventionTests/Reporting/CsvReporter.cs @@ -1,17 +1,26 @@ namespace TestStack.ConventionTests.Reporting { - using System.Collections; using System.Text; + using TestStack.ConventionTests.Internal; - public class CsvReporter + public class CsvReporter : IResultsProcessor { - public string Build(IEnumerable results, string header, DefaultFormatter formatter) + public void Process(IConventionFormatContext context, params ConventionResult[] results) { + foreach (var result in results) + { + result.WithFormattedResult(Process(context, result), "csv"); + } + } + + string Process(IConventionFormatContext context, ConventionResult result) + { + var formatter = new DefaultFormatter(result.DataType); var message = new StringBuilder(); message.AppendLine(string.Join(",", formatter.DesribeType())); - foreach (var result in results) + foreach (var item in result.Data) { - message.AppendLine(string.Join(",", formatter.DesribeItem(result))); + message.AppendLine(string.Join(",", formatter.DesribeItem(item, context))); } return message.ToString(); } diff --git a/TestStack.ConventionTests/Reporting/DefaultFormatter.cs b/TestStack.ConventionTests/Reporting/DefaultFormatter.cs index eaf7fb7..f5ea878 100644 --- a/TestStack.ConventionTests/Reporting/DefaultFormatter.cs +++ b/TestStack.ConventionTests/Reporting/DefaultFormatter.cs @@ -3,15 +3,14 @@ using System; using System.Linq; using System.Reflection; + using TestStack.ConventionTests.Internal; public class DefaultFormatter { readonly PropertyInfo[] properties; - readonly Type type; public DefaultFormatter(Type type) { - this.type = type; properties = type.GetProperties(); } @@ -26,9 +25,9 @@ string Describe(PropertyInfo property) return property.Name.Replace('_', ' '); } - public string[] DesribeItem(object result) + public string[] DesribeItem(object result, IConventionFormatContext context) { - return properties.Select(p => p.GetValue(result, null).ToString()).ToArray(); + return properties.Select(p => context.FormatData(p.GetValue(result, null)).ToString()).ToArray(); } } } \ No newline at end of file diff --git a/TestStack.ConventionTests/Reporting/FallbackFormatter.cs b/TestStack.ConventionTests/Reporting/FallbackFormatter.cs new file mode 100644 index 0000000..a7b89e5 --- /dev/null +++ b/TestStack.ConventionTests/Reporting/FallbackFormatter.cs @@ -0,0 +1,18 @@ +namespace TestStack.ConventionTests.Reporting +{ + using TestStack.ConventionTests.Internal; + + public class FallbackFormatter : IReportDataFormatter + { + public bool CanFormat(object failingData) + { + return true; + } + + public ConventionReportFailure Format(object failingData) + { + // TODO: for now + return new ConventionReportFailure(failingData.ToString()); + } + } +} \ No newline at end of file diff --git a/TestStack.ConventionTests/Reporting/MethodInfoDataFormatter.cs b/TestStack.ConventionTests/Reporting/MethodInfoDataFormatter.cs index 7bdd5a1..12de3e3 100644 --- a/TestStack.ConventionTests/Reporting/MethodInfoDataFormatter.cs +++ b/TestStack.ConventionTests/Reporting/MethodInfoDataFormatter.cs @@ -1,8 +1,10 @@ namespace TestStack.ConventionTests.Reporting -{ - using System.Reflection; - using TestStack.ConventionTests.Internal; - +{ + using System.ComponentModel; + using System.Reflection; + using ApprovalTests.Namers; + using TestStack.ConventionTests.Internal; + public class MethodInfoDataFormatter : IReportDataFormatter { public bool CanFormat(object failingData) diff --git a/TestStack.ConventionTests/Reporting/ThrowOnFailureResultsProcessor.cs b/TestStack.ConventionTests/Reporting/ThrowOnFailureResultsProcessor.cs index 5bec062..f8d565a 100644 --- a/TestStack.ConventionTests/Reporting/ThrowOnFailureResultsProcessor.cs +++ b/TestStack.ConventionTests/Reporting/ThrowOnFailureResultsProcessor.cs @@ -1,5 +1,6 @@ namespace TestStack.ConventionTests.Reporting { + using System; using System.Linq; using TestStack.ConventionTests.Internal; @@ -7,12 +8,12 @@ public class ThrowOnFailureResultsProcessor : IResultsProcessor { public void Process(IConventionFormatContext context, params ConventionResult[] results) { - var conventionReportTextRenderer = new ConventionReportTextRenderer(); - conventionReportTextRenderer.Process(context, results); - if (results.Any(r => r.HasData)) + var invalidResults = results.Where(r => r.HasData).Select(r => r.FormattedResult).ToArray(); + if (invalidResults.None()) { - throw new ConventionFailedException(conventionReportTextRenderer.Output); + return; } + throw new ConventionFailedException(string.Join(Environment.NewLine, invalidResults)); } } } \ No newline at end of file diff --git a/TestStack.ConventionTests/TestStack.ConventionTests.csproj b/TestStack.ConventionTests/TestStack.ConventionTests.csproj index 3cc1b3a..24ea926 100644 --- a/TestStack.ConventionTests/TestStack.ConventionTests.csproj +++ b/TestStack.ConventionTests/TestStack.ConventionTests.csproj @@ -63,11 +63,14 @@ + + +