public void Close_IfHasCaptureEventsAndIsClosedIsFalse_ShouldRaiseCaptureEvents() { const string expectedContent = "Test"; Encoding expectedEncoding = Encoding.UTF8; byte[] buffer = expectedEncoding.GetBytes(expectedContent); bool captureRaised = false; string actualContent = null; Encoding actualEncoding = null; object actualSender = null; CapturableStream capturableStream = new CapturableStream(Mock.Of<Stream>(), expectedEncoding); capturableStream.Capture += delegate(object sender, StreamEventArgs e) { captureRaised = true; actualSender = sender; actualContent = e.Content; actualEncoding = e.Encoding; }; Assert.IsFalse(captureRaised); capturableStream.CapturedStream.Write(buffer, 0, buffer.Length); capturableStream.Close(); Assert.IsTrue(captureRaised); Assert.AreEqual(capturableStream, actualSender); Assert.AreEqual(expectedContent, actualContent); Assert.AreEqual(expectedEncoding, actualEncoding); }
public void CapturedStreamToString_ShouldReturnTheBytesInTheCapturedStreamDecodedToAString() { const string expectedContent = "Test"; Encoding encoding = Encoding.UTF8; byte[] buffer = encoding.GetBytes(expectedContent); CapturableStream capturableStream = new CapturableStream(Mock.Of<Stream>(), encoding); capturableStream.CapturedStream.Write(buffer, 0, buffer.Length); Assert.AreEqual(expectedContent, capturableStream.CapturedStreamToString()); }
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; CapturableStream capturableStream = new CapturableStream(Mock.Of<Stream>(), expectedEncoding); capturableStream.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); capturableStream.Write(expectedBuffer, expectedOffset, expectedCount); Assert.IsTrue(captureWriteRaised); Assert.AreEqual(capturableStream, 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_IfNotCaptured_ShouldNotCallWriteOfTheCapturedStream() { byte[] buffer = new byte[4] {0, 1, 2, 3}; CapturableStream capturableStream = new CapturableStream(Mock.Of<Stream>(), Mock.Of<Encoding>()); Assert.IsFalse(capturableStream.CapturedStream.ToArray().Any()); capturableStream.Write(buffer, 0, buffer.Length); Assert.IsFalse(capturableStream.CapturedStream.ToArray().Any()); }
public void Position_Set_ShouldCallPositionSetOfTheWrappedStream() { long randomPosition = DateTime.Now.Millisecond; Mock<Stream> streamMock = new Mock<Stream>(); streamMock.SetupProperty(stream => stream.Position, 0); streamMock.VerifySet(stream => stream.Position = It.IsAny<long>(), Times.Never()); CapturableStream capturableStream = new CapturableStream(streamMock.Object, Mock.Of<Encoding>()) { Position = randomPosition }; Assert.AreEqual(randomPosition, capturableStream.Position); streamMock.VerifySet(stream => stream.Position = It.IsAny<long>(), Times.Once()); }
public void Write_IfCaptured_ShouldCallWriteOfTheCapturedStream() { byte[] buffer = new byte[4] {0, 1, 2, 3}; CapturableStream capturableStream = new CapturableStream(Mock.Of<Stream>(), Mock.Of<Encoding>()); capturableStream.Capture += (sender, e) => { }; Assert.IsFalse(capturableStream.CapturedStream.ToArray().Any()); capturableStream.Write(buffer, 0, buffer.Length); Assert.AreEqual(4, capturableStream.CapturedStream.ToArray().Length); Assert.AreEqual(0, capturableStream.CapturedStream.ToArray()[0]); Assert.AreEqual(1, capturableStream.CapturedStream.ToArray()[1]); Assert.AreEqual(2, capturableStream.CapturedStream.ToArray()[2]); Assert.AreEqual(3, capturableStream.CapturedStream.ToArray()[3]); }
public void OnCaptureWrite_ShouldRaiseCaptureWriteEvents() { int numberOfEventsRaised = 0; CapturableStream capturableStream = new CapturableStream(Mock.Of<Stream>(), Mock.Of<Encoding>()); capturableStream.CaptureWrite += (sender, e) => numberOfEventsRaised++; capturableStream.CaptureWrite += (sender, e) => numberOfEventsRaised++; capturableStream.CaptureWrite += (sender, e) => numberOfEventsRaised++; Assert.AreEqual(0, numberOfEventsRaised); capturableStream.OnCaptureWrite(new StreamWriteEventArgs(new byte[0], It.IsAny<int>(), It.IsAny<int>(), Mock.Of<Encoding>())); Assert.AreEqual(3, numberOfEventsRaised); }
public void OnCapture_ShouldRaiseCaptureEvents() { int numberOfEventsRaised = 0; CapturableStream capturableStream = new CapturableStream(Mock.Of<Stream>(), Mock.Of<Encoding>()); capturableStream.Capture += (sender, e) => numberOfEventsRaised++; capturableStream.Capture += (sender, e) => numberOfEventsRaised++; capturableStream.Capture += (sender, e) => numberOfEventsRaised++; Assert.AreEqual(0, numberOfEventsRaised); capturableStream.OnCapture(new StreamEventArgs(null, Mock.Of<Encoding>())); Assert.AreEqual(3, numberOfEventsRaised); }
public void IsCaptured_ShouldReturnHasCaptureEvents() { bool hasCaptureEvents = DateTime.Now.Second%2 == 0; CapturableStream capturableStream = new CapturableStream(Mock.Of<Stream>(), Mock.Of<Encoding>()); if(hasCaptureEvents) capturableStream.Capture += (sender, e) => { }; Assert.AreEqual(hasCaptureEvents, capturableStream.IsCaptured); }
public void IsClosed_Get_ShouldReturnFalseByDefault() { CapturableStream capturableStream = new CapturableStream(Mock.Of<Stream>(), Mock.Of<Encoding>()); Assert.IsFalse(capturableStream.IsClosed); }
public void HasCaptureWriteEvents_IfCaptureWriteEventsAreRegistered_ShouldReturnTrue() { CapturableStream capturableStream = new CapturableStream(Mock.Of<Stream>(), Mock.Of<Encoding>()); capturableStream.CaptureWrite += (sender, e) => { }; Assert.IsTrue(capturableStream.HasCaptureWriteEvents); }
public void HasCaptureWriteEvents_IfCaptureWriteEventsAreNotRegistered_ShouldReturnFalse() { CapturableStream capturableStream = new CapturableStream(Mock.Of<Stream>(), Mock.Of<Encoding>()); Assert.IsFalse(capturableStream.HasCaptureWriteEvents); capturableStream = new CapturableStream(Mock.Of<Stream>(), Mock.Of<Encoding>()); Assert.IsFalse(capturableStream.HasCaptureWriteEvents); capturableStream.CaptureWrite += CaptureWriteEventHandler; capturableStream.CaptureWrite -= CaptureWriteEventHandler; Assert.IsFalse(capturableStream.HasCaptureWriteEvents); }
public void Close_ShouldSetIsClosedToTrue() { CapturableStream capturableStream = new CapturableStream(Mock.Of<Stream>(), Mock.Of<Encoding>()); capturableStream.Close(); Assert.IsTrue(capturableStream.IsClosed); }
public void Close_ShouldCallCloseOfTheCapturedStream() { Mock<MemoryStream> memoryStreamMock = new Mock<MemoryStream>(); memoryStreamMock.Verify(memoryStream => memoryStream.Close(), Times.Never()); CapturableStream capturableStream = new CapturableStream(Mock.Of<Stream>(), Mock.Of<Encoding>()); // ReSharper disable PossibleNullReferenceException typeof(CapturableStream).GetField("_capturedStream", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(capturableStream, memoryStreamMock.Object); // ReSharper restore PossibleNullReferenceException capturableStream.Close(); memoryStreamMock.Verify(memoryStream => memoryStream.Close(), Times.Once()); }
public void Close_IfHasCaptureEventsAndIsClosedIsTrue_ShouldNotRaiseCaptureEvents() { bool captureRaised = false; CapturableStream capturableStream = new CapturableStream(Mock.Of<Stream>(), Mock.Of<Encoding>()); capturableStream.Capture += (sender, e) => { captureRaised = true; }; Assert.IsFalse(captureRaised); capturableStream.IsClosed = true; capturableStream.Close(); Assert.IsFalse(captureRaised); }