-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
261 additions
and
74 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
85 changes: 85 additions & 0 deletions
85
src/NSubstitute.Analyzers.Shared/DiagnosticAnalyzers/Substitute.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,85 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Operations; | ||
using NSubstitute.Analyzers.Shared.Extensions; | ||
|
||
namespace NSubstitute.Analyzers.Shared.DiagnosticAnalyzers; | ||
|
||
internal class Substitute | ||
{ | ||
private readonly IReadOnlyList<IArgumentOperation> _argumentOperations; | ||
|
||
public IReadOnlyList<ITypeSymbol> ArgumentsTypeSymbols { get; } | ||
|
||
private Substitute( | ||
IInvocationOperation? invocationOperation, | ||
IPropertyReferenceOperation? propertyReferenceOperation) | ||
{ | ||
var argumentOperations = invocationOperation?.Arguments ?? propertyReferenceOperation?.Arguments ?? | ||
throw new ArgumentNullException(nameof(invocationOperation), $"Both {nameof(invocationOperation)} and {nameof(propertyReferenceOperation)} are null"); | ||
|
||
_argumentOperations = argumentOperations.OrderBy(argOperation => argOperation.Parameter.Ordinal).ToList(); | ||
var typeSymbols = _argumentOperations.Select(argOperation => argOperation.GetTypeSymbol()).ToList(); | ||
|
||
// when property has a setter, we might have one more call argument than parameters in property/indexer | ||
// depending on whether value is set as a part of substitute or not | ||
// as we cant easily determine if value was set or not, we assume it was set in order not to report false positives | ||
if (propertyReferenceOperation?.Property.SetMethod != null) | ||
{ | ||
typeSymbols.Add(propertyReferenceOperation.Property.Type); | ||
} | ||
|
||
ArgumentsTypeSymbols = typeSymbols; | ||
} | ||
|
||
public static Substitute? TryCreate(IOperation operation) | ||
{ | ||
var actualOperation = GetActualOperation(operation); | ||
|
||
if (actualOperation == null) | ||
{ | ||
return null; | ||
} | ||
|
||
return new Substitute( | ||
actualOperation.Value.InvocationOperation, | ||
actualOperation.Value.PropertyReferenceOperation); | ||
} | ||
|
||
public bool HasArgumentAt(int position) => position >= 0 && position <= ArgumentsTypeSymbols.Count - 1; | ||
|
||
public bool HasArgumentOperationAt(int position) => position >= 0 && position <= _argumentOperations.Count - 1; | ||
|
||
public ITypeSymbol GetArgumentTypeAt(int position) | ||
{ | ||
if (position <= ArgumentsTypeSymbols.Count - 1) | ||
{ | ||
return ArgumentsTypeSymbols[position]; | ||
} | ||
|
||
throw new ArgumentException($"Could not find argument type at position {position}", nameof(position)); | ||
} | ||
|
||
public IArgumentOperation GetArgumentOperationAt(int position) | ||
{ | ||
if (position <= this._argumentOperations.Count - 1) | ||
{ | ||
return this._argumentOperations[position]; | ||
} | ||
|
||
throw new ArgumentException($"Could not find argument at position {position}", nameof(position)); | ||
} | ||
|
||
private static (IPropertyReferenceOperation? PropertyReferenceOperation, IInvocationOperation? InvocationOperation)? GetActualOperation(IOperation substituteOperation) | ||
{ | ||
return substituteOperation switch | ||
{ | ||
IInvocationOperation invocationOperation => (null, invocationOperation), | ||
IPropertyReferenceOperation propertyReferenceOperation => (PropertyReferenceOperation: propertyReferenceOperation, null), | ||
IConversionOperation conversionOperation => GetActualOperation(conversionOperation.Operand), | ||
_ => null | ||
}; | ||
} | ||
} |
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.