11namespace TestStack . ConventionTests
22{
3+ using System ;
34 using System . Collections . Generic ;
5+ using System . IO ;
46 using System . Linq ;
7+ using System . Reflection ;
58 using ApprovalTests ;
69 using ApprovalTests . Core . Exceptions ;
710 using TestStack . ConventionTests . Conventions ;
811 using TestStack . ConventionTests . Internal ;
912
1013 public static class Convention
1114 {
15+ static readonly HtmlReportRenderer HtmlRenderer = new HtmlReportRenderer ( AssemblyDirectory ) ;
1216 static readonly List < ConventionReport > Reports = new List < ConventionReport > ( ) ;
1317
1418 public static IEnumerable < ConventionReport > ConventionReports { get { return Reports ; } }
@@ -22,18 +26,26 @@ public static void Is<TDataSource, TDataType>(IConvention<TDataSource, TDataType
2226 public static void Is < TDataSource , TDataType > ( IConvention < TDataSource , TDataType > convention , TDataSource data , IConventionReportRenderer reporter )
2327 where TDataSource : IConventionData , ICreateReportLineFor < TDataType >
2428 {
25- var conventionResult = GetConventionReport ( convention . ConventionTitle , convention . GetFailingData ( data ) . ToArray ( ) , data ) ;
29+ try
30+ {
31+ var conventionResult = GetConventionReport ( convention . ConventionTitle , convention . GetFailingData ( data ) . ToArray ( ) , data ) ;
2632
27- Reports . Add ( conventionResult ) ;
33+ Reports . Add ( conventionResult ) ;
2834
29- new ConventionReportTraceRenderer ( ) . Render ( conventionResult ) ;
30- reporter . Render ( conventionResult ) ;
35+ new ConventionReportTraceRenderer ( ) . Render ( conventionResult ) ;
36+ reporter . Render ( conventionResult ) ;
37+ }
38+ finally
39+ {
40+ HtmlRenderer . Render ( Reports . ToArray ( ) ) ;
41+ }
3142 }
3243
3344 public static void IsWithApprovedExeptions < TDataSource , TDataType > ( IConvention < TDataSource , TDataType > convention , TDataSource data )
3445 where TDataSource : IConventionData , ICreateReportLineFor < TDataType >
3546 {
3647 var conventionResult = GetConventionReport ( convention . ConventionTitle , convention . GetFailingData ( data ) . ToArray ( ) , data ) ;
48+ Reports . Add ( conventionResult ) ;
3749
3850 try
3951 {
@@ -50,6 +62,10 @@ public static void IsWithApprovedExeptions<TDataSource, TDataType>(IConvention<T
5062 {
5163 throw new ConventionFailedException ( "Approved exceptions for convention differs\r \n \r \n " + ex . Message , ex ) ;
5264 }
65+ finally
66+ {
67+ HtmlRenderer . Render ( Reports . ToArray ( ) ) ;
68+ }
5369 }
5470
5571 public static void Is < TDataSource , TDataType > ( ISymmetricConvention < TDataSource , TDataType > convention , TDataSource data )
@@ -61,21 +77,30 @@ public static void Is<TDataSource, TDataType>(ISymmetricConvention<TDataSource,
6177 public static void Is < TDataSource , TDataType > ( ISymmetricConvention < TDataSource , TDataType > convention , TDataSource data , IConventionReportRenderer reporter )
6278 where TDataSource : IConventionData , ICreateReportLineFor < TDataType >
6379 {
64- var conventionResult = GetConventionReport ( convention . ConventionTitle , convention . GetFailingData ( data ) . ToArray ( ) , data ) ;
65- var inverseConventionResult = GetConventionReport ( convention . InverseTitle , convention . GetFailingInverseData ( data ) . ToArray ( ) , data ) ;
80+ try
81+ {
82+ var conventionResult = GetConventionReport ( convention . ConventionTitle , convention . GetFailingData ( data ) . ToArray ( ) , data ) ;
83+ var inverseConventionResult = GetConventionReport ( convention . InverseTitle , convention . GetFailingInverseData ( data ) . ToArray ( ) , data ) ;
6684
67- Reports . Add ( conventionResult ) ;
68- Reports . Add ( inverseConventionResult ) ;
85+ Reports . Add ( conventionResult ) ;
86+ Reports . Add ( inverseConventionResult ) ;
6987
70- new ConventionReportTraceRenderer ( ) . Render ( conventionResult , inverseConventionResult ) ;
71- reporter . Render ( conventionResult , inverseConventionResult ) ;
88+ new ConventionReportTraceRenderer ( ) . Render ( conventionResult , inverseConventionResult ) ;
89+ reporter . Render ( conventionResult , inverseConventionResult ) ;
90+ }
91+ finally
92+ {
93+ HtmlRenderer . Render ( Reports . ToArray ( ) ) ;
94+ }
7295 }
7396
7497 public static void IsWithApprovedExeptions < TDataSource , TDataType > ( ISymmetricConvention < TDataSource , TDataType > convention , TDataSource data )
7598 where TDataSource : IConventionData , ICreateReportLineFor < TDataType >
7699 {
77100 var conventionResult = GetConventionReport ( convention . ConventionTitle , convention . GetFailingData ( data ) . ToArray ( ) , data ) ;
78101 var inverseConventionResult = GetConventionReport ( convention . InverseTitle , convention . GetFailingInverseData ( data ) . ToArray ( ) , data ) ;
102+ Reports . Add ( conventionResult ) ;
103+ Reports . Add ( inverseConventionResult ) ;
79104
80105 try
81106 {
@@ -99,6 +124,10 @@ public static void IsWithApprovedExeptions<TDataSource, TDataType>(ISymmetricCon
99124 {
100125 throw new ConventionFailedException ( "Approved exceptions for convention differs\r \n \r \n " + ex . Message , ex ) ;
101126 }
127+ finally
128+ {
129+ HtmlRenderer . Render ( Reports . ToArray ( ) ) ;
130+ }
102131 }
103132
104133 static ConventionReport GetConventionReport < TDataSource , TDataType > ( string conventionTitle , TDataType [ ] failingData , TDataSource data )
@@ -114,5 +143,17 @@ static ConventionReport GetConventionReport<TDataSource, TDataType>(string conve
114143 failingData . Select ( data . CreateReportLine ) ) ;
115144 return conventionResult ;
116145 }
146+
147+ // http://stackoverflow.com/questions/52797/c-how-do-i-get-the-path-of-the-assembly-the-code-is-in#answer-283917
148+ static string AssemblyDirectory
149+ {
150+ get
151+ {
152+ string codeBase = Assembly . GetExecutingAssembly ( ) . CodeBase ;
153+ var uri = new UriBuilder ( codeBase ) ;
154+ string path = Uri . UnescapeDataString ( uri . Path ) ;
155+ return Path . GetDirectoryName ( path ) ;
156+ }
157+ }
117158 }
118159}
0 commit comments