public void VisitExpression_ThrowsOnInvalidContext ()
    {
      var expression = new CustomExpression (typeof (string));

      var visitor = new TestableSqlContextExpressionVisitor ((SqlExpressionContext) (-1), _stageMock, _mappingResolutionContext);
      visitor.VisitExpression (expression);
    }
    public void VisitExpression_AppliesSpecifiedSemantics ()
    {
      var entityExpression = SqlStatementModelObjectMother.CreateSqlEntityDefinitionExpression (typeof (Cook));

      var visitor = new TestableSqlContextExpressionVisitor (SqlExpressionContext.ValueRequired, _stageMock, _mappingResolutionContext);
      var result = visitor.VisitExpression (entityExpression);

      Assert.That (result, Is.SameAs (entityExpression));
    }
    public void VisitExpression_ConvertedInt_ToPredicate ()
    {
      var expression = new SqlConvertedBooleanExpression (new CustomExpression (typeof (int)));
      var nullableExpression = new SqlConvertedBooleanExpression (new CustomExpression (typeof (int?)));

      var visitor = new TestableSqlContextExpressionVisitor (SqlExpressionContext.PredicateRequired, _stageMock, _mappingResolutionContext);
      var result = visitor.VisitExpression (expression);
      var resultNullable = visitor.VisitExpression (nullableExpression);

      var expected = Expression.Equal (expression.Expression, new SqlLiteralExpression (1));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expected, result);
      var expectedNullable = Expression.Equal (nullableExpression.Expression, new SqlLiteralExpression (1, true), true, null);
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedNullable, resultNullable);
    }
    public void VisitExpression_ThrowsOnNonConvertible_ToPredicate ()
    {
      var expression = new CustomExpression (typeof (string));

      var visitor = new TestableSqlContextExpressionVisitor (SqlExpressionContext.PredicateRequired, _stageMock, _mappingResolutionContext);
      visitor.VisitExpression (expression);
    }
    public void VisitExpression_LeavesExistingPredicate ()
    {
      var expression = new CustomExpression (typeof (bool));
      var nullableExpression = new CustomExpression (typeof (bool?));

      var visitor = new TestableSqlContextExpressionVisitor (SqlExpressionContext.PredicateRequired, _stageMock, _mappingResolutionContext);
      var result = visitor.VisitExpression (expression);
      var resultNullable = visitor.VisitExpression (nullableExpression);

      Assert.That (result, Is.SameAs (expression));
      Assert.That (resultNullable, Is.SameAs (nullableExpression));
    }
    public void VisitExpression_SingleValueRequired_LeavesExistingSingleValue ()
    {
      var expression = new CustomExpression (typeof (int));

      var visitor = new TestableSqlContextExpressionVisitor (SqlExpressionContext.SingleValueRequired, _stageMock, _mappingResolutionContext);
      var result = visitor.VisitExpression (expression);

      Assert.That (result, Is.SameAs (expression));
    }
    public void VisitExpression_ValueRequired_LeavesExistingValue ()
    {
      var expression = CreateNewExpression ();

      var visitor = new TestableSqlContextExpressionVisitor (SqlExpressionContext.ValueRequired, _stageMock, _mappingResolutionContext);
      var result = visitor.VisitExpression (expression);

      Assert.That (result, Is.SameAs (expression));
    }
    public void VisitExpression_SingleValueRequired_ConvertsBool_ToSingleValue ()
    {
      var expression = new CustomExpression (typeof (bool));
      var nullableExpression = new CustomExpression (typeof (bool?));

      var visitor = new TestableSqlContextExpressionVisitor (SqlExpressionContext.SingleValueRequired, _stageMock, _mappingResolutionContext);
      var result = visitor.VisitExpression (expression);
      var resultNullable = visitor.VisitExpression (nullableExpression);

      var expected = new SqlConvertedBooleanExpression (GetNonNullablePredicateAsValueExpression (expression));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expected, result);
      var expectedNullable = new SqlConvertedBooleanExpression (GetNullablePredicateAsValueExpression (nullableExpression));
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedNullable, resultNullable);
    }
    public void VisitExpression_CallsNodeSpecificVisitMethods ()
    {
      var entityExpression = SqlStatementModelObjectMother.CreateSqlEntityDefinitionExpression (typeof (Cook));

      var visitor = new TestableSqlContextExpressionVisitor (SqlExpressionContext.SingleValueRequired, _stageMock, _mappingResolutionContext);
      Assert.That (() => visitor.VisitExpression (entityExpression), Throws.TypeOf<NotSupportedException>());
    }