private static object createInstance(Tuple <Type, Func <object> > tuple) { var factory = tuple.Item2; if (factory == null) { return(instanceCreator.CreateInstance(tuple.Item1)); } else { return(instanceCreator.CreateInstance(factory)); } }
public void CreateInstanceTest() { var actual = InstanceCreator.CreateInstance <TestModel>(); actual.Should() .NotBeNull(); }
private static void DoTestOnModelBuilder <T>(Action <ModelBuilder> checkMethod) where T : VisitableDbContext { var options = new DbContextOptionsBuilder <T>() .UseInMemoryDatabase(nameof(T02_ShouldHaveUniqueIndex)) .Options; var count = 0; using (var context = InstanceCreator.CreateInstance <T>(options)) { context.ExternalCheckModel = b => { count++; checkMethod?.Invoke(b); }; var tmp = context.Settings.ToArray(); // enforce to create model var model = context.Model; if (model == null) // enforce to create model { throw new NullReferenceException(); } } if (count == 0) { throw new Exception("checkMethod has not been invoked"); } }
/// <summary> /// Create an instance of the database with the specified type name. /// </summary> /// <param name="assembly">A string containing the name of the assembly in which the database type is found.</param> /// <param name="typeName">A string containing the type name of the database to create.</param> /// <param name="connectionParameters">A string containing the connection parameters for the database.</param> /// <returns>A T that is the created instance.</returns> public static T CreateInstance <T>(string assembly, string typeName, string connectionParameters) where T : IDatabaseBase <TParameterEnum, TTableEnum> { T ReturnValue = default(T); try { ReturnValue = InstanceCreator <T> .CreateInstance(assembly, typeName); if (ReturnValue != null) { ReturnValue.ConnectionParameters = connectionParameters; } else { throw new InvalidOperationException("The created database instance was null"); } } catch (Exception ex) { LogWriter.LogException(ex, "Unable to create an instance of the requested database type"); throw new InvalidOperationException("Unable to create an instance of the requested database type", ex); } return(ReturnValue); }
public static T CreateInstance <T>() where T : class { #if FW3 return(InstanceCreator <T> .CreateInstance()); #else return((T)Activator.CreateInstance(GetType(typeof(T)))); #endif }
public void ShouldCreateInstanceOfSimpleObject() { // when var instance = InstanceCreator.CreateInstance <SimpleObject>(); // then Check.That(instance).IsNotNull(); }
public void ShouldCreateInstanceOfObjectWithPrivateConstructor() { // when var instance = InstanceCreator.CreateInstance <PrivateConstructor>(); // then Check.That(instance).IsNotNull(); }
public void GetValueInvalidExpressionTest() { var model = InstanceCreator.CreateInstance <TestModel>(); var target = new ObjectValueProvider(model); Action test = () => target.GetValue("DOESNotExist"); test.ShouldThrow <FormatException>(); }
public void GetValueTest1() { var model = InstanceCreator.CreateInstance <TestModel>(); var target = new ObjectValueProvider(model); var actual = target.GetValue("MyInt32"); actual.Should() .Be(model.MyInt32.ToString()); }
public void GetValueTest2() { var model = InstanceCreator.CreateInstance <TestModel>(); var target = new ObjectValueProvider(model); var actual = target.GetValue("MyDouble"); actual.Should() .Be(model.MyDouble.ToString(CultureInfo.InvariantCulture)); }
public void GetValueTest3() { var model = InstanceCreator.CreateInstance <TestModel>(); var target = new ObjectValueProvider(model); var actual = target.GetValue("MyStringArray[0]"); actual.Should() .Be(model.MyStringArray[0]); }
public void ShouldCreateInstanceOfObjectWithoutDefaultConstrutor() { // when var instance = InstanceCreator.CreateInstance <NoDefaultConstructor>(); // then Check.That(instance).IsNotNull(); Check.That(instance) .IsEqualTo(new NoDefaultConstructor(a: null, b: 0, timeSpan: default(TimeSpan), obj: null)); }
public void SubModelTest() { var actual = InstanceCreator.CreateInstance <TestModel>(); actual.MySubModel.MyDateTime.HasValue.Should() .BeTrue(); actual.MySubModel.MyNullableInt32.HasValue.Should() .BeTrue(); actual.MySubModel.MyString.Should() .NotBeEmpty(); }
public void GetValueTest10() { var model = InstanceCreator.CreateInstance <TestModel>(); var target = new ObjectValueProvider(model); var actual = target.GetValue("SubModel.MyDictionaryStringSub[" + model.SubModel.MyDictionaryStringSub.First() .Key + "]"); actual.Should() .Be(model.SubModel.MyDictionaryStringSub[model.SubModel.MyDictionaryStringSub.First() .Key]); }
public void NoMatchingSelectionRuleTest() { var rules = new List <IMemberSelectionRule>(InstanceCreator.DefaultMemberSelectionRules); InstanceCreator.DefaultMemberSelectionRules.Clear(); // ReSharper disable once ReturnValueOfPureMethodIsNotUsed Action test = () => InstanceCreator.CreateInstance <TestModel>(); var exception = Assert.Throws <CreateInstanceException>(test); Assert.Equal("Found no selection rule targeting member.", exception.Message); rules.ForEach(x => InstanceCreator.DefaultMemberSelectionRules.Add(x)); }
public void NoMatchingSelectionRuleTest() { var rules = new List <IMemberSelectionRule>(InstanceCreator.DefaultMemberSelectionRules); InstanceCreator.DefaultMemberSelectionRules.Clear(); // ReSharper disable once ReturnValueOfPureMethodIsNotUsed Action test = () => InstanceCreator.CreateInstance <TestModel>(); test.ShouldThrow <CreateInstanceException>() .WithMessage("Found no selection rule targeting member."); rules.ForEach(x => InstanceCreator.DefaultMemberSelectionRules.Add(x)); }
private static object ReadComplexNode(NodeReference node, Type objectType) { var isNullable = EntityTypeDescriptor.IsNullableType(objectType); if (isNullable && IsNullableComplexColumnEmpty(node)) { return(null); } var underlyingType = !isNullable ? objectType : Nullable.GetUnderlyingType(objectType); var typeDescription = EntityTypeDescriptor.GetTypeDescription(underlyingType); var instance = InstanceCreator.CreateInstance(underlyingType); foreach (var column in typeDescription.Columns) { var subscript = column.ColumnAttribute.Name ?? column.ColumnInfo.Name; if (column.IsSimpleColumn) { node.AppendSubscript(subscript); var nodeValue = node.GetObject(); column.ColumnInfo.SetValue(instance, ConvertValue(nodeValue, column.ColumnInfo.PropertyType), null); } else if (column.IsArrayColumn) { node.AppendSubscript(subscript); column.ColumnInfo.SetValue(instance, ReadArray(node, column.ColumnInfo.PropertyType), null); } else if (column.IsEnumerableColumn) { node.AppendSubscript(subscript); column.ColumnInfo.SetValue(instance, ReadEnumerable(node, column.ColumnInfo.PropertyType), null); } else if (column.IsComplexColumn) { node.AppendSubscript(subscript); column.ColumnInfo.SetValue(instance, ReadComplexNode(node, column.ColumnInfo.PropertyType), null); } node.SetSubscriptCount(node.GetSubscriptCount() - 1); } return(instance); }
public void CreateInstanceICollectionTest() { var actual = InstanceCreator .CreateInstance <ModelWithCollection>(); actual.Should() .NotBeNull(); actual.MyArray.Should() .NotBeEmpty(); actual.MyList.Should() .NotBeEmpty(); actual.MyEnumerable.Should() .NotBeEmpty(); actual.MyListInterface.Should() .NotBeEmpty(); actual.MyCollection.Should() .NotBeEmpty(); }
public void PopulateDitionaryTest() { var actual = InstanceCreator.CreateInstance <ModelWithDitionary>(); actual.MyStringDitionary.Any() .Should() .BeTrue(); actual.MyInt32Ditionary.Any() .Should() .BeTrue(); actual.MyStringKeyDitionary.Any() .Should() .BeTrue(); actual.MyInt32KeyDitionary.Any() .Should() .BeTrue(); actual.ComplexDictionary.Any(x => x.Value.MyInt32Enumerable.Any()) .Should() .BeTrue(); }
internal static void DoTestOnModelBuilder <T>(bool useSql, Action <ModelBuilder> checkMethod, Action <T> checkContext = null, [CallerMemberName] string memberName = null) where T : VisitableDbContext { var optionsBuilder = new DbContextOptionsBuilder <T>(); optionsBuilder = useSql ? optionsBuilder.UseSqlServer(GetSqlConnectionString(memberName)) : optionsBuilder.UseInMemoryDatabase(nameof(T02_ShouldHaveUniqueIndex)); var options = optionsBuilder.Options; var count = 0; using (var context = InstanceCreator.CreateInstance <T>(EmptyShamanLogger.Instance, options)) { context.ExternalCheckModel = b => { count++; checkMethod?.Invoke(b); }; if (useSql) { Console.WriteLine("Migration"); context.Database.Migrate(); } // var tmp = context.Settings.ToArray(); // enforce to create model var model = context.Model; if (model == null) // enforce to create model { throw new NullReferenceException(); } if (checkContext != null) { checkContext(context); } } if (count == 0) { throw new Exception("checkMethod has not been invoked"); } }
public static T CreateInstance <T>() where T : class { return(InstanceCreator <T> .CreateInstance()); }
public T GetNewItem(params object[] parameters) { var returnVal = InstanceCreator.CreateInstance <T>(parameters); return(returnVal); }
public static T CreateInstance <T>() { return(InstanceCreator <T> .CreateInstance()); }