提交 2a027e26 编写于 作者: C CyrusNajmabadi

Add tests

上级 47427bf2
......@@ -384,6 +384,7 @@
<Compile Include="UseExpressionBody\Refactoring\UseExpressionBodyForOperatorsRefactoringTests.cs" />
<Compile Include="UseExpressionBody\Refactoring\UseExpressionBodyForMethodsRefactoringTests.cs" />
<Compile Include="UseNullPropagation\UseNullPropagationTests.cs" />
<Compile Include="UsePatternMatching\CSharpIsAndCastCheckCodeRefactoringProviderTests.cs" />
<Compile Include="UsePatternMatching\CSharpIsAndCastCheckDiagnosticAnalyzerTests_FixAllTests.cs" />
<Compile Include="UsePatternMatching\CSharpIsAndCastCheckDiagnosticAnalyzerTests.cs" />
<Compile Include="UsePatternMatching\CSharpAsAndNullCheckTests.cs" />
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CSharp.UsePatternMatching;
using Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeRefactorings;
using Roslyn.Test.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.UsePatternMatching
{
public partial class CSharpIsAndCastCheckCodeRefactoringProviderTests : AbstractCSharpCodeActionTest
{
protected override CodeRefactoringProvider CreateCodeRefactoringProvider(Workspace workspace, TestParameters parameters)
=> new CSharpIsAndCastCheckCodeRefactoringProvider();
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
public async Task TestBinaryExpression()
{
await TestInRegularAndScript1Async(
@"class TestFile
{
int i;
bool M(object obj)
{
return [||]obj is TestFile && ((TestFile)obj).i > 0;
}
}",
@"class TestFile
{
int i;
bool M(object obj)
{
return obj is TestFile {|Rename:file|} && file.i > 0;
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
public async Task TestExpressionBody()
{
await TestInRegularAndScript1Async(
@"class TestFile
{
int i;
bool M(object obj)
=> [||]obj is TestFile && ((TestFile)obj).i > 0;
}",
@"class TestFile
{
int i;
bool M(object obj)
=> obj is TestFile {|Rename:file|} && file.i > 0;
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
public async Task TestField()
{
await TestInRegularAndScript1Async(
@"class TestFile
{
int i;
static object obj;
bool M = [||]obj is TestFile && ((TestFile)obj).i > 0;
}",
@"class TestFile
{
int i;
static object obj;
bool M = obj is TestFile {|Rename:file|} && file.i > 0;
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
public async Task TestLambdaBody()
{
await TestInRegularAndScript1Async(
@"
using System;
class TestFile
{
int i;
void Foo(Func<bool> f) { }
bool M(object obj)
=> Foo(() => [||]obj is TestFile && ((TestFile)obj).i > 0, () => obj is TestFile && ((TestFile)obj).i > 0);
}",
@"
using System;
class TestFile
{
int i;
void Foo(Func<bool> f) { }
bool M(object obj)
=> Foo(() => obj is TestFile {|Rename:file|} && file.i > 0, () => obj is TestFile && ((TestFile)obj).i > 0);
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
public async Task TestDefiniteAssignment1()
{
await TestInRegularAndScript1Async(
@"class TestFile
{
int i;
bool M(object obj)
{
if ([||]obj is TestFile)
{
M(((TestFile)obj).i);
M(((TestFile)obj).i);
}
else
{
M(((TestFile)obj).i);
M(((TestFile)obj).i);
}
}
}",
@"class TestFile
{
int i;
bool M(object obj)
{
if (obj is TestFile {|Rename:file|})
{
M(file.i);
M(file.i);
}
else
{
M(((TestFile)obj).i);
M(((TestFile)obj).i);
}
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
public async Task TestDefiniteAssignment2()
{
await TestInRegularAndScript1Async(
@"class TestFile
{
int i;
bool M(object obj)
{
if (!([||]obj is TestFile))
{
M(((TestFile)obj).i);
M(((TestFile)obj).i);
}
else
{
M(((TestFile)obj).i);
M(((TestFile)obj).i);
}
}
}",
@"class TestFile
{
int i;
bool M(object obj)
{
if (!(obj is TestFile {|Rename:file|}))
{
M(((TestFile)obj).i);
M(((TestFile)obj).i);
}
else
{
M(file.i);
M(file.i);
}
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
public async Task TestNotOnAnalyzerMatch()
{
await TestMissingAsync(
@"class TestFile
{
bool M(object obj)
{
if ([||]obj is TestFile)
{
var file = (TestFile)obj;
}
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
public async Task TestNotOnNullable()
{
await TestMissingAsync(
@"struct TestFile
{
bool M(object obj)
{
if ([||]obj is TestFile?)
{
var i = ((TestFile?)obj).Value;
}
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
public async Task TestComplexMatch()
{
await TestInRegularAndScript1Async(
@"class TestFile
{
int i;
bool M(object obj)
{
return [||]M(null) is TestFile && ((TestFile)M(null)).i > 0;
}
}",
@"class TestFile
{
int i;
bool M(object obj)
{
return M(null) is TestFile {|Rename:file|} && file.i > 0;
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
public async Task TestTrivia()
{
await TestInRegularAndScript1Async(
@"class TestFile
{
int i;
bool M(object obj)
{
return [||]obj is TestFile && /*before*/ ((TestFile)obj) /*after*/.i > 0;
}
}",
@"class TestFile
{
int i;
bool M(object obj)
{
return obj is TestFile {|Rename:file|} && /*before*/ file /*after*/.i > 0;
}
}", ignoreTrivia: false);
}
}
}
......@@ -6,7 +6,7 @@
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.UsePatternMatching
{
public partial class CSharpIsAndCastCheckTests
public partial class CSharpIsAndCastCheckDiagnosticAnalyzerTests
{
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
public async Task FixAllInDocument1()
......
......@@ -414,7 +414,7 @@
<Compile Include="UseExpressionBody\Helpers\UseExpressionBodyHelper`1.cs" />
<Compile Include="UseObjectInitializer\CSharpUseObjectInitializerCodeFixProvider.cs" />
<Compile Include="SignatureHelp\TupleConstructionSignatureHelpProvider.cs" />
<Compile Include="UsePatternMatching\CSharpIsAndCastCodeRefactoringProvider.cs" />
<Compile Include="UsePatternMatching\CSharpIsAndCastCheckCodeRefactoringProvider.cs" />
<Compile Include="UseThrowExpression\CSharpUseThrowExpressionDiagnosticAnalyzer.cs" />
<Compile Include="Structure\CSharpBlockStructureProvider.cs" />
<Compile Include="Structure\CSharpStructureHelpers.cs" />
......
......@@ -17,7 +17,7 @@
namespace Microsoft.CodeAnalysis.CSharp.UsePatternMatching
{
[ExportCodeRefactoringProvider(LanguageNames.CSharp), Shared]
internal class CSharpIsAndCastCodeRefactoringProvider : CodeRefactoringProvider
internal class CSharpIsAndCastCheckCodeRefactoringProvider : CodeRefactoringProvider
{
private const string CS0165 = nameof(CS0165); // Use of unassigned local variable 's'
private static readonly SyntaxAnnotation s_referenceAnnotation = new SyntaxAnnotation();
......@@ -70,16 +70,6 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
var matches = new HashSet<CastExpressionSyntax>();
AddMatches(container, expr, type, matches);
//if (container is StatementSyntax containingStatement &&
// containingStatement.Parent is BlockSyntax block)
//{
// var statementIndex = block.Statements.IndexOf(containingStatement);
// for (int i = statementIndex + 1, n = block.Statements.Count; i < n; i++)
// {
// AddMatches(block.Statements[i], expr, type, matches);
// }
//}
if (matches.Count == 0)
{
return;
......@@ -153,9 +143,11 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
var localReference = SyntaxFactory.IdentifierName(localName);
foreach (var castExpression in matches)
{
var castRoot = castExpression.WalkUpParentheses();
editor.ReplaceNode(
castExpression.WalkUpParentheses(),
localReference.WithTriviaFrom(castExpression)
castRoot,
localReference.WithTriviaFrom(castRoot)
.WithAdditionalAnnotations(s_referenceAnnotation));
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册