Skip to content

Implicit bools in nested methods #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,31 @@

namespace CouchDB.Driver.CompositeExpressionsEvaluator
{
internal class BoolMemberToConstantEvaluator : ExpressionVisitor
internal class BoolMemberToConstantEvaluator : ExpressionVisitor
{
private bool _visitingWhereMethod;
private bool _isVisitingWhereMethodOrChild;

protected override Expression VisitMethodCall(MethodCallExpression m)
{
_visitingWhereMethod = m.Method.Name == nameof(Queryable.Where) && m.Method.DeclaringType == typeof(Queryable);
if (_visitingWhereMethod)
bool isRootWhereMethod = !_isVisitingWhereMethodOrChild && m.Method.Name == nameof(Queryable.Where) && m.Method.DeclaringType == typeof(Queryable);
if (isRootWhereMethod)
{
Expression result = base.VisitMethodCall(m);
_visitingWhereMethod = false;
return result;
_isVisitingWhereMethodOrChild = true;
}
return base.VisitMethodCall(m);

Expression result = base.VisitMethodCall(m);

if (isRootWhereMethod)
{
_isVisitingWhereMethodOrChild = false;
}

return result;
}

protected override Expression VisitBinary(BinaryExpression expression)
{
if (_visitingWhereMethod && expression.Right is ConstantExpression c && c.Type == typeof(bool) &&
if (_isVisitingWhereMethodOrChild && expression.Right is ConstantExpression c && c.Type == typeof(bool) &&
(expression.NodeType == ExpressionType.Equal || expression.NodeType == ExpressionType.NotEqual))
{
return expression;
Expand Down Expand Up @@ -50,8 +56,8 @@ protected override Expression VisitUnary(UnaryExpression expression)

private bool IsWhereBooleanExpression(MemberExpression expression)
{
return _visitingWhereMethod &&
expression.Member is PropertyInfo info &&
return _isVisitingWhereMethodOrChild &&
expression.Member is PropertyInfo info &&
info.PropertyType == typeof(bool);
}
}
Expand Down
5 changes: 3 additions & 2 deletions tests/CouchDB.Driver.E2ETests/Client_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace CouchDB.Driver.E2E
{
[Trait("Category", "Integration")]
public class ClientTests
{
[Fact]
Expand Down Expand Up @@ -54,10 +55,10 @@ public async Task Users()
{
users = await client.CreateDatabaseAsync<CouchUser>().ConfigureAwait(false);
}

var luke = await users.CreateAsync(new CouchUser(name: "luke", password: "lasersword")).ConfigureAwait(false);
Assert.Equal("luke", luke.Name);

luke = await users.FindAsync(luke.Id).ConfigureAwait(false);
Assert.Equal("luke", luke.Name);

Expand Down
30 changes: 30 additions & 0 deletions tests/CouchDB.Driver.UnitTests/Find/Find_Selector_Combinations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,40 @@ public void ElemMatch()
Assert.Equal(@"{""selector"":{""battles"":{""$elemMatch"":{""planet"":""Naboo""}}}}", json);
}
[Fact]
public void ElemMatchImplicitBool()
{
var json = _rebels.Where(r => r.Battles.Any(b => b.DidWin)).ToString();
Assert.Equal(@"{""selector"":{""battles"":{""$elemMatch"":{""didWin"":true}}}}", json);
}
[Fact]
public void ElemMatchBoolExplicit()
{
var json = _rebels.Where(r => r.Battles.Any(b => b.DidWin == true)).ToString();
Assert.Equal(@"{""selector"":{""battles"":{""$elemMatch"":{""didWin"":true}}}}", json);
}
[Fact]
public void ElemMatchNested()
{
var json = _rebels.Where(r => r.Battles.Any(b => b.Vehicles.Any(v => v.CanFly == true))).ToString();
Assert.Equal(@"{""selector"":{""battles"":{""$elemMatch"":{""vehicles"":{""$elemMatch"":{""canFly"":true}}}}}}", json);
}
[Fact]
public void ElemMatchNestedImplicitBool()
{
var json = _rebels.Where(r => r.Battles.Any(b => b.Vehicles.Any(v => v.CanFly))).ToString();
Assert.Equal(@"{""selector"":{""battles"":{""$elemMatch"":{""vehicles"":{""$elemMatch"":{""canFly"":true}}}}}}", json);
}
[Fact]
public void AllMatch()
{
var json = _rebels.Where(r => r.Battles.All(b => b.Planet == "Naboo")).ToString();
Assert.Equal(@"{""selector"":{""battles"":{""$allMatch"":{""planet"":""Naboo""}}}}", json);
}
[Fact]
public void AllMatchImplicitBool()
{
var json = _rebels.Where(r => r.Battles.All(b => b.DidWin)).ToString();
Assert.Equal(@"{""selector"":{""battles"":{""$allMatch"":{""didWin"":true}}}}", json);
}
}
}
2 changes: 2 additions & 0 deletions tests/CouchDB.Driver.UnitTests/_Models/Battle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ namespace CouchDB.Driver.UnitTests.Models
{
public class Battle
{
public bool DidWin { get; set; }
public string Planet { get; set; }
public DateTime Date { get; set; }
public List<Vehicle> Vehicles { get; set; } = new List<Vehicle>();
}
}
7 changes: 7 additions & 0 deletions tests/CouchDB.Driver.UnitTests/_Models/Vehicle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace CouchDB.Driver.UnitTests.Models
{
public class Vehicle
{
public bool CanFly { get; set; }
}
}