Browse Source

Implemented CSHARP-313. Give a more useful error message when attempting to write a scalar value to the root of a BSON document.

pull/71/head
rstam 14 years ago
parent
commit
21c50c17e3
  1. 18
      Bson/IO/BsonWriter.cs
  2. 1
      BsonUnitTests/BsonUnitTests.csproj
  3. 60
      BsonUnitTests/Jira/CSharp313Tests.cs

18
Bson/IO/BsonWriter.cs

@ -746,8 +746,24 @@ namespace MongoDB.Bson.IO {
string methodName,
params BsonWriterState[] validStates
) {
string message;
if (state == BsonWriterState.Initial || state == BsonWriterState.ScopeDocument || state == BsonWriterState.Done) {
if (!methodName.StartsWith("End") && methodName != "WriteName") {
var typeName = methodName.Substring(5);
if (typeName.StartsWith("Start")) {
typeName = typeName.Substring(5);
}
var article = "A";
if (new char[] { 'A', 'E', 'I', 'O', 'U' }.Contains(typeName[0])) {
article = "An";
}
message = string.Format("{0} {1} value cannot be written to the root level of a BSON document.", article, typeName);
throw new InvalidOperationException(message);
}
}
var validStatesString = string.Join(" or ", validStates.Select(s => s.ToString()).ToArray());
var message = string.Format("{0} can only be called when State is {1}, not when State is {2}", methodName, validStatesString, state);
message = string.Format("{0} can only be called when State is {1}, not when State is {2}", methodName, validStatesString, state);
throw new InvalidOperationException(message);
}
#endregion

1
BsonUnitTests/BsonUnitTests.csproj

@ -131,6 +131,7 @@
<Compile Include="Jira\CSharp276Tests.cs" />
<Compile Include="Jira\CSharp293Tests.cs" />
<Compile Include="Jira\CSharp311Tests.cs" />
<Compile Include="Jira\CSharp313Tests.cs" />
<Compile Include="Jira\CSharp70Tests.cs" />
<Compile Include="Jira\CSharp71Tests.cs" />
<Compile Include="Jira\CSharp74Tests.cs" />

60
BsonUnitTests/Jira/CSharp313Tests.cs

@ -0,0 +1,60 @@
/* Copyright 2010-2011 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.Options;
namespace MongoDB.BsonUnitTests.Jira {
[TestFixture]
public class CSharp313Tests {
private static object[] scalarValues = new object[] {
new BsonBinaryData(new byte[] { 1, 2, 3 }),
true,
DateTime.UtcNow,
1.2,
1,
1L,
new BsonJavaScript("this.x == 1"),
new BsonJavaScriptWithScope("this.x == y", new BsonDocument("y", 1)),
BsonMaxKey.Value,
BsonMinKey.Value,
BsonNull.Value,
ObjectId.GenerateNewId(),
new BsonRegularExpression("abc"),
new BsonArray { 1, 2 },
"abc",
BsonSymbol.Create("abc"),
new BsonTimestamp(1234L),
BsonUndefined.Value
};
[Test]
public void TestStringToBson() {
foreach (var scalarValue in scalarValues) {
Assert.Throws<InvalidOperationException>(() => { var bson = scalarValue.ToBson(); });
Assert.Throws<InvalidOperationException>(() => { var bson = scalarValue.ToBsonDocument(); });
}
}
}
}
Loading…
Cancel
Save