-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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 #53049 from sharwell/out-var-provider
Add OutVariableArgumentProvider for 'out' parameters
- Loading branch information
Showing
11 changed files
with
350 additions
and
11 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
139 changes: 139 additions & 0 deletions
139
...ditorFeatures/CSharpTest/Completion/ArgumentProviders/OutVariableArgumentProviderTests.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,139 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.CodeStyle; | ||
using Microsoft.CodeAnalysis.CSharp.CodeStyle; | ||
using Microsoft.CodeAnalysis.CSharp.Completion.Providers; | ||
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions; | ||
using Microsoft.CodeAnalysis.Test.Utilities; | ||
using Xunit; | ||
|
||
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Completion.ArgumentProviders | ||
{ | ||
[Trait(Traits.Feature, Traits.Features.Completion)] | ||
public class OutVariableArgumentProviderTests : AbstractCSharpArgumentProviderTests | ||
{ | ||
private static readonly OptionsCollection s_useExplicitTypeOptions = new(LanguageNames.CSharp) | ||
{ | ||
{ CSharpCodeStyleOptions.VarForBuiltInTypes, false }, | ||
{ CSharpCodeStyleOptions.VarWhenTypeIsApparent, false }, | ||
{ CSharpCodeStyleOptions.VarElsewhere, false }, | ||
}; | ||
|
||
private static readonly OptionsCollection s_useExplicitMetadataTypeOptions = new(LanguageNames.CSharp) | ||
{ | ||
{ CSharpCodeStyleOptions.VarForBuiltInTypes, false }, | ||
{ CSharpCodeStyleOptions.VarWhenTypeIsApparent, false }, | ||
{ CSharpCodeStyleOptions.VarElsewhere, false }, | ||
{ CodeStyleOptions2.PreferIntrinsicPredefinedTypeKeywordInDeclaration, false }, | ||
}; | ||
|
||
private static readonly OptionsCollection s_useImplicitTypeOptions = new(LanguageNames.CSharp) | ||
{ | ||
{ CSharpCodeStyleOptions.VarForBuiltInTypes, true }, | ||
{ CSharpCodeStyleOptions.VarWhenTypeIsApparent, true }, | ||
{ CSharpCodeStyleOptions.VarElsewhere, true }, | ||
}; | ||
|
||
internal override Type GetArgumentProviderType() | ||
=> typeof(OutVariableArgumentProvider); | ||
|
||
[Theory] | ||
[InlineData("")] | ||
[InlineData("ref")] | ||
[InlineData("in")] | ||
public async Task TestUnsupportedModifiers(string modifier) | ||
{ | ||
var markup = $@" | ||
class C | ||
{{ | ||
void Method() | ||
{{ | ||
TryParse($$) | ||
}} | ||
bool TryParse({modifier} int value) => throw null; | ||
}} | ||
"; | ||
|
||
await VerifyDefaultValueAsync(markup, expectedDefaultValue: null); | ||
} | ||
|
||
[Fact] | ||
public async Task TestDeclareVariable() | ||
{ | ||
var markup = $@" | ||
class C | ||
{{ | ||
void Method() | ||
{{ | ||
int.TryParse(""x"", $$) | ||
}} | ||
}} | ||
"; | ||
|
||
await VerifyDefaultValueAsync(markup, "out var result"); | ||
await VerifyDefaultValueAsync(markup, expectedDefaultValue: null, previousDefaultValue: "prior"); | ||
} | ||
|
||
[Theory(Skip = "https://github.com/dotnet/roslyn/issues/53056")] | ||
[CombinatorialData] | ||
public async Task TestDeclareVariableBuiltInType(bool preferVar, bool preferBuiltInType) | ||
{ | ||
var markup = $@" | ||
using System; | ||
class C | ||
{{ | ||
void Method() | ||
{{ | ||
int.TryParse(""x"", $$) | ||
}} | ||
}} | ||
"; | ||
|
||
var expected = (preferVar, preferBuiltInType) switch | ||
{ | ||
(true, _) => "out var result", | ||
(false, true) => "out int result", | ||
(false, false) => "out Int32 result", | ||
}; | ||
|
||
var options = (preferVar, preferBuiltInType) switch | ||
{ | ||
(true, _) => s_useImplicitTypeOptions, | ||
(false, true) => s_useExplicitTypeOptions, | ||
(false, false) => s_useExplicitMetadataTypeOptions, | ||
}; | ||
|
||
await VerifyDefaultValueAsync(markup, expected, options: options); | ||
} | ||
|
||
[Theory] | ||
[InlineData("string")] | ||
[InlineData("bool")] | ||
[InlineData("int?")] | ||
public async Task TestDeclareVariableEscapedIdentifier(string type) | ||
{ | ||
var markup = $@" | ||
class C | ||
{{ | ||
void Method() | ||
{{ | ||
this.Target($$); | ||
}} | ||
void Target(out {type} @void) | ||
{{ | ||
@void = default; | ||
}} | ||
}} | ||
"; | ||
|
||
await VerifyDefaultValueAsync(markup, "out var @void"); | ||
await VerifyDefaultValueAsync(markup, expectedDefaultValue: null, previousDefaultValue: "prior"); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.