-
Notifications
You must be signed in to change notification settings - Fork 282
Support C# 13 params collections in argument matching #993
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
Merged
+336
−92
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
44 changes: 44 additions & 0 deletions
44
src/NSubstitute/Core/Arguments/ParamsContentsArgumentMatcher.cs
This file contains hidden or 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,44 @@ | ||
| namespace NSubstitute.Core.Arguments; | ||
|
|
||
| public class ParamsContentsArgumentMatcher(IEnumerable<IArgumentSpecification> argumentSpecifications) : IArgumentMatcher, IArgumentFormatter | ||
| { | ||
| private readonly IArgumentSpecification[] _argumentSpecifications = argumentSpecifications.ToArray(); | ||
|
|
||
| public bool IsSatisfiedBy(object? argument) | ||
| { | ||
| if (argument != null) | ||
| { | ||
| var argumentValues = ParamsSupport.UnwrapArgument(argument).ToArray(); | ||
| if (argumentValues.Length == _argumentSpecifications.Length) | ||
| { | ||
| return _argumentSpecifications | ||
| .Select((spec, index) => spec.IsSatisfiedBy(argumentValues[index])) | ||
| .All(x => x); | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| public override string ToString() => string.Join(", ", _argumentSpecifications.Select(x => x.ToString())); | ||
|
|
||
| public string Format(object? argument, bool highlight) | ||
| { | ||
| ParamsSupport.TryUnwrapArgument(argument, out var argumentSequence); | ||
| var argumentValues = argumentSequence.ToArray(); | ||
| return Format(argumentValues, _argumentSpecifications).Join(", "); | ||
| } | ||
|
|
||
| private IEnumerable<string> Format(object?[] argumentValues, IArgumentSpecification[] specs) | ||
| { | ||
| if (specs.Any() && !argumentValues.Any()) | ||
| { | ||
| return new[] { "**" }; | ||
| } | ||
| return argumentValues.Select((arg, index) => | ||
| { | ||
| var hasSpecForThisArg = index < specs.Length; | ||
| return hasSpecForThisArg ? specs[index].FormatArgument(arg) : ArgumentFormatter.Default.Format(arg, true); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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,83 @@ | ||
| using System.Collections; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Reflection; | ||
| using NSubstitute.Exceptions; | ||
|
|
||
| namespace NSubstitute.Core.Arguments; | ||
|
|
||
| internal static class ParamsSupport | ||
| { | ||
| public static bool IsParams(ParameterInfo parameterInfo) | ||
| { | ||
| const string paramCollectionAttributeFullName = "System.Runtime.CompilerServices.ParamCollectionAttribute"; | ||
|
|
||
| return parameterInfo.IsDefined(typeof(ParamArrayAttribute), inherit: false) | ||
| // Needed because attribute is available in .NET 9+ only | ||
| || parameterInfo.GetCustomAttributesData().Any(x => x.AttributeType.FullName == paramCollectionAttributeFullName); | ||
| } | ||
|
|
||
| public static Type GetElementType(Type paramsParameterType) | ||
| { | ||
| if (paramsParameterType.IsArray) | ||
| { | ||
| return paramsParameterType.GetElementType()!; | ||
| } | ||
|
|
||
| if (TryGetEnumerableElementType(paramsParameterType, out var elementType)) | ||
| { | ||
| return elementType; | ||
| } | ||
|
|
||
| // The parameter type implements only the non-generic IEnumerable (e.g. ArrayList, or a custom | ||
| // collection-initializer-pattern type without IEnumerable<T>). There's no "T" to read off an | ||
| // interface in that case - the compiler itself falls back to whatever single-argument Add the | ||
| // type exposes, so mirror that here. | ||
| if (TryGetAddMethodParameterType(paramsParameterType, out elementType)) | ||
| { | ||
| return elementType; | ||
| } | ||
|
|
||
| throw new SubstituteInternalException($"Could not determine params element type for parameter of type '{paramsParameterType.FullName}'."); | ||
|
|
||
| static bool TryGetEnumerableElementType(Type type, [NotNullWhen(true)] out Type? elementType) | ||
| { | ||
| var enumerableOfT = type.IsConstructedGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>) | ||
| ? type | ||
| : type.GetInterfaces().FirstOrDefault(i => i.IsConstructedGenericType && i.GetGenericTypeDefinition() == typeof(IEnumerable<>)); | ||
|
|
||
| elementType = enumerableOfT?.GetGenericArguments()[0]; | ||
| return elementType != null; | ||
| } | ||
|
|
||
| static bool TryGetAddMethodParameterType(Type type, [NotNullWhen(true)] out Type? elementType) | ||
| { | ||
| var addMethod = type.GetMethods(BindingFlags.Public | BindingFlags.Instance) | ||
| .FirstOrDefault(m => m.Name == "Add" && m.GetParameters().Length == 1); | ||
|
|
||
| elementType = addMethod?.GetParameters()[0].ParameterType; | ||
| return elementType != null; | ||
| } | ||
| } | ||
|
|
||
| public static IEnumerable<object?> UnwrapArgument(object argument) | ||
| { | ||
| if (TryUnwrapArgument(argument, out var result)) | ||
| { | ||
| return result; | ||
| } | ||
|
|
||
| throw new SubstituteInternalException($"Expected to get collection argument, but got argument of '{argument.GetType().FullName}' type."); | ||
| } | ||
|
|
||
| public static bool TryUnwrapArgument(object? argument, out IEnumerable<object?> result) | ||
| { | ||
| if (argument is IEnumerable enumerable) | ||
| { | ||
| result = enumerable.Cast<object?>(); | ||
| return true; | ||
| } | ||
|
|
||
| result = []; | ||
| return false; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.