Ejemplo n.º 1
0
 public void AddToCollection(object collection, object collectionItem, int index)
 {
     AddMethod.Invoke(collection, new [] { collectionItem });
 }
Ejemplo n.º 2
0
 public static void AddEventHandler(DependencyObject source, EventHandler handler)
 {
     AddMethod.Invoke(source, new object[] { handler });
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Tests that common aspects of the specified collection are functional.
        /// </summary>
        /// <param name="collection">An object that is the collection to be tested.</param>
        public static void TestCollection(object collection)
        {
            if (collection != null)
            {
                Type MyType;

                MyType = collection.GetType();
                if (MyType.GetInterface("ICollection") != null)
                {
                    Type[] GenericTypes;

                    GenericTypes = MyType.BaseType.GetGenericArguments();
                    if (GenericTypes != null && GenericTypes.Length == 1)
                    {
                        MethodInfo   AddMethod;
                        PropertyInfo CountProperty;
                        PropertyInfo IndexerProperty;
                        MethodInfo   RemoveMethod;

                        AddMethod       = MyType.GetMethod("Add");
                        CountProperty   = MyType.GetProperty("Count");
                        IndexerProperty = MyType.GetProperty("Item", GenericTypes[0], new Type[] { typeof(int) });
                        RemoveMethod    = MyType.GetMethod("Remove");

                        if (AddMethod != null && RemoveMethod != null && CountProperty != null && IndexerProperty != null)
                        {
                            int CurrentCount;

                            CurrentCount = (int)CountProperty.GetValue(collection);
                            if (CurrentCount > 0)
                            {
                                object TestObject;

                                TestObject = IndexerProperty.GetValue(collection, new object[] { 0 });
                                if (TestObject != null)
                                {
                                    Assert.AreEqual(CurrentCount, CountProperty.GetValue(collection));
                                    RemoveMethod.Invoke(collection, new object[] { TestObject });
                                    Assert.AreEqual(CurrentCount - 1, CountProperty.GetValue(collection));
                                    AddMethod.Invoke(collection, new object[] { TestObject });
                                    Assert.AreEqual(CurrentCount, CountProperty.GetValue(collection));
                                }
                                else
                                {
                                    Assert.Fail("Indexer returned a null result");
                                }
                            }
                            else
                            {
                                throw new ArgumentException("The specified collection is empty and cannot be tested", "collection");
                            }
                        }
                        else
                        {
                            throw new ArgumentException("The specified type does not support required operations", "collection");
                        }
                    }
                    else
                    {
                        throw new ArgumentException("The specified type does not implement a generic collection", "collection");
                    }
                }
                else
                {
                    throw new ArgumentException("The specified type does not implement ICollection", "collection");
                }
            }
            else
            {
                throw new ArgumentException("The specified collection is null", "collection");
            }
        }
Ejemplo n.º 4
0
        public object MapTo(object mapped, CommerceEntity entity)
        {
            MappingType mappingType = MappingInfo.Mapping;
            object      value       = entity.GetPropertyValue(FieldName);

            if (MappingInfo.FromConverter != null)
            {
                value = ConverterLazyRegistry.Instance.Resolve(MappingInfo.FromConverter).Convert(value);
            }
            else if (value != null)
            {
                if (mappingType == MappingType.Property)
                {
                    if (value.GetType() != Info.PropertyType)
                    {
                        value = Convert.ChangeType(value, Info.PropertyType);
                    }
                }
                else if (mappingType == MappingType.Identity)
                {
                    if (MappingInfo.FromConverter == null && !String.IsNullOrEmpty(value.ToString()))
                    {
                        value = new Guid(value.ToString());
                    }
                    else
                    {
                        value = null;
                    }
                }
                else
                {
                    var mapper = new Mapper();

                    var relationshipList = value as CommerceRelationshipList;

                    if (relationshipList != null)
                    {
                        value = Activator.CreateInstance(GeneratedListType);

                        foreach (var relation in relationshipList)
                        {
                            object element = mapper.Map(relation.Target, ListElementType);

                            AddMethod.Invoke(value, new[] { element });
                        }
                    }
                    else
                    {
                        var relationship = value as CommerceRelationship;
                        if (relationship != null)
                        {
                            value = mapper.Map(relationship.Target, Info.PropertyType);
                        }
                    }
                }
            }


            if (value != null || (ReflectionHelper.IsNullableType(Info.PropertyType) || !Info.PropertyType.IsValueType))
            {
                Info.SetValue(mapped, value, null);
            }

            return(mapped);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Mocks the specified collection, optionally prepopulating it with the specified number of entries.
        /// </summary>
        /// <typeparam name="T">A T that is the collection to mock.</typeparam>
        /// <param name="prepopulateSize">An int that indicates the number of entries to prepopulate.</param>
        /// <returns>A T that was the created type instance.</returns>
        public static T MockCollection <T>(int prepopulateSize = 0)
        {
            Type MyType;
            T    ReturnValue;

            MyType = typeof(T);
            if (MyType.GetInterface("ICollection") == null &&
                MyType.GetInterface("ICollection`1") == null)
            {
                throw new ArgumentException("The specified type does not implement ICollection", "T");
            }

            ReturnValue = Activator.CreateInstance <T>();

            // Prepopulate values?
            if (prepopulateSize > 0)
            {
                Type GenericType = null;

                if (MyType.IsGenericType)
                {
                    GenericType = MyType;
                }
                else if (MyType.BaseType != null && MyType.BaseType.IsGenericType)
                {
                    GenericType = MyType.BaseType;
                }

                // Check for generic-based collections
                if (GenericType != null)
                {
                    if (GenericType.GenericTypeArguments != null && GenericType.GenericTypeArguments.Length == 1)
                    {
                        MethodInfo MockMethod;

                        MockMethod = typeof(TestDataTestHelper).GetMethod("MockType", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                        if (MockMethod != null)
                        {
                            MockMethod = MockMethod.MakeGenericMethod(GenericType.GenericTypeArguments[0]);
                            if (MockMethod != null)
                            {
                                MethodInfo AddMethod;

                                AddMethod = MyType.GetMethod("Add");
                                for (int i = 0; i < prepopulateSize; i++)
                                {
                                    object NewItem;

                                    NewItem = MockMethod.Invoke(null, new object[] { null });
                                    AddMethod.Invoke(ReturnValue, new object[] { NewItem });
                                }
                            }
                        }
                    }
                    else
                    {
                        throw new ArgumentException("Unable to mock generic collections with more than one generic type", "T");
                    }
                }
                else
                {
                    // Non generic-collection
                    throw new ArgumentException("Currently unable to mock non-generic collections", "T");
                }
            }

            return(ReturnValue);
        }