Skip to content

Commit

Permalink
Implement whence for SEEK.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChasonDeshotel committed May 28, 2024
1 parent e0ea6c7 commit 1403de2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
31 changes: 22 additions & 9 deletions lib/Test/MockFile/FileHandle.pm
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,6 @@ Arguments passed are:C<( $self, $pos, $whence )>
Moves the location of our current tell location.
B<$whence is UNIMPLEMENTED>: Open a ticket in
L<github|https://github.com/cpanelinc/Test-MockFile/issues> if you need
this feature.
No L<perldoc
documentation|http://perldoc.perl.org/perltie.html#Tying-FileHandles>
exists on this method.
Expand All @@ -415,17 +411,34 @@ exists on this method.
sub SEEK {
my ( $self, $pos, $whence ) = @_;

if ($whence) {
die('Unimplemented');
}
my $file_size = length $self->{'data'}->{'contents'};
return if $file_size < $pos;

$self->{'tell'} = $pos;
my $new_pos;

my $SEEK_SET = 0;
my $SEEK_CUR = 1;
my $SEEK_END = 2;

return $pos == 0 ? '0 but true' : $pos;
if ($whence == $SEEK_SET) {
$new_pos = $pos;
} elsif ($whence == $SEEK_CUR) {
$new_pos = $self->{'tell'} + $pos;
} elsif ($whence == $SEEK_END) {
$new_pos = $file_size + $pos;
} else {
die('Invalid whence value');
}

if ($new_pos < 0 || $new_pos > $file_size) {
return 0;
}

$self->{'tell'} = $new_pos;
return $new_pos == 0 ? '0 but true' : $new_pos;
}


=head2 TELL
Returns the numeric location we are in the file. The C<TELL> tells us
Expand Down
Binary file added t/.sysopen.t.swp
Binary file not shown.
32 changes: 32 additions & 0 deletions t/sysopen.t
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ is( \%Test::MockFile::files_being_mocked, {}, "No mock files are in cache" ) or
ok( seek( $fh, 0, 0 ), 0, "Seek to start of file returns true" );
is( sysseek( $fh, 0, 0 ), "0 but true", "sysseek to start of file returns '0 but true' to make it so." );
ok( sysseek( $fh, 0, 0 ), "sysseek to start of file returns true when checked with ok()" );

ok( sysseek( $fh, 5, 0 ), "sysseek to position 5 returns true." );
ok( sysseek( $fh, 10, 1 ), "Seek 10 bytes forward from the current position." );
is( sysseek( $fh, 0, 1 ), 15, "Current position is 15 bytes from start." );

$buf = "";
is( sysread( $fh, $buf, 2, 0 ), 2, "Read 2 bytes from current position (10)." );
is( $buf, "PQ", "Line is as expected." );

ok( sysseek( $fh, -5, 2 ), "Seek 5 bytes back from end of file." );
is( sysseek( $fh, 0, 1 ), 46, "Current position is 46 bytes from start." );

$buf = "";
is( sysread( $fh, $buf, 3, 0 ), 3, "Read 3 bytes from current position (46)." );
is( $buf, "vwx", "Line is as expected." );
}

{
Expand Down Expand Up @@ -122,6 +137,23 @@ is( \%Test::MockFile::files_being_mocked, {}, "No mock files are in cache" ) or
is( sysseek( $fh, 0, 0 ), "0 but true", "sysseek to start of file returns '0 but true' to make it so." );
ok( sysseek( $fh, 0, 0 ), "sysseek to start of file returns true when checked with ok()" );

ok( sysseek( $fh, 5, 0 ), "sysseek to position 5 returns true." );
ok( sysseek( $fh, 10, 1 ), "Seek 10 bytes forward from the current position." );
is( sysseek( $fh, 0, 1 ), 15, "Current position is 15 bytes from start." );

$buf = "";
is( sysread( $fh, $buf, 2, 0 ), 2, "Read 2 bytes from current position (10)." );
is( $buf, "PQ", "Line is as expected." );

ok( sysseek( $fh, -5, 2 ), "Seek 5 bytes back from end of file." );
is( sysseek( $fh, 0, 1 ), 46, "Current position is 46 bytes from start." );

$buf = "";
is( sysread( $fh, $buf, 3, 0 ), 3, "Read 3 bytes from current position (46)." );
is( $buf, "vwx", "Line is as expected." );

like( dies { sysseek( $fh, 10, 3 ) }, qr/Invalid whence value/, "Dies when given an invalid whence value." );

close $fh;
undef $bar;
}
Expand Down

0 comments on commit 1403de2

Please sign in to comment.