Skip to content

Commit

Permalink
defined first spec for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb committed Dec 27, 2023
1 parent 7701c67 commit 2d4ab20
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<Copyright>Copyright © 2023 Your Company</Copyright>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<NoWarn>$(NoWarn);CS1591;NU1701;CA1707;</NoWarn>
<VersionPrefix>1.0.0</VersionPrefix>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
Expand Down
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

<!-- Testing Utilities -->
<ItemGroup>
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.XUnit" Version="1.1.1" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" Version="1.1.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageVersion Include="xunit" Version="2.5.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.0" />
Expand Down
9 changes: 9 additions & 0 deletions src/Akka.Analyzers.Tests/Akka.Analyzers.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="FluentAssertions"/>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Akka.Analyzers\Akka.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
</ItemGroup>

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

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// -----------------------------------------------------------------------
// <copyright file="MustNotUseNewKeywordOnActorSpecs.cs" company="Akka.NET Project">
// Copyright (C) 2015-2023 .NET Petabridge, LLC
// </copyright>
// -----------------------------------------------------------------------

using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Testing.Verifiers;

namespace Akka.Analyzers.Tests.Analyzers.AK1000;

public class AkkaActorInstantiationAnalyzerTests
{
[Fact]
public async Task Analyzer_Should_Not_Report_Diagnostic_For_Valid_Usage()
{
var testCode = @"
using Akka.Actor;
class MyActor : ActorBase { }
class Test
{
void Method()
{
ActorSystem sys = ActorSystem.Create(""MySys"");
Props props = Props.Create(() => new MyActor());
IActorRef realActorInstance = sys.ActorOf(props);
}
}";
await new CSharpAnalyzerTest<MustNotUseNewKeywordOnActorsAnalyzer, XUnitVerifier>
{
TestCode = testCode,
}.RunAsync().ConfigureAwait(true);
}

[Fact]
public async Task Analyzer_Should_Report_Diagnostic_For_Invalid_Usage()
{
var testCode = @"
using Akka.Actor;
class MyActor : ActorBase { }
class Test
{
void Method()
{
MyActor actorInstance = new MyActor();
}
}";

await new CSharpAnalyzerTest<MustNotUseNewKeywordOnActorsAnalyzer, XUnitVerifier>
{
TestCode = testCode,
ExpectedDiagnostics =
{
// The diagnostic expected to be raised by the analyzer
DiagnosticResult.CompilerError("AkkaActorInstantiation").WithSpan(10, 31, 10, 45).WithArguments("MyActor"),
},
}.RunAsync().ConfigureAwait(false);
}
}
9 changes: 0 additions & 9 deletions src/Akka.Analyzers.Tests/UnitTest1.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// -----------------------------------------------------------------------
// <copyright file="MustNotUseNewKeywordOnActors.cs" company="Akka.NET Project">
// <copyright file="MustNotUseNewKeywordOnActorsAnalyzer.cs" company="Akka.NET Project">
// Copyright (C) 2015-2023 .NET Petabridge, LLC
// </copyright>
// -----------------------------------------------------------------------
Expand All @@ -12,7 +12,7 @@
namespace Akka.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class MustNotUseNewKeywordOnActors() : AkkaDiagnosticAnalyzer(RuleDescriptors.Ak1000DoNotNewActors)
public class MustNotUseNewKeywordOnActorsAnalyzer() : AkkaDiagnosticAnalyzer(RuleDescriptors.Ak1000DoNotNewActors)
{
public override void AnalyzeCompilation(CompilationStartAnalysisContext context, AkkaContext akkaContext)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Akka.Analyzers/Properties/Friends.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Akka.Analyzers.Tests")]

0 comments on commit 2d4ab20

Please sign in to comment.