Skip to content

Commit

Permalink
Merge pull request #577 from wgross/bsonDocumentCopyTo
Browse files Browse the repository at this point in the history
BsonDocument copies its properties by reference  to KV array
  • Loading branch information
mbdavid authored Apr 23, 2017
2 parents 66578aa + 2144a94 commit a38037b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
31 changes: 31 additions & 0 deletions LiteDB.Tests/Document/DocumentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,36 @@ public void Document_ImplicitConvert_Test()

Assert.AreEqual("{\"int\":123,\"arr\":[3.0,2,1,\"zero\",false],\"doc\":{\"a\":\"a\",\"b\":[0]}}", json);
}

[TestMethod]
public void Document_copies_properties_to_KeyValue_array()
{
// ARRANGE
// create a Bson document with all possible value types

var document = new BsonDocument();
document.Add("string", new BsonValue("string"));
document.Add("bool", new BsonValue(true));
document.Add("objectId", new BsonValue(ObjectId.NewObjectId()));
document.Add("DateTime", new BsonValue(DateTime.Now));
document.Add("decimal", new BsonValue((decimal)1));
document.Add("double", new BsonValue((double)1.0));
document.Add("guid", new BsonValue(Guid.NewGuid()));
document.Add("int", new BsonValue((int)1));
document.Add("long", new BsonValue((long)1));
document.Add("bytes", new BsonValue(new byte[] { (byte)1 }));
document.Add("bsonDocument", new BsonDocument());

// ACT
// copy all properties to destination array

var result = new KeyValuePair<string, BsonValue>[document.Count()];
document.CopyTo(result, 0);

// ASSERT
// all BsonValue instances have been added to the array by reference

Assert.IsTrue(result.All(kv => object.ReferenceEquals(document.Get(kv.Key), kv.Value)));
}
}
}
2 changes: 1 addition & 1 deletion LiteDB/Document/BsonDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public bool Contains(KeyValuePair<string, BsonValue> item)

public void CopyTo(KeyValuePair<string, BsonValue>[] array, int arrayIndex)
{
throw new NotImplementedException();
((ICollection<KeyValuePair<string, BsonValue>>)this.RawValue).CopyTo(array, arrayIndex);
}

public bool Remove(KeyValuePair<string, BsonValue> item)
Expand Down

0 comments on commit a38037b

Please sign in to comment.