Skip to content

Commit

Permalink
Merge pull request #1 from reegeek/GenericCmd
Browse files Browse the repository at this point in the history
Allow to have 2 messages with same name but not same GenericParameter…
  • Loading branch information
ltrzesniewski authored Aug 19, 2019
2 parents 96c088c + e291995 commit 0b0b645
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Abc.Zebus.MessageDsl.Ast;
using System;
using Abc.Zebus.MessageDsl.Ast;
using Abc.Zebus.MessageDsl.Generator;
using Abc.Zebus.MessageDsl.Tests.TestTools;
using NUnit.Framework;
Expand Down Expand Up @@ -451,6 +452,28 @@ public void should_leave_supplied_protomap()
code.ShouldNotContain("ProtoMap(DisableMap = true)");
}

[Test]
public void should_generate_two_classes_with_same_name_and_different_arity()
{
// Arrange
var msg1 = new MessageDefinition();
msg1.Name = "GenericCommand";
var msg2 = new MessageDefinition();
msg2.Name = msg1.Name;
msg2.GenericParameters.Add("T");
var contracts = new ParsedContracts();
contracts.Messages.Add(msg1);
contracts.Messages.Add(msg2);

// Act
var result = GenerateRaw(contracts);

// Assert
contracts.Errors.ShouldBeEmpty();
result.ShouldContain("public sealed partial class GenericCommand");
result.ShouldContain("public sealed partial class GenericCommand<T>");
}

protected override string GenerateRaw(ParsedContracts contracts) => CSharpGenerator.Generate(contracts);
}
}
19 changes: 19 additions & 0 deletions src/Abc.Zebus.MessageDsl.Tests/MessageDsl/ParsedContractsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,25 @@ public void should_return_source_text()
attr.GetSourceTextInterval().ShouldEqual(new TextInterval(3, 11));
}

[Test]
public void should_return_two_messages_with_same_name_but_different_arity()
{
// Arrange
var name = "Foo";
var genericParam = "T";
var definitionText = $"{name}()" + Environment.NewLine +
$"{name}<{genericParam}>()";

// Act
var contracts = ParseValid(definitionText);

// Assert
contracts.Messages.Count.ShouldEqual(2);
contracts.Messages[0].Name.ShouldEqual(name);
contracts.Messages[1].Name.ShouldEqual(name);
contracts.Messages[1].GenericParameters.ExpectedSingle().ShouldEqual(genericParam);
}

private static ParsedContracts ParseValid(string definitionText)
{
var contracts = Parse(definitionText);
Expand Down
22 changes: 16 additions & 6 deletions src/Abc.Zebus.MessageDsl/Analysis/AstValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,28 @@ private void DetectDuplicateTypes()

foreach (var typeNode in types)
{
var name = ((INamedNode)typeNode).Name;
var nameWithGenericArity = GetNameWithGenericArity(typeNode);

if (!seenTypes.Add(name))
duplicates.Add(name);
if (!seenTypes.Add(nameWithGenericArity))
duplicates.Add(nameWithGenericArity);
}

foreach (var typeNode in types)
{
var name = ((INamedNode)typeNode).Name;
var nameWithGenericArity = GetNameWithGenericArity(typeNode);

if (duplicates.Contains(name))
_contracts.AddError(typeNode.ParseContext, "Duplicate type name: {0}", name);
if (duplicates.Contains(nameWithGenericArity))
_contracts.AddError(typeNode.ParseContext, "Duplicate type name: {0}", nameWithGenericArity);
}

string GetNameWithGenericArity(AstNode node)
{
var name = ((INamedNode)node).Name;
if (node is MessageDefinition messageDef && messageDef.GenericParameters.Count > 0)
{
name = $"{name}`{messageDef.GenericParameters.Count}";
}
return name;
}
}

Expand Down

0 comments on commit 0b0b645

Please sign in to comment.