1
1
namespace TestStack . ConventionTests
2
2
{
3
+ using System ;
3
4
using System . Collections . Generic ;
5
+ using System . IO ;
4
6
using System . Linq ;
7
+ using System . Reflection ;
5
8
using ApprovalTests ;
6
9
using ApprovalTests . Core . Exceptions ;
7
10
using TestStack . ConventionTests . Conventions ;
8
11
using TestStack . ConventionTests . Internal ;
9
12
10
13
public static class Convention
11
14
{
15
+ static readonly HtmlReportRenderer HtmlRenderer = new HtmlReportRenderer ( AssemblyDirectory ) ;
12
16
static readonly List < ConventionReport > Reports = new List < ConventionReport > ( ) ;
13
17
14
18
public static IEnumerable < ConventionReport > ConventionReports { get { return Reports ; } }
@@ -22,18 +26,26 @@ public static void Is<TDataSource, TDataType>(IConvention<TDataSource, TDataType
22
26
public static void Is < TDataSource , TDataType > ( IConvention < TDataSource , TDataType > convention , TDataSource data , IConventionReportRenderer reporter )
23
27
where TDataSource : IConventionData , ICreateReportLineFor < TDataType >
24
28
{
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 ) ;
26
32
27
- Reports . Add ( conventionResult ) ;
33
+ Reports . Add ( conventionResult ) ;
28
34
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
+ }
31
42
}
32
43
33
44
public static void IsWithApprovedExeptions < TDataSource , TDataType > ( IConvention < TDataSource , TDataType > convention , TDataSource data )
34
45
where TDataSource : IConventionData , ICreateReportLineFor < TDataType >
35
46
{
36
47
var conventionResult = GetConventionReport ( convention . ConventionTitle , convention . GetFailingData ( data ) . ToArray ( ) , data ) ;
48
+ Reports . Add ( conventionResult ) ;
37
49
38
50
try
39
51
{
@@ -50,6 +62,10 @@ public static void IsWithApprovedExeptions<TDataSource, TDataType>(IConvention<T
50
62
{
51
63
throw new ConventionFailedException ( "Approved exceptions for convention differs\r \n \r \n " + ex . Message , ex ) ;
52
64
}
65
+ finally
66
+ {
67
+ HtmlRenderer . Render ( Reports . ToArray ( ) ) ;
68
+ }
53
69
}
54
70
55
71
public static void Is < TDataSource , TDataType > ( ISymmetricConvention < TDataSource , TDataType > convention , TDataSource data )
@@ -61,21 +77,30 @@ public static void Is<TDataSource, TDataType>(ISymmetricConvention<TDataSource,
61
77
public static void Is < TDataSource , TDataType > ( ISymmetricConvention < TDataSource , TDataType > convention , TDataSource data , IConventionReportRenderer reporter )
62
78
where TDataSource : IConventionData , ICreateReportLineFor < TDataType >
63
79
{
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 ) ;
66
84
67
- Reports . Add ( conventionResult ) ;
68
- Reports . Add ( inverseConventionResult ) ;
85
+ Reports . Add ( conventionResult ) ;
86
+ Reports . Add ( inverseConventionResult ) ;
69
87
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
+ }
72
95
}
73
96
74
97
public static void IsWithApprovedExeptions < TDataSource , TDataType > ( ISymmetricConvention < TDataSource , TDataType > convention , TDataSource data )
75
98
where TDataSource : IConventionData , ICreateReportLineFor < TDataType >
76
99
{
77
100
var conventionResult = GetConventionReport ( convention . ConventionTitle , convention . GetFailingData ( data ) . ToArray ( ) , data ) ;
78
101
var inverseConventionResult = GetConventionReport ( convention . InverseTitle , convention . GetFailingInverseData ( data ) . ToArray ( ) , data ) ;
102
+ Reports . Add ( conventionResult ) ;
103
+ Reports . Add ( inverseConventionResult ) ;
79
104
80
105
try
81
106
{
@@ -99,6 +124,10 @@ public static void IsWithApprovedExeptions<TDataSource, TDataType>(ISymmetricCon
99
124
{
100
125
throw new ConventionFailedException ( "Approved exceptions for convention differs\r \n \r \n " + ex . Message , ex ) ;
101
126
}
127
+ finally
128
+ {
129
+ HtmlRenderer . Render ( Reports . ToArray ( ) ) ;
130
+ }
102
131
}
103
132
104
133
static ConventionReport GetConventionReport < TDataSource , TDataType > ( string conventionTitle , TDataType [ ] failingData , TDataSource data )
@@ -114,5 +143,17 @@ static ConventionReport GetConventionReport<TDataSource, TDataType>(string conve
114
143
failingData . Select ( data . CreateReportLine ) ) ;
115
144
return conventionResult ;
116
145
}
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
+ }
117
158
}
118
159
}
0 commit comments