Skip to content

Added Support for large objects #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/Generator.Tests/AnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void GetEventAttribute()
eventModel.Attribute.Properties[1].Name.Should().Be("Level");
eventModel.Attribute.Properties[1].Value.Should().Be("EventLevel.Verbose");
eventModel.Attribute.Properties[2].Name.Should().Be("Message");
eventModel.Attribute.Properties[2].Value.Should().Be("\"Sent message {correlationId}/{messageId} to {to}.\"");
eventModel.Attribute.Properties[2].Value.Should().Be("\"Sent message {correlationId}/{messageType} to {to}.\"");
eventModel.Attribute.Properties[3].Name.Should().Be("Version");
eventModel.Attribute.Properties[3].Value.Should().Be("1");
}
Expand All @@ -110,16 +110,16 @@ public void GetEventParameters()
eventModel.Id.Should().Be(1);

eventModel.HasParameters.Should().BeTrue();
eventModel.Parameters[0].Name.Should().Be("messageId");
eventModel.Parameters[0].Type.Should().Be("Guid");
eventModel.Parameters[1].Name.Should().Be("correlationId");
eventModel.Parameters[1].Type.Should().Be("Guid");
eventModel.Parameters[2].Name.Should().Be("messageType");
eventModel.Parameters[2].Type.Should().Be("string");
eventModel.Parameters[3].Name.Should().Be("from");
eventModel.Parameters[3].Type.Should().Be("string");
eventModel.Parameters[4].Name.Should().Be("to");
eventModel.Parameters[4].Type.Should().Be("string");
eventModel.InputParameters[0].Name.Should().Be("messageId");
eventModel.InputParameters[0].Type.Should().Be("Guid");
eventModel.InputParameters[1].Name.Should().Be("correlationId");
eventModel.InputParameters[1].Type.Should().Be("Guid");
eventModel.InputParameters[2].Name.Should().Be("messageType");
eventModel.InputParameters[2].Type.Should().Be("string");
eventModel.InputParameters[3].Name.Should().Be("from");
eventModel.InputParameters[3].Type.Should().Be("string");
eventModel.InputParameters[4].Name.Should().Be("to");
eventModel.InputParameters[4].Type.Should().Be("string");
}

[Fact]
Expand Down
100 changes: 93 additions & 7 deletions src/Generator.Tests/EventSourceModelPostProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ public void CheckParameterIndex()
{
// arrange
Template template = new Template(Guid.NewGuid().ToString("N"),
Guid.NewGuid().ToString("N"), Enumerable.Empty<WriteMethod>(), 5);
Guid.NewGuid().ToString("N"), Enumerable.Empty<WriteMethod>(), Enumerable.Empty<NamespaceModel>(), 5, false, null);

