/// <inheritdoc /> protected override object?Generate(IExecuteStrategy executeStrategy, Type type, string?referenceName) { var zones = TimeZoneInfo.GetSystemTimeZones(); var zoneIndex = Generator.NextValue(0, zones.Count - 1); return(zones[zoneIndex]); }
protected override object?CreateInstance(IExecuteStrategy executeStrategy, Type type, string?referenceName, params object?[]?args) { throw new NotImplementedException(); }
/// <inheritdoc /> /// <exception cref="ArgumentNullException">The <paramref name="executeStrategy" /> parameter is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="type" /> parameter is <c>null</c>.</exception> protected override object?Create(IExecuteStrategy executeStrategy, Type type, string?referenceName, params object?[]?args) { executeStrategy = executeStrategy ?? throw new ArgumentNullException(nameof(executeStrategy)); type = type ?? throw new ArgumentNullException(nameof(type)); if (type.IsInterface == false) { return(CreateInstance(executeStrategy, type, referenceName, args)); } var typeToCreate = DetermineTypeToCreate(type); if (typeToCreate == null) { var format = "Unable to create type {0} using {1} because it is not compatible with {2}"; var message = string.Format(CultureInfo.CurrentCulture, format, type.FullName, nameof(EnumerableTypeCreator), "IEnumerable<T>"); var context = executeStrategy.BuildChain.Last; var buildLog = executeStrategy.Log.Output; throw new BuildException(message, type, referenceName, context, buildLog); } return(CreateInstance(executeStrategy, typeToCreate, referenceName, args)); }
/// <inheritdoc /> protected override object?Generate(IExecuteStrategy executeStrategy, Type type, string?referenceName) { var context = executeStrategy?.BuildChain?.Last; IEnumerable <Location> locations = TestData.Locations; if (context != null) { locations = FilterLocations( locations, NameExpression.Country, (item, value) => item.Country.Equals(value, StringComparison.OrdinalIgnoreCase), context); locations = FilterLocations( locations, NameExpression.State, (item, value) => item.State.Equals(value, StringComparison.OrdinalIgnoreCase), context); } var availableLocations = locations.ToList(); if (availableLocations.Count > 0) { var matchingLocation = availableLocations.Next(); return(matchingLocation.City); } // There was either no country or no match on the country var location = TestData.Locations.Next(); return(location.City); }
/// <inheritdoc /> protected override object?Generate(IExecuteStrategy executeStrategy, Type type, string?referenceName) { if (referenceName != null) { var multipleMatch = _multipleAddressExpression.Match(referenceName); if (multipleMatch.Success) { // Get the number from the match var number = int.Parse(multipleMatch.Groups["Number"].Value, CultureInfo.InvariantCulture); if (number == 1) { var floor = Generator.NextValue(1, 15); var unitIndex = Generator.NextValue(0, 15); var unit = (char)(65 + unitIndex); // Return a Unit Xy, Floor X style value return("Unit " + floor + unit + ", Floor " + floor); } if (number > 2) { // This generator will only populate the first two address lines return(string.Empty); } } } var addressNumber = Generator.NextValue(1, 1500); var location = TestData.Locations.Next(); return(addressNumber + " " + location.StreetName + " " + location.StreetSuffix); }
/// <inheritdoc /> protected override object?CreateInstance(IExecuteStrategy executeStrategy, Type type, string?referenceName, params object?[]?args) { if (args?.Length > 0) { // We have arguments supplied so we will assume that they may the resolve type return(Activator.CreateInstance(type, args)); } // Use constructor detection to figure out how to create this instance var constructorResolver = executeStrategy.Configuration.ConstructorResolver; // We aren't provided with arguments so we need to resolve the most appropriate constructor var constructor = constructorResolver.Resolve(type); if (constructor == null) { // Structs return null for a default constructor return(Activator.CreateInstance(type)); } // Create the arguments for the constructor we have found var builtArgs = executeStrategy.CreateParameters(constructor); return(constructor.Invoke(builtArgs)); }
/// <inheritdoc /> protected override object?Generate(IExecuteStrategy executeStrategy, Type type, string?referenceName) { type = type ?? throw new ArgumentNullException(nameof(type)); executeStrategy = executeStrategy ?? throw new ArgumentNullException(nameof(executeStrategy)); var generateType = type; if (generateType.IsNullable()) { if (AllowNull) { // Allow for a 10% the chance that this might be null var range = Generator.NextValue(0, 100000); if (range < 10000) { return(null); } } // Hijack the type to generator so we can continue with the normal code pointed at the correct type to generate generateType = type.GetGenericArguments()[0]; } var context = executeStrategy.BuildChain?.Last; var min = GetMinimum(generateType, referenceName, context); var max = GetMaximum(generateType, referenceName, context); return(Generator.NextValue(generateType, min, max)); }
/// <inheritdoc /> protected override object?Generate(IExecuteStrategy executeStrategy, Type type, string?referenceName) { var context = executeStrategy?.BuildChain?.Last; IEnumerable <Location> locations = TestData.Locations; if (context != null) { var country = GetValue <string>(NameExpression.Country, context); if (string.IsNullOrWhiteSpace(country) == false) { locations = locations .Where(x => x.Country.Equals(country, StringComparison.OrdinalIgnoreCase)).ToList(); } } var availableLocations = locations.ToList(); if (availableLocations.Count > 0) { var matchingLocation = availableLocations.Next(); return(matchingLocation.State); } // There was either no country or no match on the country var location = TestData.Locations.Next(); return(location.State); }
protected override object?CreateInstance(IExecuteStrategy executeStrategy, Type type, string?referenceName, params object?[]?args) { return(new List <string>()); }
/// <inheritdoc /> /// <exception cref="ArgumentNullException">The <paramref name="instance" /> parameter is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="executeStrategy" /> parameter is <c>null</c>.</exception> public virtual object Populate(IExecuteStrategy executeStrategy, object instance) { instance = instance ?? throw new ArgumentNullException(nameof(instance)); executeStrategy = executeStrategy ?? throw new ArgumentNullException(nameof(executeStrategy)); if (executeStrategy.BuildChain == null) { throw new InvalidOperationException(Resources.ExecuteStrategy_NoBuildChain); } if (CanPopulate(executeStrategy.Configuration, executeStrategy.BuildChain, instance.GetType(), null) == false) { var message = string.Format( CultureInfo.CurrentCulture, Resources.Error_GenerationNotSupportedFormat, GetType().FullName, instance.GetType(), "<null>"); throw new NotSupportedException(message); } // The default will be to not do any additional population of the instance return(PopulateInstance(executeStrategy, instance)); }
/// <inheritdoc /> protected override object?Generate(IExecuteStrategy executeStrategy, Type type, string?referenceName) { if (type == typeof(bool?)) { var source = Generator.NextValue <double>(0, 3); bool?value; if (source < 1) { value = false; } else if (source < 2) { value = true; } else { value = null; } return(value); } var nextValue = Generator.NextValue(0, 1); if (nextValue == 0) { return(false); } return(true); }
/// <summary> /// Creates an instance of the type with the specified arguments. /// </summary> /// <param name="executeStrategy">The execution strategy.</param> /// <param name="type">The type to evaluate.</param> /// <param name="referenceName">The property or parameter name to evaluate.</param> /// <param name="args">The constructor parameters to create the instance with.</param> /// <returns>A new instance.</returns> /// <exception cref="ArgumentNullException">The <paramref name="type" /> parameter is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="executeStrategy" /> parameter is <c>null</c>.</exception> protected virtual object?Create(IExecuteStrategy executeStrategy, Type type, string?referenceName, params object?[]?args) { type = type ?? throw new ArgumentNullException(nameof(type)); executeStrategy = executeStrategy ?? throw new ArgumentNullException(nameof(executeStrategy)); if (executeStrategy.BuildChain == null) { throw new InvalidOperationException(Resources.ExecuteStrategy_NoBuildChain); } var buildType = ResolveBuildType(executeStrategy.Configuration, type); if (CanCreate(executeStrategy.Configuration, executeStrategy.BuildChain, buildType, referenceName) == false) { var message = string.Format( CultureInfo.CurrentCulture, Resources.Error_GenerationNotSupportedFormat, GetType().FullName, type.FullName, referenceName ?? "<null>"); throw new NotSupportedException(message); } if (buildType != type) { executeStrategy.Log.MappedType(type, buildType); } return(CreateInstance(executeStrategy, buildType, referenceName, args)); }
/// <inheritdoc /> /// <exception cref="ArgumentNullException">The <paramref name="type" /> parameter is <c>null</c>.</exception> protected override object?CreateInstance(IExecuteStrategy executeStrategy, Type type, string?referenceName, params object?[]?args) { type = type ?? throw new ArgumentNullException(nameof(type)); var count = Generator.NextValue(MinCount, MaxCount); var parameters = new object[] { count }; // Array has a dark-magic constructor that takes an int to define the size of the array var parameterTypes = new[] { typeof(int) }; var constructor = type.GetConstructor(parameterTypes); if (constructor == null) { throw new BuildException($"No constructor was found that matches the parameters (int)"); } return(constructor.Invoke(parameters)); }
protected override object?CreateInstance(IExecuteStrategy executeStrategy, Type type, string?referenceName, params object?[]?args) { // Resolve the type being created var typeToCreate = ResolveBuildType(executeStrategy.Configuration, type); if (args?.Length > 0) { // We have arguments supplied so we will assume that they may the resolve type return(Activator.CreateInstance(typeToCreate, args)); } // Use constructor detection to figure out how to create this instance var constructorResolver = executeStrategy.Configuration.ConstructorResolver; // We aren't provided with arguments so we need to resolve the most appropriate constructor // The constructor must exist because the base class has already validated that it could be resolved var constructor = constructorResolver.Resolve(typeToCreate) !; // Create the arguments for the constructor we have found var builtArgs = executeStrategy.CreateParameters(constructor); return(constructor.Invoke(builtArgs)); }
protected override object?Generate(IExecuteStrategy executeStrategy, Type type, string?referenceName) { TypeUsed = type; ReferenceNameUsed = referenceName; return(_value); }
/// <inheritdoc /> /// <exception cref="ArgumentNullException">The <paramref name="executeStrategy" /> parameter is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="instance" /> parameter is <c>null</c>.</exception> public object Populate(IExecuteStrategy executeStrategy, object instance) { executeStrategy = executeStrategy ?? throw new ArgumentNullException(nameof(executeStrategy)); instance = instance ?? throw new ArgumentNullException(nameof(instance)); return(_populate(executeStrategy, instance)); }
/// <inheritdoc /> /// <exception cref="ArgumentNullException">The <paramref name="executeStrategy" /> parameter is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="targetType" /> parameter is <c>null</c>.</exception> public object?CreateType(IExecuteStrategy executeStrategy, Type targetType, object?[]?args) { executeStrategy = executeStrategy ?? throw new ArgumentNullException(nameof(executeStrategy)); targetType = targetType ?? throw new ArgumentNullException(nameof(targetType)); return(_createType(executeStrategy, targetType, args)); }
/// <summary> /// Writes the log entry using the specified action after the execute strategy is invoked. /// </summary> /// <param name="executeStrategy">The execute strategy to invoke.</param> /// <param name="action">The logging action to call.</param> /// <returns>The execute strategy to invoke.</returns> /// <exception cref="ArgumentNullException">The <paramref name="executeStrategy" /> parameter is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="action" /> parameter is <c>null</c>.</exception> public static IExecuteStrategy WriteLog(this IExecuteStrategy executeStrategy, Action <string> action) { executeStrategy = executeStrategy ?? throw new ArgumentNullException(nameof(executeStrategy)); action = action ?? throw new ArgumentNullException(nameof(action)); return(new LoggingExecuteStrategy <IExecuteStrategy>(executeStrategy, action)); }
protected override object?Generate(IExecuteStrategy executeStrategy, Type type, string?referenceName) { var values = Enum.GetValues(typeof(SemVerChangeType)); // Skip the first entry which should be None var valueIndex = Generator.NextValue(1, values.Length - 1); return(values.GetValue(valueIndex)); }
/// <inheritdoc /> /// <exception cref="ArgumentNullException">The <paramref name="propertyInfo" /> parameter is <c>null</c>.</exception> public object?Create(IExecuteStrategy executeStrategy, PropertyInfo propertyInfo) { if (propertyInfo == null) { throw new ArgumentNullException(nameof(propertyInfo)); } return(_valueGenerator()); }
/// <inheritdoc /> /// <exception cref="ArgumentNullException">The <paramref name="executeStrategy" /> parameter is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="propertyInfo" /> parameter is <c>null</c>.</exception> public object?CreateProperty(IExecuteStrategy executeStrategy, PropertyInfo propertyInfo, object?[]?args) { executeStrategy = executeStrategy ?? throw new ArgumentNullException(nameof(executeStrategy)); propertyInfo = propertyInfo ?? throw new ArgumentNullException(nameof(propertyInfo)); return(_createProperty(executeStrategy, propertyInfo, args)); }
/// <summary> /// Writes the log entry using the specified action after the execute strategy is invoked. /// </summary> /// <param name="executeStrategy">The execute strategy to invoke.</param> /// <param name="action">The logging action to call.</param> /// <typeparam name="T">The type of instance to create and populate.</typeparam> /// <returns>The execute strategy to invoke.</returns> public static IExecuteStrategy <T> WriteLog <T>(this IExecuteStrategy <T> executeStrategy, Action <string> action) where T : notnull { executeStrategy = executeStrategy ?? throw new ArgumentNullException(nameof(executeStrategy)); action = action ?? throw new ArgumentNullException(nameof(action)); return(new LoggingGenericExecuteStrategy <T>(executeStrategy, action)); }
/// <inheritdoc /> protected override object?CreateInstance(IExecuteStrategy executeStrategy, Type type, string?referenceName, params object?[]?args) { type = type ?? throw new ArgumentNullException(nameof(type)); return(Activator.CreateInstance(type, args)); }
/// <inheritdoc /> /// <exception cref="ArgumentNullException">The <paramref name="executeStrategy" /> parameter is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="parameterInfo" /> parameter is <c>null</c>.</exception> public object?CreateParameter(IExecuteStrategy executeStrategy, ParameterInfo parameterInfo, object?[]?args) { executeStrategy = executeStrategy ?? throw new ArgumentNullException(nameof(executeStrategy)); parameterInfo = parameterInfo ?? throw new ArgumentNullException(nameof(parameterInfo)); return(_createParameter(executeStrategy, parameterInfo, args)); }
/// <inheritdoc /> /// <exception cref="ArgumentNullException">The <paramref name="executeStrategy" /> parameter is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="propertyInfo" /> parameter is <c>null</c>.</exception> public object?Build(IExecuteStrategy executeStrategy, PropertyInfo propertyInfo, params object?[]?arguments) { executeStrategy = executeStrategy ?? throw new ArgumentNullException(nameof(executeStrategy)); propertyInfo = propertyInfo ?? throw new ArgumentNullException(nameof(propertyInfo)); var capability = new CircularReferenceCapability(); return(capability.CreateProperty(executeStrategy, propertyInfo, arguments)); }
/// <inheritdoc /> /// <exception cref="ArgumentNullException">The <paramref name="executeStrategy" /> parameter is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="type" /> parameter is <c>null</c>.</exception> public object?Build(IExecuteStrategy executeStrategy, Type type, params object?[]?arguments) { executeStrategy = executeStrategy ?? throw new ArgumentNullException(nameof(executeStrategy)); type = type ?? throw new ArgumentNullException(nameof(type)); var capability = new CircularReferenceCapability(); return(capability.CreateType(executeStrategy, type, arguments)); }
/// <inheritdoc /> protected override object?CreateInstance(IExecuteStrategy executeStrategy, Type type, string?referenceName, params object?[]?args) { var buildType = ResolveBuildType(executeStrategy.Configuration, type); // The base class has already validated CanCreate which ensures that the singleton property is available var propertyInfo = GetSingletonProperty(buildType) !; return(propertyInfo.GetValue(null, args)); }
/// <inheritdoc /> /// <exception cref="ArgumentNullException">The <paramref name="parameterInfo" /> parameter is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="executeStrategy" /> parameter is <c>null</c>.</exception> public virtual object?Generate(IExecuteStrategy executeStrategy, ParameterInfo parameterInfo) { parameterInfo = parameterInfo ?? throw new ArgumentNullException(nameof(parameterInfo)); executeStrategy = executeStrategy ?? throw new ArgumentNullException(nameof(executeStrategy)); var type = parameterInfo.ParameterType; var name = parameterInfo.Name; return(Generate(executeStrategy, type, name)); }
/// <inheritdoc /> /// <exception cref="ArgumentNullException">The <paramref name="type" /> parameter is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="executeStrategy" /> parameter is <c>null</c>.</exception> public object?Generate(IExecuteStrategy executeStrategy, Type type) { type = type ?? throw new ArgumentNullException(nameof(type)); if (executeStrategy == null) { throw new ArgumentNullException(nameof(type)); } return(Generate(executeStrategy, type, null)); }
/// <inheritdoc /> protected override object?Generate(IExecuteStrategy executeStrategy, Type type, string?referenceName) { if (IsMale(executeStrategy)) { // Use a male first name return(TestData.MaleNames.Next()); } // Use a female name return(TestData.FemaleNames.Next()); }
/// <inheritdoc /> /// <exception cref="ArgumentNullException">The <paramref name="instance" /> parameter is null.</exception> /// <exception cref="ArgumentNullException">The <paramref name="executeStrategy" /> parameter is null.</exception> public override object Populate(object instance, IExecuteStrategy executeStrategy) { if (instance == null) { throw new ArgumentNullException(nameof(instance)); } if (executeStrategy == null) { throw new ArgumentNullException(nameof(executeStrategy)); } var instanceType = instance.GetType(); VerifyCreateRequest(instanceType, null, executeStrategy.BuildChain); var target = instance as Array; if (target?.Length == 0) { return base.Populate(instance, executeStrategy); } Type itemType; // The array has entries for which we may be able to get a type var firstInstance = target.GetValue(0); if (firstInstance != null) { itemType = firstInstance.GetType(); } else { // The type of item in the array has a default value of null so we need to attempt to parse the name from the name of the array type itself var typeName = instanceType.AssemblyQualifiedName?.Replace("[]", string.Empty); itemType = Type.GetType(typeName); } object previousItem = null; for (var index = 0; index < target.Length; index++) { var childInstance = CreateChildItem(itemType, executeStrategy, previousItem); target.SetValue(childInstance, index); previousItem = childInstance; } return base.Populate(instance, executeStrategy); }
protected override object CreateChildItem(Type type, IExecuteStrategy executeStrategy, object previousItem) { if (previousItem == null) { return base.CreateChildItem(type, executeStrategy, null); } // Use a double as the base type then convert later var value = Convert.ToDouble(previousItem); value++; var converted = Convert.ChangeType(value, type); return converted; }
/// <inheritdoc /> /// <exception cref="ArgumentNullException">The <paramref name="instance" /> parameter is null.</exception> /// <exception cref="ArgumentNullException">The <paramref name="executeStrategy" /> parameter is null.</exception> public virtual object Populate(object instance, IExecuteStrategy executeStrategy) { if (instance == null) { throw new ArgumentNullException(nameof(instance)); } if (executeStrategy == null) { throw new ArgumentNullException(nameof(executeStrategy)); } VerifyPopulateRequest(instance.GetType(), null, executeStrategy.BuildChain); // The default will be to not do any additional population of the instance return PopulateInstance(instance, executeStrategy); }
protected override object PopulateInstance(object instance, IExecuteStrategy executeStrategy) { Debug.Assert(instance != null, "instance != null"); var type = instance.GetType(); var internalType = FindEnumerableTypeArgument(type); var collectionGenericTypeDefinition = typeof(ICollection<string>).GetGenericTypeDefinition(); var collectionType = collectionGenericTypeDefinition.MakeGenericType(internalType); // Get the Add method var addMethod = collectionType.GetMethod("Add"); object previousItem = null; for (var index = 0; index < AutoPopulateCount; index++) { var childInstance = CreateChildItem(internalType, executeStrategy, previousItem); addMethod.Invoke( instance, new[] { childInstance }); previousItem = childInstance; } return instance; }
protected override object PopulateInstance(object instance, IExecuteStrategy executeStrategy) { return instance; }
/// <summary> /// Creates a child item given the context of a possible previous item being created. /// </summary> /// <param name="type">The type of value to generate.</param> /// <param name="executeStrategy">The execute strategy.</param> /// <param name="previousItem">The previous item generated, or <c>null</c>.</param> /// <returns>The new item generated.</returns> /// <exception cref="ArgumentNullException">The <paramref name="executeStrategy" /> parameter is null.</exception> protected virtual object CreateChildItem(Type type, IExecuteStrategy executeStrategy, object previousItem) { if (executeStrategy == null) { throw new ArgumentNullException(nameof(executeStrategy)); } return executeStrategy.CreateWith(type); }
/// <inheritdoc /> protected override object PopulateInstance(object instance, IExecuteStrategy executeStrategy) { // There is no out of the box population and this is left up to the execution strategy return instance; }
/// <summary> /// Populates the specified instance using an execution strategy. /// </summary> /// <param name="instance">The instance to populate.</param> /// <param name="executeStrategy">The execution strategy.</param> /// <returns>The populated instance.</returns> protected abstract object PopulateInstance(object instance, IExecuteStrategy executeStrategy);
public void CreateItem(Type type, IExecuteStrategy executeStrategy, object item) { CreateChildItem(type, executeStrategy, item); }
protected override object PopulateInstance(object instance, IExecuteStrategy executeStrategy) { throw new NotImplementedException(); }