Skip to main content

Stream

Describes the possible expectations for Stream and BufferedStream.

ExpectationNegatedSummary
IsReadableIsNotReadablesupports reading
IsWritableIsNotWritablesupports writing
IsSeekableIsNotSeekablesupports seeking
IsReadOnlyIsNotReadOnlysupports reading, but not writing
IsWriteOnlyIsNotWriteOnlysupports writing, but not reading
HasLengthnegated comparisonhas the expected length in bytes
HasPositionnegated comparisonis at the expected position
HasBufferSizenegated comparisona BufferedStream with the expected buffer size

Capabilities​

You can verify what the Stream supports, or that it does not support it:

Stream playlist = new MemoryStream();

await Expect.That(playlist).IsReadable();
await Expect.That(playlist).IsSeekable();
await Expect.That(playlist).IsWritable();
await Expect.That(playlist).IsNotReadOnly();
await Expect.That(File.Open("album.txt", FileMode.OpenOrCreate, FileAccess.Read)).IsReadOnly()
.Because("the file was opened with Read access");
await Expect.That(File.Open("album.log", FileMode.OpenOrCreate, FileAccess.Write)).IsWriteOnly()
.Because("the file was opened with Write access");

Length​

You can verify the length of the Stream:

Stream playlist = new MemoryStream("foo"u8.ToArray());

await Expect.That(playlist).HasLength(3);
await Expect.That(playlist).HasLength().Between(2).And(4);

Position​

You can verify the position of the Stream:

Stream playlist = new MemoryStream("foo"u8.ToArray());
playlist.Seek(2, SeekOrigin.Current);

await Expect.That(playlist).HasPosition(2);
await Expect.That(playlist).HasPosition().GreaterThan(1);

Buffer size​

.NET 8 or later

The buffer size expectations are only available on .NET 8 or later.

You can verify the buffer size of the BufferedStream:

BufferedStream playlist = new(new MemoryStream("foo"u8.ToArray()), 2);

await Expect.That(playlist).HasBufferSize(2);
await Expect.That(playlist).HasBufferSize().NotEqualTo(3);

Comparisons​

The Has… expectations for a number or a TimeSpan, e.g. HasLength(), HasCount(), HasYear() or HasMajor(), continue with one of the following comparisons, each with a negated counterpart:

ComparisonNegatedSucceeds when the value is
EqualTo(x)NotEqualTo(x)equal to x
GreaterThan(x)NotGreaterThan(x)greater than x
GreaterThanOrEqualTo(x)NotGreaterThanOrEqualTo(x)greater than or equal to x
LessThan(x)NotLessThan(x)less than x
LessThanOrEqualTo(x)NotLessThanOrEqualTo(x)less than or equal to x
Between(min).And(max)NotBetween(min).And(max)between min and max, both bounds included

Passing the value directly, e.g. HasLength(10), is a shorthand for EqualTo.

The expected value is nullable, e.g. to pass a value mapped from a property. As the actual value is never null, EqualTo(null) fails and NotEqualTo(null) succeeds, while a comparison of order against null, such as GreaterThan(null) or Between(null).And(3), fails even when negated.