Add source type namespace to usings if needed.
This commit is contained in:
parent
1f517dd196
commit
450de3dedc
|
@ -66,6 +66,7 @@ namespace MapTo
|
|||
classModifiers: classSyntax.GetClassModifier(),
|
||||
className: classSyntax.GetClassName(),
|
||||
properties: classSymbol.GetAllMembersOfType<IPropertySymbol>(),
|
||||
destinationNamespace: destinationTypeSymbol.ContainingNamespace.Name,
|
||||
destinationClassName: destinationTypeSymbol.Name,
|
||||
destinationTypeProperties: destinationTypeSymbol.GetAllMembersOfType<IPropertySymbol>());
|
||||
}
|
||||
|
|
|
@ -6,12 +6,13 @@ namespace MapTo.Models
|
|||
{
|
||||
public class MapModel
|
||||
{
|
||||
public MapModel(string? ns, string classModifiers, string className, IEnumerable<IPropertySymbol> properties, string destinationClassName, IEnumerable<IPropertySymbol> destinationTypeProperties)
|
||||
public MapModel(string? ns, string classModifiers, string className, IEnumerable<IPropertySymbol> properties, string destinationNamespace, string destinationClassName, IEnumerable<IPropertySymbol> destinationTypeProperties)
|
||||
{
|
||||
Namespace = ns;
|
||||
ClassModifiers = classModifiers;
|
||||
ClassName = className;
|
||||
Properties = properties;
|
||||
DestinationNamespace = destinationNamespace;
|
||||
DestinationClassName = destinationClassName;
|
||||
DestinationTypeProperties = destinationTypeProperties;
|
||||
}
|
||||
|
@ -24,6 +25,8 @@ namespace MapTo.Models
|
|||
|
||||
public IEnumerable<IPropertySymbol> Properties { get; }
|
||||
|
||||
public string DestinationNamespace { get; }
|
||||
|
||||
public string DestinationClassName { get; }
|
||||
|
||||
public IEnumerable<IPropertySymbol> DestinationTypeProperties { get; }
|
||||
|
|
|
@ -47,7 +47,7 @@ namespace MapTo
|
|||
|
||||
builder
|
||||
.AppendFileHeader()
|
||||
.GenerateUsings();
|
||||
.GenerateUsings(model);
|
||||
|
||||
// Namespace declaration
|
||||
builder
|
||||
|
@ -75,17 +75,22 @@ namespace MapTo
|
|||
return (builder.ToString(), $"{model.ClassName}.cs");
|
||||
}
|
||||
|
||||
private static StringBuilder GenerateUsings(this StringBuilder builder)
|
||||
private static StringBuilder GenerateUsings(this StringBuilder builder, MapModel model)
|
||||
{
|
||||
return builder
|
||||
.AppendLine("using System;")
|
||||
.AppendLine();
|
||||
builder.AppendLine("using System;");
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(model.DestinationNamespace) && model.Namespace != model.DestinationNamespace)
|
||||
{
|
||||
builder.AppendFormat("using {0};", model.DestinationNamespace).AppendLine();
|
||||
}
|
||||
|
||||
return builder.AppendLine();
|
||||
}
|
||||
|
||||
|
||||
private static StringBuilder GenerateConstructor(this StringBuilder builder, MapModel model, out List<IPropertySymbol> mappedProperties)
|
||||
{
|
||||
var destinationClassParameterName = model.DestinationClassName.ToCamelCase();
|
||||
|
||||
|
||||
builder
|
||||
.PadLeft(Indent2)
|
||||
.AppendFormat("public {0}({1} {2})", model.ClassName, model.DestinationClassName, destinationClassParameterName)
|
||||
|
@ -120,7 +125,7 @@ namespace MapTo
|
|||
.AppendFormat("public static {0} From({1} {2})", model.ClassName, model.DestinationClassName, destinationClassParameterName)
|
||||
.AppendOpeningBracket(Indent2)
|
||||
.PadLeft(Indent3)
|
||||
.AppendFormat("return {0} == null ? null : new {1}({0});", destinationClassParameterName, model.ClassName )
|
||||
.AppendFormat("return {0} == null ? null : new {1}({0});", destinationClassParameterName, model.ClassName)
|
||||
.AppendClosingBracket(Indent2);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System.Linq;
|
||||
using System.Text;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
@ -196,12 +197,36 @@ namespace Test
|
|||
compilation.SyntaxTrees.Count().ShouldBe(3);
|
||||
compilation.SyntaxTrees.Last().ToString().ShouldContain(expectedResult.Trim());
|
||||
}
|
||||
|
||||
private static string GetSourceText(bool includeAttributeNamespace = false)
|
||||
|
||||
[Fact]
|
||||
public void When_SourceTypeHasDifferentNamespace_Should_AddToUsings()
|
||||
{
|
||||
return @$"
|
||||
// Arrange
|
||||
var source = GetSourceText(sourceClassNamespace: "Bazaar");
|
||||
|
||||
const string expectedResult = @"
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Bazaar;
|
||||
";
|
||||
|
||||
// Act
|
||||
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
|
||||
|
||||
// Assert
|
||||
diagnostics.ShouldBeSuccessful();
|
||||
compilation.SyntaxTrees.Count().ShouldBe(3);
|
||||
compilation.SyntaxTrees.Last().ToString().ShouldStartWith(expectedResult.Trim());
|
||||
}
|
||||
|
||||
private static string GetSourceText(bool includeAttributeNamespace = false, string sourceClassNamespace = "Test")
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
builder.AppendLine($@"
|
||||
namespace Test
|
||||
{{
|
||||
{(sourceClassNamespace != "Test" && !includeAttributeNamespace ? $"using {sourceClassNamespace};": string.Empty)}
|
||||
|
||||
{(includeAttributeNamespace ? "[MapTo.MapFrom(typeof(Baz))]" : "[MapFrom(typeof(Baz))]")}
|
||||
public partial class Foo
|
||||
{{
|
||||
|
@ -209,7 +234,12 @@ namespace Test
|
|||
public int Prop2 {{ get; }}
|
||||
public int Prop3 {{ get; }}
|
||||
}}
|
||||
}}
|
||||
");
|
||||
|
||||
builder.AppendLine($@"
|
||||
namespace {sourceClassNamespace}
|
||||
{{
|
||||
public class Baz
|
||||
{{
|
||||
public int Prop1 {{ get; set; }}
|
||||
|
@ -217,7 +247,9 @@ namespace Test
|
|||
public int Prop3 {{ get; set; }}
|
||||
}}
|
||||
}}
|
||||
";
|
||||
");
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue