Add test console app and fix generator not running during compile time.

This commit is contained in:
Mohammadreza Taikandi 2020-12-21 10:20:29 +00:00
parent 85099ed080
commit 1968184e77
10 changed files with 93 additions and 13 deletions

View File

@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MapTo", "MapTo\MapTo.csproj
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MapToTests", "MapToTests\MapToTests.csproj", "{797DA57B-AC7E-468B-8799-44C5A574C0E3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestConsoleApp", "TestConsoleApp\TestConsoleApp.csproj", "{5BE2551A-9EF9-42FA-B6D1-5B5E6A90CC85}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -18,5 +20,9 @@ Global
{797DA57B-AC7E-468B-8799-44C5A574C0E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{797DA57B-AC7E-468B-8799-44C5A574C0E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{797DA57B-AC7E-468B-8799-44C5A574C0E3}.Release|Any CPU.Build.0 = Release|Any CPU
{5BE2551A-9EF9-42FA-B6D1-5B5E6A90CC85}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5BE2551A-9EF9-42FA-B6D1-5B5E6A90CC85}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5BE2551A-9EF9-42FA-B6D1-5B5E6A90CC85}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5BE2551A-9EF9-42FA-B6D1-5B5E6A90CC85}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq;
using MapTo.Extensions;
using MapTo.Models;
using Microsoft.CodeAnalysis;
@ -7,6 +6,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace MapTo
{
[Generator]
public class MapToGenerator : ISourceGenerator
{
/// <inheritdoc />

View File

@ -1,17 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>MapTo</AssemblyName>
<Description>Generates mapping code between two types using Roslyn code generator.</Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<IncludeSymbols>true</IncludeSymbols>
<NoWarn>NU5128</NoWarn>
<PackageId>MapTo</PackageId>
<PackageProjectUrl>https://github.com/mrtaikandi/mapto</PackageProjectUrl>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageVersion>$(Version)</PackageVersion>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<RepositoryUrl>https://github.com/mrtaikandi/mapto</RepositoryUrl>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<RootNamespace>MapTo</RootNamespace>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>9</LangVersion>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.1">
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.8.0" />
</ItemGroup>
<ItemGroup>
<None Remove="bin\Debug\netstandard2.0\MapTo.dll" />
</ItemGroup>
<ItemGroup>
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>
</Project>

View File

@ -37,7 +37,7 @@ namespace MapTo
}
";
context.AddSource("MapFromAttribute", SourceText.From(source, Encoding.UTF8));
context.AddSource("MapFromAttribute.g.cs", source);
}
internal static (string source, string hintName) GenerateSource(MapModel model)
@ -81,7 +81,7 @@ namespace MapTo
// End namespace declaration
.AppendClosingBracket();
return (builder.ToString(), $"{model.ClassName}.cs");
return (builder.ToString(), $"{model.ClassName}.g.cs");
}
private static StringBuilder GenerateUsings(this StringBuilder builder, MapModel model)

View File

@ -24,10 +24,10 @@ namespace MapToTests
internal static void ShouldBeSuccessful(this ImmutableArray<Diagnostic> diagnostics)
{
Assert.False(diagnostics.Any(d => d.Severity >= DiagnosticSeverity.Warning), $"Failed: {diagnostics.FirstOrDefault()?.GetMessage()}");
Assert.False(diagnostics.Any(d => d.Severity >= DiagnosticSeverity.Warning), $"Failed: {Environment.NewLine}{string.Join($"{Environment.NewLine}- ", diagnostics.Select(c => c.GetMessage()))}");
}
internal static (Compilation compilation, ImmutableArray<Diagnostic> diagnostics) GetOutputCompilation(string source)
internal static (Compilation compilation, ImmutableArray<Diagnostic> diagnostics) GetOutputCompilation(string source, bool assertCompilation = false)
{
var syntaxTree = CSharpSyntaxTree.ParseText(source);
var references = AppDomain.CurrentDomain.GetAssemblies()
@ -37,9 +37,12 @@ namespace MapToTests
var compilation = CSharpCompilation.Create("foo", new[] { syntaxTree }, references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
// NB: Uncomment this line if you want to fail tests when the injected program isn't valid _before_ running generators
// var compileDiagnostics = compilation.GetDiagnostics();
// Assert.False(compileDiagnostics.Any(d => d.Severity == DiagnosticSeverity.Error), "Failed: " + compileDiagnostics.FirstOrDefault()?.GetMessage());
if (assertCompilation)
{
// NB: fail tests when the injected program isn't valid _before_ running generators
var compileDiagnostics = compilation.GetDiagnostics();
Assert.False(compileDiagnostics.Any(d => d.Severity == DiagnosticSeverity.Error), $"Failed: {Environment.NewLine}{string.Join($"{Environment.NewLine}- ", compileDiagnostics.Select(c => c.GetMessage()))}");
}
ISourceGenerator generator = new MapToGenerator();
var driver = CSharpGeneratorDriver.Create(generator);

View File

@ -66,9 +66,11 @@ namespace MapTo
{
// Arrange
const string source = @"
using MapTo;
namespace Test
{
[MapFrom(typeof(Baz)]
[MapFrom(typeof(Baz))]
public partial class Foo
{
@ -113,7 +115,7 @@ namespace Test
const string source = @"
namespace Test
{
[MapTo.MapFrom(typeof(Baz)]
[MapTo.MapFrom(typeof(Baz))]
public partial class Foo
{
@ -248,6 +250,7 @@ using Bazaar;
{
var builder = new StringBuilder();
builder.AppendLine($@"
{(includeAttributeNamespace ? string.Empty : "using MapTo;")}
namespace Test
{{
{(sourceClassNamespace != "Test" && !includeAttributeNamespace ? $"using {sourceClassNamespace};": string.Empty)}

View File

@ -0,0 +1,13 @@
namespace TestConsoleApp.Data.Models
{
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName => $"{FirstName} {LastName}";
}
}

13
TestConsoleApp/Program.cs Normal file
View File

@ -0,0 +1,13 @@
using System;
using TestConsoleApp.ViewModels;
namespace TestConsoleApp
{
class Program
{
static void Main(string[] args)
{
var userViewModel = User.From(new Data.Models.User());
}
}
}

View File

@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\MapTo\MapTo.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,11 @@

// using MapTo;
namespace TestConsoleApp.ViewModels
{
[MapTo.MapFrom(typeof(Data.Models.User))]
public partial class User
{
public string FirstName { get; }
}
}