-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working on defining assembly contexts (#3)
* working on defining assembly contexts Basic Roslyn context to define rules and whether or not they apply to the current unit of compilation. * working on `IAkkaContext` * cleanup * finished base class definitions * cleaning up * starting work on semantic models for first rule * push * have analyzer syntax working * adding test project * defined first spec for testing * working on fixing tests * first happy path spec is passing * fdgdfg * have test setup working correctly * added test cases to check for other ActorBase implementations * fixed `IsActorBaseSubclass`
- Loading branch information
1 parent
cf2ba49
commit d72b1eb
Showing
21 changed files
with
710 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<!-- Download packages referenced by ReferenceAssembliesHelper --> | ||
<PackageDownload Include="Akka" Version="[1.5.14]" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk"/> | ||
<PackageReference Include="xunit"/> | ||
<PackageReference Include="xunit.runner.visualstudio"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="FluentAssertions"/> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Akka.Analyzers\Akka.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Akka.Analyzers\Akka.Analyzers.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
118 changes: 118 additions & 0 deletions
118
src/Akka.Analyzers.Tests/Analyzers/AK1000/MustNotUseNewKeywordOnActorSpecs.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,118 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="MustNotUseNewKeywordOnActorSpecs.cs" company="Akka.NET Project"> | ||
// Copyright (C) 2015-2023 .NET Petabridge, LLC | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using Akka.Analyzers.Tests.Utility; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Testing; | ||
using Microsoft.CodeAnalysis.Testing; | ||
using static Akka.Analyzers.RuleDescriptors; | ||
using Verify = Akka.Analyzers.Tests.Utility.AkkaVerifier<Akka.Analyzers.MustNotUseNewKeywordOnActorsAnalyzer>; | ||
|
||
namespace Akka.Analyzers.Tests.Analyzers.AK1000; | ||
|
||
public class AkkaActorInstantiationAnalyzerTests | ||
{ | ||
public static readonly TheoryData<string> SuccessCases = new() | ||
{ | ||
// ActorBase | ||
""" | ||
using Akka.Actor; | ||
class MyActor : ActorBase { | ||
protected override bool Receive(object message) { | ||
return true; | ||
} | ||
} | ||
class Test | ||
{ | ||
void Method() | ||
{ | ||
ActorSystem sys = ActorSystem.Create("MySys"); | ||
Props props = Props.Create(() => new MyActor()); | ||
IActorRef realActorInstance = sys.ActorOf(props); | ||
} | ||
} | ||
""", | ||
|
||
// UntypedActor | ||
""" | ||
using Akka.Actor; | ||
class MyActor : UntypedActor { | ||
protected override void OnReceive(object message) { | ||
} | ||
} | ||
class Test | ||
{ | ||
void Method() | ||
{ | ||
ActorSystem sys = ActorSystem.Create("MySys"); | ||
Props props = Props.Create(() => new MyActor()); | ||
IActorRef realActorInstance = sys.ActorOf(props); | ||
} | ||
} | ||
""", | ||
|
||
// ReceiveActor | ||
""" | ||
using Akka.Actor; | ||
class MyActor : ReceiveActor { | ||
public MyActor(){ | ||
ReceiveAny(_ => { }); | ||
} | ||
} | ||
class Test | ||
{ | ||
void Method() | ||
{ | ||
ActorSystem sys = ActorSystem.Create("MySys"); | ||
Props props = Props.Create(() => new MyActor()); | ||
IActorRef realActorInstance = sys.ActorOf(props); | ||
} | ||
} | ||
""" | ||
}; | ||
|
||
[Theory] | ||
[MemberData(nameof(SuccessCases))] | ||
public async Task SuccessCase(string testCode) | ||
{ | ||
await Verify.VerifyAnalyzer(testCode).ConfigureAwait(true); | ||
} | ||
|
||
[Fact] | ||
public async Task FailureCase() | ||
{ | ||
const string testCode = """ | ||
using Akka.Actor; | ||
class MyActor : ActorBase { | ||
protected override bool Receive(object message) { | ||
return true; | ||
} | ||
} | ||
class Test | ||
{ | ||
void Method() | ||
{ | ||
MyActor actorInstance = new MyActor(); | ||
} | ||
} | ||
"""; | ||
|
||
var expected = Verify.Diagnostic() | ||
.WithSpan(13, 33, 13, 46) | ||
.WithArguments("MyActor") | ||
.WithSeverity(DiagnosticSeverity.Error); | ||
|
||
await Verify.VerifyAnalyzer(testCode, expected).ConfigureAwait(true); | ||
} | ||
} |
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 @@ | ||
global using Xunit; |
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,61 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="AkkaVerifier.cs" company="Akka.NET Project"> | ||
// Copyright (C) 2015-2023 .NET Petabridge, LLC | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using System.Globalization; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Testing; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Testing; | ||
|
||
namespace Akka.Analyzers.Tests.Utility; | ||
|
||
[SuppressMessage("Design", "CA1000:Do not declare static members on generic types")] | ||
public sealed class AkkaVerifier<TAnalyzer> where TAnalyzer : DiagnosticAnalyzer, new() | ||
{ | ||
/// <summary> | ||
/// Creates a diagnostic result for the diagnostic referenced in <see cref="TAnalyzer"/>. | ||
/// </summary> | ||
public static DiagnosticResult Diagnostic() => | ||
CSharpCodeFixVerifier<TAnalyzer, EmptyCodeFixProvider, DefaultVerifier>.Diagnostic(); | ||
|
||
public static Task VerifyAnalyzer(string source, params DiagnosticResult[] diagnostics) | ||
{ | ||
return VerifyAnalyzer(new[] { source }, diagnostics); | ||
} | ||
|
||
public static Task VerifyAnalyzer(string[] sources, params DiagnosticResult[] diagnostics) | ||
{ | ||
Guard.AssertIsNotNull(sources); | ||
|
||
var test = new AkkaTest(); | ||
#pragma warning disable CA1062 | ||
foreach (var source in sources) | ||
#pragma warning restore CA1062 | ||
test.TestState.Sources.Add(source); | ||
|
||
test.ExpectedDiagnostics.AddRange(diagnostics); | ||
return test.RunAsync(); | ||
} | ||
|
||
private sealed class AkkaTest() : TestBase(ReferenceAssembliesHelper.CurrentAkka); | ||
|
||
private class TestBase : CSharpAnalyzerTest<TAnalyzer, DefaultVerifier> | ||
{ | ||
protected TestBase(ReferenceAssemblies referenceAssemblies) | ||
{ | ||
ReferenceAssemblies = referenceAssemblies; | ||
|
||
// Diagnostics are reported in both normal and generated code | ||
TestBehaviors |= TestBehaviors.SkipGeneratedCodeCheck; | ||
|
||
// Tests that check for messages should run independent of current system culture. | ||
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture; | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Akka.Analyzers.Tests/Utility/ReferenceAssembliesHelper.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,39 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="ReferenceAssembliesHelper.cs" company="Akka.NET Project"> | ||
// Copyright (C) 2015-2023 .NET Petabridge, LLC | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis.Testing; | ||
|
||
namespace Akka.Analyzers.Tests.Utility; | ||
|
||
/// <summary> | ||
/// Used to help us load the correct reference assemblies for the versions of Akka.NET being tested. | ||
/// </summary> | ||
/// <remarks> | ||
/// When making changes to any of these assemblies, make sure to update the 'PackageDownload' elements | ||
/// inside of the 'Akka.Analyzers.Tests.csproj' file. | ||
/// </remarks> | ||
internal static class ReferenceAssembliesHelper | ||
{ | ||
public static readonly ReferenceAssemblies CurrentAkka; | ||
|
||
#pragma warning disable CA1810 | ||
static ReferenceAssembliesHelper() | ||
#pragma warning restore CA1810 | ||
{ | ||
// Can't use ReferenceAssemblies.Net.Net80 because it's too new for Microsoft.CodeAnalysis 4.2.0 | ||
var defaultAssemblies = | ||
new ReferenceAssemblies( | ||
"net8.0", | ||
new PackageIdentity("Microsoft.NETCore.App.Ref", "8.0.0"), | ||
Path.Combine("ref", "net8.0") | ||
); | ||
|
||
CurrentAkka = defaultAssemblies.AddPackages( | ||
[new PackageIdentity("Akka", "1.5.14")] | ||
); | ||
} | ||
} |
Oops, something went wrong.