-
Notifications
You must be signed in to change notification settings - Fork 0
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 #18 from protomorphine/feat/record-params
Feat/record params
- Loading branch information
Showing
25 changed files
with
684 additions
and
341 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
27 changes: 27 additions & 0 deletions
27
ImmutableAnalyzer/ImmutableAnalyzer.Tests/AnalyzerTestFactory.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,27 @@ | ||
using System.IO; | ||
using Microsoft.CodeAnalysis.CSharp.Testing; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Testing; | ||
using Microsoft.CodeAnalysis.Testing.Verifiers; | ||
|
||
namespace ImmutableAnalyzer.Tests; | ||
|
||
public static class AnalyzerTestFactory | ||
{ | ||
public static CSharpAnalyzerTest<TAnalyzer, XUnitVerifier> CreateCSharpAnalyzerTest<TAnalyzer>(string source) | ||
where TAnalyzer : DiagnosticAnalyzer, new() | ||
{ | ||
var referenceAssembly = new ReferenceAssemblies("net6.0", | ||
new PackageIdentity("Microsoft.NETCore.App.Ref", "6.0.0"), | ||
Path.Combine("ref", "net6.0")); | ||
|
||
return new CSharpAnalyzerTest<TAnalyzer, XUnitVerifier> | ||
{ | ||
TestState = | ||
{ | ||
Sources = {source}, | ||
ReferenceAssemblies = referenceAssembly | ||
} | ||
}; | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
...tableAnalyzer.Tests/PropertyAnalyzersTests/PropertyTypeTests/PropertyTypeAnalyzerTests.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,87 @@ | ||
using System.Threading.Tasks; | ||
using ImmutableAnalyzer.PropertyAnalyzers.PropertyType; | ||
using Xunit; | ||
using Verifier = | ||
Microsoft.CodeAnalysis.CSharp.Testing.XUnit.AnalyzerVerifier< | ||
ImmutableAnalyzer.PropertyAnalyzers.PropertyType.PropertyTypeAnalyzer>; | ||
|
||
namespace ImmutableAnalyzer.Tests.PropertyAnalyzersTests.PropertyTypeTests; | ||
|
||
/// <summary> | ||
/// Tests for <see cref="PropertyTypeAnalyzer"/> | ||
/// </summary> | ||
public class PropertyTypeAnalyzerTests | ||
{ | ||
[Theory] | ||
[ClassData(typeof(TestData.ImmutableTypes))] | ||
[ClassData(typeof(TestData.ImmutableGenericsTypes))] | ||
public async Task Class_with_immutable_property_should_be_immutable(string property) | ||
{ | ||
var source = SourceFactory.ImmutableClassWithProperty(property, out _, out _); | ||
var test = AnalyzerTestFactory.CreateCSharpAnalyzerTest<PropertyTypeAnalyzer>(source); | ||
|
||
await test.RunAsync().ConfigureAwait(false); | ||
Assert.True(true); // SonarLint S2699 | ||
} | ||
|
||
[Theory] | ||
[ClassData(typeof(TestData.MutableTypes))] | ||
public async Task Class_with_mutable_property_should_not_be_immutable(string property) | ||
{ | ||
var source = SourceFactory.ImmutableClassWithProperty(property, out var line, out var column); | ||
|
||
var expected = Verifier.Diagnostic().WithLocation(line, column).WithArguments(property); | ||
await Verifier.VerifyAnalyzerAsync(source, expected).ConfigureAwait(false); | ||
} | ||
|
||
[Fact] | ||
public async Task Class_with_user_defined_immutable_property_type_should_be_immutable() | ||
{ | ||
const string className = "Person"; | ||
|
||
var source = | ||
SourceFactory.ImmutableClassWithProperty(propertyType: className, out _, out _) + | ||
PureClassWithImmutableProperty(name: className, "[Immutable]"); | ||
|
||
await Verifier.VerifyAnalyzerAsync(source).ConfigureAwait(false); | ||
} | ||
|
||
[Fact] | ||
public async Task Class_with_user_defined_mutable_property_type_should_not_be_immutable() | ||
{ | ||
const string className = "Person"; | ||
|
||
var source = | ||
SourceFactory.ImmutableClassWithProperty(propertyType: className, out var line, out var column) + | ||
PureClassWithImmutableProperty(name: className); | ||
|
||
var expectedDiagnostic = Verifier.Diagnostic().WithLocation(line, column).WithArguments(className); | ||
await Verifier.VerifyAnalyzerAsync(source, expectedDiagnostic).ConfigureAwait(false); | ||
} | ||
|
||
[Fact] | ||
public async Task Class_with_enum_property_type_should_be_immutable() | ||
{ | ||
const string enumName = "TestEnum"; | ||
var source = | ||
SourceFactory.ImmutableClassWithProperty(enumName, out _, out _) + | ||
$"public enum {enumName} {{ Value1, Value2, Value3 }}"; | ||
|
||
await Verifier.VerifyAnalyzerAsync(source); | ||
} | ||
|
||
/// <summary> | ||
/// Creates source code of class (which contains 1 property named `Id` with type `long`) with given name and attributes. | ||
/// </summary> | ||
/// <param name="name">Class name.</param> | ||
/// <param name="attribute">Attributes.</param> | ||
/// <returns>Source code of class.</returns> | ||
private static string PureClassWithImmutableProperty(string name, string attribute = "") => | ||
$@" | ||
{attribute} | ||
public class {name} | ||
{{ | ||
public long Id {{ get; init; }} | ||
}} | ||
"; | ||
} |
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
33 changes: 33 additions & 0 deletions
33
ImmutableAnalyzer/ImmutableAnalyzer.Tests/RecordAnalyzersTests/ParameterTypeAnalyzerTests.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,33 @@ | ||
using System.Threading.Tasks; | ||
using ImmutableAnalyzer.ParameterAnalyzers; | ||
using Xunit; | ||
using Verifier = | ||
Microsoft.CodeAnalysis.CSharp.Testing.XUnit.AnalyzerVerifier< | ||
ImmutableAnalyzer.ParameterAnalyzers.ParameterTypeAnalyzer>; | ||
|
||
namespace ImmutableAnalyzer.Tests.RecordAnalyzersTests; | ||
|
||
public class ParameterTypeAnalyzerTests | ||
{ | ||
[Theory] | ||
[ClassData(typeof(TestData.ImmutableTypes))] | ||
[ClassData(typeof(TestData.ImmutableGenericsTypes))] | ||
public async Task Record_with_immutable_property_should_be_immutable(string property) | ||
{ | ||
var source = SourceFactory.ImmutableRecordWithParameter(property, out _, out _); | ||
var test = AnalyzerTestFactory.CreateCSharpAnalyzerTest<ParameterTypeAnalyzer>(source); | ||
|
||
await test.RunAsync().ConfigureAwait(false); | ||
Assert.True(true); // SonarLint S2699 | ||
} | ||
|
||
[Theory] | ||
[ClassData(typeof(TestData.MutableTypes))] | ||
public async Task Record_with_mutable_property_should_not_be_immutable(string property) | ||
{ | ||
var source = SourceFactory.ImmutableRecordWithParameter(property, out var line, out var column); | ||
|
||
var expected = Verifier.Diagnostic().WithLocation(line, column).WithArguments(property); | ||
await Verifier.VerifyAnalyzerAsync(source, expected).ConfigureAwait(false); | ||
} | ||
} |
Oops, something went wrong.