public void BsonBinaryReader_should_support_reading_multiple_documents( [Range(0, 3)] int numberOfDocuments) { var document = new BsonDocument("x", 1); var bson = document.ToBson(); var input = Enumerable.Repeat(bson, numberOfDocuments).Aggregate(Enumerable.Empty<byte>(), (a, b) => a.Concat(b)).ToArray(); var expectedResult = Enumerable.Repeat(document, numberOfDocuments); using (var stream = new MemoryStream(input)) using (var binaryReader = new BsonBinaryReader(stream)) { var result = new List<BsonDocument>(); while (!binaryReader.IsAtEndOfFile()) { binaryReader.ReadStartDocument(); var name = binaryReader.ReadName(); var value = binaryReader.ReadInt32(); binaryReader.ReadEndDocument(); var resultDocument = new BsonDocument(name, value); result.Add(resultDocument); } result.Should().Equal(expectedResult); } }
public void TestBsonAwesome() { string byteString = @"1\x00\x00\x00\x04BSON\x00&\x00\x00\x00\x020\x00\x08\x00\x00\x00awesome\x00\x011\x00333333\x14@\x102\x00\xc2\x07\x00\x00\x00\x00"; byte[] bytes = DecodeByteString(byteString); MemoryStream stream = new MemoryStream(bytes); using (BsonReader bsonReader = new BsonBinaryReader(stream)) { bsonReader.ReadStartDocument(); Assert.AreEqual(BsonType.Array, bsonReader.ReadBsonType()); Assert.AreEqual("BSON", bsonReader.ReadName()); bsonReader.ReadStartArray(); Assert.AreEqual(BsonType.String, bsonReader.ReadBsonType()); Assert.AreEqual("awesome", bsonReader.ReadString()); Assert.AreEqual(BsonType.Double, bsonReader.ReadBsonType()); Assert.AreEqual(5.05, bsonReader.ReadDouble()); Assert.AreEqual(BsonType.Int32, bsonReader.ReadBsonType()); Assert.AreEqual(1986, bsonReader.ReadInt32()); bsonReader.ReadEndArray(); bsonReader.ReadEndDocument(); } }