コード例 #1
0
        public void TestWrapWithNullCheckWithNoneNullable()
        {
            // GIVEN an expression for the parameter
            ParameterExpression parameter = Expression.Parameter(typeof(TestClass));

            // AND a nullable expression
            Expression nullableExpression  = Expression.Constant(10);
            Expression processedExpression = Expression.Constant("Hello");

            // WHEN wrapping a nullable
            ParameterProvider <TestClass> parameterProvider = new ParameterProvider <TestClass>(parameter, BindingFlags.Instance | BindingFlags.Public, NullCheckMode.None);
            Expression result = parameterProvider.WrapWithNullCheck(nullableExpression, processedExpression);

            // THEN the returned expression is the processedExpression
            Assert.AreSame(processedExpression, result);
        }
コード例 #2
0
        public void TestWrapWithNullCheckWithUseEmptyStringNullCheckModeAndReferenceType()
        {
            // GIVEN expressions for parameters
            ParameterExpression parameter = Expression.Parameter(typeof(TestClass));

            // AND a nullable expression
            Expression nullableExpression  = Expression.Constant(new object());
            Expression processedExpression = Expression.Constant("Hello");

            // WHEN the null check mode is set to UseEmptyString and we pass a referenec type
            ParameterProvider <TestClass> parameterProvider = new ParameterProvider <TestClass>(parameter, BindingFlags.Instance | BindingFlags.Public, NullCheckMode.UseEmptyString);
            Expression result = parameterProvider.WrapWithNullCheck(nullableExpression, processedExpression);

            // THEN the returned expression is a conditional
            Assert.IsInstanceOfType(result, typeof(ConditionalExpression));

            // AND the test is an equals comparison against null
            Expression test = ((ConditionalExpression)result).Test;

            Assert.IsInstanceOfType(test, typeof(BinaryExpression));
            Assert.AreEqual(ExpressionType.Equal, ((BinaryExpression)test).NodeType);

            Assert.AreSame(nullableExpression, ((BinaryExpression)test).Left);

            Assert.IsInstanceOfType(((BinaryExpression)test).Right, typeof(ConstantExpression));
            Assert.IsNull(((ConstantExpression)((BinaryExpression)test).Right).Value);

            // AND the true branch is a constant of an empty string
            Expression trueBranch = ((ConditionalExpression)result).IfTrue;

            Assert.IsInstanceOfType(trueBranch, typeof(ConstantExpression));
            Assert.AreEqual("", ((ConstantExpression)trueBranch).Value);

            // AND the false branch is the processed expression
            Expression falseBranch = ((ConditionalExpression)result).IfFalse;

            Assert.AreSame(processedExpression, falseBranch);
        }