public static bool TryFind(this IEnumerable <Setup> innerMockSetups, Invocation invocation, out Setup setup) { foreach (Setup innerMockSetup in innerMockSetups) { if (innerMockSetup.Matches(invocation)) { setup = innerMockSetup; return(true); } } setup = default; return(false); }
private static readonly int AccessorPrefixLength = "?et_".Length; // get_ or set_ public static bool Handle(Invocation invocation, Mock mock) { if (mock.AutoSetupPropertiesDefaultValueProvider == null) { return(false); } MethodInfo invocationMethod = invocation.Method; if (invocationMethod.IsPropertyAccessor()) { string propertyNameToSearch = invocationMethod.Name.Substring(AccessorPrefixLength); PropertyInfo property = invocationMethod.DeclaringType.GetProperty(propertyNameToSearch); if (property == null) { return(false); } var expression = GetPropertyExpression(invocationMethod.DeclaringType, property); object propertyValue; Setup getterSetup = null; if (property.CanRead(out var getter)) { if (ProxyFactory.Instance.IsMethodVisible(getter, out _)) { propertyValue = CreateInitialPropertyValue(mock, getter); getterSetup = new AutoImplementedPropertyGetterSetup(mock, expression, getter, () => propertyValue); mock.MutableSetups.Add(getterSetup); } // If we wanted to optimise for speed, we'd probably be forgiven // for removing the above `IsMethodVisible` guard, as it's rather // unlikely to encounter non-public getters such as the following // in real-world code: // // public T Property { internal get; set; } // // Usually, it's only the setters that are made non-public. For // now however, we prefer correctness. } Setup setterSetup = null; if (property.CanWrite(out var setter)) { if (ProxyFactory.Instance.IsMethodVisible(setter, out _)) { setterSetup = new AutoImplementedPropertySetterSetup(mock, expression, setter, (newValue) => { propertyValue = newValue; }); mock.MutableSetups.Add(setterSetup); } } Setup setupToExecute = invocationMethod.IsGetAccessor() ? getterSetup : setterSetup; setupToExecute.Execute(invocation); return(true); } else { return(false); } }
public static bool TryFind(this IEnumerable <Setup> innerMockSetups, InvocationShape expectation, out Setup setup) { Debug.Assert(innerMockSetups.All(s => s.ReturnsInnerMock(out _))); foreach (Setup innerMockSetup in innerMockSetups) { if (innerMockSetup.Expectation.Equals(expectation)) { setup = innerMockSetup; return(true); } } setup = default; return(false); }
internal void MarkAsMatchedBy(Setup setup) { Debug.Assert(this.matchingSetup == null); this.matchingSetup = setup; }