Skip to content

Commit

Permalink
Switch to new Can{Read,Write} methods almost everywhere
Browse files Browse the repository at this point in the history
Ideally, we'd use the new methods almost everywhere as they're more
accurate than the plain `PropertyInfo.Can{Read,Write}` properties. Un-
fortunately, there are some places in the code base where it's not
immediately obvious whether conversion will change Moq's behavior. The
changes made in this commit however seem fairly safe.
  • Loading branch information
stakx committed Oct 19, 2019
1 parent eae318b commit da1973b
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/Moq/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ internal static PropertyInfo GetReboundProperty(this MemberExpression expression
.SingleOrDefault(p => p.PropertyType == property.PropertyType);
if (derivedProperty != null)
{
if ((derivedProperty.CanRead && derivedProperty.GetGetMethod(true).GetBaseDefinition() == property.GetGetMethod(true)) ||
(derivedProperty.CanWrite && derivedProperty.GetSetMethod(true).GetBaseDefinition() == property.GetSetMethod(true)))
if ((derivedProperty.CanRead(out var getter) && getter.GetBaseDefinition() == property.GetGetMethod(true)) ||
(derivedProperty.CanWrite(out var setter) && setter.GetBaseDefinition() == property.GetSetMethod(true)))
return derivedProperty;
}
}
Expand Down
16 changes: 2 additions & 14 deletions src/Moq/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,24 +250,12 @@ public static bool CanOverride(this MethodBase method)

public static bool CanOverrideGet(this PropertyInfo property)
{
if (property.CanRead)
{
var getter = property.GetGetMethod(true);
return getter != null && getter.CanOverride();
}

return false;
return property.CanRead(out var getter) && getter.CanOverride();
}

public static bool CanOverrideSet(this PropertyInfo property)
{
if (property.CanWrite)
{
var setter = property.GetSetMethod(true);
return setter != null && setter.CanOverride();
}

return false;
return property.CanWrite(out var setter) && setter.CanOverride();
}

public static IEnumerable<MethodInfo> GetMethods(this Type type, string name)
Expand Down
2 changes: 1 addition & 1 deletion src/Moq/Linq/MockSetupsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private static Expression ConvertToSetupProperty(Expression targetObject, Expres
// where Mock.Get(foo).SetupProperty(mock => mock.Name, "bar") != null

// if the property is readonly, we can only do a Setup(...) which is the same as a method setup.
if (!propertyInfo.CanWrite || propertyInfo.GetSetMethod(true).IsPrivate)
if (!propertyInfo.CanWrite(out var setter) || setter.IsPrivate)
return ConvertToSetup(targetObject, left, right);

// This will get up to and including the Mock.Get(foo).Setup(mock => mock.Name) call.
Expand Down
2 changes: 1 addition & 1 deletion src/Moq/Mock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ internal void AddInnerMockSetup(MethodInfo method, IReadOnlyList<Expression> arg
var property = expression.ToPropertyInfo();
Guard.CanRead(property);

Debug.Assert(method == property.GetGetMethod(true));
Debug.Assert(property.CanRead(out var getter) && method == getter);
}

this.Setups.Add(new InnerMockSetup(new InvocationShape(expression, method, arguments, exactGenericTypeArguments: true), returnValue));
Expand Down
4 changes: 2 additions & 2 deletions src/Moq/Protected/ProtectedAsMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@ private static bool IsCorrespondingProperty(PropertyInfo duckProperty, PropertyI
{
return candidateTargetProperty.Name == duckProperty.Name
&& candidateTargetProperty.PropertyType == duckProperty.PropertyType
&& candidateTargetProperty.CanRead == duckProperty.CanRead
&& candidateTargetProperty.CanWrite == duckProperty.CanWrite;
&& candidateTargetProperty.CanRead(out _) == duckProperty.CanRead(out _)
&& candidateTargetProperty.CanWrite(out _) == duckProperty.CanWrite(out _);

// TODO: parameter lists should be compared, too, to properly support indexers.
}
Expand Down
4 changes: 2 additions & 2 deletions src/Moq/Protected/ProtectedMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ private static void ThrowIfPublicMethod(MethodInfo method, string reflectedTypeN

private static void ThrowIfPublicGetter(PropertyInfo property, string reflectedTypeName)
{
if (property.CanRead && property.GetGetMethod() != null)
if (property.CanRead(out var getter) && getter.IsPublic)
{
throw new ArgumentException(string.Format(
CultureInfo.CurrentCulture,
Expand All @@ -342,7 +342,7 @@ private static void ThrowIfPublicGetter(PropertyInfo property, string reflectedT

private static void ThrowIfPublicSetter(PropertyInfo property, string reflectedTypeName)
{
if (property.CanWrite && property.GetSetMethod() != null)
if (property.CanWrite(out var setter) && setter.IsPublic)
{
throw new ArgumentException(string.Format(
CultureInfo.CurrentCulture,
Expand Down

0 comments on commit da1973b

Please sign in to comment.