public static void TestSimple() { using (MemoryStream stream = FakeRuntimeFileInfo.ExpandableMemoryStream(Encoding.UTF8.GetBytes("A short dummy stream"))) { IRuntimeFileInfo fileInfo = OS.Current.FileInfo(_davidCopperfieldTxtPath); using (LockingStream lockingStream = new LockingStream(fileInfo, stream)) { Assert.That(FileLock.IsLocked(fileInfo), "The file should be locked now."); Assert.That(lockingStream.CanRead, "The stream should be readable."); Assert.That(lockingStream.CanSeek, "The stream should be seekable."); Assert.That(lockingStream.CanWrite, "The stream should be writeable."); Assert.That(lockingStream.Length, Is.EqualTo("A short dummy stream".Length), "The length should be the same as the string."); byte[] b = new byte[1]; int read = lockingStream.Read(b, 0, 1); Assert.That(read, Is.EqualTo(1), "There should be one byte read."); Assert.That(b[0], Is.EqualTo(Encoding.UTF8.GetBytes("A")[0]), "The byte read should be an 'A'."); Assert.That(lockingStream.Position, Is.EqualTo(1), "After reading the first byte, the position should be at one."); lockingStream.Write(b, 0, 1); lockingStream.Position = 1; read = lockingStream.Read(b, 0, 1); Assert.That(read, Is.EqualTo(1), "There should be one byte read."); Assert.That(b[0], Is.EqualTo(Encoding.UTF8.GetBytes("A")[0]), "The byte read should be an 'A'."); lockingStream.Seek(-1, SeekOrigin.End); Assert.That(lockingStream.Position, Is.EqualTo(lockingStream.Length - 1), "The position should be set by the Seek()."); lockingStream.SetLength(5); lockingStream.Seek(0, SeekOrigin.End); Assert.That(lockingStream.Position, Is.EqualTo(5), "After setting the length to 5, seeking to the end should set the position at 5."); Assert.DoesNotThrow(() => { lockingStream.Flush(); }, "It's hard to test Flush() behavior here, not worth the trouble, but it should not throw!"); } Assert.That(!FileLock.IsLocked(fileInfo), "The file should be unlocked now."); Assert.Throws <ObjectDisposedException>(() => { stream.Position = 0; }, "The underlying stream should be disposed."); } }