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:

using Stream playlist = new MemoryStream();

await That(playlist).IsReadable();
await That(playlist).IsSeekable();
await That(playlist).IsWritable();
await That(playlist).IsNotReadOnly();

using FileStream album = File.Open("album.txt", FileMode.OpenOrCreate, FileAccess.Read);
await That(album).IsReadOnly()
.Because("the file was opened with Read access");
using FileStream log = File.Open("album.log", FileMode.OpenOrCreate, FileAccess.Write);
await That(log).IsWriteOnly()
.Because("the file was opened with Write access");

Length​

You can verify the length of the Stream:

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

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

The Has… expectations whose value is 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(3), is a shorthand for EqualTo.

Comparing with null

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.

Position​

You can verify the position of the Stream:

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

await That(playlist).HasPosition(2);
await 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:

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

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