public void Write_IfNotCaptured_ShouldNotCallWriteOfTheCapturedStream()
 {
     byte[] buffer = new byte[4] {0, 1, 2, 3};
     TransformableStream transformableStream = new TransformableStream(Mock.Of<Stream>(), Mock.Of<Encoding>());
     Assert.IsFalse(transformableStream.CapturedStream.ToArray().Any());
     transformableStream.Write(buffer, 0, buffer.Length);
     Assert.IsFalse(transformableStream.CapturedStream.ToArray().Any());
 }
 public void Write_IfHasTransformEvents_ShouldNotCallWriteOfTheWrappedStream()
 {
     Mock<Stream> streamMock = new Mock<Stream>();
     streamMock.Verify(stream => stream.Write(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()), Times.Never());
     TransformableStream transformableStream = new TransformableStream(streamMock.Object, Mock.Of<Encoding>());
     transformableStream.Transform += (sender, args) => { };
     byte[] buffer = new byte[DateTime.Now.Millisecond];
     transformableStream.Write(buffer, 0, buffer.Length);
     streamMock.Verify(stream => stream.Write(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()), Times.Never());
 }
 public void Write_IfHasCaptureWriteEvents_ShouldRaiseCaptureWriteEvents()
 {
     byte[] expectedBuffer = new byte[] {0, 1, 2, 3};
     const int expectedOffset = 1;
     const int expectedCount = 2;
     Encoding expectedEncoding = Encoding.UTF8;
     bool captureWriteRaised = false;
     IEnumerable<byte> actualBuffer = null;
     int? actualOffset = null;
     int? actualCount = null;
     Encoding actualEncoding = null;
     object actualSender = null;
     TransformableStream transformableStream = new TransformableStream(Mock.Of<Stream>(), expectedEncoding);
     transformableStream.CaptureWrite += delegate(object sender, StreamWriteEventArgs e)
     {
         captureWriteRaised = true;
         actualSender = sender;
         actualBuffer = e.Buffer;
         actualOffset = e.Offset;
         actualCount = e.Count;
         actualEncoding = e.Encoding;
     };
     Assert.IsFalse(captureWriteRaised);
     transformableStream.Write(expectedBuffer, expectedOffset, expectedCount);
     Assert.IsTrue(captureWriteRaised);
     Assert.AreEqual(transformableStream, actualSender);
     // ReSharper disable PossibleMultipleEnumeration
     Assert.AreEqual(expectedBuffer.Length, actualBuffer.Count());
     Assert.AreEqual(expectedBuffer[0], actualBuffer.ElementAt(0));
     Assert.AreEqual(expectedBuffer[1], actualBuffer.ElementAt(1));
     Assert.AreEqual(expectedBuffer[2], actualBuffer.ElementAt(2));
     Assert.AreEqual(expectedBuffer[3], actualBuffer.ElementAt(3));
     // ReSharper restore PossibleMultipleEnumeration
     Assert.AreEqual(expectedOffset, actualOffset);
     Assert.AreEqual(expectedCount, actualCount);
     Assert.AreEqual(expectedEncoding, actualEncoding);
 }
 public void Write_IfCaptured_ShouldCallWriteOfTheCapturedStream()
 {
     byte[] buffer = new byte[4] {0, 1, 2, 3};
     TransformableStream transformableStream = new TransformableStream(Mock.Of<Stream>(), Mock.Of<Encoding>());
     transformableStream.Capture += (sender, e) => { };
     Assert.IsFalse(transformableStream.CapturedStream.ToArray().Any());
     transformableStream.Write(buffer, 0, buffer.Length);
     Assert.AreEqual(4, transformableStream.CapturedStream.ToArray().Length);
     Assert.AreEqual(0, transformableStream.CapturedStream.ToArray()[0]);
     Assert.AreEqual(1, transformableStream.CapturedStream.ToArray()[1]);
     Assert.AreEqual(2, transformableStream.CapturedStream.ToArray()[2]);
     Assert.AreEqual(3, transformableStream.CapturedStream.ToArray()[3]);
 }
        public void TransformWriteTest()
        {
            byte[] capturedBuffer = null;
            int? capturedOffset = null;
            int? capturedCount = null;

            Encoding encoding = Mock.Of<Encoding>();
            using(MemoryStream memoryStream = new MemoryStream())
            {
                TransformableStream transformableStream = new TransformableStream(memoryStream, encoding);
                transformableStream.TransformWrite += delegate(object sender, StreamWriteTransformingEventArgs e)
                {
                    e.TransformedBuffer.Clear();
                    e.TransformedBuffer.Add(10);
                    e.TransformedBuffer.Add(11);
                    e.TransformedBuffer.Add(12);
                    e.TransformedBuffer.Add(13);
                    e.TransformedOffset = 1;
                    e.TransformedCount = 2;
                };
                transformableStream.CaptureWrite += delegate(object sender, StreamWriteEventArgs e)
                {
                    capturedBuffer = e.Buffer.ToArray();
                    capturedOffset = e.Offset;
                    capturedCount = e.Count;
                };
                transformableStream.Write(new byte[] {1, 2, 3, 4, 5, 6, 7, 8}, 2, 4);
                byte[] toArray = memoryStream.ToArray();
                Assert.AreEqual(2, toArray.Length);
                Assert.AreEqual(11, toArray[0]);
                Assert.AreEqual(12, toArray[1]);
                Assert.AreEqual(4, capturedBuffer.Length);
                Assert.AreEqual(10, capturedBuffer[0]);
                Assert.AreEqual(11, capturedBuffer[1]);
                Assert.AreEqual(12, capturedBuffer[2]);
                Assert.AreEqual(13, capturedBuffer[3]);
                Assert.AreEqual(1, capturedOffset);
                Assert.AreEqual(2, capturedCount);
            }
        }
        public void TransformTest()
        {
            const string originalContent = "Original content";
            const string transformedContent = "Transformed content";
            Encoding encoding = Encoding.UTF8;
            byte[] originalBuffer = encoding.GetBytes(originalContent);
            string capturedContent = null;

            MemoryStream memoryStream = new MemoryStream();
            TransformableStream transformableStream = new TransformableStream(memoryStream, encoding);
            transformableStream.Transform += delegate(object sender, StreamTransformingEventArgs e) { e.TransformedContent = transformedContent; };
            transformableStream.Capture += delegate(object sender, StreamEventArgs e) { capturedContent = e.Content; };
            for(int i = 0; i < originalBuffer.Length; i++)
            {
                transformableStream.Write(originalBuffer, i, 1);
            }
            Assert.AreEqual(0, memoryStream.ToArray().Length);
            Assert.AreEqual(originalBuffer.Length, transformableStream.CapturedStream.ToArray().Length);
            transformableStream.Flush();
            Assert.AreEqual(0, memoryStream.ToArray().Length);
            Assert.AreEqual(originalBuffer.Length, transformableStream.CapturedStream.ToArray().Length);
            transformableStream.Close();
            Assert.AreEqual(transformedContent, encoding.GetString(memoryStream.ToArray()));
            Assert.AreEqual(transformedContent, capturedContent);
        }