Skip to content

Commit 65e8bfb

Browse files
committed
CSHARP-1546: Simplified tests and add more test cases.
1 parent 7d91245 commit 65e8bfb

File tree

1 file changed

+14
-135
lines changed

1 file changed

+14
-135
lines changed

src/MongoDB.Bson.Tests/IO/BsonBinaryReaderTests.cs

Lines changed: 14 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -57,146 +57,25 @@ public void BsonBinaryReader_should_support_reading_multiple_documents(
5757
}
5858
}
5959

60-
[Test]
61-
public void ReadBsonType_should_throw_when_bson_type_is_invalid_for_a()
62-
{
63-
var bytes = new byte[] { 0, 0, 0, 0, 0xf0, 97, 0 };
64-
var expectedMessage = $"Detected unknown BSON type \"\\xf0\" for fieldname \"a\". Are you using the latest driver version?";
65-
66-
using (var memoryStream = new MemoryStream(bytes))
67-
using (var subject = new BsonBinaryReader(memoryStream))
68-
{
69-
subject.ReadStartDocument();
70-
71-
Action action = () => subject.ReadBsonType();
72-
73-
action.ShouldThrow<FormatException>().WithMessage(expectedMessage);
74-
}
75-
}
76-
77-
[Test]
78-
public void ReadBsonType_should_throw_when_bson_type_is_invalid_for_a_0()
79-
{
80-
var bytes = new byte[] { 0, 0, 0, 0, 4, 97, 0, 0, 0, 0, 0, 0xf0, 48, 0 };
81-
var expectedMessage = $"Detected unknown BSON type \"\\xf0\" for fieldname \"a.0\". Are you using the latest driver version?";
82-
83-
using (var memoryStream = new MemoryStream(bytes))
84-
using (var subject = new BsonBinaryReader(memoryStream))
85-
{
86-
subject.ReadStartDocument();
87-
subject.ReadBsonType();
88-
subject.ReadName();
89-
subject.ReadStartArray();
90-
91-
Action action = () => subject.ReadBsonType();
92-
93-
action.ShouldThrow<FormatException>().WithMessage(expectedMessage);
94-
}
95-
}
96-
97-
[Test]
98-
public void ReadBsonType_should_throw_when_bson_type_is_invalid_for_a_0_b()
99-
{
100-
var bytes = new byte[] { 0, 0, 0, 0, 4, 97, 0, 0, 0, 0, 0, 3, 48, 0, 0, 0, 0, 0, 0xf0, 98, 0 };
101-
var expectedMessage = $"Detected unknown BSON type \"\\xf0\" for fieldname \"a.0.b\". Are you using the latest driver version?";
102-
103-
using (var memoryStream = new MemoryStream(bytes))
104-
using (var subject = new BsonBinaryReader(memoryStream))
105-
{
106-
subject.ReadStartDocument();
107-
subject.ReadBsonType();
108-
subject.ReadName();
109-
subject.ReadStartArray();
110-
subject.ReadBsonType();
111-
subject.ReadStartDocument();
112-
113-
Action action = () => subject.ReadBsonType();
114-
115-
action.ShouldThrow<FormatException>().WithMessage(expectedMessage);
116-
}
117-
}
118-
119-
[Test]
120-
public void ReadBsonType_should_throw_when_bson_type_is_invalid_for_a_1()
60+
[TestCase("00000000 f0 6100", "a")]
61+
[TestCase("00000000 08 6100 00 f0 6200", "b")]
62+
[TestCase("00000000 03 6100 00000000 f0 6200", "a.b")]
63+
[TestCase("00000000 03 6100 00000000 08 6200 00 f0 6300", "a.c")]
64+
[TestCase("00000000 04 6100 00000000 f0", "a.0")]
65+
[TestCase("00000000 04 6100 00000000 08 3000 00 f0", "a.1")]
66+
[TestCase("00000000 04 6100 00000000 03 3000 00000000 f0 6200", "a.0.b")]
67+
[TestCase("00000000 04 6100 00000000 03 3000 00000000 08 6200 00 f0 6300", "a.0.c")]
68+
[TestCase("00000000 04 6100 00000000 08 3000 00 03 3100 00000000 f0 6200", "a.1.b")]
69+
[TestCase("00000000 04 6100 00000000 08 3000 00 03 3200 00000000 08 6200 00 f0 6300", "a.1.c")]
70+
public void ReadBsonType_should_throw_when_bson_type_is_invalid(string hexBytes, string expectedElementName)
12171
{
122-
var bytes = new byte[] { 0, 0, 0, 0, 4, 97, 0, 0, 0, 0, 0, 0x10, 48, 0, 0, 0, 0, 0, 0xf0, 49, 0 };
123-
var expectedMessage = $"Detected unknown BSON type \"\\xf0\" for fieldname \"a.1\". Are you using the latest driver version?";
72+
var bytes = BsonUtils.ParseHexString(hexBytes.Replace(" ", ""));
73+
var expectedMessage = $"Detected unknown BSON type \"\\xf0\" for fieldname \"{expectedElementName}\". Are you using the latest driver version?";
12474

12575
using (var memoryStream = new MemoryStream(bytes))
12676
using (var subject = new BsonBinaryReader(memoryStream))
12777
{
128-
subject.ReadStartDocument();
129-
subject.ReadBsonType();
130-
subject.ReadName();
131-
subject.ReadStartArray();
132-
subject.ReadBsonType();
133-
subject.ReadInt32();
134-
135-
Action action = () => subject.ReadBsonType();
136-
137-
action.ShouldThrow<FormatException>().WithMessage(expectedMessage);
138-
}
139-
}
140-
141-
[Test]
142-
public void ReadBsonType_should_throw_when_bson_type_is_invalid_for_a_1_b()
143-
{
144-
var bytes = new byte[] { 0, 0, 0, 0, 4, 97, 0, 0, 0, 0, 0, 0x10, 48, 0, 0, 0, 0, 0, 3, 49, 0, 0, 0, 0, 0, 0xf0, 98, 0 };
145-
var expectedMessage = $"Detected unknown BSON type \"\\xf0\" for fieldname \"a.1.b\". Are you using the latest driver version?";
146-
147-
using (var memoryStream = new MemoryStream(bytes))
148-
using (var subject = new BsonBinaryReader(memoryStream))
149-
{
150-
subject.ReadStartDocument();
151-
subject.ReadBsonType();
152-
subject.ReadName();
153-
subject.ReadStartArray();
154-
subject.ReadBsonType();
155-
subject.ReadInt32();
156-
subject.ReadBsonType();
157-
subject.ReadStartDocument();
158-
159-
Action action = () => subject.ReadBsonType();
160-
161-
action.ShouldThrow<FormatException>().WithMessage(expectedMessage);
162-
}
163-
}
164-
165-
[Test]
166-
public void ReadBsonType_should_throw_when_bson_type_is_invalid_for_a_b()
167-
{
168-
var bytes = new byte[] { 0, 0, 0, 0, 3, 97, 0, 0, 0, 0, 0, 0xf0, 98, 0 };
169-
var expectedMessage = $"Detected unknown BSON type \"\\xf0\" for fieldname \"a.b\". Are you using the latest driver version?";
170-
171-
using (var memoryStream = new MemoryStream(bytes))
172-
using (var subject = new BsonBinaryReader(memoryStream))
173-
{
174-
subject.ReadStartDocument();
175-
subject.ReadBsonType();
176-
subject.ReadName();
177-
subject.ReadStartDocument();
178-
179-
Action action = () => subject.ReadBsonType();
180-
181-
action.ShouldThrow<FormatException>().WithMessage(expectedMessage);
182-
}
183-
}
184-
185-
[Test]
186-
public void ReadBsonType_should_throw_when_bson_type_is_invalid_for_b()
187-
{
188-
var bytes = new byte[] { 0, 0, 0, 0, 0x10, 97, 0, 0, 0, 0, 0, 0xf0, 98, 0 };
189-
var expectedMessage = $"Detected unknown BSON type \"\\xf0\" for fieldname \"b\". Are you using the latest driver version?";
190-
191-
using (var memoryStream = new MemoryStream(bytes))
192-
using (var subject = new BsonBinaryReader(memoryStream))
193-
{
194-
subject.ReadStartDocument();
195-
subject.ReadBsonType();
196-
subject.ReadName();
197-
subject.ReadInt32();
198-
199-
Action action = () => subject.ReadBsonType();
78+
Action action = () => BsonSerializer.Deserialize<BsonDocument>(subject);
20079

20180
action.ShouldThrow<FormatException>().WithMessage(expectedMessage);
20281
}

0 commit comments

Comments
 (0)