예제 #1
0
 public void Position_WhenNotCanSeek_ThrowsNotSupportedException()
 {
     var stub = new FakeStream { OnCanReadGet = () => true, OnCanSeekGet = () => false };
     using (var pushbackStream = new PushbackInputStream(stub))
     {
         Assert.Throws<NotSupportedException>(() => pushbackStream.Position);
     }
 }
예제 #2
0
 public void CanWrite_WhenNotCanSeek_ReturnsFalse()
 {
     using (var stub = new FakeStream { OnCanReadGet = () => true, OnCanSeekGet = () => true })
     using (var pushbackStream = new PushbackInputStream(stub))
     {
         Assert.False(pushbackStream.CanWrite);
     }
 }
예제 #3
0
 public void CanWrite_WhenNotCanSeek_ReturnsFalse()
 {
     using (var stub = new FakeStream {
         OnCanReadGet = () => true, OnCanSeekGet = () => true
     })
         using (var pushbackStream = new PushbackInputStream(stub))
         {
             Assert.False(pushbackStream.CanWrite);
         }
 }
예제 #4
0
        public void CanWrite_WhenCanSeek_ReturnsTrue()
        {
            var stub = new FakeStream { OnCanReadGet = () => true, OnCanSeekGet = () => false };

            var sut = new PushbackInputStream(stub);
            using (stub)
            using (sut)
            {
                Assert.True(sut.CanWrite);
            }
        }
예제 #5
0
        public void Position_WhenNotCanSeek_ThrowsNotSupportedException()
        {
            var stub = new FakeStream {
                OnCanReadGet = () => true, OnCanSeekGet = () => false
            };

            using (var pushbackStream = new PushbackInputStream(stub))
            {
                Assert.Throws <NotSupportedException>(() => pushbackStream.Position);
            }
        }
예제 #6
0
 public void Position_WhenCanSeek_ReturnsPositionOfUnderlyingStream()
 {
     var stub = new FakeStream
                    {
                        OnCanReadGet = () => true,
                        OnCanSeekGet = () => true,
                        OnPositionGet = () => 128
                    };
     using (var pushbackStream = new PushbackInputStream(stub))
     {
         var position = pushbackStream.Position;
         Assert.Equal(128, position);
     }
 }
예제 #7
0
        public void CanWrite_WhenCanSeek_ReturnsTrue()
        {
            var stub = new FakeStream {
                OnCanReadGet = () => true, OnCanSeekGet = () => false
            };

            var sut = new PushbackInputStream(stub);

            using (stub)
                using (sut)
                {
                    Assert.True(sut.CanWrite);
                }
        }
예제 #8
0
        public void Position_WhenCanSeek_ReturnsPositionOfUnderlyingStream()
        {
            var stub = new FakeStream
            {
                OnCanReadGet  = () => true,
                OnCanSeekGet  = () => true,
                OnPositionGet = () => 128
            };

            using (var pushbackStream = new PushbackInputStream(stub))
            {
                var position = pushbackStream.Position;
                Assert.Equal(128, position);
            }
        }
예제 #9
0
        public void Write_WhenCanSeek_ThrowsIOException()
        {
            var stub = new FakeStream
            {
                OnCanReadGet  = () => true,
                OnCanSeekGet  = () => true,
                OnPositionGet = () => 128
            };

            var pushbackBytes = new byte[8];

            using (var pushbackStream = new PushbackInputStream(stub))
            {
                Assert.Throws <IOException>(() => pushbackStream.Write(pushbackBytes, 0, pushbackBytes.Length));
            }
        }
예제 #10
0
        public void Write_DoesNotWriteToUnderlyingStream()
        {
            var callsToWrite = 0;
            var stub         = new FakeStream
            {
                OnCanReadGet  = () => true,
                OnCanWriteGet = () => true,
                OnCanSeekGet  = () => false,
                OnWriteByteArrayInt32Int32 = (_, __, ___) => callsToWrite++
            };
            var sut = new PushbackInputStream(stub);

            using (sut)
            {
                var buffer = new byte[] { 0x8a, 0x8f, 0x1c, 0xdd, 0x7a, 0xa7, 0xd0, 0x99, 0xa3, 0x4b };
                sut.Write(buffer, 0, 10);
            }

            Assert.Equal(0, callsToWrite);
        }
