Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix but and remove namespace fix #8

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,52 @@ public void TestMethod()
CSharpAnalyzerVerifier<NUnitToShouldlyConverterAnalyzer, NUnitVerifier>
.Diagnostic(NUnitToShouldlyConverterAnalyzer.Rule)
.WithSpan(19, 13, 19, 34));


await codeFixTest.RunAsync(CancellationToken.None);
}


[Test]
public async Task TestConversion2()
{
var test = @"
using NUnit.Framework;
using Shouldly;

namespace TestNamespace
{
public class TestClass
{
[Test]
public void TestMethod()
{
int i = 0;
Assert.IsTrue(i == 0);
}
}
}";

var expected = @"
using NUnit.Framework;
using Shouldly;

namespace TestNamespace
{
public class TestClass
{
[Test]
public void TestMethod()
{
int i = 0;
(i == 0).ShouldBeTrue();
}
}
}";
var codeFixTest = new CodeFixTest(test, expected,
CSharpAnalyzerVerifier<NUnitToShouldlyConverterAnalyzer, NUnitVerifier>
.Diagnostic(NUnitToShouldlyConverterAnalyzer.Rule)
.WithSpan(13, 13, 13, 35));

await codeFixTest.RunAsync(CancellationToken.None);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(NUnitToShouldlyConverterAnalyzer.DiagnosticId);

public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
public override async Task RegisterCodeFixesAsync(CodeFixContext context)

Check warning on line 24 in src/Shouldly.FromAssert/NUnitAssertToShouldyConverterCodeFixProvider.cs

View workflow job for this annotation

GitHub Actions / Build Package

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 24 in src/Shouldly.FromAssert/NUnitAssertToShouldyConverterCodeFixProvider.cs

View workflow job for this annotation

GitHub Actions / Build Package

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
var diagnostic = context.Diagnostics.First(x => x.Id == NUnitToShouldlyConverterAnalyzer.DiagnosticId);
context.RegisterCodeFix(
Expand All @@ -36,7 +36,7 @@
CancellationToken cancellationToken)
{
var editor = await DocumentEditor.CreateAsync(context.Document, cancellationToken);
var shouldyNamespace = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("Shouldly"));
//var shouldyNamespace = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("Shouldly"));

var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var diagnosticSpan = diagnostic.Location.SourceSpan;
Expand All @@ -56,7 +56,7 @@
var newExpression = SyntaxFactory.ParseExpression($"{invocation.GetLeadingTrivia().ToFullString()}{underTest}.ShouldBe({expectedValue})");

editor.ReplaceNode(expression.Parent, newExpression);
editor.AddUsingDirective(shouldyNamespace);
//editor.AddUsingDirective(shouldyNamespace);

return editor.GetChangedDocument();
}
Expand All @@ -74,7 +74,7 @@
var newExpression = SyntaxFactory.ParseExpression($"{invocation2.GetLeadingTrivia().ToFullString()}{underTest}.{should}({expected})");

editor.ReplaceNode(expression.Parent, newExpression);
editor.AddUsingDirective(shouldyNamespace);
//editor.AddUsingDirective(shouldyNamespace);

return editor.GetChangedDocument();
}
Expand All @@ -87,11 +87,13 @@
invocation1.ArgumentList.Arguments.Count == 1)
{
var underTest = invocation1.ArgumentList.Arguments[0].Expression;

var should = NUnitToShouldlyConverterAnalyzer.ListOfSingleParameterMethods[methodAccess1.Name.Identifier.ValueText];
var newExpression = SyntaxFactory.ParseExpression($"{invocation1.GetLeadingTrivia().ToFullString()}{underTest}.{should}()");
var str = ContainsOperator(underTest) ? "(" + underTest + ")" : underTest.ToString();
var newExpression = SyntaxFactory.ParseExpression($"{invocation1.GetLeadingTrivia().ToFullString()}{str}.{should}()");

editor.ReplaceNode(expression.Parent, newExpression);
editor.AddUsingDirective(shouldyNamespace);
//editor.AddUsingDirective(shouldyNamespace);

return editor.GetChangedDocument();
}
Expand All @@ -108,12 +110,26 @@
var newExpression = SyntaxFactory.ParseExpression($"{identifierNameT.GetLeadingTrivia().ToFullString()}Should.{thrower}<{genericName}>({underTest})");

editor.ReplaceNode(expression.Parent, newExpression);
editor.AddUsingDirective(shouldyNamespace);
//editor.AddUsingDirective(shouldyNamespace);

return editor.GetChangedDocument();
}
return context.Document;
}


private bool ContainsOperator(ExpressionSyntax expression)
{
switch (expression)
{
case BinaryExpressionSyntax _:
case PrefixUnaryExpressionSyntax _:
case PostfixUnaryExpressionSyntax _:
case AssignmentExpressionSyntax _:
case ConditionalExpressionSyntax _:
return true;
default:
return false;
}
}
}
}
28 changes: 13 additions & 15 deletions src/Shouldly.FromAssert/Shouldly.FromAssert.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.1</TargetFrameworks>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
Expand All @@ -10,7 +9,7 @@
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<IncludeBuildOutput>false</IncludeBuildOutput>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>
<PropertyGroup>
Expand All @@ -22,20 +21,19 @@
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<Description>A set of code fixes to migrate code from NUnit Asserts to Shouldly Shoulds.</Description>
</PropertyGroup>
<ItemGroup>
<None Include="bin\Release\netstandard2.1\Shouldly.FromAssert.dll" Pack="true" PackagePath="analyzers\dotnet\cs" Visible="false" />
<None Include="bin\Release\netstandard2.1\**\Shouldly.FromAssert.resources.dll" Pack="true" PackagePath="analyzers\dotnet\cs" Visible="false" />
<None Include="bin\Release\netstandard2.1\Shouldly.FromAssert.pdb" Pack="true" PackagePath="analyzers\dotnet\cs" Visible="false" />

<ItemGroup>
<None Include="bin\$(Configuration)\netstandard2.1\Shouldly.FromAssert.dll" Pack="true" PackagePath="analyzers\dotnet\cs" Visible="false" />
<None Include="bin\$(Configuration)\netstandard2.1\**\Shouldly.FromAssert.resources.dll" Pack="true" PackagePath="analyzers\dotnet\cs" Visible="false" />
<None Include="bin\$(Configuration)\netstandard2.1\Shouldly.FromAssert.pdb" Pack="true" PackagePath="analyzers\dotnet\cs" Visible="false" />
</ItemGroup>
<ItemGroup>
<None Include="tools\install.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="tools\uninstall.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Include="tools\install.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="tools\uninstall.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
<PrivateAssets>all</PrivateAssets>
Expand Down
Loading