forked from devlooped/moq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "set" methods to ProtectedAsMock
- Loading branch information
1 parent
a6fde8b
commit c7df19e
Showing
5 changed files
with
234 additions
and
1 deletion.
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
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,52 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
|
||
namespace Moq.Protected | ||
{ | ||
internal sealed class DuckSetterReplacer<TMock, TAnalog> : ExpressionVisitor | ||
{ | ||
private ParameterExpression parameterToReplace; | ||
private ParameterExpression mockParameter; | ||
private Type mockType = typeof(TMock); | ||
|
||
private PropertyInfo GetMockProperty(PropertyInfo property) | ||
{ | ||
return mockType.GetProperty( | ||
property.Name, | ||
BindingFlags.NonPublic | BindingFlags.Instance, | ||
null, | ||
property.PropertyType, | ||
property.GetIndexParameters().Select(p => p.ParameterType).ToArray(), | ||
new ParameterModifier[] { } | ||
); | ||
} | ||
|
||
protected override Expression VisitIndex(IndexExpression node) | ||
{ | ||
if (node.Object is ParameterExpression parameterExpression && parameterExpression == parameterToReplace) | ||
{ | ||
return Expression.MakeIndex(mockParameter, GetMockProperty(node.Indexer), node.Arguments); | ||
} | ||
return base.VisitIndex(node); | ||
} | ||
|
||
protected override Expression VisitMember(MemberExpression node) | ||
{ | ||
if (node.Expression is ParameterExpression parameterExpression && parameterExpression == parameterToReplace) | ||
{ | ||
return Expression.MakeMemberAccess(mockParameter, GetMockProperty(node.Member as PropertyInfo)); | ||
} | ||
return base.VisitMember(node); | ||
} | ||
|
||
public Expression<Action<TMock>> Replace(Expression<Action<TAnalog>> expression) | ||
{ | ||
parameterToReplace = expression.Parameters[0]; | ||
mockParameter = Expression.Parameter(typeof(TMock), parameterToReplace.Name); | ||
return Expression.Lambda<Action<TMock>>(expression.Body.Apply(this), mockParameter); | ||
} | ||
|
||
} | ||
} |
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