예제 #11
0
        public async void ReadAsync_ReadsFromBufferFirst()
        {
            const int Position    = 128;
            var       callsToRead = 0;

            // MEMO: all calls to PushbackInputStream.ReadAsync(...) are routed to PushbackInputStream.Read(...)
            // if we mocked FakeStream.ReadAsync(...), it would never be called
            // that is why the synchronous method is mocked instead
            var mock = new FakeStream
            {
                OnCanReadGet              = () => true,
                OnCanSeekGet              = () => false,
                OnPositionGet             = () => Position,
                OnReadByteArrayInt32Int32 = (_, __, ___) =>
                {
                    callsToRead++;
                    return(0);
                }
            };

            using (var pushbackStream = new PushbackInputStream(mock))
            {
                var pushbackBytes = new byte[] { 1, 2, 4, 8 };
                pushbackStream.Write(pushbackBytes, 0, pushbackBytes.Length);

                // Read as many bytes as we wrote to the pusback buffer
                // The stream wrapper should not touch the underlying stream
                var result    = new byte[pushbackBytes.Length];
                var bytesRead = await pushbackStream.ReadAsync(result, 0, result.Length);

                Assert.Equal(0, callsToRead);
                Assert.Equal(pushbackBytes.Length, bytesRead);
                Assert.Equal(pushbackBytes, result);

                // The pushback buffer should be empty at this point;
                // a subsequent read should hit the underlying stream
                await pushbackStream.ReadAsync(result, 0, result.Length, CancellationToken.None);

                Assert.Equal(1, callsToRead);
            }
        }
예제 #12
0
        public void Read_ReadsFromBufferFirst()
        {
            const int Position    = 128;
            var       callsToRead = 0;
            var       mock        = new FakeStream
            {
                OnCanReadGet              = () => true,
                OnCanSeekGet              = () => false,
                OnPositionGet             = () => Position,
                OnReadByteArrayInt32Int32 = (_, __, ___) =>
                {
                    callsToRead++;
                    return(0);
                }
            };

            using (var pushbackStream = new PushbackInputStream(mock))
            {
                var pushbackBytes = new byte[] { 1, 2, 4, 8 };
                pushbackStream.Write(pushbackBytes, 0, pushbackBytes.Length);

                // Read as many bytes as we wrote to the pusback buffer
                // The stream wrapper should not touch the underlying stream
                var result    = new byte[pushbackBytes.Length];
                var bytesRead = pushbackStream.Read(result, 0, result.Length);

                Assert.Equal(0, callsToRead);
                Assert.Equal(pushbackBytes.Length, bytesRead);
                Assert.Equal(pushbackBytes, result);

                // The pushback buffer should be empty at this point;
                // a subsequent read should hit the underlying stream
                pushbackStream.Read(result, 0, result.Length);
                Assert.Equal(1, callsToRead);
            }
        }
예제 #13
0
        public async void WriteAsync_DoesNotWriteToUnderlyingStream()
        {
            var callsToWrite = 0;

            // MEMO: all calls to PushbackInputStream.WriteAsync(...) are routed to PushbackInputStream.Write(...)
            // if we mocked FakeStream.WriteAsync(...), it would never be called
            // that is why the synchronous method is mocked instead
            var stub = new FakeStream
            {
                OnCanReadGet  = () => true,
                OnCanWriteGet = () => true,
                OnCanSeekGet  = () => false,
                OnWriteByteArrayInt32Int32 = (_, __, ___) => callsToWrite++
            };
            var sut = new PushbackInputStream(stub);

            using (sut)
            {
                var buffer = new byte[] { 0x8a, 0x8f, 0x1c, 0xdd, 0x7a, 0xa7, 0xd0, 0x99, 0xa3, 0x4b };
                await sut.WriteAsync(buffer, 0, 10, CancellationToken.None);
            }

            Assert.Equal(0, callsToWrite);
        }
