Skip to content

Commit 3cb584b

Browse files
authored
Added Support for large objects
2 parents 17f8359 + fe4333b commit 3cb584b

23 files changed

+819
-128
lines changed

src/Generator.Tests/AnalyzerTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void GetEventAttribute()
8787
eventModel.Attribute.Properties[1].Name.Should().Be("Level");
8888
eventModel.Attribute.Properties[1].Value.Should().Be("EventLevel.Verbose");
8989
eventModel.Attribute.Properties[2].Name.Should().Be("Message");
90-
eventModel.Attribute.Properties[2].Value.Should().Be("\"Sent message {correlationId}/{messageId} to {to}.\"");
90+
eventModel.Attribute.Properties[2].Value.Should().Be("\"Sent message {correlationId}/{messageType} to {to}.\"");
9191
eventModel.Attribute.Properties[3].Name.Should().Be("Version");
9292
eventModel.Attribute.Properties[3].Value.Should().Be("1");
9393
}
@@ -110,16 +110,16 @@ public void GetEventParameters()
110110
eventModel.Id.Should().Be(1);
111111

112112
eventModel.HasParameters.Should().BeTrue();
113-
eventModel.Parameters[0].Name.Should().Be("messageId");
114-
eventModel.Parameters[0].Type.Should().Be("Guid");
115-
eventModel.Parameters[1].Name.Should().Be("correlationId");
116-
eventModel.Parameters[1].Type.Should().Be("Guid");
117-
eventModel.Parameters[2].Name.Should().Be("messageType");
118-
eventModel.Parameters[2].Type.Should().Be("string");
119-
eventModel.Parameters[3].Name.Should().Be("from");
120-
eventModel.Parameters[3].Type.Should().Be("string");
121-
eventModel.Parameters[4].Name.Should().Be("to");
122-
eventModel.Parameters[4].Type.Should().Be("string");
113+
eventModel.InputParameters[0].Name.Should().Be("messageId");
114+
eventModel.InputParameters[0].Type.Should().Be("Guid");
115+
eventModel.InputParameters[1].Name.Should().Be("correlationId");
116+
eventModel.InputParameters[1].Type.Should().Be("Guid");
117+
eventModel.InputParameters[2].Name.Should().Be("messageType");
118+
eventModel.InputParameters[2].Type.Should().Be("string");
119+
eventModel.InputParameters[3].Name.Should().Be("from");
120+
eventModel.InputParameters[3].Type.Should().Be("string");
121+
eventModel.InputParameters[4].Name.Should().Be("to");
122+
eventModel.InputParameters[4].Type.Should().Be("string");
123123
}
124124

125125
[Fact]

src/Generator.Tests/EventSourceModelPostProcessorTests.cs

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ public void CheckParameterIndex()
1515
{
1616
// arrange
1717
Template template = new Template(Guid.NewGuid().ToString("N"),
18-
Guid.NewGuid().ToString("N"), Enumerable.Empty<WriteMethod>(), 5);
18+
Guid.NewGuid().ToString("N"), Enumerable.Empty<WriteMethod>(), Enumerable.Empty<NamespaceModel>(), 5, false, null);
1919

