Skip to content

Commit 77ac96b

Browse files
author
rstam
committed
Modified some unit tests that were failing on Mono because Mono words some error messages differently.
1 parent 069dac1 commit 77ac96b

File tree

3 files changed

+129
-16
lines changed

3 files changed

+129
-16
lines changed

DriverOnlineTests/DriverOnlineTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<Compile Include="Core\MongoDatabaseTests.cs" />
8989
<Compile Include="Core\MongoServerTests.cs" />
9090
<Compile Include="Configuration.cs" />
91+
<Compile Include="ExpectedErrorMessage.cs" />
9192
<Compile Include="GridFS\MongoGridFSStreamTests.cs" />
9293
<Compile Include="GridFS\MongoGridFSTests.cs" />
9394
<Compile Include="Jira\CSharp101Tests.cs" />
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/* Copyright 2010-2012 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;
17+
using System.Collections.Generic;
18+
using System.Linq;
19+
using System.Text;
20+
21+
namespace MongoDB.DriverOnlineTests
22+
{
23+
/// <summary>
24+
/// A static class to hold expected error messages that differ between .NET and Mono.
25+
/// </summary>
26+
public static class ExpectedErrorMessage
27+
{
28+
// private static fields
29+
private static string _firstEmptySequence;
30+
private static string _lastEmptySequence;
31+
private static string _singleEmptySequence;
32+
private static string _singleLongSequence;
33+
34+
// static constructor
35+
static ExpectedErrorMessage()
36+
{
37+
var emptySequence = new List<int>();
38+
var longElementSequence = new List<int> { 1, 2, 3 };
39+
40+
try
41+
{
42+
var f = emptySequence.First();
43+
}
44+
catch (Exception ex)
45+
{
46+
_firstEmptySequence = ex.Message;
47+
}
48+
49+
try
50+
{
51+
var f = emptySequence.Last();
52+
}
53+
catch (Exception ex)
54+
{
55+
_lastEmptySequence = ex.Message;
56+
}
57+
58+
try
59+
{
60+
var f = emptySequence.Single();
61+
}
62+
catch (Exception ex)
63+
{
64+
_singleEmptySequence = ex.Message;
65+
}
66+
67+
try
68+
{
69+
var f = longElementSequence.Single();
70+
}
71+
catch (Exception ex)
72+
{
73+
_singleLongSequence = ex.Message;
74+
}
75+
}
76+
77+
// public static properties
78+
public static string FirstEmptySequence
79+
{
80+
get { return _firstEmptySequence; }
81+
}
82+
83+
public static string LastEmptySequence
84+
{
85+
get { return _lastEmptySequence; }
86+
}
87+
88+
public static string SingleEmptySequence
89+
{
90+
get { return _singleEmptySequence; }
91+
}
92+
93+
public static string SingleLongSequence
94+
{
95+
get { return _singleLongSequence; }
96+
}
97+
}
98+
}

DriverOnlineTests/Linq/SelectQueryTests.cs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -798,11 +798,14 @@ public void TestFirstWithPredicateAfterWhere()
798798
}
799799

800800
[Test]
801-
[ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Sequence contains no elements")]
802801
public void TestFirstWithPredicateNoMatch()
803802
{
804-
var result = (from c in _collection.AsQueryable<C>()
805-
select c).First(c => c.X == 9);
803+
var ex = Assert.Throws<InvalidOperationException>(() =>
804+
{
805+
var result = (from c in _collection.AsQueryable<C>()
806+
select c).First(c => c.X == 9);
807+
});
808+
Assert.AreEqual(ExpectedErrorMessage.FirstEmptySequence, ex.Message);
806809
}
807810

808811
[Test]
@@ -1103,11 +1106,14 @@ public void TestLastWithPredicateAfterWhere()
11031106
}
11041107

11051108
[Test]
1106-
[ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Sequence contains no elements")]
11071109
public void TestLastWithPredicateNoMatch()
11081110
{
1109-
var result = (from c in _collection.AsQueryable<C>()
1110-
select c).Last(c => c.X == 9);
1111+
var ex = Assert.Throws<InvalidOperationException>(() =>
1112+
{
1113+
var result = (from c in _collection.AsQueryable<C>()
1114+
select c).Last(c => c.X == 9);
1115+
});
1116+
Assert.AreEqual(ExpectedErrorMessage.LastEmptySequence, ex.Message);
11111117
}
11121118

11131119
[Test]
@@ -1668,12 +1674,15 @@ public void TestSingleOrDefaultWithPredicateOneMatch()
16681674
Assert.AreEqual(33, result.Y);
16691675
}
16701676

1671-
[ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Sequence contains more than one element")]
16721677
[Test]
16731678
public void TestSingleOrDefaultWithPredicateTwoMatches()
16741679
{
1675-
var result = (from c in _collection.AsQueryable<C>()
1676-
select c).SingleOrDefault(c => c.Y == 11);
1680+
var ex = Assert.Throws<InvalidOperationException>(() =>
1681+
{
1682+
var result = (from c in _collection.AsQueryable<C>()
1683+
select c).SingleOrDefault(c => c.Y == 11);
1684+
});
1685+
Assert.AreEqual(ExpectedErrorMessage.SingleLongSequence, ex.Message);
16771686
}
16781687

16791688
[Test]
@@ -1731,12 +1740,14 @@ public void TestSingleWithPredicateAfterWhere()
17311740
}
17321741

17331742
[Test]
1734-
[ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Sequence contains no elements")]
17351743
public void TestSingleWithPredicateNoMatch()
17361744
{
1737-
var result = (from c in _collection.AsQueryable<C>()
1738-
select c).Single(c => c.X == 9);
1739-
Assert.IsNull(result);
1745+
var ex = Assert.Throws<InvalidOperationException>(() =>
1746+
{
1747+
var result = (from c in _collection.AsQueryable<C>()
1748+
select c).Single(c => c.X == 9);
1749+
});
1750+
Assert.AreEqual(ExpectedErrorMessage.SingleEmptySequence, ex.Message);
17401751
}
17411752

17421753
[Test]
@@ -1749,11 +1760,14 @@ public void TestSingleWithPredicateOneMatch()
17491760
}
17501761

17511762
[Test]
1752-
[ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Sequence contains more than one element")]
17531763
public void TestSingleWithPredicateTwoMatches()
17541764
{
1755-
var result = (from c in _collection.AsQueryable<C>()
1756-
select c).Single(c => c.Y == 11);
1765+
var ex = Assert.Throws<InvalidOperationException>(() =>
1766+
{
1767+
var result = (from c in _collection.AsQueryable<C>()
1768+
select c).Single(c => c.Y == 11);
1769+
});
1770+
Assert.AreEqual(ExpectedErrorMessage.SingleLongSequence, ex.Message);
17571771
}
17581772

17591773
[Test]

0 commit comments

Comments
 (0)