本文整理汇总了C#中ISpecimenContext类的典型用法代码示例。如果您正苦于以下问题:C# ISpecimenContext类的具体用法?C# ISpecimenContext怎么用?C# ISpecimenContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ISpecimenContext类属于命名空间,在下文中一共展示了ISpecimenContext类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
}
}
开发者ID:RyanLiu99,项目名称:AutoFixture,代码行数:37,代码来源:MailAddressGenerator.cs
示例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;
}
开发者ID:Galad,项目名称:Hanno,代码行数:30,代码来源:AsyncMoqCustomization.cs
示例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);
}
开发者ID:Tungsten78,项目名称:AutoFixture,代码行数:46,代码来源:ArrayRelay.cs
示例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));
}
开发者ID:nrjohnstone,项目名称:AutoFixture,代码行数:36,代码来源:RangeAttributeRelay.cs
示例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;
}
开发者ID:Tungsten78,项目名称:AutoFixture,代码行数:48,代码来源:StringSeedRelay.cs
示例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;
}
开发者ID:kevintan1983,项目名称:AutoFixture,代码行数:43,代码来源:SubstituteRelay.cs
示例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);
}
开发者ID:RyanLiu99,项目名称:AutoFixture,代码行数:36,代码来源:SubstituteAttributeRelay.cs
示例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));
}
开发者ID:AutoFixture,项目名称:AutoFixture,代码行数:40,代码来源:StringLengthAttributeRelay.cs
示例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--;
}
}
}
开发者ID:Tungsten78,项目名称:AutoFixture,代码行数:42,代码来源:TracingBuilder.cs
示例10: GetRandomNumberOfTicks
private long GetRandomNumberOfTicks(ISpecimenContext context)
{
var randomNumberOfTicks = (long)_randomizer.Create(typeof(long), context);
if (_noFractions)
randomNumberOfTicks = randomNumberOfTicks - (randomNumberOfTicks % TimeSpan.TicksPerSecond);
return randomNumberOfTicks;
}
开发者ID:PolarbearDK,项目名称:Miracle.FileZilla.Api,代码行数:7,代码来源:RandomTimeSpanSequenceGenerator.cs
示例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);
}
开发者ID:dharnitski,项目名称:Sitecore.FakeDb,代码行数:25,代码来源:ContentAttributeRelay.cs
示例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();
}
开发者ID:rajeshgupthar,项目名称:AutoFixture,代码行数:13,代码来源:CompositeSpecimenBuilder.cs
示例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);
}
}
}
开发者ID:jwChung,项目名称:AutoFixture,代码行数:39,代码来源:MockVirtualMethodsCommand.cs
示例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;
}
开发者ID:dwdkls,项目名称:pizzamvc,代码行数:7,代码来源:DateTimeWithAccuracySpecimenBuilder.cs
示例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);
}
开发者ID:Tungsten78,项目名称:AutoFixture,代码行数:40,代码来源:UriGenerator.cs
示例16: Create
/// <summary>
/// Creates a specimen based on a requested enumerable parameter.
/// </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 specimen created from a <see cref="SeededRequest"/> encapsulating
/// the parameter type and name of the requested parameter, if
/// possible; otherwise, a <see cref="NoSpecimen" />.
/// </returns>
/// <remarks>
/// <para>
/// This method only handles <see cref="ParameterInfo" /> instances
/// where <see cref="ParameterInfo.ParameterType" /> is
/// <see cref="IEnumerable{T}" />. If <paramref name="context" />
/// returns an <see cref="OmitSpecimen" /> instance, an empty array of
/// the correct type is returned instead.
/// </para>
/// </remarks>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="context" /> is <see langword="null"/>
/// </exception>
public object Create(object request, ISpecimenContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
var pi = request as ParameterInfo;
if (pi == null)
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
if (!pi.ParameterType.IsGenericType)
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
if (IsNotEnumerable(pi))
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
var returnValue = context.Resolve(
new SeededRequest(
pi.ParameterType,
pi.Name));
if (returnValue is OmitSpecimen)
return Array.CreateInstance(
pi.ParameterType.GetGenericArguments().Single(),
0);
return returnValue;
}
开发者ID:Tungsten78,项目名称:AutoFixture,代码行数:59,代码来源:OmitEnumerableParameterRequestRelay.cs
示例17: 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>
/// A mock instance created by FakeItEasy if appropriate; otherwise a
/// <see cref="NoSpecimen"/> instance.
/// </returns>
/// <remarks>
/// <para>
/// The Create method checks whether a request is for an interface or abstract class. If so
/// it delegates the call to the decorated <see cref="Builder"/>. When the specimen is
/// returned from the decorated <see cref="ISpecimenBuilder"/> the method checks whether
/// the returned instance is a FakeItEasy instance.
/// </para>
/// <para>
/// If all pre- and post-conditions are satisfied, the mock instance is returned; otherwise
/// a <see cref="NoSpecimen" /> instance.
/// </para>
/// </remarks>
public object Create(object request, ISpecimenContext context)
{
var type = request as Type;
if (!type.IsFake())
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
var fake = this.builder.Create(request, context) as FakeItEasy.Configuration.IHideObjectMembers;
if (fake == null)
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
var fakeType = type.GetFakedType();
if (fake.GetType().GetFakedType() != fakeType)
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
return fake;
}
开发者ID:RyanLiu99,项目名称:AutoFixture,代码行数:49,代码来源:FakeItEasyBuilder.cs
示例18: Create
/// <summary>
/// Creates a random number within the request range.
/// </summary>
/// <param name="request">The request that describes what to create. Other requests for same type and limits
/// denote the same set. </param>
/// <param name="context">A context that can be used to create other specimens.</param>
/// <returns>
/// The next random number in a range <paramref name="request"/> is a request
/// for a numeric value; 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 rangedNumberRequest = request as RangedNumberRequest;
if (rangedNumberRequest == null)
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
try
{
return SelectGenerator(rangedNumberRequest).Create(rangedNumberRequest.OperandType, context);
}
catch (ArgumentException)
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
}
开发者ID:josephdecock,项目名称:AutoFixture,代码行数:36,代码来源:RandomRangedNumberGenerator.cs
示例19: AddMany
public static void AddMany(object specimen, ISpecimenContext context)
{
if (specimen == null)
{
throw new ArgumentNullException("specimen");
}
if (context == null)
{
throw new ArgumentNullException("context");
}
var typeArguments = specimen.GetType().GetGenericArguments();
if (typeArguments.Length != 2)
{
throw new ArgumentException("The specimen must be an instance of IDictionary<TKey, TValue>.", "specimen");
}
if (!typeof(IDictionary<,>).MakeGenericType(typeArguments).IsAssignableFrom(specimen.GetType()))
{
throw new ArgumentException("The specimen must be an instance of IDictionary<TKey, TValue>.", "specimen");
}
var kvpType = typeof(KeyValuePair<,>).MakeGenericType(typeArguments);
var enumerable = context.Resolve(new MultipleRequest(kvpType)) as IEnumerable;
foreach (var item in enumerable)
{
var addMethod = typeof(ICollection<>).MakeGenericType(kvpType).GetMethod("Add", new[] { kvpType });
addMethod.Invoke(specimen, new[] { item });
}
}
开发者ID:rajeshgupthar,项目名称:AutoFixture,代码行数:30,代码来源:DictionaryFiller.cs
示例20: 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("context");
}
var seededRequest = request as SeededRequest;
if (seededRequest == null ||
seededRequest.Request != typeof(string))
{
return new NoSpecimen(request);
}
var seed = seededRequest.Seed as string;
if (seed == null)
{
return new NoSpecimen(request);
}
var containerResult = context.Resolve(typeof(string));
if (containerResult is NoSpecimen)
{
return containerResult;
}
return seed + containerResult;
}
开发者ID:rajeshgupthar,项目名称:AutoFixture,代码行数:44,代码来源:StringSeedRelay.cs
注:本文中的ISpecimenContext类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论