protected override byte CreateValue(ValueProviderContext <byte> context) { var randomBytes = new byte[1]; context.Random.NextBytes(randomBytes); return(randomBytes[0]); }
protected override Guid CreateValue(ValueProviderContext <Guid> context) { var guid = new byte[16]; context.Random.NextBytes(guid); return(new Guid(guid)); }
protected override DateTime CreateValue(ValueProviderContext <DateTime> context) { var start = new DateTime(1900, 1, 1); var end = new DateTime(2100, 1, 1); var range = (end - start).Days; return(start.AddDays(context.Random.Next(range)).AddTicks(context.Random.Next(0, TimeSpan.TicksPerDay))); }
Arrangement <RandomSyllabileGenerator, Dummy, object, object> SeededContext(int seed) { return(x => { var customValueProviderObjectContext = new CustomValueProviderObjectContext(new DefaultRandom(seed)); var customValueProviderContext = new CustomValueProviderContext(customValueProviderObjectContext); Context = customValueProviderContext; }); }
protected override AbstractVehicle CreateValue(ValueProviderContext <AbstractVehicle> context) { return(context.TargetValueType == typeof(AirVehicle) ? (AbstractVehicle) new AirVehicle { Engine = new PropellorEngine() } : new LandVehicle { Tire = new Tire() }); }
/// <summary> /// Fills a stringBuilder within the given context with a random syllabile /// </summary> /// <param name="context"></param> /// <param name="stringBuilder"></param> public void Fill(ValueProviderContext <string> context, StringBuilder stringBuilder) { var len = context.Random.Next(_min, _max); for (var i = 0; i < len; i++) { var c = i % 2 == 0 ? s_consonants[context.Random.Next(s_consonants.Length)] : s_vowels[context.Random.Next(s_vowels.Length)]; stringBuilder.Append(c); } }
protected override IEnumerable <TMember> CreateManyValues( ValueProviderContext <TMember> context, [CanBeNull] IList <object> metadatas, int itemCount) { var typeInfo = context.Advanced.FastReflection.GetTypeInfo(context.Advanced.Key.Type); var ctorValuesCollections = InitializeCtorValues(itemCount, typeInfo); var ctorMembers = typeInfo.CtorArguments.Select( c => c.ToMember(context.Advanced.ParameterConversionService)).ToList(); var sortedCtorMembers = context.Advanced.MemberSorter.Sort(ctorMembers, context.Advanced.Key).ToList(); //Note: We use a dictionary because when we have many ctor members, we gain some performance. var toUnsortedDictionary = ctorMembers.Select((value, index) => new { value, index }).ToDictionary(x => x.value, x => x.index); var sortedToUnsorted = sortedCtorMembers.Select(member => toUnsortedDictionary[member]).ToList(); List <object> resolvedMetadatas = null; for (var argumentIndex = 0; argumentIndex < sortedCtorMembers.Count; argumentIndex++) { var ctorMember = sortedCtorMembers[argumentIndex]; var unsortedArgumentIndex = sortedToUnsorted[argumentIndex]; var memberKey = context.Advanced.Key.CreateKey(ctorMember); //we only resolve once per parent (not for each member), so the data is shared between all members. if (resolvedMetadatas == null && context.Advanced.MetadataResolver.NeedsMetadata(memberKey)) { var dependendArguments = sortedCtorMembers.Take(argumentIndex).ToList(); var metadataContexts = GetMetadataContexts( context.Advanced.Key, dependendArguments, ctorValuesCollections, sortedToUnsorted, context.TestDataGenerator).ToList(); resolvedMetadatas = context.Advanced.MetadataResolver.Resolve(memberKey, metadataContexts).ToList(); } //Note: Here we have a recursion to the compound value provider. e.g. other immutable types could be a ctor argument var ctorMemberValues = context.Advanced.AdvancedTestDataGenerator.CreateMany(memberKey, resolvedMetadatas, itemCount, maxRecursionDepth: 2); for (var valueIndex = 0; valueIndex < ctorMemberValues.Count; valueIndex++) { ctorValuesCollections[valueIndex][unsortedArgumentIndex] = ctorMemberValues[valueIndex]; } } var typeFactoryWithArguments = FastActivator.GetFactory(context.Advanced.Key.Type, typeInfo.CtorArguments); return(ctorValuesCollections.Select(ctorValues => typeFactoryWithArguments(ctorValues)).Cast <TMember>()); }
protected override Enum CreateValue(ValueProviderContext <Enum> context) { var enumNames = Enum.GetNames(context.TargetValueType); if (enumNames.Length == 0) { return(null); } var randomValue = enumNames[context.Random.Next(enumNames.Length)]; return((Enum)Enum.Parse(context.TargetValueType, randomValue)); }
protected override IList <TResult> CreateValue([NotNull] ValueProviderContext <IList <TResult> > context) { var exclusiveMaxItemCount = _maxItemCount + 1; var numberOfReturnedItems = context.Random.Next(_minItemCount, exclusiveMaxItemCount); Trace.Assert(numberOfReturnedItems <= _items.Count); Func <int> randomIntGenerator = () => context.Random.Next(int.MinValue, int.MaxValue); return(Enumerable.Range(0, _items.Count) .Shuffle(randomIntGenerator) .Take(numberOfReturnedItems) .Select(i => _items[i]) .Select(_conversionFunc) .ToList()); }
protected override AbstractVehicle CreateValue(ValueProviderContext <AbstractVehicle> context) { if (context.TargetValueType == typeof(AirVehicle)) { return(new AirVehicle { Engine = new JetEngine { FuelUsePerSecond = 20 } }); } if (context.TargetValueType == typeof(LandVehicle)) { return(new LandVehicle { Tire = new Tire { Diameter = 10 } }); } throw new InvalidOperationException("property of type " + context.TargetValueType + " is not supported by " + GetType().FullName); }
protected override TMember CreateValue(ValueProviderContext <TMember> context) { //we implement it like this, to be able to make some performance optimizations in the create many method. return(CreateManyValues(context, context.InternalMetadata == null ? null : new[] { context.InternalMetadata }, 1).Single()); }
protected override bool CreateValue(ValueProviderContext <bool> context) { return(context.Random.NextDouble() >= 0.5d); }
protected override sbyte CreateValue(ValueProviderContext <sbyte> context) { return((sbyte)context.Random.Next(sbyte.MinValue, sbyte.MaxValue)); }
protected override TMember CreateValue(ValueProviderContext <TMember> context) { return(_valueFunc(context)); }
protected override object CreateValue(ValueProviderContext <object> context) { return(_valueFunc(context)); }
protected override decimal CreateValue(ValueProviderContext <decimal> context) { return(context.Random.Next(decimal.MinValue)); }
protected override short CreateValue(ValueProviderContext <short> context) { return(context.Random.NextShort()); }
protected override IVector CreateValue(ValueProviderContext <IVector> context) { return(new Vector { Id = _vectorId }); }
protected override TResult CreateValue([NotNull] ValueProviderContext <TResult> context) { var item = _items[context.Random.Next(0, _items.Count)]; return(_conversionFunc(item)); }
protected override char CreateValue(ValueProviderContext <char> context) { return(context.Random.NextDouble() >= 0.2d ? (char)context.Random.Next(33, 126) : (char)context.Random.Next(161, 591)); }