本文整理汇总了C#中ISpecimenContext类的典型用法代码示例。如果您正苦于以下问题:C# ISpecimenContext类的具体用法?C# ISpecimenContext怎么用?C# ISpecimenContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ISpecimenContext类属于命名空间,在下文中一共展示了ISpecimenContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Create
/// <summary>
/// Creates a new MailAddress.
/// </summary>
/// <param name="request">The request that describes what to create.</param>
/// <param name="context">A context that can be used to create other specimens.</param>
/// <returns>
/// The requested specimen if possible; otherwise a <see cref="NoSpecimen"/> instance.
/// </returns>
/// <remarks>>
/// The generated MailAddress will have one of the reserved domains,
/// so as to avoid any possibility of tests bothering real email addresses
/// </remarks>
public object Create(object request, ISpecimenContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (!typeof(MailAddress).Equals(request))
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
try
{
return TryCreateMailAddress(request, context);
}
catch (FormatException)
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
}
示例2: Create
public object Create(object request, ISpecimenContext context)
{
var specimen = _postprocessor.Create(request, context);
if (specimen.GetType() == typeof (NoSpecimen))
{
return specimen;
}
var mockedType = GetMockedType(specimen);
var methodsWithTasks = GetMethodsWithTasks(mockedType);
var isAny = typeof (It).GetMethod("IsAny");
foreach (var method in methodsWithTasks)
{
var mockSetupMethod = GetMockSetupMethod(mockedType, method.ReturnType);
var mockReturnMethod = GetMockReturnsMethod(mockedType, method.ReturnType);
var returnTaskType = GetReturnType(method.ReturnType);
var returnValue = context.Resolve(returnTaskType);
var returnTask = typeof (Task).GetMethod("FromResult")
.MakeGenericMethod(returnTaskType)
.Invoke(null, new object[] {returnValue});
var parameters = method.GetParameters()
.Select(info =>(Expression) Expression.Constant(isAny.MakeGenericMethod(info.ParameterType).Invoke(null, new object[] {})))
.ToArray();
var parameter = Expression.Parameter(mockedType);
var body = Expression.Call(parameter, method, parameters);
var lambda = Expression.Lambda(body, parameter);
var setup = mockSetupMethod.Invoke(specimen, new object[] {lambda});
mockReturnMethod.Invoke(setup, new object[] {returnTask});
}
return specimen;
}
示例3: Create
/// <summary>
/// Creates a new array based on a request.
/// </summary>
/// <param name="request">The request that describes what to create.</param>
/// <param name="context">A context that can be used to create other specimens.</param>
/// <returns>
/// An array of the requested type if possible; otherwise a <see cref="NoSpecimen"/>
/// instance.
/// </returns>
/// <remarks>
/// <para>
/// If <paramref name="request"/> is a request for an array and <paramref name="context"/>
/// can satisfy a <see cref="MultipleRequest"/> for the element type, the return value is a
/// populated array of the requested type. If not, the return value is a
/// <see cref="NoSpecimen"/> instance.
/// </para>
/// </remarks>
public object Create(object request, ISpecimenContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
// This is performance-sensitive code when used repeatedly over many requests.
// See discussion at https://github.com/AutoFixture/AutoFixture/pull/218
var type = request as Type;
if (type == null)
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
if (!type.IsArray)
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
var elementType = type.GetElementType();
var specimen = context.Resolve(new MultipleRequest(elementType));
if (specimen is OmitSpecimen)
return specimen;
var elements = specimen as IEnumerable;
if (elements == null)
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
return ArrayRelay.ToArray(elements, elementType);
}
示例4: Create
/// <summary>
/// Creates a new specimen based on a requested range.
/// </summary>
/// <param name="request">The request that describes what to create.</param>
/// <param name="context">A container that can be used to create other specimens.</param>
/// <returns>
/// A specimen created from a <see cref="RangedNumberRequest"/> encapsulating the operand
/// type, the minimum and the maximum of the requested number, if possible; otherwise,
/// a <see cref="NoSpecimen"/> instance.
/// </returns>
public object Create(object request, ISpecimenContext context)
{
if (request == null)
{
return new NoSpecimen();
}
if (context == null)
{
throw new ArgumentNullException("context");
}
var customAttributeProvider = request as ICustomAttributeProvider;
if (customAttributeProvider == null)
{
return new NoSpecimen(request);
}
var rangeAttribute = customAttributeProvider.GetCustomAttributes(typeof(RangeAttribute), inherit: true).Cast<RangeAttribute>().SingleOrDefault();
if (rangeAttribute == null)
{
return new NoSpecimen(request);
}
return context.Resolve(RangeAttributeRelay.Create(rangeAttribute, request));
}
示例5: Create
/// <summary>
/// Creates an anonymous string based on a seed.
/// </summary>
/// <param name="request">The request that describes what to create.</param>
/// <param name="context">A context that can be used to create other specimens.</param>
/// <returns>
/// A string with the seed prefixed to a string created by <paramref name="context"/> if
/// possible; otherwise, <see langword="null"/>.
/// </returns>
/// <remarks>
/// <para>
/// This method only returns an instance if a number of conditions are satisfied.
/// <paramref name="request"/> must represent a request for a seed string, and
/// <paramref name="context"/> must be able to create a string.
/// </para>
/// </remarks>
public object Create(object request, ISpecimenContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var seededRequest = request as SeededRequest;
if (seededRequest == null ||
(!seededRequest.Request.Equals(typeof(string))))
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
var seed = seededRequest.Seed as string;
if (seed == null)
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
var containerResult = context.Resolve(typeof(string));
if (containerResult is NoSpecimen)
{
return containerResult;
}
return seed + containerResult;
}
示例6: Create
/// <summary>
/// Creates a substitute when request is an abstract type.
/// </summary>
/// <returns>
/// A substitute resolved from the <paramref name="context"/> when <paramref name="request"/> is an abstract
/// type or <see cref="NoSpecimen"/> for all other requests.
/// </returns>
/// <exception cref="InvalidOperationException">
/// An attempt to resolve a substitute from the <paramref name="context"/> returned an object that was not
/// created by NSubstitute.
/// </exception>
public object Create(object request, ISpecimenContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
var requestedType = request as Type;
if (requestedType == null || !requestedType.IsAbstract)
{
return new NoSpecimen(request);
}
object substitute = context.Resolve(new SubstituteRequest(requestedType));
try
{
SubstitutionContext.Current.GetCallRouterFor(substitute);
}
catch (NotASubstituteException e)
{
throw new InvalidOperationException(
string.Format(
CultureInfo.CurrentCulture,
"Object resolved by request for substitute of {0} was not created by NSubstitute. " +
"Ensure that {1} was added to Fixture.Customizations.",
requestedType.FullName, typeof(SubstituteRequestHandler).FullName),
e);
}
return substitute;
}
示例7: Create
/// <summary>
/// Creates a relayed request based on the <see cref="SubstituteAttribute"/> applied to a code element and
/// resolves it from the given <paramref name="context"/>.
/// </summary>
/// <returns>
/// A specimen resolved from the <paramref name="context"/> based on a relayed request.
/// If the <paramref name="request"/> code element does not have <see cref="SubstituteAttribute"/> applied,
/// returns <see cref="NoSpecimen"/>.
/// </returns>
public object Create(object request, ISpecimenContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
var customAttributeProvider = request as ICustomAttributeProvider;
if (customAttributeProvider == null)
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
var attribute = customAttributeProvider.GetCustomAttributes(typeof(SubstituteAttribute), true)
.OfType<SubstituteAttribute>().FirstOrDefault();
if (attribute == null)
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
object substituteRequest = CreateSubstituteRequest(customAttributeProvider, attribute);
return context.Resolve(substituteRequest);
}
示例8: Create
/// <summary>
/// Creates a new specimen based on a specified length of characters that are allowed.
/// </summary>
/// <param name="request">The request that describes what to create.</param>
/// <param name="context">A container that can be used to create other specimens.</param>
/// <returns>
/// A specimen created from a <see cref="RangedNumberRequest"/> encapsulating the operand
/// type, the minimum and the maximum of the requested number, if possible; otherwise,
/// a <see cref="NoSpecimen"/> instance.
/// </returns>
public object Create(object request, ISpecimenContext context)
{
if (request == null)
{
return new NoSpecimen();
}
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var customAttributeProvider = request as ICustomAttributeProvider;
if (customAttributeProvider == null)
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
var stringLengthAttribute = customAttributeProvider.GetCustomAttributes(typeof(StringLengthAttribute), inherit: true).Cast<StringLengthAttribute>().SingleOrDefault();
if (stringLengthAttribute == null)
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
return context.Resolve(new ConstrainedStringRequest(stringLengthAttribute.MinimumLength, stringLengthAttribute.MaximumLength));
}
示例9: Create
/// <summary>
/// Creates a new specimen based on a request and raises events tracing the progress.
/// </summary>
/// <param name="request">The request that describes what to create.</param>
/// <param name="context">A context that can be used to create other specimens.</param>
/// <returns>
/// The requested specimen if possible; otherwise a <see cref="NoSpecimen"/> instance.
/// </returns>
/// <remarks>
/// <para>
/// The <paramref name="request"/> can be any object, but will often be a
/// <see cref="Type"/> or other <see cref="System.Reflection.MemberInfo"/> instances.
/// </para>
/// </remarks>
public object Create(object request, ISpecimenContext context)
{
var isFilterSatisfied = this.filter.IsSatisfiedBy(request);
if (isFilterSatisfied)
{
this.OnSpecimenRequested(new RequestTraceEventArgs(request, ++this.depth));
}
bool specimenWasCreated = false;
object specimen = null;
try
{
specimen = this.Builder.Create(request, context);
specimenWasCreated = true;
return specimen;
}
finally
{
if (isFilterSatisfied)
{
if (specimenWasCreated)
{
this.OnSpecimenCreated(new SpecimenCreatedEventArgs(request, specimen, this.depth));
}
this.depth--;
}
}
}
示例10: GetRandomNumberOfTicks
private long GetRandomNumberOfTicks(ISpecimenContext context)
{
var randomNumberOfTicks = (long)_randomizer.Create(typeof(long), context);
if (_noFractions)
randomNumberOfTicks = randomNumberOfTicks - (randomNumberOfTicks % TimeSpan.TicksPerSecond);
return randomNumberOfTicks;
}
示例11: Create
public object Create(object request, ISpecimenContext context)
{
var customAttributeProvider = request as ICustomAttributeProvider;
if (customAttributeProvider == null)
{
return new NoSpecimen(request);
}
var attribute =
customAttributeProvider.GetCustomAttributes(typeof(ContentAttribute), true)
.OfType<ContentAttribute>()
.FirstOrDefault();
if (attribute == null)
{
return new NoSpecimen(request);
}
var parameterInfo = request as ParameterInfo;
if (parameterInfo == null)
{
return new NoSpecimen(request);
}
return context.Resolve(parameterInfo.ParameterType);
}
示例12: Create
/// <summary>
/// Creates a new specimen by delegating to <see cref="Builders"/>.
/// </summary>
/// <param name="request">The request that describes what to create.</param>
/// <param name="context">A container that can be used to create other specimens.</param>
/// <returns>The first result created by <see cref="Builders"/>.</returns>
public object Create(object request, ISpecimenContext context)
{
return (from b in this.Builders
let result = b.Create(request, context)
where !(result is NoSpecimen)
select result).DefaultIfEmpty(new NoSpecimen(request)).FirstOrDefault();
}
示例13: Execute
/// <summary>
/// Sets up a mocked object's methods so that the return values will be retrieved from a fixture,
/// instead of being created directly by Moq.
/// </summary>
/// <param name="specimen">The mock to setup.</param>
/// <param name="context">The context of the mock.</param>
public void Execute(object specimen, ISpecimenContext context)
{
if (specimen == null) throw new ArgumentNullException("specimen");
if (context == null) throw new ArgumentNullException("context");
var mock = specimen as Mock;
if (mock == null)
return;
var mockType = mock.GetType();
var mockedType = mockType.GetMockedType();
var methods = GetConfigurableMethods(mockedType);
foreach (var method in methods)
{
var returnType = method.ReturnType;
var methodInvocationLambda = MakeMethodInvocationLambda(mockedType, method, context);
if (method.IsVoid())
{
//call `Setup`
mock.Setup(methodInvocationLambda);
}
else
{
//call `Setup`
var setup = mock.Setup(returnType, methodInvocationLambda);
//call `Returns`
setup.ReturnsUsingContext(context, mockedType, returnType);
}
}
}
示例14: GetRandomNumberOfTicks
private long GetRandomNumberOfTicks(ISpecimenContext context)
{
long randomValue = (long)this.randomizer.Create(typeof(long), context);
long correctValue = randomValue / this.accuracy * this.accuracy;
return correctValue;
}
示例15: Create
/// <summary>
/// Creates a new specimen based on a request.
/// </summary>
/// <param name="request">The request that describes what to create.</param>
/// <param name="context">A context that can be used to create other specimens.</param>
/// <returns>
/// The requested specimen if possible; otherwise a <see cref="NoSpecimen"/> instance.
/// </returns>
public object Create(object request, ISpecimenContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (!typeof(Uri).Equals(request))
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
var scheme = context.Resolve(typeof(UriScheme)) as UriScheme;
if (scheme == null)
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
var authority = context.Resolve(typeof(string)) as string;
if (authority == null)
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
return UriGenerator.CreateAnonymous(scheme, authority);
}