@@ -15,6 +15,34 @@ namespace Thor.Generator
15
15
{
16
16
public class EventSourceTemplateEngineTests
17
17
{
18
+ [ Fact ]
19
+ public void DontGenerateCSharpEventSource ( )
20
+ {
21
+ // arrange
22
+ TemplateStorage templateStorage = new TemplateStorage ( ) ;
23
+ Template template = templateStorage . GetTemplate ( ProjectSystem . Language . CSharp ) ;
24
+
25
+ EventSourceDefinitionVisitor eventSourceDefinitionVisitor = new EventSourceDefinitionVisitor ( ) ;
26
+ SyntaxTree syntaxTree = CSharpSyntaxTree . ParseText ( Resources . EventSourceWithComplexType ) ;
27
+ eventSourceDefinitionVisitor . Visit ( syntaxTree . GetRoot ( ) ) ;
28
+
29
+ Exception ex = null ;
30
+
31
+ // act
32
+ EventSourceTemplateEngine templateEngine = new EventSourceTemplateEngine ( template ) ;
33
+ try
34
+ {
35
+ string eventSourceCode = templateEngine . Generate ( eventSourceDefinitionVisitor . EventSource ) ;
36
+ }
37
+ catch ( ArgumentException e )
38
+ {
39
+ ex = e ;
40
+ }
41
+ // assert
42
+ Assert . NotNull ( ex ) ;
43
+ ex . Message . Should ( ) . Be ( "ComplexType parameters are not allowed by the template." ) ;
44
+ }
45
+
18
46
[ Fact ]
19
47
public void GenerateCSharpEventSource ( )
20
48
{
@@ -41,13 +69,90 @@ public void GenerateCSharpEventSource()
41
69
. Be ( eventSourceDefinitionVisitor . EventSource . Name ) ;
42
70
43
71
eventSourceVisitor . MethodSignatures [ 0 ] . Should ( )
44
- . Be ( "One(Guid messageId , Guid correlationId, string messageType, string from, string to)" ) ;
72
+ . Be ( "One(Guid ex , Guid correlationId, string messageType, string from, string to)" ) ;
45
73
eventSourceVisitor . MethodSignatures [ 1 ] . Should ( )
46
74
. Be ( "Two(Guid messageId, Guid correlationId, string messageType, string from, string to)" ) ;
47
75
eventSourceVisitor . MethodSignatures [ 2 ] . Should ( )
48
76
. Be ( "WriteCore(int eventId, Guid a, Guid b, string c, string d, string e)" ) ;
49
77
eventSourceVisitor . MethodSignatures [ 3 ] . Should ( )
50
78
. Be ( "SetToEmptyIfNull(string value)" ) ;
79
+
80
+ eventSourceVisitor . ImportedNamespaces . Count . Should ( )
81
+ . Be ( 8 ) ;
82
+ eventSourceVisitor . ImportedNamespaces [ 0 ] . Should ( )
83
+ . Be ( "using static Newtonsoft.Json;" ) ;
84
+ eventSourceVisitor . ImportedNamespaces [ 1 ] . Should ( )
85
+ . Be ( "using System;" ) ;
86
+ eventSourceVisitor . ImportedNamespaces [ 2 ] . Should ( )
87
+ . Be ( "using Gen = System.Generic;" ) ;
88
+ eventSourceVisitor . ImportedNamespaces [ 3 ] . Should ( )
89
+ . Be ( "using Io = System.IO;" ) ;
90
+ eventSourceVisitor . ImportedNamespaces [ 4 ] . Should ( )
91
+ . Be ( "using System.Linq;" ) ;
92
+ eventSourceVisitor . ImportedNamespaces [ 5 ] . Should ( )
93
+ . Be ( "using static System.Math;" ) ;
94
+ eventSourceVisitor . ImportedNamespaces [ 6 ] . Should ( )
95
+ . Be ( "using System.Text;" ) ;
96
+ eventSourceVisitor . ImportedNamespaces [ 7 ] . Should ( )
97
+ . Be ( "using Tasks = System.Threading.Tasks;" ) ;
98
+ }
99
+
100
+ [ Fact ]
101
+ public void GenerateCsharpComplexEventSource ( )
102
+ {
103
+ // arrange
104
+ TemplateStorage templateStorage = new TemplateStorage ( ) ;
105
+ Template template = templateStorage . GetCustomTemplate ( "Defaults\\ CSharpWithComplex" ) ;
106
+
107
+ EventSourceDefinitionVisitor eventSourceDefinitionVisitor = new EventSourceDefinitionVisitor ( ) ;
108
+ SyntaxTree syntaxTree = CSharpSyntaxTree . ParseText ( Resources . EventSourceWithComplexType ) ;
109
+ eventSourceDefinitionVisitor . Visit ( syntaxTree . GetRoot ( ) ) ;
110
+
111
+ // act
112
+ EventSourceTemplateEngine templateEngine = new EventSourceTemplateEngine ( template ) ;
113
+ string eventSourceCode = templateEngine . Generate ( eventSourceDefinitionVisitor . EventSource ) ;
114
+
115
+ // assert
116
+ syntaxTree = CSharpSyntaxTree . ParseText ( eventSourceCode ) ;
117
+
118
+ EventSourceVisitor eventSourceVisitor = new EventSourceVisitor ( ) ;
119
+ eventSourceVisitor . Visit ( syntaxTree . GetRoot ( ) ) ;
120
+
121
+ eventSourceVisitor . Classes . Should ( ) . HaveCount ( 1 ) ;
122
+ eventSourceVisitor . Classes . First ( ) . Should ( )
123
+ . Be ( eventSourceDefinitionVisitor . EventSource . Name ) ;
124
+
125
+ eventSourceVisitor . MethodSignatures [ 0 ] . Should ( )
126
+ . Be ( "One(Exception ex, Guid correlationId, string messageType, string from, string to)" ) ;
127
+ eventSourceVisitor . MethodSignatures [ 1 ] . Should ( )
128
+ . Be ( "One(int applicationId, Guid activityId, String attachmentId, Guid correlationId, string messageType, string from, string to)" ) ;
129
+ eventSourceVisitor . MethodSignatures [ 2 ] . Should ( )
130
+ . Be ( "Two(Guid messageId, Guid correlationId, string messageType, string from, string to)" ) ;
131
+ eventSourceVisitor . MethodSignatures [ 3 ] . Should ( )
132
+ . Be ( "Two(int applicationId, Guid activityId, Guid messageId, Guid correlationId, string messageType, string from, string to)" ) ;
133
+ eventSourceVisitor . MethodSignatures [ 4 ] . Should ( )
134
+ . Be ( "WriteCore(int eventId, int applicationId, Guid activityId, string a, Guid b, string c, string d, string e)" ) ;
135
+ eventSourceVisitor . MethodSignatures [ 5 ] . Should ( )
136
+ . Be ( "WriteCore(int eventId, int applicationId, Guid activityId, Guid a, Guid b, string c, string d, string e)" ) ;
137
+
138
+ eventSourceVisitor . ImportedNamespaces . Count . Should ( )
139
+ . Be ( 8 ) ;
140
+ eventSourceVisitor . ImportedNamespaces [ 0 ] . Should ( )
141
+ . Be ( "using ChilliCream.Tracing.Abstractions;" ) ;
142
+ eventSourceVisitor . ImportedNamespaces [ 1 ] . Should ( )
143
+ . Be ( "using System;" ) ;
144
+ eventSourceVisitor . ImportedNamespaces [ 2 ] . Should ( )
145
+ . Be ( "using System.Collections.Generic;" ) ;
146
+ eventSourceVisitor . ImportedNamespaces [ 3 ] . Should ( )
147
+ . Be ( "using Gen = System.Generic;" ) ;
148
+ eventSourceVisitor . ImportedNamespaces [ 4 ] . Should ( )
149
+ . Be ( "using System.Linq;" ) ;
150
+ eventSourceVisitor . ImportedNamespaces [ 5 ] . Should ( )
151
+ . Be ( "using static System.Math;" ) ;
152
+ eventSourceVisitor . ImportedNamespaces [ 6 ] . Should ( )
153
+ . Be ( "using System.Text;" ) ;
154
+ eventSourceVisitor . ImportedNamespaces [ 7 ] . Should ( )
155
+ . Be ( "using Tasks = System.Threading.Tasks;" ) ;
51
156
}
52
157
53
158
[ Fact ]
@@ -120,6 +225,8 @@ public class EventSourceVisitor
120
225
public IList < string > ClassDocumentations { get ; } = new List < string > ( ) ;
121
226
public IList < string > MethodSignatures { get ; } = new List < string > ( ) ;
122
227
public IList < string > MethodDocumentations { get ; } = new List < string > ( ) ;
228
+ public IList < string > ImportedNamespaces { get ; } = new List < string > ( ) ;
229
+
123
230
124
231
public override void VisitClassDeclaration ( ClassDeclarationSyntax node )
125
232
{
@@ -155,6 +262,13 @@ public override void VisitMethodDeclaration(MethodDeclarationSyntax node)
155
262
base . VisitMethodDeclaration ( node ) ;
156
263
}
157
264
265
+ public override void VisitUsingDirective ( UsingDirectiveSyntax node )
266
+ {
267
+ ImportedNamespaces . Add ( node . ToString ( ) ) ;
268
+
269
+ base . VisitUsingDirective ( node ) ;
270
+ }
271
+
158
272
private static string GetDocumentationXml ( CSharpSyntaxNode syntaxNode )
159
273
{
160
274
SyntaxTrivia documentation = syntaxNode . GetLeadingTrivia ( )
0 commit comments