PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 704 lines | 564 code | 125 blank | 15 comment | 1 complexity | 8096e084e9e8c2c48d612637ceffeddc MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2010-2014 MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.IO;
  17. using System.Linq;
  18. using System.Reflection;
  19. using FluentAssertions;
  20. using MongoDB.Bson.IO;
  21. using NSubstitute;
  22. using NUnit.Framework;
  23. namespace MongoDB.Bson.Tests.IO
  24. {
  25. [TestFixture]
  26. public class BsonStreamExtensionsTests
  27. {
  28. [Test]
  29. public void BackpatchSize_should_backpatch_the_size(
  30. [Values(0, 1, 5)]
  31. int startPosition)
  32. {
  33. var bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  34. var length = bytes.Length - startPosition;
  35. var expectedBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  36. Array.Copy(BitConverter.GetBytes(length), 0, expectedBytes, startPosition, 4);
  37. using (var memoryStream = new MemoryStream())
  38. using (var stream = new BsonStreamAdapter(memoryStream))
  39. {
  40. stream.WriteBytes(bytes, 0, bytes.Length);
  41. var position = stream.Position;
  42. stream.BackpatchSize(startPosition);
  43. memoryStream.ToArray().Should().Equal(expectedBytes);
  44. stream.Position.Should().Be(position);
  45. }
  46. }
  47. [Test]
  48. public void BackpatchSize_should_throw_when_startPosition_is_out_of_range(
  49. [Values(-1, 4)]
  50. int startPosition)
  51. {
  52. using (var stream = Substitute.For<BsonStream>())
  53. {
  54. stream.Length.Returns(3);
  55. Action action = () => stream.BackpatchSize(startPosition);
  56. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("startPosition");
  57. }
  58. }
  59. [Test]
  60. public void BackpatchSize_should_throw_when_stream_is_null()
  61. {
  62. BsonStream stream = null;
  63. Action action = () => stream.BackpatchSize(0);
  64. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("stream");
  65. }
  66. [TestCase(0, BsonBinarySubType.Binary)]
  67. [TestCase(1, BsonBinarySubType.Function)]
  68. #pragma warning disable 618
  69. [TestCase(2, BsonBinarySubType.OldBinary)]
  70. #pragma warning restore
  71. [TestCase(3, BsonBinarySubType.UuidLegacy)]
  72. [TestCase(4, BsonBinarySubType.UuidStandard)]
  73. [TestCase(5, BsonBinarySubType.MD5)]
  74. [TestCase(0x80, BsonBinarySubType.UserDefined)]
  75. public void ReadBinarySubType_should_return_expected_result(int n, BsonBinarySubType expectedResult)
  76. {
  77. var bytes = new byte[] { (byte)n };
  78. using (var memoryStream = new MemoryStream(bytes))
  79. using (var stream = new BsonStreamAdapter(memoryStream))
  80. {
  81. var result = stream.ReadBinarySubType();
  82. result.Should().Be(expectedResult);
  83. }
  84. }
  85. [Test]
  86. public void ReadBinarySubType_should_throw_when_at_end_of_stream()
  87. {
  88. using (var memoryStream = new MemoryStream())
  89. using (var stream = new BsonStreamAdapter(memoryStream))
  90. {
  91. Action action = () => stream.ReadBinarySubType();
  92. action.ShouldThrow<EndOfStreamException>();
  93. }
  94. }
  95. [Test]
  96. public void ReadBinarySubType_should_throw_when_stream_is_null()
  97. {
  98. BsonStream stream = null;
  99. Action action = () => stream.ReadBinarySubType();
  100. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("stream");
  101. }
  102. [TestCase(0, false)]
  103. [TestCase(1, true)]
  104. public void ReadBoolean_should_return_expected_result(int n, bool expectedResult)
  105. {
  106. var bytes = new byte[] { (byte)n };
  107. using (var memoryStream = new MemoryStream(bytes))
  108. using (var stream = new BsonStreamAdapter(memoryStream))
  109. {
  110. var result = stream.ReadBoolean();
  111. result.Should().Be(expectedResult);
  112. }
  113. }
  114. [Test]
  115. public void ReadBoolean_should_throw_when_at_end_of_stream()
  116. {
  117. using (var memoryStream = new MemoryStream())
  118. using (var stream = new BsonStreamAdapter(memoryStream))
  119. {
  120. Action action = () => stream.ReadBoolean();
  121. action.ShouldThrow<EndOfStreamException>();
  122. }
  123. }
  124. [Test]
  125. public void ReadBoolean_should_throw_when_stream_is_null()
  126. {
  127. BsonStream stream = null;
  128. Action action = () => stream.ReadBoolean();
  129. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("stream");
  130. }
  131. [TestCase(0x00, BsonType.EndOfDocument)]
  132. [TestCase(0x01, BsonType.Double)]
  133. [TestCase(0x02, BsonType.String)]
  134. [TestCase(0x03, BsonType.Document)]
  135. [TestCase(0x04, BsonType.Array)]
  136. [TestCase(0x05, BsonType.Binary)]
  137. [TestCase(0x06, BsonType.Undefined)]
  138. [TestCase(0x07, BsonType.ObjectId)]
  139. [TestCase(0x08, BsonType.Boolean)]
  140. [TestCase(0x09, BsonType.DateTime)]
  141. [TestCase(0x0a, BsonType.Null)]
  142. [TestCase(0x0b, BsonType.RegularExpression)]
  143. [TestCase(0x0d, BsonType.JavaScript)]
  144. [TestCase(0x0e, BsonType.Symbol)]
  145. [TestCase(0x0f, BsonType.JavaScriptWithScope)]
  146. [TestCase(0x10, BsonType.Int32)]
  147. [TestCase(0x11, BsonType.Timestamp)]
  148. [TestCase(0x12, BsonType.Int64)]
  149. [TestCase(0xff, BsonType.MinKey)]
  150. [TestCase(0x7f, BsonType.MaxKey)]
  151. public void ReadBsonType_should_return_expected_result(int n, BsonType expectedResult)
  152. {
  153. var bytes = new byte[] { (byte)n };
  154. using (var memoryStream = new MemoryStream(bytes))
  155. using (var stream = new BsonStreamAdapter(memoryStream))
  156. {
  157. var result = stream.ReadBsonType();
  158. result.Should().Be(expectedResult);
  159. }
  160. }
  161. [Test]
  162. public void ReadBsonType_should_throw_when_at_end_of_stream()
  163. {
  164. using (var memoryStream = new MemoryStream())
  165. using (var stream = new BsonStreamAdapter(memoryStream))
  166. {
  167. Action action = () => stream.ReadBsonType();
  168. action.ShouldThrow<EndOfStreamException>();
  169. }
  170. }
  171. [Test]
  172. public void ReadBsonType_should_throw_when_stream_is_null()
  173. {
  174. BsonStream stream = null;
  175. Action action = () => stream.ReadBsonType();
  176. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("stream");
  177. }
  178. [TestCase(0x0c)]
  179. [TestCase(0x13)]
  180. [TestCase(0xfe)]
  181. public void ReadBsonType_should_throw_when_value_is_invalid(int n)
  182. {
  183. var bytes = new byte[] { (byte)n };
  184. using (var memoryStream = new MemoryStream(bytes))
  185. using (var stream = new BsonStreamAdapter(memoryStream))
  186. {
  187. Action action = () => stream.ReadBsonType();
  188. action.ShouldThrow<FormatException>();
  189. }
  190. }
  191. [Test]
  192. public void ReadBytes_with_buffer_should_handle_partial_reads()
  193. {
  194. using (var baseStream = Substitute.For<BsonStream>())
  195. using (var stream = new BsonStreamAdapter(baseStream))
  196. {
  197. var buffer = new byte[3];
  198. baseStream.Read(Arg.Any<byte[]>(), 0, 3).Returns(x => { var b = (byte[])x[0]; b[0] = 1; return 1; });
  199. baseStream.Read(Arg.Any<byte[]>(), 1, 2).Returns(x => { var b = (byte[])x[0]; b[1] = 2; b[2] = 3; return 2; });
  200. stream.ReadBytes(buffer, 0, 3);
  201. buffer.Should().Equal(new byte[] { 1, 2, 3 });
  202. baseStream.Received(1).Read(buffer, 0, 3);
  203. baseStream.Received(1).Read(buffer, 1, 2);
  204. }
  205. }
  206. [Test]
  207. public void ReadBytes_with_buffer_should_optimize_count_of_one()
  208. {
  209. using (var baseStream = Substitute.For<Stream>())
  210. using (var stream = new BsonStreamAdapter(baseStream))
  211. {
  212. baseStream.ReadByte().Returns(1);
  213. var buffer = new byte[1];
  214. stream.ReadBytes(buffer, 0, 1);
  215. buffer.Should().Equal(new byte[] { 1 });
  216. baseStream.Received(1).ReadByte();
  217. }
  218. }
  219. [Test]
  220. public void ReadBytes_with_buffer_should_return_expected_result(
  221. [Values(0, 1, 2, 16)]
  222. int length)
  223. {
  224. var bytes = Enumerable.Range(0, length).Select(n => (byte)n).ToArray();
  225. using (var memoryStream = new MemoryStream(bytes))
  226. using (var stream = new BsonStreamAdapter(memoryStream))
  227. {
  228. var buffer = new byte[length];
  229. stream.ReadBytes(buffer, 0, length);
  230. buffer.Should().Equal(bytes);
  231. stream.Position.Should().Be(length);
  232. }
  233. }
  234. [Test]
  235. public void ReadBytes_with_buffer_should_throw_when_at_end_of_stream(
  236. [Values(0, 1, 2, 16)]
  237. int length)
  238. {
  239. var bytes = Enumerable.Range(0, length).Select(n => (byte)n).ToArray();
  240. using (var memoryStream = new MemoryStream(bytes))
  241. using (var stream = new BsonStreamAdapter(memoryStream))
  242. {
  243. var buffer = new byte[length + 1];
  244. Action action = () => stream.ReadBytes(buffer, 0, length + 1);
  245. action.ShouldThrow<EndOfStreamException>();
  246. }
  247. }
  248. [Test]
  249. public void ReadBytes_with_buffer_should_throw_when_buffer_is_null()
  250. {
  251. var stream = Substitute.For<BsonStream>();
  252. Action action = () => stream.ReadBytes(null, 0, 0);
  253. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("buffer");
  254. }
  255. [TestCase(0, 0, 1)]
  256. [TestCase(1, 0, 2)]
  257. [TestCase(1, 1, 1)]
  258. [TestCase(2, 0, 3)]
  259. [TestCase(2, 1, 2)]
  260. [TestCase(2, 2, 1)]
  261. public void ReadBytes_with_buffer_should_throw_when_count_extends_beyond_end_of_buffer(
  262. int length,
  263. int offset,
  264. int count)
  265. {
  266. using (var stream = Substitute.For<BsonStream>())
  267. {
  268. var buffer = new byte[length];
  269. Action action = () => stream.ReadBytes(buffer, offset, count);
  270. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("count");
  271. }
  272. }
  273. [TestCase(0, 0, 1)]
  274. [TestCase(1, 0, 2)]
  275. [TestCase(1, 1, 1)]
  276. [TestCase(2, 0, 3)]
  277. [TestCase(2, 1, 2)]
  278. [TestCase(2, 2, 1)]
  279. public void ReadBytes_with_buffer_should_throw_when_count_is_out_of_range(int length, int offset, int count)
  280. {
  281. using (var stream = Substitute.For<BsonStream>())
  282. {
  283. var buffer = new byte[length];
  284. Action action = () => stream.ReadBytes(buffer, offset, count);
  285. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("count");
  286. }
  287. }
  288. [TestCase(0, -1)]
  289. [TestCase(0, 1)]
  290. [TestCase(1, -1)]
  291. [TestCase(1, 2)]
  292. [TestCase(2, -1)]
  293. [TestCase(2, 3)]
  294. public void ReadBytes_with_buffer_should_throw_when_offset_is_out_of_range(int length, int count)
  295. {
  296. using (var stream = Substitute.For<BsonStream>())
  297. {
  298. var buffer = new byte[length];
  299. Action action = () => stream.ReadBytes(buffer, count, 0);
  300. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("offset");
  301. }
  302. }
  303. [Test]
  304. public void ReadBytes_with_buffer_should_throw_when_stream_is_null()
  305. {
  306. BsonStream stream = null;
  307. var buffer = new byte[0];
  308. Action action = () => stream.ReadBytes(buffer, 0, 0);
  309. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("stream");
  310. }
  311. [Test]
  312. public void ReadBytes_with_count_should_return_expected_result(
  313. [Values(0, 1, 2, 16)]
  314. int length)
  315. {
  316. var bytes = Enumerable.Range(0, length).Select(n => (byte)n).ToArray();
  317. using (var memoryStream = new MemoryStream(bytes))
  318. using (var stream = new BsonStreamAdapter(memoryStream))
  319. {
  320. var result = stream.ReadBytes(length);
  321. result.Should().Equal(bytes);
  322. stream.Position.Should().Be(length);
  323. }
  324. }
  325. [Test]
  326. public void ReadBytes_with_count_should_throw_when_at_end_of_stream()
  327. {
  328. using (var memoryStream = new MemoryStream())
  329. using (var stream = new BsonStreamAdapter(memoryStream))
  330. {
  331. Action action = () => stream.ReadBytes(1);
  332. action.ShouldThrow<EndOfStreamException>();
  333. }
  334. }
  335. [Test]
  336. public void ReadBytes_with_count_should_throw_when_count_is_negative()
  337. {
  338. using (var stream = Substitute.For<BsonStream>())
  339. {
  340. Action action = () => stream.ReadBytes(-1);
  341. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("count");
  342. }
  343. }
  344. [Test]
  345. public void ReadBytes_with_count_should_throw_when_stream_is_null()
  346. {
  347. BsonStream stream = null;
  348. Action action = () => stream.ReadBytes(1);
  349. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("stream");
  350. }
  351. [Test]
  352. public void static_constructor_should_initialize_validBsonTypes()
  353. {
  354. var validBsonTypes = Reflector.__validBsonTypes;
  355. validBsonTypes.Should().HaveCount(256);
  356. for (var n = 0; n < 256; n++)
  357. {
  358. var expectedValue = Enum.IsDefined(typeof(BsonType), n);
  359. validBsonTypes[n].Should().Be(expectedValue);
  360. }
  361. }
  362. [TestCase(BsonBinarySubType.Binary, 0)]
  363. [TestCase(BsonBinarySubType.Function, 1)]
  364. #pragma warning disable 618
  365. [TestCase(BsonBinarySubType.OldBinary, 2)]
  366. #pragma warning restore
  367. [TestCase(BsonBinarySubType.UuidLegacy, 3)]
  368. [TestCase(BsonBinarySubType.UuidStandard, 4)]
  369. [TestCase(BsonBinarySubType.MD5, 5)]
  370. [TestCase(BsonBinarySubType.UserDefined, 0x80)]
  371. public void WriteBinarySubType_should_have_expected_effect(
  372. BsonBinarySubType value,
  373. byte expectedByte)
  374. {
  375. using (var memoryStream = new MemoryStream())
  376. using (var stream = new BsonStreamAdapter(memoryStream))
  377. {
  378. var expectedBytes = new byte[] { expectedByte };
  379. stream.WriteBinarySubType(value);
  380. memoryStream.ToArray().Should().Equal(expectedBytes);
  381. }
  382. }
  383. [Test]
  384. public void WriteBinarySubType_should_throw_when_stream_is_null()
  385. {
  386. BsonStream stream = null;
  387. Action action = () => stream.WriteBinarySubType(0);
  388. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("stream");
  389. }
  390. [TestCase(false, 0)]
  391. [TestCase(true, 1)]
  392. public void WriteBoolean_should_have_expected_effect(
  393. bool value,
  394. byte expectedByte)
  395. {
  396. using (var memoryStream = new MemoryStream())
  397. using (var stream = new BsonStreamAdapter(memoryStream))
  398. {
  399. var expectedBytes = new byte[] { expectedByte };
  400. stream.WriteBoolean(value);
  401. memoryStream.ToArray().Should().Equal(expectedBytes);
  402. }
  403. }
  404. [Test]
  405. public void WriteBoolean_should_throw_when_stream_is_null()
  406. {
  407. BsonStream stream = null;
  408. Action action = () => stream.WriteBoolean(false);
  409. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("stream");
  410. }
  411. [TestCase(BsonType.EndOfDocument, 0x00)]
  412. [TestCase(BsonType.Double, 0x01)]
  413. [TestCase(BsonType.String, 0x02)]
  414. [TestCase(BsonType.Document, 0x03)]
  415. [TestCase(BsonType.Array, 0x04)]
  416. [TestCase(BsonType.Binary, 0x05)]
  417. [TestCase(BsonType.Undefined, 0x06)]
  418. [TestCase(BsonType.ObjectId, 0x07)]
  419. [TestCase(BsonType.Boolean, 0x08)]
  420. [TestCase(BsonType.DateTime, 0x09)]
  421. [TestCase(BsonType.Null, 0x0a)]
  422. [TestCase(BsonType.RegularExpression, 0x0b)]
  423. [TestCase(BsonType.JavaScript, 0x0d)]
  424. [TestCase(BsonType.Symbol, 0x0e)]
  425. [TestCase(BsonType.JavaScriptWithScope, 0x0f)]
  426. [TestCase(BsonType.Int32, 0x10)]
  427. [TestCase(BsonType.Timestamp, 0x11)]
  428. [TestCase(BsonType.Int64, 0x12)]
  429. [TestCase(BsonType.MinKey, 0xff)]
  430. [TestCase(BsonType.MaxKey, 0x7f)]
  431. public void WriteBsonType_should_have_expected_effect(
  432. BsonType value,
  433. byte expectedByte)
  434. {
  435. using (var memoryStream = new MemoryStream())
  436. using (var stream = new BsonStreamAdapter(memoryStream))
  437. {
  438. var expectedBytes = new byte[] { expectedByte };
  439. stream.WriteBsonType(value);
  440. memoryStream.ToArray().Should().Equal(expectedBytes);
  441. }
  442. }
  443. [Test]
  444. public void WriteBsonType_should_throw_when_stream_is_null()
  445. {
  446. BsonStream stream = null;
  447. Action action = () => stream.WriteBsonType(0);
  448. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("stream");
  449. }
  450. [TestCase(0, 0, 0)]
  451. [TestCase(1, 0, 0)]
  452. [TestCase(1, 0, 1)]
  453. [TestCase(1, 1, 0)]
  454. [TestCase(2, 0, 0)]
  455. [TestCase(2, 0, 1)]
  456. [TestCase(2, 0, 2)]
  457. [TestCase(2, 1, 1)]
  458. [TestCase(2, 2, 0)]
  459. public void WriteBytes_should_have_expected_effect(
  460. int length,
  461. int offset,
  462. int count)
  463. {
  464. using (var memoryStream = new MemoryStream())
  465. using (var stream = new BsonStreamAdapter(memoryStream))
  466. {
  467. var buffer = Enumerable.Range(0, length).Select(n => (byte)n).ToArray();
  468. stream.WriteBytes(buffer, offset, count);
  469. memoryStream.ToArray().Should().Equal(buffer.Skip(offset).Take(count));
  470. }
  471. }
  472. [Test]
  473. public void WriteBytes_should_optimize_count_of_one()
  474. {
  475. using (var baseStream = Substitute.For<Stream>())
  476. using (var stream = new BsonStreamAdapter(baseStream))
  477. {
  478. var buffer = new byte[] { 1 };
  479. stream.WriteBytes(buffer, 0, 1);
  480. baseStream.Received(1).WriteByte(1);
  481. }
  482. }
  483. [Test]
  484. public void WriteBytes_should_throw_when_buffer_is_null()
  485. {
  486. using (var stream = Substitute.For<BsonStream>())
  487. {
  488. byte[] buffer = null;
  489. var offset = 0;
  490. var count = 0;
  491. Action action = () => stream.WriteBytes(buffer, offset, count);
  492. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("buffer");
  493. }
  494. }
  495. [TestCase(0, 0, 1)]
  496. [TestCase(1, 0, 2)]
  497. [TestCase(1, 1, 1)]
  498. public void WriteBytes_should_throw_when_count_is_out_of_range(
  499. int length,
  500. int offset,
  501. int count)
  502. {
  503. using (var stream = Substitute.For<BsonStream>())
  504. {
  505. var buffer = new byte[length];
  506. Action action = () => stream.WriteBytes(buffer, offset, count);
  507. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("count");
  508. }
  509. }
  510. [TestCase(0, -1)]
  511. [TestCase(0, 1)]
  512. [TestCase(1, -1)]
  513. [TestCase(1, 2)]
  514. public void WriteBytes_should_throw_when_offset_is_out_of_range(
  515. int length,
  516. int offset)
  517. {
  518. using (var stream = Substitute.For<BsonStream>())
  519. {
  520. var buffer = new byte[length];
  521. var count = 0;
  522. Action action = () => stream.WriteBytes(buffer, offset, count);
  523. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("offset");
  524. }
  525. }
  526. [Test]
  527. public void WriteBytes_should_throw_when_stream_is_null()
  528. {
  529. BsonStream stream = null;
  530. var buffer = new byte[0];
  531. var offset = 0;
  532. var count = 0;
  533. Action action = () => stream.WriteBytes(buffer, offset, count);
  534. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("stream");
  535. }
  536. [Test]
  537. public void WriteSlice_should_have_expected_effect(
  538. [Values(0, 1, 2, 16)]
  539. int length)
  540. {
  541. using (var memoryStream = new MemoryStream())
  542. using (var stream = new BsonStreamAdapter(memoryStream))
  543. {
  544. var bytes = Enumerable.Range(0, length).Select(n => (byte)n).ToArray();
  545. var slice = new ByteArrayBuffer(bytes, isReadOnly: true);
  546. stream.WriteSlice(slice);
  547. memoryStream.ToArray().Should().Equal(bytes);
  548. }
  549. }
  550. [Test]
  551. public void WriteSlice_should_throw_when_slice_is_null()
  552. {
  553. var stream = Substitute.For<BsonStream>();
  554. IByteBuffer slice = null;
  555. Action action = () => stream.WriteSlice(slice);
  556. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("slice");
  557. }
  558. [Test]
  559. public void WriteSlice_should_throw_when_stream_is_null()
  560. {
  561. BsonStream stream = null;
  562. IByteBuffer slice = null;
  563. Action action = () => stream.WriteSlice(slice);
  564. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("stream");
  565. }
  566. // nested types
  567. private class Reflector
  568. {
  569. public static bool[] __validBsonTypes
  570. {
  571. get
  572. {
  573. var field = typeof(BsonStreamExtensions).GetField("__validBsonTypes", BindingFlags.Static | BindingFlags.NonPublic);
  574. return (bool[])field.GetValue(null);
  575. }
  576. }
  577. }
  578. }
  579. }