public void Ctor_checks_test_types()
        {
            var ex = Assert.Throws <ArgumentException>(
                () => new CaseExpression(
                    operand: Expression.Constant(false),
                    new CaseWhenClause(Expression.Constant(0), Expression.Constant(false))));

            Assert.Contains(
                RelationalStrings.CaseWhenClauseTestTypeUnexpected(typeof(int), typeof(bool)),
                ex.Message);
            Assert.Equal("whenClauses", ex.ParamName);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="CaseExpression" /> class.
        /// </summary>
        /// <param name="operand"> The case operand expression. </param>
        /// <param name="whenClauses"> The list of when clauses. </param>
        /// <param name="elseResult"> The else result expression. </param>
        public CaseExpression(
            [CanBeNull] Expression operand,
            [NotNull] IReadOnlyList <CaseWhenClause> whenClauses,
            [CanBeNull] Expression elseResult)
        {
            Check.NotEmpty(whenClauses, nameof(whenClauses));

            var resultType = whenClauses[0].Result.Type;
            var expectedWhenOperandType = operand?.Type ?? typeof(bool);

            foreach (var whenClause in whenClauses)
            {
                if (operand != null &&
                    whenClause.Test.Type != expectedWhenOperandType)
                {
                    throw new ArgumentException(
                              RelationalStrings.CaseWhenClauseTestTypeUnexpected(
                                  whenClause.Test.Type,
                                  expectedWhenOperandType),
                              nameof(whenClauses));
                }

                if (whenClause.Result.Type != resultType)
                {
                    throw new ArgumentException(
                              RelationalStrings.CaseWhenClauseResultTypeUnexpected(whenClause.Result.Type, resultType),
                              nameof(whenClauses));
                }
            }

            if (elseResult != null &&
                elseResult.Type != resultType)
            {
                throw new ArgumentException(
                          RelationalStrings.CaseElseResultTypeUnexpected(elseResult.Type, resultType),
                          nameof(elseResult));
            }

            Type        = resultType;
            Operand     = operand;
            ElseResult  = elseResult;
            WhenClauses = whenClauses;
        }