Skip to content

Commit

Permalink
CSHARP-1255: fixed issue with prefixed mapped fields interferring wit…
Browse files Browse the repository at this point in the history
…h other unmapped and ignored fields.
  • Loading branch information
craiggwilson committed May 1, 2015
1 parent 5bce427 commit c5d064f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/MongoDB.Bson.Tests/IO/TrieNameDecoderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* Copyright 2010-2014 MongoDB 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.IO;
using FluentAssertions;
using MongoDB.Bson.IO;
using NUnit.Framework;

namespace MongoDB.Bson.Tests.IO
{
public class TrieNameDecoderTests
{
[Test]
public void Should_read_name_when_trie_does_not_know_about_the_name()
{
var trie = new BsonTrie<int>();
trie.Add("known", 10);

Assert(trie, "different");
}

[Test]
public void Should_read_name_when_trie_holds_a_longer_version_of_the_name()
{
var trie = new BsonTrie<int>();
trie.Add("longer", 10);

Assert(trie, "long");
}

[Test]
public void Should_read_name_when_trie_knows_about_the_name()
{
var trie = new BsonTrie<int>();
trie.Add("known", 10);

Assert(trie, "known");
}

private void Assert(BsonTrie<int> trie, string name)
{
var subject = new TrieNameDecoder<int>(trie);

using (var memoryStream = new MemoryStream())
using (var bsonStream = new BsonStreamAdapter(memoryStream))
{
bsonStream.WriteCString(name);
bsonStream.WriteInt32(20);
bsonStream.Position = 0;

var result = subject.Decode(bsonStream, Utf8Encodings.Strict);

result.Should().Be(name);
}
}
}
}
1 change: 1 addition & 0 deletions src/MongoDB.Bson.Tests/MongoDB.Bson.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<Compile Include="IO\MultiChunkBufferTests.cs" />
<Compile Include="IO\OutputBufferChunkSourceTests.cs" />
<Compile Include="IO\SingleChunkBufferTests.cs" />
<Compile Include="IO\TrieNameDecoderTests.cs" />
<Compile Include="Jira\CSharp728Tests.cs" />
<Compile Include="Jira\CSharp708Tests.cs" />
<Compile Include="Jira\CSharp476Tests.cs" />
Expand Down
3 changes: 3 additions & 0 deletions src/MongoDB.Bson/IO/TrieNameDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public TValue Value
public string Decode(BsonStream stream, UTF8Encoding encoding)
{
BsonTrieNode<TValue> node;
var oldPosition = stream.Position;
if (_trie.TryGetNode(stream, out node))
{
if (node.HasValue)
Expand All @@ -85,6 +86,8 @@ public string Decode(BsonStream stream, UTF8Encoding encoding)
_value = node.Value;
return node.ElementName;
}

stream.Position = oldPosition;
}

return stream.ReadCString(encoding);
Expand Down

0 comments on commit c5d064f

Please sign in to comment.