public void BaseStream_should_return_expected_resut()
        {
            var stream = Substitute.For<Stream>();
            var subject = new BsonStreamAdapter(stream);

            var result = subject.BaseStream;

            result.Should().Be(stream);
        }
        public void BaseStream_should_return_expected_resut()
        {
            var mockStream = new Mock<Stream>();
            var subject = new BsonStreamAdapter(mockStream.Object);

            var result = subject.BaseStream;

            result.Should().Be(mockStream.Object);
        }
        public void BaseStream_should_throw_when_subject_is_disposed()
        {
            var stream = Substitute.For<Stream>();
            var subject = new BsonStreamAdapter(stream);
            subject.Dispose();

            Action action = () => { var _ = subject.BaseStream; };

            action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("BsonStreamAdapter");
        }
        public void CanRead_should_call_wrapped_stream(
            [Values(false, true)]
            bool canRead)
        {
            var mockStream = new Mock<Stream>();
            var subject = new BsonStreamAdapter(mockStream.Object);
            mockStream.SetupGet(s => s.CanRead).Returns(canRead);

            var result = subject.CanRead;

            result.Should().Be(canRead);
            mockStream.VerifyGet(s => s.CanRead, Times.Once);
        }
        public void BeginRead_should_throw_when_subject_is_diposed()
        {
            var stream = Substitute.For<Stream>();
            var subject = new BsonStreamAdapter(stream);
            var buffer = new byte[0];
            var offset = 1;
            var count = 2;
            var callback = Substitute.For<AsyncCallback>();
            var state = new object();
            subject.Dispose();

            Action action = () => subject.BeginRead(buffer, offset, count, callback, state);

            action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("BsonStreamAdapter");
        }
        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);
            }
        }
        public void BeginRead_should_call_wrapped_stream()
        {
            var mockStream = new Mock<Stream>();
            var subject = new BsonStreamAdapter(mockStream.Object);
            var buffer = new byte[0];
            var offset = 1;
            var count = 2;
            var mockCallback = new Mock<AsyncCallback>();
            var state = new object();
            var mockAsyncResult = new Mock<IAsyncResult>();
            mockStream.Setup(s => s.BeginRead(buffer, offset, count, mockCallback.Object, state)).Returns(mockAsyncResult.Object);

            var result = subject.BeginRead(buffer, offset, count, mockCallback.Object, state);

            result.Should().BeSameAs(mockAsyncResult.Object);
            mockStream.Verify(s => s.BeginRead(buffer, offset, count, mockCallback.Object, state), Times.Once);
        }
        public void BeginRead_should_call_wrapped_stream()
        {
            var stream = Substitute.For<Stream>();
            var subject = new BsonStreamAdapter(stream);
            var buffer = new byte[0];
            var offset = 1;
            var count = 2;
            var callback = Substitute.For<AsyncCallback>();
            var state = new object();
            var asyncResult = Substitute.For<IAsyncResult>();
            stream.BeginRead(buffer, offset, count, callback, state).Returns(asyncResult);

            var result = subject.BeginRead(buffer, offset, count, callback, state);

            result.Should().Be(asyncResult);
            stream.Received(1).BeginRead(buffer, offset, count, callback, state);
        }
        public void BackpatchSize_should_backpatch_the_size(
            [Values(0, 1, 5)]
            int startPosition)
        {
            var bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            var length = bytes.Length - startPosition;
            var expectedBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            Array.Copy(BitConverter.GetBytes(length), 0, expectedBytes, startPosition, 4);

            using (var memoryStream = new MemoryStream())
            using (var stream = new BsonStreamAdapter(memoryStream))
            {
                stream.WriteBytes(bytes, 0, bytes.Length);
                var position = stream.Position;

                stream.BackpatchSize(startPosition);

                memoryStream.ToArray().Should().Equal(expectedBytes);
                stream.Position.Should().Be(position);
            }
        }
        public void ReadObjectId_should_be_big_endian()
        {
            var bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
            var stream = new MemoryStream(bytes);
            var subject = new BsonStreamAdapter(stream);
            var expectedResult = new ObjectId(0x01020304, 0x050607, 0x0809, 0x0a0b0c);

            var result = subject.ReadObjectId();

            result.Should().Be(expectedResult);
        }
        public void ReadInt64_should_return_expected_result(
            [Values(-1, 0, 1, long.MaxValue, long.MinValue)]
            long value)
        {
            var bytes = BitConverter.GetBytes(value);
            var stream = new MemoryStream(bytes);
            var subject = new BsonStreamAdapter(stream);

            var result = subject.ReadInt64();

            result.Should().Be(value);
            subject.Position.Should().Be(8);
        }
        public void ReadInt64_should_be_little_endian()
        {
            var bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            var stream = new MemoryStream(bytes);
            var subject = new BsonStreamAdapter(stream);

            var result = subject.ReadInt64();

            result.Should().Be(0x0807060504030201);
        }
        public void ReadDouble_should_return_expected_result(
            [Values(-1.0, 0.0, 1.0, double.Epsilon, double.MaxValue, double.MinValue, double.NaN, double.NegativeInfinity, double.PositiveInfinity)]
            double value
            )
        {
            var bytes = BitConverter.GetBytes(value);
            var stream = new MemoryStream(bytes);
            var subject = new BsonStreamAdapter(stream);

            var result = subject.ReadDouble();

            result.Should().Be(value);
            subject.Position.Should().Be(8);
        }
        public void Length_get_should_call_wrapped_stream(
            [Values(0L, 1L, 2L)]
            long length)
        {
            var stream = Substitute.For<Stream>();
            var subject = new BsonStreamAdapter(stream);
            stream.Length.Returns(length);

            var result = subject.Length;

            result.Should().Be(length);
            var temp = stream.Received(1).Length;
        }
        public void ReadCStringBytes_should_throw_when_terminating_null_byte_is_missing()
        {
            var bytes = new byte[] { 0, 97, 98 };
            var stream = new MemoryStream(bytes);
            var subject = new BsonStreamAdapter(stream);
            subject.SetLength(3);
            subject.Position = 1;

            Action action = () => subject.ReadCStringBytes();

            action.ShouldThrow<EndOfStreamException>();
        }
        public void ReadString_should_throw_when_subject_is_disposed()
        {
            var stream = Substitute.For<Stream>();
            var subject = new BsonStreamAdapter(stream);
            subject.Dispose();

            Action action = () => subject.ReadString(Utf8Encodings.Strict);

            action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("BsonStreamAdapter");
        }
        public void ReadTimeout_get_should_call_wrapped_stream(
            [Values(0, 1, 2)]
            int readTimeout)
        {
            var stream = Substitute.For<Stream>();
            var subject = new BsonStreamAdapter(stream);
            stream.ReadTimeout.Returns(readTimeout);

            var result = subject.ReadTimeout;

            result.Should().Be(readTimeout);
            var temp = stream.Received(1).ReadTimeout;
        }
        public void ReadString_should_return_expected_result(byte[] bytes, string expectedResult)
        {
            var stream = new MemoryStream(bytes);
            var subject = new BsonStreamAdapter(stream);

            var result = subject.ReadString(Utf8Encodings.Strict);

            result.Should().Be(expectedResult);
            subject.Position.Should().Be(bytes.Length);
        }
        public void ReadObjectId_should_return_expected_result()
        {
            var objectId = ObjectId.GenerateNewId();
            var bytes = objectId.ToByteArray();
            var stream = new MemoryStream(bytes);
            var subject = new BsonStreamAdapter(stream);

            var result = subject.ReadObjectId();

            result.Should().Be(objectId);
            subject.Position.Should().Be(12);
        }
        public void Read_should_call_wrapped_stream()
        {
            var stream = Substitute.For<Stream>();
            var subject = new BsonStreamAdapter(stream);
            var buffer = new byte[3];
            var offset = 1;
            var count = 2;
            var numberOfBytesRead = 1;
            stream.Read(buffer, offset, count).Returns(numberOfBytesRead);

            var result = subject.Read(buffer, offset, count);

            result.Should().Be(numberOfBytesRead);
            stream.Received(1).Read(buffer, offset, count);
        }
        public void Position_set_should_call_wrapped_stream(
            [Values(0, 1, 2)]
            long position)
        {
            var stream = Substitute.For<Stream>();
            var subject = new BsonStreamAdapter(stream);

            subject.Position = position;

            stream.Received(1).Position = position;
        }
        public void Position_get_should_call_wrapped_stream(
            [Values(0, 1, 2)]
            long position)
        {
            var stream = Substitute.For<Stream>();
            var subject = new BsonStreamAdapter(stream);
            stream.Position.Returns(position);

            var result = subject.Position;

            result.Should().Be(position);
            var temp = stream.Received(1).Position;
        }
        public void ReadSlice_should_return_expected_result()
        {
            var bytes = new byte[] { 7, 0, 0, 0, 1, 2, 3 };
            var stream = new MemoryStream(bytes);
            var subject = new BsonStreamAdapter(stream);

            var result = subject.ReadSlice();

            result.IsReadOnly.Should().BeTrue();
            var segment = result.AccessBackingBytes(0);
            segment.Array.Skip(segment.Offset).Take(segment.Count).Should().Equal(bytes);
            subject.Position.Should().Be(7);
        }
        public void ReadAsync_should_call_wrapped_stream()
        {
            var stream = Substitute.For<Stream>();
            var subject = new BsonStreamAdapter(stream);
            var task = new TaskCompletionSource<int>().Task;
            var buffer = new byte[3];
            var offset = 1;
            var count = 2;
            var cancellationToken = new CancellationTokenSource().Token;
            stream.ReadAsync(buffer, offset, count, cancellationToken).Returns(task);

            var result = subject.ReadAsync(buffer, offset, count, cancellationToken);

            result.Should().Be(task);
            stream.Received(1).ReadAsync(buffer, offset, count, cancellationToken);
        }
        public void ReadString_should_throw_when_encoding_is_null()
        {
            var stream = Substitute.For<Stream>();
            var subject = new BsonStreamAdapter(stream);

            Action action = () => subject.ReadString(null);

            action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("encoding");
        }
        public void ReadAsync_should_throw_when_subject_is_disposed()
        {
            var stream = Substitute.For<Stream>();
            var subject = new BsonStreamAdapter(stream);
            var buffer = new byte[3];
            var offset = 1;
            var count = 2;
            var cancellationToken = new CancellationTokenSource().Token;
            subject.Dispose();

            Action action = () => subject.ReadAsync(buffer, offset, count, cancellationToken);

            action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("BsonStreamAdapter");
        }
        public void ReadString_should_throw_when_terminating_null_byte_is_missing()
        {
            var bytes = new byte[] { 2, 0, 0, 0, 97, 1 };
            var stream = new MemoryStream(bytes);
            var subject = new BsonStreamAdapter(stream);

            Action action = () => subject.ReadString(Utf8Encodings.Strict);

            action.ShouldThrow<FormatException>();
        }
        public void ReadByte_should_call_wrapped_stream()
        {
            var stream = Substitute.For<Stream>();
            var subject = new BsonStreamAdapter(stream);
            stream.ReadByte().Returns(1);

            var result = subject.ReadByte();

            result.Should().Be(1);
            stream.Received(1).ReadByte();
        }
        public void ReadTimeout_set_should_call_wrapped_stream(
            [Values(0, 1, 2)]
            int readTimeout)
        {
            var stream = Substitute.For<Stream>();
            var subject = new BsonStreamAdapter(stream);

            subject.ReadTimeout = readTimeout;

            stream.Received(1).ReadTimeout = readTimeout;
        }
        public void ReadCStringBytes_should_return_expected_result(byte[] bytes, string expectedResult)
        {
            var stream = new MemoryStream(bytes);
            var subject = new BsonStreamAdapter(stream);

            var result = subject.ReadCStringBytes();

            result.Array.Skip(result.Offset).Take(result.Count).Should().Equal(bytes.Take(bytes.Length - 1));
            subject.Position.Should().Be(bytes.Length);
        }