-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: extract type validation logic to the common library #28
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/CompositeKey.Analyzers.Common.UnitTests/CompositeKey.Analyzers.Common.UnitTests.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
63 changes: 63 additions & 0 deletions
63
src/CompositeKey.Analyzers.Common.UnitTests/TestHelpers/CompilationTestHelper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
DrBarnabus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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"); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.