Skip to content
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
1 change: 1 addition & 0 deletions CompositeKey.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<Project Path="src\CompositeKey\CompositeKey.csproj" Type="Classic C#" />
</Folder>
<Folder Name="/tests/">
<Project Path="src\CompositeKey.Analyzers.Common.UnitTests\CompositeKey.Analyzers.Common.UnitTests.csproj" Type="Classic C#" />
<Project Path="src\CompositeKey.SourceGeneration.FunctionalTests\CompositeKey.SourceGeneration.FunctionalTests.csproj" Type="Classic C#" />
<Project Path="src\CompositeKey.SourceGeneration.UnitTests\CompositeKey.SourceGeneration.UnitTests.csproj" Type="Classic C#" />
</Folder>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<Using Include="Shouldly" />
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="JUnitXml.TestLogger" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Shouldly" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CompositeKey\CompositeKey.csproj" />
<ProjectReference Include="..\CompositeKey.Analyzers.Common\CompositeKey.Analyzers.Common.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace CompositeKey.Analyzers.Common.UnitTests.TestHelpers;

/// <summary>
/// Test helper for creating Roslyn compilation objects for testing validation logic.
/// </summary>
public static class CompilationTestHelper
{
private static readonly Assembly SystemRuntimeAssembly = Assembly.Load(new AssemblyName("System.Runtime"));
private static readonly CSharpParseOptions DefaultParseOptions = new(LanguageVersion.CSharp11);

/// <summary>
/// Creates a C# compilation from source code.
/// </summary>
public static CSharpCompilation CreateCompilation(string source, string assemblyName = "TestAssembly")
{
List<MetadataReference> references =
[
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
MetadataReference.CreateFromFile(typeof(Guid).Assembly.Location),
MetadataReference.CreateFromFile(SystemRuntimeAssembly.Location),
MetadataReference.CreateFromFile(typeof(CompositeKeyAttribute).Assembly.Location),
];

return CSharpCompilation.Create(
assemblyName,
[CSharpSyntaxTree.ParseText(source, DefaultParseOptions)],
references,
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
}

/// <summary>
/// Gets the semantic model and type declaration syntax for the first type in the compilation.
/// </summary>
public static (SemanticModel SemanticModel, TypeDeclarationSyntax TypeDeclaration, INamedTypeSymbol TypeSymbol)
GetFirstTypeInfo(CSharpCompilation compilation, string typeName)
{
var syntaxTree = compilation.SyntaxTrees.First();
var semanticModel = compilation.GetSemanticModel(syntaxTree);

var typeDeclaration = syntaxTree.GetRoot()
.DescendantNodes()
.OfType<TypeDeclarationSyntax>()
.First(t => t.Identifier.ValueText == typeName);

var typeSymbol = semanticModel.GetDeclaredSymbol(typeDeclaration)
?? throw new InvalidOperationException($"Could not get type symbol for {typeName}");

return (semanticModel, typeDeclaration, typeSymbol);
}

/// <summary>
/// Gets the CompositeKeyConstructorAttribute type symbol from the compilation.
/// </summary>
public static INamedTypeSymbol? GetCompositeKeyConstructorAttributeSymbol(CSharpCompilation compilation)
{
return compilation.GetTypeByMetadataName("CompositeKey.CompositeKeyConstructorAttribute");
}
}
Loading