EventSourceModel eventSourceModel = new EventSourceModel();
EventModel eventModel = new EventModel();
eventModel.Parameters.Add(new EventParameterModel
eventModel.InputParameters.Add(new EventParameterModel
{
Type = "string"
});
eventModel.Parameters.Add(new EventParameterModel
eventModel.InputParameters.Add(new EventParameterModel
{
Type = "int"
});
Expand All @@ -43,27 +43,113 @@ EventSourceModelPostProcessor postProcessor
writeCoreModel.Parameters.Last().Position.Should().Be(6);
}

[Fact]
public void CheckComplexParameter()
{
// arrange
Template template = new Template(Guid.NewGuid().ToString("N"),
Guid.NewGuid().ToString("N"), Enumerable.Empty<WriteMethod>(), Enumerable.Empty<NamespaceModel>(), 5, true, "attachmentId");

EventSourceModel eventSourceModel = new EventSourceModel();
EventModel eventModel = new EventModel();
eventModel.InputParameters.Add(new EventParameterModel
{
Type = "string"
});
eventModel.InputParameters.Add(new EventParameterModel
{
Type = "int"
});
eventModel.InputParameters.Add(new EventParameterModel
{
Type = "Exception"
});
eventSourceModel.Events.Add(eventModel);

// act
EventSourceModelPostProcessor postProcessor
= new EventSourceModelPostProcessor(template);
postProcessor.Process(eventSourceModel);

// assert
eventSourceModel.WriteMethods.Should().HaveCount(1);

WriteCoreModel writeCoreModel = eventSourceModel.WriteMethods.First();
writeCoreModel.Parameters.Should().HaveCount(3);
writeCoreModel.Parameters.First().Position.Should().Be(5);
writeCoreModel.Parameters.First().Type.Should().Be("string");
writeCoreModel.Parameters.ElementAt(1).Position.Should().Be(6);
writeCoreModel.Parameters.ElementAt(1).Type.Should().Be("string");
writeCoreModel.Parameters.Last().Position.Should().Be(7);
writeCoreModel.Parameters.Last().Type.Should().Be("int");
}

[Fact]
public void CheckMultipleComplexParameters()
{
// arrange
Template template = new Template(Guid.NewGuid().ToString("N"),
Guid.NewGuid().ToString("N"), Enumerable.Empty<WriteMethod>(), Enumerable.Empty<NamespaceModel>(), 5, true, "attachmentId");

EventSourceModel eventSourceModel = new EventSourceModel();
EventModel eventModel = new EventModel();
eventModel.InputParameters.Add(new EventParameterModel
{
Type = "string"
});
eventModel.InputParameters.Add(new EventParameterModel
{
Type = "int"
});
eventModel.InputParameters.Add(new EventParameterModel
{
Type = "Exception"
});
eventModel.InputParameters.Add(new EventParameterModel
{
Type = "ContractInfo"
});
eventSourceModel.Events.Add(eventModel);

// act
EventSourceModelPostProcessor postProcessor
= new EventSourceModelPostProcessor(template);
postProcessor.Process(eventSourceModel);

// assert
eventSourceModel.WriteMethods.Should().HaveCount(1);

WriteCoreModel writeCoreModel = eventSourceModel.WriteMethods.First();
writeCoreModel.Parameters.Should().HaveCount(3);
writeCoreModel.Parameters.First().Position.Should().Be(5);
writeCoreModel.Parameters.First().Type.Should().Be("string");
writeCoreModel.Parameters.ElementAt(1).Position.Should().Be(6);
writeCoreModel.Parameters.ElementAt(1).Type.Should().Be("string");
writeCoreModel.Parameters.Last().Position.Should().Be(7);
writeCoreModel.Parameters.Last().Type.Should().Be("int");
}

[Fact]
public void DoNotAddTemplateWriteMethods()
{
// arrange
Template template = new Template(Guid.NewGuid().ToString("N"),
Guid.NewGuid().ToString("N"), new[] { new WriteMethod(new[] { "string" }) }, 5);
Guid.NewGuid().ToString("N"), new[] { new WriteMethod(new[] { "string" }) }, Enumerable.Empty<NamespaceModel>(), 5, false, null);

EventSourceModel eventSourceModel = new EventSourceModel();
EventModel eventModel = new EventModel();
eventModel.Parameters.Add(new EventParameterModel
eventModel.InputParameters.Add(new EventParameterModel
{
Type = "string"
});
eventModel.Parameters.Add(new EventParameterModel
eventModel.InputParameters.Add(new EventParameterModel
{
Type = "int"
});
eventSourceModel.Events.Add(eventModel);

eventModel = new EventModel();
eventModel.Parameters.Add(new EventParameterModel
eventModel.InputParameters.Add(new EventParameterModel
{
Type = "string"
});
Expand Down
116 changes: 115 additions & 1 deletion src/Generator.Tests/EventSourceTemplateEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,34 @@ namespace Thor.Generator
{
public class EventSourceTemplateEngineTests
{
[Fact]
public void DontGenerateCSharpEventSource()
{
// arrange
TemplateStorage templateStorage = new TemplateStorage();
Template template = templateStorage.GetTemplate(ProjectSystem.Language.CSharp);

EventSourceDefinitionVisitor eventSourceDefinitionVisitor = new EventSourceDefinitionVisitor();
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(Resources.EventSourceWithComplexType);
eventSourceDefinitionVisitor.Visit(syntaxTree.GetRoot());

Exception ex = null;

// act
EventSourceTemplateEngine templateEngine = new EventSourceTemplateEngine(template);
try
{
string eventSourceCode = templateEngine.Generate(eventSourceDefinitionVisitor.EventSource);
}
catch (ArgumentException e)
{
ex = e;
}
// assert
Assert.NotNull(ex);
ex.Message.Should().Be("ComplexType parameters are not allowed by the template.");
}

[Fact]
public void GenerateCSharpEventSource()
{
Expand All @@ -41,13 +69,90 @@ public void GenerateCSharpEventSource()
.Be(eventSourceDefinitionVisitor.EventSource.Name);

eventSourceVisitor.MethodSignatures[0].Should()
.Be("One(Guid messageId, Guid correlationId, string messageType, string from, string to)");
.Be("One(Guid ex, Guid correlationId, string messageType, string from, string to)");
eventSourceVisitor.MethodSignatures[1].Should()
.Be("Two(Guid messageId, Guid correlationId, string messageType, string from, string to)");
eventSourceVisitor.MethodSignatures[2].Should()
.Be("WriteCore(int eventId, Guid a, Guid b, string c, string d, string e)");
eventSourceVisitor.MethodSignatures[3].Should()
.Be("SetToEmptyIfNull(string value)");

eventSourceVisitor.ImportedNamespaces.Count.Should()
.Be(8);
eventSourceVisitor.ImportedNamespaces[0].Should()
.Be("using static Newtonsoft.Json;");
eventSourceVisitor.ImportedNamespaces[1].Should()
.Be("using System;");
eventSourceVisitor.ImportedNamespaces[2].Should()
.Be("using Gen = System.Generic;");
eventSourceVisitor.ImportedNamespaces[3].Should()
.Be("using Io = System.IO;");
eventSourceVisitor.ImportedNamespaces[4].Should()
.Be("using System.Linq;");
eventSourceVisitor.ImportedNamespaces[5].Should()
.Be("using static System.Math;");
eventSourceVisitor.ImportedNamespaces[6].Should()
.Be("using System.Text;");
eventSourceVisitor.ImportedNamespaces[7].Should()
.Be("using Tasks = System.Threading.Tasks;");
}

[Fact]
public void GenerateCsharpComplexEventSource()
{
// arrange
TemplateStorage templateStorage = new TemplateStorage();
Template template = templateStorage.GetCustomTemplate("Defaults\\CSharpWithComplex");

EventSourceDefinitionVisitor eventSourceDefinitionVisitor = new EventSourceDefinitionVisitor();
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(Resources.EventSourceWithComplexType);
eventSourceDefinitionVisitor.Visit(syntaxTree.GetRoot());

// act
EventSourceTemplateEngine templateEngine = new EventSourceTemplateEngine(template);
string eventSourceCode = templateEngine.Generate(eventSourceDefinitionVisitor.EventSource);

// assert
syntaxTree = CSharpSyntaxTree.ParseText(eventSourceCode);

EventSourceVisitor eventSourceVisitor = new EventSourceVisitor();
eventSourceVisitor.Visit(syntaxTree.GetRoot());

eventSourceVisitor.Classes.Should().HaveCount(1);
eventSourceVisitor.Classes.First().Should()
.Be(eventSourceDefinitionVisitor.EventSource.Name);

eventSourceVisitor.MethodSignatures[0].Should()
.Be("One(Exception ex, Guid correlationId, string messageType, string from, string to)");
eventSourceVisitor.MethodSignatures[1].Should()
.Be("One(int applicationId, Guid activityId, String attachmentId, Guid correlationId, string messageType, string from, string to)");
eventSourceVisitor.MethodSignatures[2].Should()
.Be("Two(Guid messageId, Guid correlationId, string messageType, string from, string to)");
eventSourceVisitor.MethodSignatures[3].Should()
.Be("Two(int applicationId, Guid activityId, Guid messageId, Guid correlationId, string messageType, string from, string to)");
eventSourceVisitor.MethodSignatures[4].Should()
.Be("WriteCore(int eventId, int applicationId, Guid activityId, string a, Guid b, string c, string d, string e)");
eventSourceVisitor.MethodSignatures[5].Should()
.Be("WriteCore(int eventId, int applicationId, Guid activityId, Guid a, Guid b, string c, string d, string e)");

eventSourceVisitor.ImportedNamespaces.Count.Should()
.Be(8);
eventSourceVisitor.ImportedNamespaces[0].Should()
.Be("using ChilliCream.Tracing.Abstractions;");
eventSourceVisitor.ImportedNamespaces[1].Should()
.Be("using System;");
eventSourceVisitor.ImportedNamespaces[2].Should()
.Be("using System.Collections.Generic;");
eventSourceVisitor.ImportedNamespaces[3].Should()
.Be("using Gen = System.Generic;");
eventSourceVisitor.ImportedNamespaces[4].Should()
.Be("using System.Linq;");
eventSourceVisitor.ImportedNamespaces[5].Should()
.Be("using static System.Math;");
eventSourceVisitor.ImportedNamespaces[6].Should()
.Be("using System.Text;");
eventSourceVisitor.ImportedNamespaces[7].Should()
.Be("using Tasks = System.Threading.Tasks;");
}

[Fact]
Expand Down Expand Up @@ -120,6 +225,8 @@ public class EventSourceVisitor
public IList<string> ClassDocumentations { get; } = new List<string>();
public IList<string> MethodSignatures { get; } = new List<string>();
public IList<string> MethodDocumentations { get; } = new List<string>();
public IList<string> ImportedNamespaces { get; } = new List<string>();


public override void VisitClassDeclaration(ClassDeclarationSyntax node)
{
Expand Down Expand Up @@ -155,6 +262,13 @@ public override void VisitMethodDeclaration(MethodDeclarationSyntax node)
base.VisitMethodDeclaration(node);
}

public override void VisitUsingDirective(UsingDirectiveSyntax node)
{
ImportedNamespaces.Add(node.ToString());

base.VisitUsingDirective(node);
}

private static string GetDocumentationXml(CSharpSyntaxNode syntaxNode)
{
SyntaxTrivia documentation = syntaxNode.GetLeadingTrivia()
Expand Down
Loading