Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ In.AllLoadedAssemblies().Methods()
// Filter by parameters
In.AllLoadedAssemblies().Methods()
.WithoutParameters()
.WithParameter<string>()
.WithParameter<string>() // Parameter of type string or a subtype
.WithExactParameter<string>() // Parameter of exactly type string
.WithParameter<int>("count")
.WithParameterCount(2)

Expand Down Expand Up @@ -185,7 +186,8 @@ In.AllLoadedAssemblies().Public.Events()
// Constructors
In.AllLoadedAssemblies().Public.Constructors()
.WithoutParameters()
.WithParameter<string>()
.WithParameter<string>() // Parameter of type string or a subtype
.WithExactParameter<string>() // Parameter of exactly type string
.WithParameterCount(1)
.With<JsonConstructorAttribute>()
```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Linq;
using System.Reflection;
using aweXpect.Core;
using aweXpect.Options;
using aweXpect.Reflection.Collections;
using aweXpect.Reflection.Helpers;
using aweXpect.Reflection.Options;

namespace aweXpect.Reflection;

public static partial class ConstructorFilters
{
/// <summary>
/// Filter for constructors with a parameter of exact type <typeparamref name="T" />.
/// </summary>
public static ConstructorsWithParameter<T> WithExactParameter<T>(this Filtered.Constructors @this)
{
Type parameterType = typeof(T);
CollectionIndexOptions collectionIndexOptions = new();
ParameterFilterOptions parameterFilterOptions = new(p => p.ParameterType.IsOrInheritsFrom(parameterType, true),
() => $"of exact type {Formatter.Format(parameterType)}");
IAsyncChangeableFilter<ConstructorInfo> filter = Filter.Suffix<ConstructorInfo>(
constructorInfo =>
{
ParameterInfo[] parameters = constructorInfo.GetParameters();
return parameters.AnyAsync(async (p, i) =>
{
bool? isIndexInRange = collectionIndexOptions.Match switch
{
CollectionIndexOptions.IMatchFromBeginning fromBeginning => fromBeginning.MatchesIndex(i),
CollectionIndexOptions.IMatchFromEnd fromEnd => fromEnd.MatchesIndex(i, parameters.Length),
_ => false,
};
return isIndexInRange == true &&
await parameterFilterOptions.Matches(p);
});
},
()
=> $"with parameter {parameterFilterOptions.GetDescription()}{collectionIndexOptions.Match.GetDescription()} ");
return new ConstructorsWithParameter<T>(@this.Which(filter), collectionIndexOptions, parameterFilterOptions);
}

/// <summary>
/// Filter for constructors with a parameter of exact type <typeparamref name="T" /> with the
/// <paramref name="expected" /> name.
/// </summary>
public static ConstructorsWithNamedParameter<T> WithExactParameter<T>(this Filtered.Constructors @this, string expected)
{
Type parameterType = typeof(T);
StringEqualityOptions stringEqualityOptions = new();
ParameterFilterOptions parameterFilterOptions = new(p => p.ParameterType.IsOrInheritsFrom(parameterType, true),
() => $"of exact type {Formatter.Format(parameterType)}");
parameterFilterOptions.AddPredicate(p => stringEqualityOptions.AreConsideredEqual(p.Name, expected),
() => $"name {stringEqualityOptions.GetExpectation(expected, ExpectationGrammars.None)}");
CollectionIndexOptions collectionIndexOptions = new();
IAsyncChangeableFilter<ConstructorInfo> filter = Filter.Suffix<ConstructorInfo>(
constructorInfo =>
{
ParameterInfo[] parameters = constructorInfo.GetParameters();
return parameters.AnyAsync(async (p, i) =>
{
bool? isIndexInRange = collectionIndexOptions.Match switch
{
CollectionIndexOptions.IMatchFromBeginning fromBeginning => fromBeginning.MatchesIndex(i),
CollectionIndexOptions.IMatchFromEnd fromEnd => fromEnd.MatchesIndex(i, parameters.Length),
_ => false,
};
return isIndexInRange == true &&
await parameterFilterOptions.Matches(p);
});
},
()
=> $"with parameter {parameterFilterOptions.GetDescription()}{collectionIndexOptions.Match.GetDescription()} ");
return new ConstructorsWithNamedParameter<T>(@this.Which(filter), collectionIndexOptions, parameterFilterOptions, stringEqualityOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static ConstructorsWithParameter<T> WithParameter<T>(this Filtered.Constr
{
Type parameterType = typeof(T);
CollectionIndexOptions collectionIndexOptions = new();
ParameterFilterOptions parameterFilterOptions = new(p => p.ParameterType == parameterType,
ParameterFilterOptions parameterFilterOptions = new(p => p.ParameterType.IsOrInheritsFrom(parameterType),
() => $"of type {Formatter.Format(parameterType)}");
IAsyncChangeableFilter<ConstructorInfo> filter = Filter.Suffix<ConstructorInfo>(
constructorInfo =>
Expand Down Expand Up @@ -51,7 +51,7 @@ public static ConstructorsWithNamedParameter<T> WithParameter<T>(this Filtered.C
{
Type parameterType = typeof(T);
StringEqualityOptions stringEqualityOptions = new();
ParameterFilterOptions parameterFilterOptions = new(p => p.ParameterType == parameterType,
ParameterFilterOptions parameterFilterOptions = new(p => p.ParameterType.IsOrInheritsFrom(parameterType),
() => $"of type {Formatter.Format(parameterType)}");
parameterFilterOptions.AddPredicate(p => stringEqualityOptions.AreConsideredEqual(p.Name, expected),
() => $"name {stringEqualityOptions.GetExpectation(expected, ExpectationGrammars.None)}");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Linq;
using System.Reflection;
using aweXpect.Core;
using aweXpect.Options;
using aweXpect.Reflection.Collections;
using aweXpect.Reflection.Helpers;
using aweXpect.Reflection.Options;

namespace aweXpect.Reflection;

public static partial class MethodFilters
{
/// <summary>
/// Filter for methods with a parameter of exact type <typeparamref name="T" />.
/// </summary>
public static MethodsWithParameter<T> WithExactParameter<T>(this Filtered.Methods @this)
{
Type parameterType = typeof(T);
CollectionIndexOptions collectionIndexOptions = new();
ParameterFilterOptions parameterFilterOptions = new(p => p.ParameterType.IsOrInheritsFrom(parameterType, true),
() => $"of exact type {Formatter.Format(parameterType)}");
IAsyncChangeableFilter<MethodInfo> filter = Filter.Suffix<MethodInfo>(
methodInfo =>
{
ParameterInfo[] parameters = methodInfo.GetParameters();
return parameters.AnyAsync(async (p, i) =>
{
bool? isIndexInRange = collectionIndexOptions.Match switch
{
CollectionIndexOptions.IMatchFromBeginning fromBeginning => fromBeginning.MatchesIndex(i),
CollectionIndexOptions.IMatchFromEnd fromEnd => fromEnd.MatchesIndex(i, parameters.Length),
_ => false,
};
return isIndexInRange == true &&
await parameterFilterOptions.Matches(p);
});
},
()
=> $"with parameter {parameterFilterOptions.GetDescription()}{collectionIndexOptions.Match.GetDescription()} ");
return new MethodsWithParameter<T>(@this.Which(filter), collectionIndexOptions, parameterFilterOptions);
}

/// <summary>
/// Filter for methods with a parameter of exact type <typeparamref name="T" /> with the <paramref name="expected" />
/// name.
/// </summary>
public static MethodsWithNamedParameter<T> WithExactParameter<T>(this Filtered.Methods @this, string expected)
{
Type parameterType = typeof(T);
StringEqualityOptions stringEqualityOptions = new();
ParameterFilterOptions parameterFilterOptions = new(p => p.ParameterType.IsOrInheritsFrom(parameterType, true),
() => $"of exact type {Formatter.Format(parameterType)}");
parameterFilterOptions.AddPredicate(p => stringEqualityOptions.AreConsideredEqual(p.Name, expected),
() => $"name {stringEqualityOptions.GetExpectation(expected, ExpectationGrammars.None)}");
CollectionIndexOptions collectionIndexOptions = new();
IAsyncChangeableFilter<MethodInfo> filter = Filter.Suffix<MethodInfo>(
methodInfo =>
{
ParameterInfo[] parameters = methodInfo.GetParameters();
return parameters.AnyAsync(async (p, i) =>
{
bool? isIndexInRange = collectionIndexOptions.Match switch
{
CollectionIndexOptions.IMatchFromBeginning fromBeginning => fromBeginning.MatchesIndex(i),
CollectionIndexOptions.IMatchFromEnd fromEnd => fromEnd.MatchesIndex(i, parameters.Length),
_ => false,
};
return isIndexInRange == true &&
await parameterFilterOptions.Matches(p);
});
},
()
=> $"with parameter {parameterFilterOptions.GetDescription()}{collectionIndexOptions.Match.GetDescription()} ");
return new MethodsWithNamedParameter<T>(@this.Which(filter), collectionIndexOptions, parameterFilterOptions, stringEqualityOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static MethodsWithParameter<T> WithParameter<T>(this Filtered.Methods @th
{
Type parameterType = typeof(T);
CollectionIndexOptions collectionIndexOptions = new();
ParameterFilterOptions parameterFilterOptions = new(p => p.ParameterType == parameterType,
ParameterFilterOptions parameterFilterOptions = new(p => p.ParameterType.IsOrInheritsFrom(parameterType),
() => $"of type {Formatter.Format(parameterType)}");
IAsyncChangeableFilter<MethodInfo> filter = Filter.Suffix<MethodInfo>(
methodInfo =>
Expand Down Expand Up @@ -51,7 +51,7 @@ public static MethodsWithNamedParameter<T> WithParameter<T>(this Filtered.Method
{
Type parameterType = typeof(T);
StringEqualityOptions stringEqualityOptions = new();
ParameterFilterOptions parameterFilterOptions = new(p => p.ParameterType == parameterType,
ParameterFilterOptions parameterFilterOptions = new(p => p.ParameterType.IsOrInheritsFrom(parameterType),
() => $"of type {Formatter.Format(parameterType)}");
parameterFilterOptions.AddPredicate(p => stringEqualityOptions.AreConsideredEqual(p.Name, expected),
() => $"name {stringEqualityOptions.GetExpectation(expected, ExpectationGrammars.None)}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ namespace aweXpect.Reflection
where TAttribute : System.Attribute { }
public static aweXpect.Reflection.ConstructorFilters.ConstructorsWith With<TAttribute>(this aweXpect.Reflection.Collections.Filtered.Constructors @this, System.Func<TAttribute, bool>? predicate, [System.Runtime.CompilerServices.CallerArgumentExpression("predicate")] string doNotPopulateThisValue = "")
where TAttribute : System.Attribute { }
public static aweXpect.Reflection.ConstructorFilters.ConstructorsWithParameter<T> WithExactParameter<T>(this aweXpect.Reflection.Collections.Filtered.Constructors @this) { }
public static aweXpect.Reflection.ConstructorFilters.ConstructorsWithNamedParameter<T> WithExactParameter<T>(this aweXpect.Reflection.Collections.Filtered.Constructors @this, string expected) { }
public static aweXpect.Reflection.ConstructorFilters.ConstructorsWithNamedParameter<object?> WithParameter(this aweXpect.Reflection.Collections.Filtered.Constructors @this, string expected) { }
public static aweXpect.Reflection.ConstructorFilters.ConstructorsWithParameter<T> WithParameter<T>(this aweXpect.Reflection.Collections.Filtered.Constructors @this) { }
public static aweXpect.Reflection.ConstructorFilters.ConstructorsWithNamedParameter<T> WithParameter<T>(this aweXpect.Reflection.Collections.Filtered.Constructors @this, string expected) { }
Expand Down Expand Up @@ -197,6 +199,8 @@ namespace aweXpect.Reflection
where TAttribute : System.Attribute { }
public static aweXpect.Reflection.MethodFilters.MethodsWith With<TAttribute>(this aweXpect.Reflection.Collections.Filtered.Methods @this, System.Func<TAttribute, bool>? predicate, bool inherit = true, [System.Runtime.CompilerServices.CallerArgumentExpression("predicate")] string doNotPopulateThisValue = "")
where TAttribute : System.Attribute { }
public static aweXpect.Reflection.MethodFilters.MethodsWithParameter<T> WithExactParameter<T>(this aweXpect.Reflection.Collections.Filtered.Methods @this) { }
public static aweXpect.Reflection.MethodFilters.MethodsWithNamedParameter<T> WithExactParameter<T>(this aweXpect.Reflection.Collections.Filtered.Methods @this, string expected) { }
public static aweXpect.Reflection.Collections.Filtered.Methods.StringEqualityResultType WithName(this aweXpect.Reflection.Collections.Filtered.Methods @this, string expected) { }
public static aweXpect.Reflection.MethodFilters.MethodsWithNamedParameter<object?> WithParameter(this aweXpect.Reflection.Collections.Filtered.Methods @this, string expected) { }
public static aweXpect.Reflection.MethodFilters.MethodsWithParameter<T> WithParameter<T>(this aweXpect.Reflection.Collections.Filtered.Methods @this) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ namespace aweXpect.Reflection
where TAttribute : System.Attribute { }
public static aweXpect.Reflection.ConstructorFilters.ConstructorsWith With<TAttribute>(this aweXpect.Reflection.Collections.Filtered.Constructors @this, System.Func<TAttribute, bool>? predicate, [System.Runtime.CompilerServices.CallerArgumentExpression("predicate")] string doNotPopulateThisValue = "")
where TAttribute : System.Attribute { }
public static aweXpect.Reflection.ConstructorFilters.ConstructorsWithParameter<T> WithExactParameter<T>(this aweXpect.Reflection.Collections.Filtered.Constructors @this) { }
public static aweXpect.Reflection.ConstructorFilters.ConstructorsWithNamedParameter<T> WithExactParameter<T>(this aweXpect.Reflection.Collections.Filtered.Constructors @this, string expected) { }
public static aweXpect.Reflection.ConstructorFilters.ConstructorsWithNamedParameter<object?> WithParameter(this aweXpect.Reflection.Collections.Filtered.Constructors @this, string expected) { }
public static aweXpect.Reflection.ConstructorFilters.ConstructorsWithParameter<T> WithParameter<T>(this aweXpect.Reflection.Collections.Filtered.Constructors @this) { }
public static aweXpect.Reflection.ConstructorFilters.ConstructorsWithNamedParameter<T> WithParameter<T>(this aweXpect.Reflection.Collections.Filtered.Constructors @this, string expected) { }
Expand Down Expand Up @@ -197,6 +199,8 @@ namespace aweXpect.Reflection
where TAttribute : System.Attribute { }
public static aweXpect.Reflection.MethodFilters.MethodsWith With<TAttribute>(this aweXpect.Reflection.Collections.Filtered.Methods @this, System.Func<TAttribute, bool>? predicate, bool inherit = true, [System.Runtime.CompilerServices.CallerArgumentExpression("predicate")] string doNotPopulateThisValue = "")
where TAttribute : System.Attribute { }
public static aweXpect.Reflection.MethodFilters.MethodsWithParameter<T> WithExactParameter<T>(this aweXpect.Reflection.Collections.Filtered.Methods @this) { }
public static aweXpect.Reflection.MethodFilters.MethodsWithNamedParameter<T> WithExactParameter<T>(this aweXpect.Reflection.Collections.Filtered.Methods @this, string expected) { }
public static aweXpect.Reflection.Collections.Filtered.Methods.StringEqualityResultType WithName(this aweXpect.Reflection.Collections.Filtered.Methods @this, string expected) { }
public static aweXpect.Reflection.MethodFilters.MethodsWithNamedParameter<object?> WithParameter(this aweXpect.Reflection.Collections.Filtered.Methods @this, string expected) { }
public static aweXpect.Reflection.MethodFilters.MethodsWithParameter<T> WithParameter<T>(this aweXpect.Reflection.Collections.Filtered.Methods @this) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ namespace aweXpect.Reflection
where TAttribute : System.Attribute { }
public static aweXpect.Reflection.ConstructorFilters.ConstructorsWith With<TAttribute>(this aweXpect.Reflection.Collections.Filtered.Constructors @this, System.Func<TAttribute, bool>? predicate, [System.Runtime.CompilerServices.CallerArgumentExpression("predicate")] string doNotPopulateThisValue = "")
where TAttribute : System.Attribute { }
public static aweXpect.Reflection.ConstructorFilters.ConstructorsWithParameter<T> WithExactParameter<T>(this aweXpect.Reflection.Collections.Filtered.Constructors @this) { }
public static aweXpect.Reflection.ConstructorFilters.ConstructorsWithNamedParameter<T> WithExactParameter<T>(this aweXpect.Reflection.Collections.Filtered.Constructors @this, string expected) { }
public static aweXpect.Reflection.ConstructorFilters.ConstructorsWithNamedParameter<object?> WithParameter(this aweXpect.Reflection.Collections.Filtered.Constructors @this, string expected) { }
public static aweXpect.Reflection.ConstructorFilters.ConstructorsWithParameter<T> WithParameter<T>(this aweXpect.Reflection.Collections.Filtered.Constructors @this) { }
public static aweXpect.Reflection.ConstructorFilters.ConstructorsWithNamedParameter<T> WithParameter<T>(this aweXpect.Reflection.Collections.Filtered.Constructors @this, string expected) { }
Expand Down Expand Up @@ -197,6 +199,8 @@ namespace aweXpect.Reflection
where TAttribute : System.Attribute { }
public static aweXpect.Reflection.MethodFilters.MethodsWith With<TAttribute>(this aweXpect.Reflection.Collections.Filtered.Methods @this, System.Func<TAttribute, bool>? predicate, bool inherit = true, [System.Runtime.CompilerServices.CallerArgumentExpression("predicate")] string doNotPopulateThisValue = "")
where TAttribute : System.Attribute { }
public static aweXpect.Reflection.MethodFilters.MethodsWithParameter<T> WithExactParameter<T>(this aweXpect.Reflection.Collections.Filtered.Methods @this) { }
public static aweXpect.Reflection.MethodFilters.MethodsWithNamedParameter<T> WithExactParameter<T>(this aweXpect.Reflection.Collections.Filtered.Methods @this, string expected) { }
public static aweXpect.Reflection.Collections.Filtered.Methods.StringEqualityResultType WithName(this aweXpect.Reflection.Collections.Filtered.Methods @this, string expected) { }
public static aweXpect.Reflection.MethodFilters.MethodsWithNamedParameter<object?> WithParameter(this aweXpect.Reflection.Collections.Filtered.Methods @this, string expected) { }
public static aweXpect.Reflection.MethodFilters.MethodsWithParameter<T> WithParameter<T>(this aweXpect.Reflection.Collections.Filtered.Methods @this) { }
Expand Down
Loading
Loading