-
-
Notifications
You must be signed in to change notification settings - Fork 0
coverage: add internal tests for analyzer helpers #697
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| #region License | ||
| // MIT License | ||
| // | ||
| // Copyright (c) Manuel Römer | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files (the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in all | ||
| // copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| // SOFTWARE. | ||
| #endregion | ||
|
|
||
| #if !NULLABLE_ATTRIBUTES_DISABLE | ||
| #nullable enable | ||
| #pragma warning disable | ||
|
|
||
| namespace System.Diagnostics.CodeAnalysis | ||
| { | ||
| using global::System; | ||
|
|
||
| #if DEBUG | ||
| /// <summary> | ||
| /// Specifies that when a method returns <see cref="ReturnValue"/>, | ||
| /// the parameter will not be <see langword="null"/> even if the corresponding type allows it. | ||
| /// </summary> | ||
| #endif | ||
| [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] | ||
| #if !NULLABLE_ATTRIBUTES_INCLUDE_IN_CODE_COVERAGE | ||
| [ExcludeFromCodeCoverage, DebuggerNonUserCode] | ||
| #endif | ||
| internal sealed class NotNullWhenAttribute : Attribute | ||
| { | ||
| #if DEBUG | ||
| /// <summary> | ||
| /// Gets the return value condition. | ||
| /// If the method returns this value, the associated parameter will not be <see langword="null"/>. | ||
| /// </summary> | ||
| #endif | ||
| public bool ReturnValue { get; } | ||
|
|
||
| #if DEBUG | ||
| /// <summary> | ||
| /// Initializes the attribute with the specified return value condition. | ||
| /// </summary> | ||
| /// <param name="returnValue"> | ||
| /// The return value condition. | ||
| /// If the method returns this value, the associated parameter will not be <see langword="null"/>. | ||
| /// </param> | ||
| #endif | ||
| public NotNullWhenAttribute(bool returnValue) | ||
| { | ||
| ReturnValue = returnValue; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #pragma warning restore | ||
| #nullable restore | ||
| #endif // NULLABLE_ATTRIBUTES_DISABLE | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,10 @@ | |
| <NoWarn>S3776</NoWarn> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <InternalsVisibleTo Include="Mockolate.SourceGenerators.Tests" PublicKey="$(PublicKey)"/> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" PrivateAssets="all"/> | ||
| <PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all"/> | ||
|
|
@@ -20,4 +24,8 @@ | |
| <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false"/> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Remove="Nullable"/> | ||
| </ItemGroup> | ||
|
Comment on lines
+27
to
+29
|
||
|
|
||
| </Project> | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,71 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #region License | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // MIT License | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Copyright (c) Manuel Römer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // of this software and associated documentation files (the "Software"), to deal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // in the Software without restriction, including without limitation the rights | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // copies of the Software, and to permit persons to whom the Software is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // furnished to do so, subject to the following conditions: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // The above copyright notice and this permission notice shall be included in all | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // copies or substantial portions of the Software. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // SOFTWARE. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endregion | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #if !NULLABLE_ATTRIBUTES_DISABLE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #nullable enable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #pragma warning disable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| namespace System.Diagnostics.CodeAnalysis | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using global::System; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #if DEBUG | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Specifies that when a method returns <see cref="ReturnValue"/>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// the parameter will not be <see langword="null"/> even if the corresponding type allows it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #if !NULLABLE_ATTRIBUTES_INCLUDE_IN_CODE_COVERAGE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [ExcludeFromCodeCoverage, DebuggerNonUserCode] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| internal sealed class NotNullWhenAttribute : Attribute | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #if DEBUG | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Gets the return value condition. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// If the method returns this value, the associated parameter will not be <see langword="null"/>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public bool ReturnValue { get; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #if DEBUG | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Initializes the attribute with the specified return value condition. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// <param name="returnValue"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// The return value condition. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// If the method returns this value, the associated parameter will not be <see langword="null"/>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// </param> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public NotNullWhenAttribute(bool returnValue) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ReturnValue = returnValue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+31
to
+66
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using global::System; | |
| #if DEBUG | |
| /// <summary> | |
| /// Specifies that when a method returns <see cref="ReturnValue"/>, | |
| /// the parameter will not be <see langword="null"/> even if the corresponding type allows it. | |
| /// </summary> | |
| #endif | |
| [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] | |
| #if !NULLABLE_ATTRIBUTES_INCLUDE_IN_CODE_COVERAGE | |
| [ExcludeFromCodeCoverage, DebuggerNonUserCode] | |
| #endif | |
| internal sealed class NotNullWhenAttribute : Attribute | |
| { | |
| #if DEBUG | |
| /// <summary> | |
| /// Gets the return value condition. | |
| /// If the method returns this value, the associated parameter will not be <see langword="null"/>. | |
| /// </summary> | |
| #endif | |
| public bool ReturnValue { get; } | |
| #if DEBUG | |
| /// <summary> | |
| /// Initializes the attribute with the specified return value condition. | |
| /// </summary> | |
| /// <param name="returnValue"> | |
| /// The return value condition. | |
| /// If the method returns this value, the associated parameter will not be <see langword="null"/>. | |
| /// </param> | |
| #endif | |
| public NotNullWhenAttribute(bool returnValue) | |
| { | |
| ReturnValue = returnValue; | |
| } | |
| } | |
| using global::System; | |
| #if DEBUG | |
| /// <summary> | |
| /// Specifies that when a method returns <see cref="ReturnValue"/>, | |
| /// the parameter will not be <see langword="null"/> even if the corresponding type allows it. | |
| /// </summary> | |
| #endif | |
| [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] | |
| #if !NULLABLE_ATTRIBUTES_INCLUDE_IN_CODE_COVERAGE | |
| [ExcludeFromCodeCoverage, DebuggerNonUserCode] | |
| #endif | |
| internal sealed class NotNullWhenAttribute : Attribute | |
| { | |
| #if DEBUG | |
| /// <summary> | |
| /// Gets the return value condition. | |
| /// If the method returns this value, the associated parameter will not be <see langword="null"/>. | |
| /// </summary> | |
| #endif | |
| public bool ReturnValue { get; } | |
| #if DEBUG | |
| /// <summary> | |
| /// Initializes the attribute with the specified return value condition. | |
| /// </summary> | |
| /// <param name="returnValue"> | |
| /// The return value condition. | |
| /// If the method returns this value, the associated parameter will not be <see langword="null"/>. | |
| /// </param> | |
| #endif | |
| public NotNullWhenAttribute(bool returnValue) | |
| { | |
| ReturnValue = returnValue; | |
| } | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using aweXpect; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Xunit; | ||
| using static aweXpect.Expect; | ||
|
|
||
| namespace Mockolate.Analyzers.Tests; | ||
|
|
||
| public class AnalyzerHelpersTests | ||
| { | ||
| [Fact] | ||
| public async Task WhenInvokedMethodIsNotGeneric_ShouldNotReturnAnyTypeArgument() | ||
| { | ||
|
vbreuss marked this conversation as resolved.
|
||
| const string source = """ | ||
| public class C | ||
| { | ||
| public void Foo() { } | ||
| public void Bar() { Foo(); } | ||
| } | ||
| """; | ||
| IMethodSymbol method = GetInvokedMethod(source, "Foo"); | ||
|
|
||
| ITypeSymbol? result = AnalyzerHelpers.GetSingleInvocationTypeArgumentOrNull(method); | ||
|
|
||
| await That(result).IsNull(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenInvokedMethodIsGeneric_ShouldReturnFirstTypeArgument() | ||
| { | ||
| const string source = """ | ||
| public class C | ||
| { | ||
| public T Foo<T>() => default!; | ||
| public void Bar() { Foo<int>(); } | ||
| } | ||
| """; | ||
| IMethodSymbol method = GetInvokedMethod(source, "Foo"); | ||
|
|
||
| ITypeSymbol? result = AnalyzerHelpers.GetSingleInvocationTypeArgumentOrNull(method); | ||
|
|
||
| await That(result).IsNotNull(); | ||
| await That(result!.SpecialType).IsEqualTo(SpecialType.System_Int32); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenSyntaxIsNotInvocationExpression_ShouldNotReturnAnyLocation() | ||
| { | ||
| const string source = """ | ||
| public class C | ||
| { | ||
| public int Foo() => 0; | ||
| } | ||
| """; | ||
| SyntaxTree tree = CSharpSyntaxTree.ParseText(source); | ||
| CSharpCompilation compilation = CreateCompilation(tree); | ||
| SemanticModel model = compilation.GetSemanticModel(tree); | ||
| MethodDeclarationSyntax declaration = tree.GetRoot().DescendantNodes() | ||
| .OfType<MethodDeclarationSyntax>() | ||
| .Single(); | ||
| IMethodSymbol symbol = (IMethodSymbol)model.GetDeclaredSymbol(declaration)!; | ||
|
|
||
| Location? result = AnalyzerHelpers.GetTypeArgumentLocation(declaration, symbol.ReturnType); | ||
|
|
||
| await That(result).IsNull(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenInvocationHasGenericNameSyntax_ShouldReturnTypeArgumentLocation() | ||
| { | ||
| const string source = """ | ||
| public static class S | ||
| { | ||
| public static T Make<T>() => default!; | ||
| } | ||
|
|
||
| public class C | ||
| { | ||
| public void Foo() { S.Make<int>(); } | ||
| } | ||
| """; | ||
| SyntaxTree tree = CSharpSyntaxTree.ParseText(source); | ||
| CSharpCompilation compilation = CreateCompilation(tree); | ||
| SemanticModel model = compilation.GetSemanticModel(tree); | ||
| InvocationExpressionSyntax invocation = tree.GetRoot().DescendantNodes() | ||
| .OfType<InvocationExpressionSyntax>() | ||
| .Single(i => i.Expression is MemberAccessExpressionSyntax { Name: GenericNameSyntax, }); | ||
| IMethodSymbol method = (IMethodSymbol)model.GetSymbolInfo(invocation).Symbol!; | ||
| ITypeSymbol typeArgument = method.TypeArguments[0]; | ||
|
|
||
| Location? result = AnalyzerHelpers.GetTypeArgumentLocation(invocation, typeArgument); | ||
|
|
||
| await That(result).IsNotNull(); | ||
| } | ||
|
|
||
| private static IMethodSymbol GetInvokedMethod(string source, string methodName) | ||
| { | ||
| SyntaxTree tree = CSharpSyntaxTree.ParseText(source); | ||
| CSharpCompilation compilation = CreateCompilation(tree); | ||
| SemanticModel model = compilation.GetSemanticModel(tree); | ||
| InvocationExpressionSyntax invocation = tree.GetRoot().DescendantNodes() | ||
| .OfType<InvocationExpressionSyntax>() | ||
| .Single(i => InvocationName(i) == methodName); | ||
| return (IMethodSymbol)model.GetSymbolInfo(invocation).Symbol!; | ||
| } | ||
|
|
||
| private static string? InvocationName(InvocationExpressionSyntax invocation) => invocation.Expression switch | ||
| { | ||
| IdentifierNameSyntax id => id.Identifier.Text, | ||
| GenericNameSyntax generic => generic.Identifier.Text, | ||
| MemberAccessExpressionSyntax member => member.Name.Identifier.Text, | ||
| _ => null, | ||
| }; | ||
|
|
||
| private static CSharpCompilation CreateCompilation(SyntaxTree tree) => CSharpCompilation.Create( | ||
| "TestAssembly", | ||
| [tree,], | ||
| [MetadataReference.CreateFromFile(typeof(object).Assembly.Location),], | ||
| new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is indented with spaces, but the repo’s .editorconfig requires tab indentation for .cs files (
indent_style = tab). Reformatting to tabs will keep formatting consistent and avoid editor/CI formatting churn.