-
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.
* refactor * feat(code-fix): added new code fix and test refactor
- Loading branch information
1 parent
e52d3c4
commit 624e304
Showing
19 changed files
with
310 additions
and
111 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
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
71 changes: 65 additions & 6 deletions
71
...mmutableAnalyzer.Tests/PropertyAnalyzersTests/SetAccessorTests/SetAccessorCodeFixTests.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 |
---|---|---|
@@ -1,23 +1,82 @@ | ||
using System.Threading.Tasks; | ||
using System; | ||
using System.Threading.Tasks; | ||
using ImmutableAnalyzer.PropertyAnalyzers.SetAccessor; | ||
using ImmutableAnalyzer.Tests.Factories; | ||
using Microsoft.CodeAnalysis.Testing; | ||
using Xunit; | ||
using Verifier = Microsoft.CodeAnalysis.CSharp.Testing.XUnit.CodeFixVerifier< | ||
ImmutableAnalyzer.PropertyAnalyzers.SetAccessor.SetAccessorAnalyzer, | ||
ImmutableAnalyzer.PropertyAnalyzers.SetAccessor.SetAccessorCodeFixProvider | ||
>; | ||
using SetAccessorCodeFixCodeFixTest = Microsoft.CodeAnalysis.CSharp.Testing.CSharpCodeFixTest< | ||
ImmutableAnalyzer.PropertyAnalyzers.SetAccessor.SetAccessorAnalyzer, | ||
ImmutableAnalyzer.PropertyAnalyzers.SetAccessor.SetAccessorCodeFixProvider, | ||
Microsoft.CodeAnalysis.Testing.Verifiers.XUnitVerifier | ||
>; | ||
|
||
namespace ImmutableAnalyzer.Tests.PropertyAnalyzersTests.SetAccessorTests; | ||
|
||
public class SetAccessorCodeFixTests | ||
{ | ||
[Fact] | ||
public async Task Immutable_class_with_public_set_accessor_should_be_fixed_with_init_keyword() | ||
{ | ||
var test = CreateTest(SetAccessorCodeFixProvider.FixStrategy.ToInit); | ||
test.CodeActionValidationMode = CodeActionValidationMode.None; | ||
|
||
var exception = await Record.ExceptionAsync(() => test.RunAsync()); | ||
Assert.Null(exception); | ||
} | ||
|
||
[Fact] | ||
public async Task Immutable_class_with_public_set_accessor_should_be_fixed_with_private_set() | ||
{ | ||
var test = CreateTest(SetAccessorCodeFixProvider.FixStrategy.ToPrivate); | ||
|
||
var exception = await Record.ExceptionAsync(() => test.RunAsync()); | ||
Assert.Null(exception); | ||
} | ||
|
||
[Fact] | ||
public async Task Immutable_class_with_public_set_accessor_should_be_removed() | ||
{ | ||
var test = CreateTest(SetAccessorCodeFixProvider.FixStrategy.Remove); | ||
|
||
var exception = await Record.ExceptionAsync(() => test.RunAsync()); | ||
Assert.Null(exception); | ||
} | ||
|
||
/// <summary> | ||
/// Creates testcase for <see cref="SetAccessorAnalyzer"/> and <see cref="SetAccessorCodeFixProvider"/>. | ||
/// </summary> | ||
/// <param name="fixStrategy">Code fix strategy.</param> | ||
/// <returns>Tes case.</returns> | ||
/// <exception cref="ArgumentOutOfRangeException">Raises when <see cref="fixStrategy"/> is invalid.</exception> | ||
private static SetAccessorCodeFixCodeFixTest CreateTest(SetAccessorCodeFixProvider.FixStrategy fixStrategy) | ||
{ | ||
var source = SourceFactory.ImmutableClassWithPropertyAccessor("set", out var line, out var column); | ||
var fixedSource = SourceFactory.ImmutableClassWithPropertyAccessor("private set", out _, out _); | ||
var expectedDiagnostic = Verifier.Diagnostic().WithLocation(line, column).WithArguments("set"); | ||
|
||
var fixedSource = fixStrategy switch | ||
{ | ||
SetAccessorCodeFixProvider.FixStrategy.Remove => | ||
SourceFactory.ImmutableClassWithGetOnlyPropertyAccessor(), | ||
|
||
SetAccessorCodeFixProvider.FixStrategy.ToInit => | ||
SourceFactory.ImmutableClassWithPropertyAccessor("init", out _, out _), | ||
|
||
SetAccessorCodeFixProvider.FixStrategy.ToPrivate => | ||
SourceFactory.ImmutableClassWithPropertyAccessor("private set", out _, out _), | ||
|
||
_ => throw new ArgumentOutOfRangeException(nameof(fixStrategy), fixStrategy, null) | ||
}; | ||
|
||
var expected = Verifier.Diagnostic() | ||
.WithLocation(line, column) | ||
.WithArguments("set"); | ||
await Verifier.VerifyCodeFixAsync(source, expected, fixedSource).ConfigureAwait(false); | ||
return new SetAccessorCodeFixCodeFixTest | ||
{ | ||
TestCode = source, | ||
FixedCode = fixedSource, | ||
CodeActionIndex = (int) fixStrategy, | ||
ExpectedDiagnostics = {expectedDiagnostic} | ||
}; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +0,0 @@ | ||
## Release 1.2.0 | ||
|
||
### New Rules | ||
|
||
Rule ID | Category | Severity | Notes | ||
--------|----------|----------|------------------------------------------------------------- | ||
|
||
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
Oops, something went wrong.