Skip to content

Commit 050fe3f

Browse files
author
Sridhar Nanjundeswaran
committed
CSHARP-718 - Normalize VB Nothing comparison
1 parent 324da6b commit 050fe3f

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed

MongoDB.Driver/Linq/Expressions/ExpressionNormalizer.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,23 @@ protected override Expression VisitBinary(BinaryExpression node)
102102
}
103103
}
104104

105+
// VB introduces a Convert on the LHS with a Nothing comparison, so we make it look like C# which does not have
106+
// any with a comparison to null
107+
if ((node.NodeType == ExpressionType.Equal || node.NodeType == ExpressionType.NotEqual) &&
108+
node.Left.NodeType == ExpressionType.Convert &&
109+
node.Right.NodeType == ExpressionType.Constant)
110+
{
111+
var left = (UnaryExpression)node.Left;
112+
var right = (ConstantExpression)node.Right;
113+
if (left.Type == typeof(object) && right.Value == null)
114+
{
115+
result = Expression.MakeBinary(
116+
node.NodeType,
117+
left.Operand,
118+
node.Right);
119+
}
120+
}
121+
105122
// VB creates coalescing operations when dealing with nullable value comparisons, so we try and make this look like C#
106123
if (node.NodeType == ExpressionType.Coalesce)
107124
{
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* Copyright 2010-2013 10gen Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.Linq;
17+
using MongoDB.Driver;
18+
using MongoDB.Driver.Linq;
19+
using NUnit.Framework;
20+
21+
namespace MongoDB.DriverUnitTests.Jira
22+
{
23+
[TestFixture]
24+
public class CSharp718
25+
{
26+
public class C
27+
{
28+
public int Id;
29+
public int[] Foo;
30+
}
31+
32+
private MongoServer _server;
33+
private MongoDatabase _database;
34+
private MongoCollection<C> _collection;
35+
36+
[TestFixtureSetUp]
37+
public void Setup()
38+
{
39+
_server = Configuration.TestServer;
40+
_database = Configuration.TestDatabase;
41+
_collection = Configuration.GetTestCollection<C>();
42+
TestSetup();
43+
}
44+
45+
[Test]
46+
public void TestLinqNullEquality()
47+
{
48+
var postsWithFoo = (from d in _collection.AsQueryable<C>()
49+
where d.Foo == null
50+
select d).Count();
51+
Assert.AreEqual(2, postsWithFoo);
52+
}
53+
54+
[Test]
55+
public void TestLinqNullInequality()
56+
{
57+
var postsWithFoo = (from d in _collection.AsQueryable<C>()
58+
where d.Foo != null
59+
select d).Count();
60+
Assert.AreEqual(3, postsWithFoo);
61+
}
62+
63+
private void TestSetup()
64+
{
65+
_collection.RemoveAll();
66+
_collection.Insert(new C() { Id = 1});
67+
_collection.Insert(new C() { Id = 2, Foo = null});
68+
_collection.Insert(new C() { Id = 3, Foo = new int[] {1}});
69+
_collection.Insert(new C() { Id = 4, Foo = new int[] { 1, 2 } });
70+
_collection.Insert(new C() { Id = 5, Foo = new int[] { 1, 2, 3 } });
71+
}
72+
}
73+
}

MongoDB.DriverUnitTests/MongoDB.DriverUnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
<Compile Include="Jira\CSharp542Tests.cs" />
121121
<Compile Include="Jira\CSharp653Tests.cs" />
122122
<Compile Include="Jira\CSharp714Tests.cs" />
123+
<Compile Include="Jira\CSharp718Tests.cs" />
123124
<Compile Include="MongoClientSettingsTests.cs" />
124125
<Compile Include="ReadPreferenceTests.cs" />
125126
<Compile Include="MongoCollectionSettingsTests.cs" />
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
' Copyright 2010-2013 10gen Inc.
2+
'
3+
' Licensed under the Apache License, Version 2.0 (the "License");
4+
' you may not use this file except in compliance with the License.
5+
' You may obtain a copy of the License at
6+
'
7+
' http://www.apache.org/licenses/LICENSE-2.0
8+
'
9+
' Unless required by applicable law or agreed to in writing, software
10+
' distributed under the License is distributed on an "AS IS" BASIS,
11+
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
' See the License for the specific language governing permissions and
13+
' limitations under the License.
14+
'
15+
16+
Imports System.Linq
17+
Imports MongoDB.Driver
18+
Imports MongoDB.Driver.Linq
19+
Imports NUnit.Framework
20+
21+
Namespace MongoDB.DriverUnitTests.Jira
22+
23+
<TestFixture()>
24+
Public Class CSharp718
25+
26+
Public Class C
27+
Public Id As Integer
28+
Public Foo() As Integer
29+
End Class
30+
31+
Private _server As MongoServer
32+
Private _database As MongoDatabase
33+
Private _collection As MongoCollection(Of C)
34+
35+
<TestFixtureSetUp()>
36+
Public Sub Setup()
37+
_server = Configuration.TestServer
38+
_database = Configuration.TestDatabase
39+
_collection = Configuration.GetTestCollection(Of C)()
40+
TestSetup()
41+
End Sub
42+
43+
<Test()>
44+
Public Sub TestLinqIsNothing()
45+
Dim postsWithFoo = (From d In _collection.AsQueryable(Of C)()
46+
Where d.Foo Is Nothing
47+
Select d).Count()
48+
Assert.AreEqual(2, postsWithFoo)
49+
End Sub
50+
51+
<Test()>
52+
Public Sub TestLinqIsNotNothing()
53+
Dim postsWithFoo = (From d In _collection.AsQueryable(Of C)()
54+
Where d.Foo IsNot Nothing
55+
Select d).Count()
56+
Assert.AreEqual(3, postsWithFoo)
57+
End Sub
58+
59+
Private Sub TestSetup()
60+
_collection.RemoveAll()
61+
_collection.Insert(New C() With { .Id = 1})
62+
_collection.Insert(New C() With {.Id = 2, .Foo = Nothing})
63+
_collection.Insert(New C() With {.Id = 3, .Foo = {1}})
64+
_collection.Insert(New C() With {.Id = 4, .Foo = {1, 2}})
65+
_collection.Insert(New C() With {.Id = 5, .Foo = {1, 2, 3}})
66+
End Sub
67+
End Class
68+
End Namespace
69+

MongoDB.DriverUnitTestsVB/MongoDB.DriverUnitTestsVB.vbproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
</ItemGroup>
7070
<ItemGroup>
7171
<Compile Include="Jira\CSharp542.vb" />
72+
<Compile Include="Jira\CSharp718.vb" />
7273
<Compile Include="Linq\MongoQueryableTests.vb" />
7374
<Compile Include="Linq\MongoQueryProviderTests.vb" />
7475
<Compile Include="Linq\SelectDictionaryTests.vb" />

0 commit comments

Comments
 (0)