예제 #14
0
        public async void ReadAsync_ReadsFromBufferFirst()
        {
            const int Position = 128;
            var callsToRead = 0;

            // MEMO: all calls to PushbackInputStream.ReadAsync(...) are routed to PushbackInputStream.Read(...)
            // if we mocked FakeStream.ReadAsync(...), it would never be called
            // that is why the synchronous method is mocked instead
            var mock = new FakeStream
                           {
                               OnCanReadGet = () => true,
                               OnCanSeekGet = () => false,
                               OnPositionGet = () => Position,
                               OnReadByteArrayInt32Int32 = (_, __, ___) =>
                                   {
                                       callsToRead++;
                                       return 0;
                                   }
                           };
            using (var pushbackStream = new PushbackInputStream(mock))
            {
                var pushbackBytes = new byte[] { 1, 2, 4, 8 };
                pushbackStream.Write(pushbackBytes, 0, pushbackBytes.Length);

                // Read as many bytes as we wrote to the pusback buffer
                // The stream wrapper should not touch the underlying stream
                var result = new byte[pushbackBytes.Length];
                var bytesRead = await pushbackStream.ReadAsync(result, 0, result.Length);

                Assert.Equal(0, callsToRead);
                Assert.Equal(pushbackBytes.Length, bytesRead);
                Assert.Equal(pushbackBytes, result);

                // The pushback buffer should be empty at this point;
                // a subsequent read should hit the underlying stream
                await pushbackStream.ReadAsync(result, 0, result.Length, CancellationToken.None);
                Assert.Equal(1, callsToRead);
            }
        }
예제 #15
0
        public void Write_DoesNotWriteToUnderlyingStream()
        {
            var callsToWrite = 0;
            var stub = new FakeStream
                           {
                               OnCanReadGet = () => true,
                               OnCanWriteGet = () => true,
                               OnCanSeekGet = () => false,
                               OnWriteByteArrayInt32Int32 = (_, __, ___) => callsToWrite++
                           };
            var sut = new PushbackInputStream(stub);
            using (sut)
            {
                var buffer = new byte[] { 0x8a, 0x8f, 0x1c, 0xdd, 0x7a, 0xa7, 0xd0, 0x99, 0xa3, 0x4b };
                sut.Write(buffer, 0, 10);
            }

            Assert.Equal(0, callsToWrite);
        }
예제 #16
0
        public void Write_WhenCanSeek_ThrowsIOException()
        {
            var stub = new FakeStream
                           {
                               OnCanReadGet = () => true,
                               OnCanSeekGet = () => true,
                               OnPositionGet = () => 128
                           };

            var pushbackBytes = new byte[8];
            using (var pushbackStream = new PushbackInputStream(stub))
            {
                Assert.Throws<IOException>(() => pushbackStream.Write(pushbackBytes, 0, pushbackBytes.Length));
            }
        }
예제 #17
0
        public async void WriteAsync_DoesNotWriteToUnderlyingStream()
        {
            var callsToWrite = 0;

            // MEMO: all calls to PushbackInputStream.WriteAsync(...) are routed to PushbackInputStream.Write(...)
            // if we mocked FakeStream.WriteAsync(...), it would never be called
            // that is why the synchronous method is mocked instead
            var stub = new FakeStream
                           {
                               OnCanReadGet = () => true,
                               OnCanWriteGet = () => true,
                               OnCanSeekGet = () => false,
                               OnWriteByteArrayInt32Int32 = (_, __, ___) => callsToWrite++
                           };
            var sut = new PushbackInputStream(stub);
            using (sut)
            {
                var buffer = new byte[] { 0x8a, 0x8f, 0x1c, 0xdd, 0x7a, 0xa7, 0xd0, 0x99, 0xa3, 0x4b };
                await sut.WriteAsync(buffer, 0, 10, CancellationToken.None);
            }

            Assert.Equal(0, callsToWrite);
        }
예제 #18
0
        public void Read_ReadsFromBufferFirst()
        {
            const int Position = 128;
            var callsToRead = 0;
            var mock = new FakeStream
                           {
                               OnCanReadGet = () => true,
                               OnCanSeekGet = () => false,
                               OnPositionGet = () => Position,
                               OnReadByteArrayInt32Int32 = (_, __, ___) =>
                                   {
                                       callsToRead++;
                                       return 0;
                                   }
                           };
            using (var pushbackStream = new PushbackInputStream(mock))
            {
                var pushbackBytes = new byte[] { 1, 2, 4, 8 };
                pushbackStream.Write(pushbackBytes, 0, pushbackBytes.Length);

                // Read as many bytes as we wrote to the pusback buffer
                // The stream wrapper should not touch the underlying stream
                var result = new byte[pushbackBytes.Length];
                var bytesRead = pushbackStream.Read(result, 0, result.Length);

                Assert.Equal(0, callsToRead);
                Assert.Equal(pushbackBytes.Length, bytesRead);
                Assert.Equal(pushbackBytes, result);

                // The pushback buffer should be empty at this point;
                // a subsequent read should hit the underlying stream
                pushbackStream.Read(result, 0, result.Length);
                Assert.Equal(1, callsToRead);
            }
        }