2020
EventSourceModel eventSourceModel = new EventSourceModel();
2121
EventModel eventModel = new EventModel();
22-
eventModel.Parameters.Add(new EventParameterModel
22+
eventModel.InputParameters.Add(new EventParameterModel
2323
{
2424
Type = "string"
2525
});
26-
eventModel.Parameters.Add(new EventParameterModel
26+
eventModel.InputParameters.Add(new EventParameterModel
2727
{
2828
Type = "int"
2929
});
@@ -43,27 +43,113 @@ EventSourceModelPostProcessor postProcessor
4343
writeCoreModel.Parameters.Last().Position.Should().Be(6);
4444
}
4545

46+
[Fact]
47+
public void CheckComplexParameter()
48+
{
49+
// arrange
50+
Template template = new Template(Guid.NewGuid().ToString("N"),
51+
Guid.NewGuid().ToString("N"), Enumerable.Empty<WriteMethod>(), Enumerable.Empty<NamespaceModel>(), 5, true, "attachmentId");
52+
53+
EventSourceModel eventSourceModel = new EventSourceModel();
54+
EventModel eventModel = new EventModel();
55+
eventModel.InputParameters.Add(new EventParameterModel
56+
{
57+
Type = "string"
58+
});
59+
eventModel.InputParameters.Add(new EventParameterModel
60+
{
61+
Type = "int"
62+
});
63+
eventModel.InputParameters.Add(new EventParameterModel
64+
{
65+
Type = "Exception"
66+
});
67+
eventSourceModel.Events.Add(eventModel);
68+
69+
// act
70+
EventSourceModelPostProcessor postProcessor
71+
= new EventSourceModelPostProcessor(template);
72+
postProcessor.Process(eventSourceModel);
73+
74+
// assert
75+
eventSourceModel.WriteMethods.Should().HaveCount(1);
76+
77+
WriteCoreModel writeCoreModel = eventSourceModel.WriteMethods.First();
78+
writeCoreModel.Parameters.Should().HaveCount(3);
79+
writeCoreModel.Parameters.First().Position.Should().Be(5);
80+
writeCoreModel.Parameters.First().Type.Should().Be("string");
81+
writeCoreModel.Parameters.ElementAt(1).Position.Should().Be(6);
82+
writeCoreModel.Parameters.ElementAt(1).Type.Should().Be("string");
83+
writeCoreModel.Parameters.Last().Position.Should().Be(7);
84+
writeCoreModel.Parameters.Last().Type.Should().Be("int");
85+
}
86+
87+
[Fact]
88+
public void CheckMultipleComplexParameters()
89+
{
90+
// arrange
91+
Template template = new Template(Guid.NewGuid().ToString("N"),
92+
Guid.NewGuid().ToString("N"), Enumerable.Empty<WriteMethod>(), Enumerable.Empty<NamespaceModel>(), 5, true, "attachmentId");
93+
94+
EventSourceModel eventSourceModel = new EventSourceModel();
95+
EventModel eventModel = new EventModel();
96+
eventModel.InputParameters.Add(new EventParameterModel
97+
{
98+
Type = "string"
99+
});
100+
eventModel.InputParameters.Add(new EventParameterModel
101+
{
102+
Type = "int"
103+
});
104+
eventModel.InputParameters.Add(new EventParameterModel
105+
{
106+
Type = "Exception"
107+
});
108+
eventModel.InputParameters.Add(new EventParameterModel
109+
{
110+
Type = "ContractInfo"
111+
});
112+
eventSourceModel.Events.Add(eventModel);
113+
114+
// act
115+
EventSourceModelPostProcessor postProcessor
116+
= new EventSourceModelPostProcessor(template);
117+
postProcessor.Process(eventSourceModel);
118+
119+
// assert
120+
eventSourceModel.WriteMethods.Should().HaveCount(1);
121+
122+
WriteCoreModel writeCoreModel = eventSourceModel.WriteMethods.First();
123+
writeCoreModel.Parameters.Should().HaveCount(3);
124+
writeCoreModel.Parameters.First().Position.Should().Be(5);
125+
writeCoreModel.Parameters.First().Type.Should().Be("string");
126+
writeCoreModel.Parameters.ElementAt(1).Position.Should().Be(6);
127+
writeCoreModel.Parameters.ElementAt(1).Type.Should().Be("string");
128+
writeCoreModel.Parameters.Last().Position.Should().Be(7);
129+
writeCoreModel.Parameters.Last().Type.Should().Be("int");
130+
}
131+
46132
[Fact]
47133
public void DoNotAddTemplateWriteMethods()
48134
{
49135
// arrange
50136
Template template = new Template(Guid.NewGuid().ToString("N"),
51-
Guid.NewGuid().ToString("N"), new[] { new WriteMethod(new[] { "string" }) }, 5);
137+
Guid.NewGuid().ToString("N"), new[] { new WriteMethod(new[] { "string" }) }, Enumerable.Empty<NamespaceModel>(), 5, false, null);
52138

53139
EventSourceModel eventSourceModel = new EventSourceModel();
54140
EventModel eventModel = new EventModel();
55-
eventModel.Parameters.Add(new EventParameterModel
141+
eventModel.InputParameters.Add(new EventParameterModel
56142
{
57143
Type = "string"
58144
});
59-
eventModel.Parameters.Add(new EventParameterModel
145+
eventModel.InputParameters.Add(new EventParameterModel
60146
{
61147
Type = "int"
62148
});
63149
eventSourceModel.Events.Add(eventModel);
64150

65151
eventModel = new EventModel();
66-
eventModel.Parameters.Add(new EventParameterModel
152+
eventModel.InputParameters.Add(new EventParameterModel
67153
{
68154
Type = "string"
69155
});

src/Generator.Tests/EventSourceTemplateEngineTests.cs

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,34 @@ namespace Thor.Generator
1515
{
1616
public class EventSourceTemplateEngineTests
1717
{
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+
1846
[Fact]
1947
public void GenerateCSharpEventSource()
2048
{
@@ -41,13 +69,90 @@ public void GenerateCSharpEventSource()
4169
.Be(eventSourceDefinitionVisitor.EventSource.Name);
4270

4371
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)");
4573
eventSourceVisitor.MethodSignatures[1].Should()
4674
.Be("Two(Guid messageId, Guid correlationId, string messageType, string from, string to)");
4775
eventSourceVisitor.MethodSignatures[2].Should()
4876
.Be("WriteCore(int eventId, Guid a, Guid b, string c, string d, string e)");
4977
eventSourceVisitor.MethodSignatures[3].Should()
5078
.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;");
51156
}
52157

53158
[Fact]
@@ -120,6 +225,8 @@ public class EventSourceVisitor
120225
public IList<string> ClassDocumentations { get; } = new List<string>();
121226
public IList<string> MethodSignatures { get; } = new List<string>();
122227
public IList<string> MethodDocumentations { get; } = new List<string>();
228+
public IList<string> ImportedNamespaces { get; } = new List<string>();
229+
123230

124231
public override void VisitClassDeclaration(ClassDeclarationSyntax node)
125232
{
@@ -155,6 +262,13 @@ public override void VisitMethodDeclaration(MethodDeclarationSyntax node)
155262
base.VisitMethodDeclaration(node);
156263
}
157264

265+
public override void VisitUsingDirective(UsingDirectiveSyntax node)
266+
{
267+
ImportedNamespaces.Add(node.ToString());
268+
269+
base.VisitUsingDirective(node);
270+
}
271+
158272
private static string GetDocumentationXml(CSharpSyntaxNode syntaxNode)
159273
{
160274
SyntaxTrivia documentation = syntaxNode.GetLeadingTrivia()

0 commit comments

Comments
 (0)