-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #140 from dragonfruitnetwork/2024-sourcegen-rewrite
2024 Source Generator Rewrite
- Loading branch information
Showing
130 changed files
with
3,384 additions
and
2,803 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
122 changes: 122 additions & 0 deletions
122
DragonFruit.Data.Roslyn.Tests/ApiRequestAnalyzerTests.cs
This file contains 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,122 @@ | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using DragonFruit.Data.Roslyn.Fixes; | ||
using Microsoft.CodeAnalysis.CSharp.Testing; | ||
using Microsoft.CodeAnalysis.Testing; | ||
using Xunit; | ||
|
||
namespace DragonFruit.Data.Roslyn.Tests | ||
{ | ||
public class ApiRequestAnalyzerTests | ||
{ | ||
private readonly string _testDataPath; | ||
|
||
public ApiRequestAnalyzerTests() | ||
{ | ||
_testDataPath = Path.Combine(GetSolutionRoot(), "DragonFruit.Data.Roslyn.Tests", "_TestData"); | ||
} | ||
|
||
[Fact] | ||
public async Task TestNonPartialClassDetectionAndFix() | ||
{ | ||
var test = new CSharpCodeFixTest<ApiRequestAnalyzer, ApiRequestClassFixProvider, DefaultVerifier> | ||
{ | ||
TestCode = await File.ReadAllTextAsync(Path.Combine(_testDataPath, "DA0001.cs")), | ||
FixedCode = await File.ReadAllTextAsync(Path.Combine(_testDataPath, "DA0001.Fix.cs")), | ||
ExpectedDiagnostics = { DiagnosticResult.CompilerError(ApiRequestAnalyzer.PartialClassRule.Id).WithSpan(8, 18, 8, 24).WithArguments("DA0001") } | ||
}; | ||
|
||
await PerformTest(test); | ||
} | ||
|
||
public static readonly TheoryData<string, DiagnosticResult[]> AnalyzerDetectionData = new() | ||
{ | ||
{ | ||
// DA0002: nested class | ||
"DA0002.cs", new[] { DiagnosticResult.CompilerError(ApiRequestAnalyzer.NestedClassNotAllowedRule.Id).WithSpan(10, 30, 10, 40).WithArguments("DA0002_Req") } | ||
}, | ||
{ | ||
// DA0003: property has no getter | ||
"DA0003.cs", new[] { DiagnosticResult.CompilerWarning(ApiRequestAnalyzer.PropertyNoGetterRule.Id).WithSpan(14, 20, 14, 26).WithArguments("Param2") } | ||
}, | ||
{ | ||
// DA0004: property or method not in apirequest | ||
"DA0004.cs", new[] | ||
{ | ||
// method not in apirequest | ||
DiagnosticResult.CompilerWarning(ApiRequestAnalyzer.PropertyOrMethodNotInApiRequestRule.Id).WithSpan(11, 23, 11, 32).WithArguments("NotAParam"), | ||
|
||
// property not in apirequest | ||
DiagnosticResult.CompilerWarning(ApiRequestAnalyzer.PropertyOrMethodNotInApiRequestRule.Id).WithSpan(14, 20, 14, 28).WithArguments("TestData"), | ||
} | ||
}, | ||
{ | ||
// DA0005: property or method is inaccessible | ||
"DA0005.cs", new[] | ||
{ | ||
// private getter with public setter | ||
DiagnosticResult.CompilerWarning(ApiRequestAnalyzer.PropertyOrMethodInaccessibleRule.Id).WithSpan(11, 19, 11, 28).WithArguments("Parameter"), | ||
|
||
// private method | ||
DiagnosticResult.CompilerWarning(ApiRequestAnalyzer.PropertyOrMethodInaccessibleRule.Id).WithSpan(14, 18, 14, 32).WithArguments("IsParameterSet"), | ||
} | ||
}, | ||
{ | ||
// DA0006: method returns void | ||
"DA0006.cs", new[] | ||
{ | ||
DiagnosticResult.CompilerError(ApiRequestAnalyzer.MethodReturnsVoidRule.Id).WithSpan(11, 17, 11, 28).WithArguments("GetUserData") | ||
} | ||
}, | ||
{ | ||
// DA0007: method has parameters | ||
"DA0007.cs", new[] | ||
{ | ||
DiagnosticResult.CompilerError(ApiRequestAnalyzer.MethodHasParametersRule.Id).WithSpan(11, 19, 11, 25).WithArguments("UserId") | ||
} | ||
} | ||
}; | ||
|
||
[Theory] | ||
[MemberData(nameof(AnalyzerDetectionData))] | ||
public async Task TestRequestAnalysisDetections(string fileName, DiagnosticResult[] diagnosticResults) | ||
{ | ||
var test = new CSharpAnalyzerTest<ApiRequestAnalyzer, DefaultVerifier> | ||
{ | ||
TestCode = await File.ReadAllTextAsync(Path.Combine(_testDataPath, fileName)) | ||
}; | ||
|
||
test.ExpectedDiagnostics.AddRange(diagnosticResults); | ||
|
||
await PerformTest(test); | ||
} | ||
|
||
private async Task PerformTest(AnalyzerTest<DefaultVerifier> test) | ||
{ | ||
var content = ("Common.cs", await File.ReadAllTextAsync(Path.Combine(_testDataPath, "Common.cs"))); | ||
|
||
// add common.cs to test sources | ||
test.TestState.Sources.Add(content); | ||
|
||
if (test is CodeFixTest<DefaultVerifier> verifier) | ||
{ | ||
verifier.FixedState.Sources.Add(content); | ||
} | ||
|
||
await test.RunAsync(); | ||
} | ||
|
||
private string GetSolutionRoot() | ||
{ | ||
var currentDirectory = Directory.GetCurrentDirectory(); | ||
|
||
while (Directory.EnumerateFiles(currentDirectory).All(x => Path.GetFileName(x) != "DragonFruit.Data.sln")) | ||
{ | ||
currentDirectory = Path.Combine(currentDirectory, ".."); | ||
} | ||
|
||
return Path.GetFullPath(currentDirectory); | ||
} | ||
} | ||
} |
This file contains 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,31 @@ | ||
// DragonFruit.Data Copyright DragonFruit Network | ||
// Licensed under the MIT License. Please refer to the LICENSE file at the root of this project for details | ||
|
||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Scriban; | ||
using Xunit; | ||
|
||
namespace DragonFruit.Data.Roslyn.Tests | ||
{ | ||
public class ApiRequestTemplateTests | ||
{ | ||
[Fact] | ||
public async Task TestTemplateParse() | ||
{ | ||
var assembly = typeof(ApiRequestSourceGenerator).Assembly; | ||
using var template = assembly.GetManifestResourceStream(ApiRequestSourceGenerator.TemplateName); | ||
|
||
Assert.NotNull(template); | ||
|
||
using var templateReader = new StreamReader(template); | ||
var templateText = await templateReader.ReadToEndAsync(); | ||
|
||
Assert.True(templateText.Length > 0); | ||
|
||
var templateAst = Template.ParseLiquid(templateText); | ||
|
||
Assert.False(templateAst.HasErrors); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
DragonFruit.Data.Roslyn.Tests/DragonFruit.Data.Roslyn.Tests.csproj
This file contains 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,30 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" Version="1.1.1"/> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing" Version="1.1.1"/> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="Scriban" Version="5.9.0" /> | ||
<PackageReference Include="xunit" Version="2.6.3" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="_TestData\**" /> | ||
<Compile Remove="_TestData\**"/> | ||
<EmbeddedResource Remove="_TestData\**" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\DragonFruit.Data.Roslyn\DragonFruit.Data.Roslyn.csproj" OutputItemType="Analyzer"/> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains 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,15 @@ | ||
using System; | ||
|
||
namespace DragonFruit.Data | ||
{ | ||
public class ApiRequest; | ||
} | ||
|
||
namespace DragonFruit.Data.Requests | ||
{ | ||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property)] | ||
public class RequestParameterAttribute : Attribute; | ||
|
||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property)] | ||
public class RequestBodyAttribute : Attribute; | ||
} |
This file contains 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,13 @@ | ||
using DragonFruit.Data.Requests; | ||
|
||
namespace DragonFruit.Data.Roslyn.Tests.TestData | ||
{ | ||
/// <summary> | ||
/// Dummy request with no partial class modifier (DA0001) | ||
/// </summary> | ||
public partial class DA0001 : ApiRequest | ||
{ | ||
[RequestParameter] | ||
public string TestParam { get; set; } | ||
} | ||
} |
This file contains 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,13 @@ | ||
using DragonFruit.Data.Requests; | ||
|
||
namespace DragonFruit.Data.Roslyn.Tests.TestData | ||
{ | ||
/// <summary> | ||
/// Dummy request with no partial class modifier (DA0001) | ||
/// </summary> | ||
public class DA0001 : ApiRequest | ||
{ | ||
[RequestParameter] | ||
public string TestParam { get; set; } | ||
} | ||
} |
Oops, something went wrong.