Skip to content
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

Apply a few optimizations #513

Merged
merged 3 commits into from
Nov 4, 2017
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
4 changes: 2 additions & 2 deletions Source/EmptyDefaultValueProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ private static object GetReferenceTypeDefault(Type valueType)
var genericType = valueType.GetGenericArguments()[0];
var genericListType = typeof(List<>).MakeGenericType(genericType);

return typeof(Queryable).GetMethods()
.Single(x => x.Name == "AsQueryable" && x.IsGenericMethod)
return typeof(Queryable).GetMethods("AsQueryable")
.Single(x => x.IsGenericMethod)
.MakeGenericMethod(genericType)
.Invoke(null, new[] { Activator.CreateInstance(genericListType) });
}
Expand Down
2 changes: 1 addition & 1 deletion Source/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public static Expression PartialMatcherAwareEval(this Expression expression)

private static bool ReturnsMatch(MethodCallExpression expression)
{
if (expression.Method.GetCustomAttribute<AdvancedMatcherAttribute>(true) == null)
if (!expression.Method.IsDefined(typeof(AdvancedMatcherAttribute), true))
{
using (var context = new FluentMockContext())
{
Expand Down
2 changes: 1 addition & 1 deletion Source/ExpressionStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ private void ToStringMethodCall(MethodCallExpression node)
var paramFrom = 0;
var expression = node.Object;

var hasExtensionAttribute = node.Method.GetCustomAttribute<ExtensionAttribute>() != null;
var hasExtensionAttribute = node.Method.IsDefined(typeof(ExtensionAttribute));

if (hasExtensionAttribute)
{
Expand Down
5 changes: 5 additions & 0 deletions Source/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ public static TAttribute GetCustomAttribute<TAttribute>(this ICustomAttributePro
}
#endif

public static IEnumerable<MethodInfo> GetMethods(this Type type, string name)
{
return type.GetMember(name).OfType<MethodInfo>();
}

public static bool HasCompatibleParameterTypes(this MethodInfo method, Type[] paramTypes, bool exactParameterMatch)
{
var types = method.GetParameterTypes().ToArray();
Expand Down
6 changes: 3 additions & 3 deletions Source/Linq/FluentMockVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ protected override Expression VisitMember(MemberExpression node)
// compiler-generated types as they are typically the
// anonymous types generated to build up the query expressions.
if (node.Expression.NodeType == ExpressionType.Parameter &&
node.Expression.Type.GetTypeInfo().GetCustomAttribute<CompilerGeneratedAttribute>(false) != null)
node.Expression.Type.GetTypeInfo().IsDefined(typeof(CompilerGeneratedAttribute), false))
{
var memberType = ((PropertyInfo)node.Member).PropertyType;

Expand Down Expand Up @@ -191,8 +191,8 @@ private static MethodInfo GetSetupMethod(Type objectType, Type returnType)
{
return typeof(Mock<>)
.MakeGenericType(objectType)
.GetMethods()
.First(mi => mi.Name == "Setup" && mi.IsGenericMethod)
.GetMethods("Setup")
.First(mi => mi.IsGenericMethod)
.MakeGenericMethod(returnType);
}
}
Expand Down
3 changes: 1 addition & 2 deletions Source/Matchers/MatcherAttributeMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ private static MethodInfo ResolveValidatorMethod(Expression expression)
// passing generic type arguments for the query.
var genericArgs = call.Method.GetGenericArguments();

method = call.Method.DeclaringType.GetMethods()
method = call.Method.DeclaringType.GetMethods(call.Method.Name)
.Where(m =>
m.Name == call.Method.Name &&
m.IsGenericMethodDefinition &&
m.GetGenericArguments().Length ==
call.Method.GetGenericMethodDefinition().GetGenericArguments().Length &&
Expand Down
2 changes: 1 addition & 1 deletion Source/MethodCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public MethodCall(Mock mock, Condition condition, Expression originalExpression,
}
else
{
var isParamArray = parameter.GetCustomAttribute<ParamArrayAttribute>(true) != null;
var isParamArray = parameter.IsDefined(typeof(ParamArrayAttribute), true);
argumentMatchers.Add(MatcherFactory.CreateMatcher(argument, isParamArray));
}
}
Expand Down
17 changes: 12 additions & 5 deletions Source/Mock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -781,10 +781,10 @@ private static void SetupAllProperties(Mock mock, Stack<Type> mockedTypesStack)
ProxyFactory.IsMethodVisible(p.GetGetMethod(), out _))
.Distinct();

var setupPropertyMethod = mock.GetType().GetMethods()
.First(m => m.Name == "SetupProperty" && m.GetParameters().Length == 2);
var setupGetMethod = mock.GetType().GetMethods()
.First(m => m.Name == "SetupGet" && m.GetParameters().Length == 1);
var setupPropertyMethod = mock.GetType().GetMethods("SetupProperty")
.First(m => m.GetParameters().Length == 2);
var setupGetMethod = mock.GetType().GetMethods("SetupGet")
.First(m => m.GetParameters().Length == 1);

foreach (var property in properties)
{
Expand Down Expand Up @@ -854,6 +854,13 @@ private static Expression GetPropertyExpression(Type mockType, PropertyInfo prop
/// </summary>
private static Interceptor GetInterceptor(Expression fluentExpression, Mock mock)
{
if (fluentExpression is ParameterExpression)
{
// fast path for single-dot setup expressions;
// no need for expensive lambda compilation.
return mock.Interceptor;
}

var targetExpression = FluentMockVisitor.Accept(fluentExpression, mock);
var targetLambda = Expression.Lambda<Func<Mock>>(Expression.Convert(targetExpression, typeof(Mock)));

Expand Down Expand Up @@ -1004,7 +1011,7 @@ protected override Expression VisitMember(MemberExpression node)
// compiler-generated types as they are typically the
// anonymous types generated to build up the query expressions.
if (node.Expression.NodeType == ExpressionType.Parameter &&
node.Expression.Type.GetTypeInfo().GetCustomAttribute<CompilerGeneratedAttribute>(false) != null)
node.Expression.Type.GetTypeInfo().IsDefined(typeof(CompilerGeneratedAttribute), false))
{
var memberType = node.Member is FieldInfo ?
((FieldInfo)node.Member).FieldType :
Expand Down