Ejemplo n.º 1
0
        public void GetTypeArgumentsForUpdatableGenericCollection_ModelTypeWrongNumberOfGenericArguments_Fail()
        {
            // Arrange
            ModelMetadata modelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(KeyValuePair <int, string>));

            // Act
            Type[] typeArguments = CollectionModelBinderUtil.GetTypeArgumentsForUpdatableGenericCollection(typeof(ICollection <>), null, modelMetadata);

            // Assert
            Assert.Null(typeArguments);
        }
Ejemplo n.º 2
0
        public void GetTypeArgumentsForUpdatableGenericCollection_ModelTypeOpenGeneric_Fail()
        {
            // Arrange
            ModelMetadata modelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(IList <>));

            // Act
            Type[] typeArguments = CollectionModelBinderUtil.GetTypeArgumentsForUpdatableGenericCollection(null, null, modelMetadata);

            // Assert
            Assert.Null(typeArguments);
        }
Ejemplo n.º 3
0
        public void GetTypeArgumentsForUpdatableGenericCollection_ModelTypeNotGeneric_Fail()
        {
            // Arrange
            ModelMetadata modelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int));

            // Act
            Type[] typeArguments = CollectionModelBinderUtil.GetTypeArgumentsForUpdatableGenericCollection(null, null, modelMetadata);

            // Assert
            Assert.IsNull(typeArguments, "The given model type was not generic.");
        }
Ejemplo n.º 4
0
        public void GetTypeArgumentsForUpdatableGenericCollection_ReadWriteReference_NewInstanceNotAssignableToModelType_MutableInstance_Success()
        {
            // Arrange
            ModelMetadata modelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(() => new Collection <int>(), typeof(Collection <int>));

            modelMetadata.IsReadOnly = false;

            // Act
            Type[] typeArguments = CollectionModelBinderUtil.GetTypeArgumentsForUpdatableGenericCollection(typeof(ICollection <>), typeof(List <>), modelMetadata);

            // Assert
            Assert.Equal(new[] { typeof(int) }, typeArguments);
        }
Ejemplo n.º 5
0
        public void GetTypeArgumentsForUpdatableGenericCollection_ReadOnlyReference_ModelIsNull_Fail()
        {
            // Arrange
            ModelMetadata modelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(IList <int>));

            modelMetadata.IsReadOnly = true;

            // Act
            Type[] typeArguments = CollectionModelBinderUtil.GetTypeArgumentsForUpdatableGenericCollection(typeof(ICollection <>), typeof(List <>), modelMetadata);

            // Assert
            Assert.Null(typeArguments);
        }
Ejemplo n.º 6
0
        public void GetTypeArgumentsForUpdatableGenericCollection_ReadOnlyReference_ModelInstanceMutable_Valid()
        {
            // Arrange
            ModelMetadata modelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(() => new List <int>(), typeof(IList <int>));

            modelMetadata.IsReadOnly = true;

            // Act
            Type[] typeArguments = CollectionModelBinderUtil.GetTypeArgumentsForUpdatableGenericCollection(typeof(IList <>), typeof(List <>), modelMetadata);

            // Assert
            Assert.Equal(new[] { typeof(int) }, typeArguments);
        }
Ejemplo n.º 7
0
        public void GetTypeArgumentsForUpdatableGenericCollection_ReadWriteReference_NewInstanceAssignableToModelType_Success()
        {
            // Arrange
            ModelMetadata modelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(IList <int>));

            modelMetadata.IsReadOnly = false;

            // Act
            Type[] typeArguments = CollectionModelBinderUtil.GetTypeArgumentsForUpdatableGenericCollection(typeof(ICollection <>), typeof(List <>), modelMetadata);

            // Assert
            CollectionAssert.AreEqual(new Type[] { typeof(int) }, typeArguments, "Mutable reference can be overwritten.");
        }
Ejemplo n.º 8
0
        public void GetTypeArgumentsForUpdatableGenericCollection_ReadOnlyReference_ModelInstanceImmutable_Valid()
        {
            // Arrange
            ModelMetadata modelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(() => new int[0], typeof(IList <int>));

            modelMetadata.IsReadOnly = true;

            // Act
            Type[] typeArguments = CollectionModelBinderUtil.GetTypeArgumentsForUpdatableGenericCollection(typeof(IList <>), typeof(List <>), modelMetadata);

            // Assert
            Assert.IsNull(typeArguments, "Collection instance is immutable and reference is readonly.");
        }
Ejemplo n.º 9
0
        public void GetTypeArgumentsForUpdatableGenericCollection_ReadOnlyReference_ModelInstanceOfWrongType_Fail()
        {
            // Arrange
            ModelMetadata modelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(() => new HashSet <int>(), typeof(ICollection <int>));

            modelMetadata.IsReadOnly = true;

            // Act
            Type[] typeArguments = CollectionModelBinderUtil.GetTypeArgumentsForUpdatableGenericCollection(typeof(IList <>), typeof(List <>), modelMetadata);

            // Assert
            // HashSet<> is not an IList<>, so we can't update
            Assert.Null(typeArguments);
        }