示例#1
0
        public void SetCollectionProperty_NonSettableProperty_NonNullValue_WithAddMethod(string propertyName)
        {
            object value = new SampleClassWithNonSettableCollectionProperties();

            ODataEntityDeserializer.SetCollectionProperty(value, propertyName, isDelta: false, value: new List <int> {
                1, 2, 3
            });
            Assert.Equal(
                new[] { 1, 2, 3 },
                value.GetType().GetProperty(propertyName).GetValue(value, index: null) as IEnumerable <int>);
        }
示例#2
0
        public void SetCollectionProperty_CollectionTypeCannotBeInstantiated_And_SettableProperty_Throws(string propertyName)
        {
            object value = new SampleClassWithSettableCollectionProperties();

            Assert.Throws <InvalidOperationException>(
                () => ODataEntityDeserializer.SetCollectionProperty(value, propertyName, isDelta: false, value: new List <int> {
                1, 2, 3
            }),
                String.Format("The property '{0}' on type 'System.Web.Http.OData.Formatter.Deserialization.ODataEntryDeserializerTests+SampleClassWithSettableCollectionProperties' returned a null value. " +
                              "The input stream contains collection items which cannot be added if the instance is null.", propertyName));
        }
示例#3
0
        public void SetCollectionProperty_CanConvertEnumCollection()
        {
            SampleClassWithDifferentCollectionProperties value = new SampleClassWithDifferentCollectionProperties();

            ODataEntityDeserializer.SetCollectionProperty(value, "FlagsEnum", isDelta: false, value: new List <string> {
                "One", "Four, Two"
            });
            Assert.Equal(
                new FlagsEnum[] { FlagsEnum.One, FlagsEnum.Four | FlagsEnum.Two },
                value.FlagsEnum);
        }
示例#4
0
        public void SetCollectionProperty_CanConvertNonStandardEdmTypes()
        {
            SampleClassWithDifferentCollectionProperties value = new SampleClassWithDifferentCollectionProperties();

            ODataEntityDeserializer.SetCollectionProperty(value, "UnsignedArray", isDelta: false, value: new List <int> {
                1, 2, 3
            });
            Assert.Equal(
                new uint[] { 1, 2, 3 },
                value.UnsignedArray);
        }
示例#5
0
        public void SetCollectionProperty_NonSettableProperty_ArrayValue_FixedSize_Throws(string propertyName)
        {
            object value        = new SampleClassWithNonSettableCollectionProperties();
            Type   propertyType = typeof(SampleClassWithNonSettableCollectionProperties).GetProperty(propertyName).PropertyType;

            Assert.Throws <InvalidOperationException>(
                () => ODataEntityDeserializer.SetCollectionProperty(value, propertyName, isDelta: false, value: new List <int> {
                1, 2, 3
            }),
                String.Format("The value of the property '{0}' on type 'System.Web.Http.OData.Formatter.Deserialization.ODataEntryDeserializerTests+SampleClassWithNonSettableCollectionProperties' is an array. " +
                              "Consider adding a setter for the property.", propertyName));
        }
示例#6
0
        public void SetCollectionProperty_NonSettableProperty_NonNullValue_NoAdd_Throws(string propertyName)
        {
            object value        = new SampleClassWithNonSettableCollectionProperties();
            Type   propertyType = typeof(SampleClassWithNonSettableCollectionProperties).GetProperty(propertyName).PropertyType;

            Assert.Throws <InvalidOperationException>(
                () => ODataEntityDeserializer.SetCollectionProperty(value, propertyName, isDelta: false, value: new List <int> {
                1, 2, 3
            }),
                String.Format("The type '{0}' of the property '{1}' on type 'System.Web.Http.OData.Formatter.Deserialization.ODataEntryDeserializerTests+SampleClassWithNonSettableCollectionProperties' does not have an Add method. " +
                              "Consider using a collection type that does have an Add method - for example IList<T> or ICollection<T>.", propertyType.FullName, propertyName));
        }
示例#7
0
        public void SetCollectionProperty_OnNonCollection_ThrowsInvalidOp(string propertyName)
        {
            object value        = new SampleClassWithDifferentCollectionProperties();
            Type   propertyType = typeof(SampleClassWithDifferentCollectionProperties).GetProperty(propertyName).PropertyType;

            Assert.Throws <InvalidOperationException>(
                () => ODataEntityDeserializer.SetCollectionProperty(value, propertyName, isDelta: false, value: new List <int> {
                1, 2, 3
            }),
                Error.Format(
                    "The type '{0}' of the property '{1}' on type 'System.Web.Http.OData.Formatter.Deserialization.ODataEntryDeserializerTests+SampleClassWithDifferentCollectionProperties' must be a collection.",
                    propertyType.FullName,
                    propertyName));
        }