コード例 #1
0
        public void EnsureCorrectType_SafeValueConversion()
        {
            var expression   = ExpressionTreeObjectMother.GetSomeExpression(typeof(int));
            var expectedType = typeof(long);

            ExpressionTypeUtility.EnsureCorrectType(expression, expectedType);
        }
コード例 #2
0
        public void EnsureCorrectType_UnsafeCastRequired()
        {
            var expression   = ExpressionTreeObjectMother.GetSomeExpression(typeof(object));
            var expectedType = typeof(string);

            ExpressionTypeUtility.EnsureCorrectType(expression, expectedType);
        }
コード例 #3
0
        public void EnsureCorrectType_CompletelyUnrelated()
        {
            var expression   = ExpressionTreeObjectMother.GetSomeExpression(typeof(string));
            var expectedType = typeof(List <int>);

            ExpressionTypeUtility.EnsureCorrectType(expression, expectedType);
        }
コード例 #4
0
        public void EnsureCorrectType_BoxingConvertibleToInterface()
        {
            var expression   = ExpressionTreeObjectMother.GetSomeExpression(typeof(int));
            var expectedType = typeof(IComparable);

            var result = ExpressionTypeUtility.EnsureCorrectType(expression, expectedType);

            CheckExpressionIsConverted(expression, expectedType, result);
        }
コード例 #5
0
        public void EnsureCorrectType_ReferenceAssignable()
        {
            var expression   = ExpressionTreeObjectMother.GetSomeExpression(typeof(string));
            var expectedType = typeof(object);

            var result = ExpressionTypeUtility.EnsureCorrectType(expression, expectedType);

            Assert.That(result, Is.SameAs(expression));
        }
コード例 #6
0
        public void EnsureCorrectType_Exact_ValueTypes()
        {
            var expectedType = typeof(int);
            var expression   = ExpressionTreeObjectMother.GetSomeExpression(expectedType);

            var result = ExpressionTypeUtility.EnsureCorrectType(expression, expectedType);

            Assert.That(result, Is.SameAs(expression));
        }
コード例 #7
0
        public void EnsureCorrectType_SafeValueConversion()
        {
            var expression   = ExpressionTreeObjectMother.GetSomeExpression(typeof(int));
            var expectedType = typeof(long);

            Assert.That(
                () => ExpressionTypeUtility.EnsureCorrectType(expression, expectedType),
                Throws.InvalidOperationException
                .With.Message.EqualTo(
                    "Type 'System.Int32' cannot be implicitly converted to type 'System.Int64'. "
                    + "Use Expression.Convert or Expression.ConvertChecked to make the conversion explicit."));
        }
コード例 #8
0
        public void EnsureCorrectType_UnsafeCastRequired()
        {
            var expression   = ExpressionTreeObjectMother.GetSomeExpression(typeof(object));
            var expectedType = typeof(string);

            Assert.That(
                () => ExpressionTypeUtility.EnsureCorrectType(expression, expectedType),
                Throws.InvalidOperationException
                .With.Message.EqualTo(
                    "Type 'System.Object' cannot be implicitly converted to type 'System.String'. "
                    + "Use Expression.Convert or Expression.ConvertChecked to make the conversion explicit."));
        }
コード例 #9
0
        public void EnsureCorrectType_CompletelyUnrelated()
        {
            var expression   = ExpressionTreeObjectMother.GetSomeExpression(typeof(string));
            var expectedType = typeof(List <int>);

            Assert.That(
                () => ExpressionTypeUtility.EnsureCorrectType(expression, expectedType),
                Throws.InvalidOperationException
                .With.Message.EqualTo(
                    "Type 'System.String' cannot be implicitly converted to type 'System.Collections.Generic.List`1[System.Int32]'. "
                    + "Use Expression.Convert or Expression.ConvertChecked to make the conversion explicit."));
        }
コード例 #10
0
        public void EnsureCorrectType_NonVoidExpression_WrappedAsVoid()
        {
            var expectedType = typeof(void);
            var expression   = ExpressionTreeObjectMother.GetSomeExpression(typeof(string));

            var result = ExpressionTypeUtility.EnsureCorrectType(expression, expectedType);

            Assert.That(result, Is.AssignableTo <BlockExpression>());
            var blockExpression = (BlockExpression)result;

            Assert.That(blockExpression.Type, Is.SameAs(expectedType));
            Assert.That(blockExpression.Expressions, Is.EqualTo(new[] { expression }));
        }