public void Close_IfHasTransformEventsAndIsClosedIsFalse_ShouldRaiseTransformEvents() { const string expectedContent = "Test"; Encoding expectedEncoding = Encoding.UTF8; const string expectedTransformedContent = expectedContent; byte[] buffer = expectedEncoding.GetBytes(expectedContent); bool transformRaised = false; string actualContent = null; Encoding actualEncoding = null; string actualTransformedContent = null; object actualSender = null; TransformableStream transformableStream = new TransformableStream(Mock.Of<Stream>(), expectedEncoding); transformableStream.Transform += delegate(object sender, StreamTransformingEventArgs e) { transformRaised = true; actualSender = sender; actualContent = e.Content; actualEncoding = e.Encoding; actualTransformedContent = e.TransformedContent; }; Assert.IsFalse(transformRaised); transformableStream.CapturedStream.Write(buffer, 0, buffer.Length); transformableStream.Close(); Assert.IsTrue(transformRaised); Assert.AreEqual(transformableStream, actualSender); Assert.AreEqual(expectedContent, actualContent); Assert.AreEqual(expectedEncoding, actualEncoding); Assert.AreEqual(expectedTransformedContent, actualTransformedContent); }
public void Close_IfHasTransformEventsAndIsClosedIsTrue_ShouldNotRaiseTransformEvents() { bool transformRaised = false; TransformableStream transformableStream = new TransformableStream(Mock.Of<Stream>(), Mock.Of<Encoding>()); transformableStream.Transform += (sender, e) => { transformRaised = true; }; Assert.IsFalse(transformRaised); transformableStream.IsClosed = true; transformableStream.Close(); Assert.IsFalse(transformRaised); }
public void Close_ShouldCallCloseOfTheCapturedStream() { Mock<MemoryStream> memoryStreamMock = new Mock<MemoryStream>(); memoryStreamMock.Verify(memoryStream => memoryStream.Close(), Times.Never()); TransformableStream transformableStream = new TransformableStream(Mock.Of<Stream>(), Mock.Of<Encoding>()); // ReSharper disable PossibleNullReferenceException typeof(CapturableStream).GetField("_capturedStream", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(transformableStream, memoryStreamMock.Object); // ReSharper restore PossibleNullReferenceException transformableStream.Close(); memoryStreamMock.Verify(memoryStream => memoryStream.Close(), Times.Once()); }
public void Close_ShouldSetIsClosedToTrue() { TransformableStream transformableStream = new TransformableStream(Mock.Of<Stream>(), Mock.Of<Encoding>()); transformableStream.Close(); Assert.IsTrue(transformableStream.IsClosed); }
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); }