Exemplo n.º 1
0
        public void TestBinaryConstant()
        {
            var constant = new EdmBinaryConstant(new byte[] { 1, 1 });
            Assert.AreEqual(EdmValueKind.Binary, constant.ValueKind, "Invalid value kind.");

            this.ValidateEdmValueKindRoundTrip(EdmValueKind.Binary, EdmCoreModel.Instance.GetBinary(true), constant);
        }
        public void EdmBinaryConstant()
        {
            var e = new EdmBinaryConstant(new byte[] { 1, 2, 3 });
            Assert.AreEqual(EdmExpressionKind.BinaryConstant, e.ExpressionKind, "e.ExpressionKind");
            Assert.IsNull(e.Type, "e.Type");
            Assert.AreEqual(3, e.Value[2], "e.Value[2]");

            e = new EdmBinaryConstant(EdmCoreModel.Instance.GetBinary(true, null, true), new byte[] { 3, 2, 1 });
            Assert.AreEqual(true, e.Type.IsNullable, "e.Type.IsNullable");
            Assert.AreEqual(true, e.Type.AsBinary().IsUnbounded, "e.Type.AsBinary().isUnbounded");
            Assert.AreEqual(1, e.Value[2], "e.Value[2]");

            e = new EdmBinaryConstant(null, new byte[] { 3, 2, 1 });
            Assert.IsNull(e.Type, "e.Type");

            Assert.IsFalse(e.IsBad(), "Expression not bad.");
            Assert.AreEqual(0, e.Errors().Count(), "Expression has no errors");

            try
            {
                new EdmBinaryConstant(null);
                Assert.Fail("exception expected.");
            }
            catch (Exception ex1)
            {
                Assert.AreEqual(typeof(ArgumentNullException), ex1.GetType(), "ArgumentNullException expected");
            }
            try
            {
                new EdmBinaryConstant(EdmCoreModel.Instance.GetBinary(true), null);
                Assert.Fail("exception expected.");
            }
            catch (Exception ex2)
            {
                Assert.AreEqual(typeof(ArgumentNullException), ex2.GetType(), "ArgumentNullException expected");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Tries to convert the given value if it is of a type specific to the client library but still able to be mapped to EDM.
        /// </summary>
        /// <param name="primitiveValue">The value to convert.</param>
        /// <param name="type">The expected type of the value or null.</param>
        /// <param name="convertedValue">The converted value, if conversion was possible.</param>
        /// <returns>Whether or not conversion was possible.</returns>
        private static bool TryConvertClientSpecificPrimitiveValue(object primitiveValue, IEdmPrimitiveTypeReference type, out IEdmDelayedValue convertedValue)
        {
            byte[] byteArray;
            if (ClientConvert.TryConvertBinaryToByteArray(primitiveValue, out byteArray))
            {
                type = EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Binary);
                convertedValue = new EdmBinaryConstant((IEdmBinaryTypeReference)type, byteArray);
                return true;
            }

            PrimitiveType clientPrimitiveType;
            if (PrimitiveType.TryGetPrimitiveType(primitiveValue.GetType(), out clientPrimitiveType))
            {
                type = EnsurePrimitiveType(type, clientPrimitiveType.PrimitiveKind);
                if (clientPrimitiveType.PrimitiveKind == EdmPrimitiveTypeKind.String)
                {
                    {
                        convertedValue = new EdmStringConstant((IEdmStringTypeReference)type, clientPrimitiveType.TypeConverter.ToString(primitiveValue));
                        return true;
                    }
                }
            }

            convertedValue = null;
            return false;
        }