Exemple #1
0
 public void ConvertEnumValue_ThrowsValidationException_NonODataEnumValue()
 {
     // Arrange & Act & Assert
     ExceptionAssert.Throws <ValidationException>(
         () => EnumDeserializationHelpers.ConvertEnumValue(42, typeof(EnumColor)),
         "The value with type 'Int32' must have type 'ODataEnumValue'.");
 }
Exemple #2
0
        /// <summary>
        /// Binds a <see cref="CollectionConstantNode"/> to create a LINQ <see cref="Expression"/> that
        /// represents the semantics of the <see cref="CollectionConstantNode"/>.
        /// </summary>
        /// <param name="node">The node to bind.</param>
        /// <returns>The LINQ <see cref="Expression"/> created.</returns>
        public virtual Expression BindCollectionConstantNode(CollectionConstantNode node)
        {
            // It's fine if the collection is empty; the returned value will be an empty list.
            ConstantNode firstNode = node.Collection.FirstOrDefault();
            object       value     = null;

            if (firstNode != null)
            {
                value = firstNode.Value;
            }

            Type  constantType = RetrieveClrTypeForConstant(node.ItemType, ref value);
            Type  listType     = typeof(List <>).MakeGenericType(constantType);
            IList castedList   = Activator.CreateInstance(listType) as IList;

            // Getting a LINQ expression to dynamically cast each item in the Collection during runtime is tricky,
            // so using a foreach loop and doing an implicit cast from object to the CLR type of ItemType.
            foreach (ConstantNode item in node.Collection)
            {
                object member = constantType.IsEnum ? (EnumDeserializationHelpers.ConvertEnumValue(item.Value, constantType)) : item.Value;
                castedList.Add(member);
            }

            return(Expression.Constant(castedList));
        }
Exemple #3
0
        public void ConvertEnumValue_ReturnEnumValue_ForEnumType()
        {
            // Arrange
            object value = new ODataEnumValue("Red");
            Type   type  = typeof(Color);

            // Act & Assert
            Assert.Equal(Enum.ToObject(typeof(Color), Color.Red), EnumDeserializationHelpers.ConvertEnumValue(value, type));
        }
Exemple #4
0
        public void ConvertEnumValue_ThrowsValidationException_NonEnumType()
        {
            // Arrange & Act & Assert
            ODataEnumValue enumValue = new ODataEnumValue("Red");

            ExceptionAssert.Throws <InvalidOperationException>(
                () => EnumDeserializationHelpers.ConvertEnumValue(enumValue, typeof(int)),
                "The type 'Int32' must be an enum or Nullable<T> where T is an enum type.");
        }
Exemple #5
0
        public void ConvertEnumValue_Throws_ForNonEnumValue()
        {
            // Arrange
            object value = new ODataPrimitiveValue(0);
            Type   type  = typeof(Color);

            // Act & Assert
            ExceptionAssert.Throws <ValidationException>(
                () => EnumDeserializationHelpers.ConvertEnumValue(value, type),
                "The value with type 'ODataPrimitiveValue' must have type 'ODataEnumValue'.");
        }
Exemple #6
0
        public void ConvertEnumValue_Throws_ForNullTypeParameter()
        {
            // Arrange
            object value = new ODataEnumValue("Red");
            Type   type  = null;

            // Act & Assert
            ExceptionAssert.ThrowsArgumentNull(
                () => EnumDeserializationHelpers.ConvertEnumValue(value, type),
                "type");
        }
Exemple #7
0
        public void ConvertEnumValue_Throws_ForNullValueParameter()
        {
            // Arrange
            object value = null;
            Type   type  = typeof(Color);

            // Act & Assert
            ExceptionAssert.ThrowsArgumentNull(
                () => EnumDeserializationHelpers.ConvertEnumValue(value, type),
                "value");
        }
        public void ConvertEnumValue_Throws_ForNonEnumType()
        {
            // Arrange
            object value = new ODataEnumValue("Red");
            Type   type  = typeof(int);

            // Act & Assert
            Assert.Throws <InvalidOperationException>(
                () => EnumDeserializationHelpers.ConvertEnumValue(value, type),
                "The type 'Int32' must be an enum or Nullable<T> where T is an enum type.");
        }
Exemple #9
0
        public void ConvertEnumValue_Returns_ForODataEnumValue()
        {
            // Arrange & Act & Assert
            ODataEnumValue enumValue = new ODataEnumValue("Red");

            Assert.Equal(EnumColor.Red, EnumDeserializationHelpers.ConvertEnumValue(enumValue, typeof(EnumColor)));

            // Arrange & Act & Assert
            enumValue = new ODataEnumValue("Green");
            Assert.Equal(EnumColor.Green, EnumDeserializationHelpers.ConvertEnumValue(enumValue, typeof(EnumColor)));
        }
Exemple #10
0
 public void ConvertEnumValue_Returns_ForPocoEnum()
 {
     // Arrange & Act & Assert
     Assert.Equal(EnumColor.Blue, EnumDeserializationHelpers.ConvertEnumValue(EnumColor.Blue, typeof(EnumColor)));
     Assert.Equal(EnumColor.Blue, EnumDeserializationHelpers.ConvertEnumValue(EnumColor.Blue, typeof(EnumColor?)));
 }
Exemple #11
0
 public void ConvertEnumValue_ThrowsArgumentNull_ForInputParameters()
 {
     // Arrange & Act & Assert
     ExceptionAssert.ThrowsArgumentNull(() => EnumDeserializationHelpers.ConvertEnumValue(null, null), "value");
     ExceptionAssert.ThrowsArgumentNull(() => EnumDeserializationHelpers.ConvertEnumValue(42, null), "type");
 }