Skip to content

Commit

Permalink
🐛 Fix Position getter uninit LazyFileStream returning 0
Browse files Browse the repository at this point in the history
  • Loading branch information
Benito Palacios Sanchez committed Jul 15, 2024
1 parent e206c92 commit 34efc81
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/Yarhl.UnitTests/IO/StreamFormat/LazyFileStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,29 @@ public void ConstructorSetFileLengthOrZero()
}

[Test]
public void GetPositionWithoutInitializeReturnsZero()
public void GetPositionWithoutInitializeReturnsSameAsSet()
{
using var stream = new LazyFileStream(tempFile, FileOpenMode.ReadWrite);
Assert.That(stream.Position, Is.EqualTo(0));

stream.Position = 0x42;
Assert.That(stream.BaseStream, Is.Null);
Assert.That(stream.Position, Is.EqualTo(0x42));
}

[Test]
public void FileOpenedWithInitialPosition()
{
byte[] fileContent = { 0xFF, 0x42 };
File.WriteAllBytes(tempFile, fileContent);
using var stream = new LazyFileStream(tempFile, FileOpenMode.ReadWrite);
stream.Position = 1;

Assert.That(stream.BaseStream, Is.Null);
byte actual = (byte)stream.ReadByte();

Assert.That(actual, Is.EqualTo(fileContent[1]));
Assert.That(stream.Position, Is.EqualTo(2));
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion src/Yarhl/IO/StreamFormat/LazyFileStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public LazyFileStream(string path, FileOpenMode mode)
/// <inheritdoc/>
public override long Position
{
get => BaseStream?.Position ?? 0;
get => BaseStream?.Position ?? initialPosition;
set
{
if (BaseStream is null) {
Expand Down

0 comments on commit 34efc81

Please sign in to comment.