Skip to content

Commit 01c34eb

Browse files
committed
CSHARP-1064: Have pre-created BsonValue instances for common values.
1 parent 51587ec commit 01c34eb

File tree

12 files changed

+483
-16
lines changed

12 files changed

+483
-16
lines changed

src/MongoDB.Bson.Tests/MongoDB.Bson.Tests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@
105105
<Compile Include="Jira\CSharp648Tests.cs" />
106106
<Compile Include="Jira\CSharp783Tests.cs" />
107107
<Compile Include="Jira\CSharp803Tests.cs" />
108+
<Compile Include="ObjectModel\BsonBooleanTests.cs" />
109+
<Compile Include="ObjectModel\BsonDoubleTests.cs" />
110+
<Compile Include="ObjectModel\BsonInt32Tests.cs" />
111+
<Compile Include="ObjectModel\BsonInt64Tests.cs" />
112+
<Compile Include="ObjectModel\BsonStringTests.cs" />
108113
<Compile Include="ObjectModel\LazyBsonArrayTests.cs" />
109114
<Compile Include="ObjectModel\LazyBsonDocumentTests.cs" />
110115
<Compile Include="ObjectModel\RawBsonArrayTests.cs" />
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* Copyright 2015 MongoDB 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+
using System.Threading.Tasks;
21+
using FluentAssertions;
22+
using NUnit.Framework;
23+
24+
namespace MongoDB.Bson.Tests.ObjectModel
25+
{
26+
[TestFixture]
27+
public class BsonBooleanTests
28+
{
29+
[Test]
30+
public void implicit_conversion_from_bool_should_return_precreated_instance(
31+
[Values(false, true)]
32+
bool value)
33+
{
34+
var result1 = (BsonBoolean)value;
35+
var result2 = (BsonBoolean)value;
36+
37+
result2.Should().BeSameAs(result1);
38+
}
39+
40+
[Test]
41+
public void precreated_instances_should_have_the_expected_value(
42+
[Values(false, true)]
43+
bool value)
44+
{
45+
var result = (BsonBoolean)value;
46+
47+
result.Value.Should().Be(value);
48+
}
49+
}
50+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* Copyright 2015 MongoDB 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+
using System.Threading.Tasks;
21+
using FluentAssertions;
22+
using NUnit.Framework;
23+
24+
namespace MongoDB.Bson.Tests.ObjectModel
25+
{
26+
[TestFixture]
27+
public class BsonDoubleTests
28+
{
29+
[Test]
30+
public void implicit_conversion_from_double_should_return_new_instance(
31+
[Values(-101.0, 101.0)]
32+
double value)
33+
{
34+
var result1 = (BsonDouble)value;
35+
var result2 = (BsonDouble)value;
36+
37+
result2.Should().NotBeSameAs(result1);
38+
}
39+
40+
[Test]
41+
public void implicit_conversion_from_double_should_return_precreated_instance(
42+
[Range(-100.0, 100.0, 1.0)]
43+
double value)
44+
{
45+
var result1 = (BsonDouble)value;
46+
var result2 = (BsonDouble)value;
47+
48+
result2.Should().BeSameAs(result1);
49+
}
50+
51+
[Test]
52+
public void precreated_instances_should_have_the_expected_value(
53+
[Range(-100.0, 100.0, 1.0)]
54+
double value)
55+
{
56+
var result = (BsonDouble)value;
57+
58+
result.Value.Should().Be(value);
59+
}
60+
}
61+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* Copyright 2015 MongoDB 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+
using System.Threading.Tasks;
21+
using FluentAssertions;
22+
using NUnit.Framework;
23+
24+
namespace MongoDB.Bson.Tests.ObjectModel
25+
{
26+
[TestFixture]
27+
public class BsonInt32Tests
28+
{
29+
[Test]
30+
public void implicit_conversion_from_int_should_return_new_instance(
31+
[Values(-101, 101)]
32+
int value)
33+
{
34+
var result1 = (BsonInt32)value;
35+
var result2 = (BsonInt32)value;
36+
37+
result2.Should().NotBeSameAs(result1);
38+
}
39+
40+
[Test]
41+
public void implicit_conversion_from_int_should_return_precreated_instance(
42+
[Range(-100, 100)]
43+
int value)
44+
{
45+
var result1 = (BsonInt32)value;
46+
var result2 = (BsonInt32)value;
47+
48+
result2.Should().BeSameAs(result1);
49+
}
50+
51+
[Test]
52+
public void precreated_instances_should_have_the_expected_value(
53+
[Range(-100, 100)]
54+
int value)
55+
{
56+
var result = (BsonInt32)value;
57+
58+
result.Value.Should().Be(value);
59+
}
60+
}
61+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* Copyright 2015 MongoDB 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+
using System.Threading.Tasks;
21+
using FluentAssertions;
22+
using NUnit.Framework;
23+
24+
namespace MongoDB.Bson.Tests.ObjectModel
25+
{
26+
[TestFixture]
27+
public class BsonInt64Tests
28+
{
29+
[Test]
30+
public void implicit_conversion_from_long_should_return_new_instance(
31+
[Values(-101L, 101L)]
32+
long value)
33+
{
34+
var result1 = (BsonInt64)value;
35+
var result2 = (BsonInt64)value;
36+
37+
result2.Should().NotBeSameAs(result1);
38+
}
39+
40+
[Test]
41+
public void implicit_conversion_from_long_should_return_precreated_instance(
42+
[Range(-100L, 100L, 1L)]
43+
long value)
44+
{
45+
var result1 = (BsonInt64)value;
46+
var result2 = (BsonInt64)value;
47+
48+
result2.Should().BeSameAs(result1);
49+
}
50+
51+
[Test]
52+
public void precreated_instances_should_have_the_expected_value(
53+
[Range(-100L, 100L, 1L)]
54+
long value)
55+
{
56+
var result = (BsonInt64)value;
57+
58+
result.Value.Should().Be(value);
59+
}
60+
}
61+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* Copyright 2015 MongoDB 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+
using System.Threading.Tasks;
21+
using FluentAssertions;
22+
using NUnit.Framework;
23+
24+
namespace MongoDB.Bson.Tests.ObjectModel
25+
{
26+
[TestFixture]
27+
public class BsonStringTests
28+
{
29+
[Test]
30+
public void implicit_conversion_from_string_should_return_new_instance(
31+
[Values("x")]
32+
string value)
33+
{
34+
var result1 = (BsonString)value;
35+
var result2 = (BsonString)value;
36+
37+
result2.Should().NotBeSameAs(result1);
38+
}
39+
40+
[Test]
41+
public void implicit_conversion_from_string_should_return_precreated_instance(
42+
[Values("")]
43+
string value)
44+
{
45+
var result1 = (BsonString)value;
46+
var result2 = (BsonString)value;
47+
48+
result2.Should().BeSameAs(result1);
49+
}
50+
51+
[Test]
52+
public void precreated_instances_should_have_the_expected_value(
53+
[Values("")]
54+
string value)
55+
{
56+
var result = (BsonString)value;
57+
58+
result.Value.Should().Be(value);
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)