| 1 |
use Test2::Bundle::Extended -target => 'Test2::Harness::Util::File'; |
| 2 |
# HARNESS-DURATION-SHORT |
| 3 |
|
| 4 |
use ok $CLASS; |
| 5 |
|
| 6 |
can_ok($CLASS, qw/name done set_done/); |
| 7 |
|
| 8 |
like( |
| 9 |
dies { $CLASS->new }, |
| 10 |
qr/'name' is a required attribute/, |
| 11 |
"Must provide the 'name' attribute" |
| 12 |
); |
| 13 |
|
| 14 |
open(my $tmpfh, '<', __FILE__) or die "Could not open file: $!"; |
| 15 |
my $zed = $CLASS->new(name => __FILE__, fh => $tmpfh); |
| 16 |
is($zed->_init_fh, $tmpfh, "saved fh"); |
| 17 |
is($zed->fh->blocking, 0, "fh was set to non-blocking"); |
| 18 |
$zed = undef; |
| 19 |
|
| 20 |
my $one = $CLASS->new(name => __FILE__); |
| 21 |
my $two = $CLASS->new(name => '/some/super/fake/file/that must not exist'); |
| 22 |
ok($one->exists, "This file exists"); |
| 23 |
ok(!$two->exists, "The file does not exist"); |
| 24 |
|
| 25 |
is($one->decode('xxx'), 'xxx', "base class decode does nothing"); |
| 26 |
is($one->encode('xxx'), 'xxx', "base class encode does nothing"); |
| 27 |
|
| 28 |
ok(my $fh = $one->open_file, "opened file (for reading)"); |
| 29 |
ok(dies { $two->open_file }, "Cannot open file (for reading)"); |
| 30 |
|
| 31 |
my ($line) = split /\n/, $one->maybe_read, 2; |
| 32 |
like( |
| 33 |
$line, |
| 34 |
q{use Test2::Bundle::Extended -target => 'Test2::Harness::Util::File';}, |
| 35 |
"Can read file (using maybe_read)" |
| 36 |
); |
| 37 |
|
| 38 |
is( |
| 39 |
$two->maybe_read, |
| 40 |
undef, |
| 41 |
"maybe_read returns undef for non-existant file" |
| 42 |
); |
| 43 |
|
| 44 |
($line) = split /\n/, $one->read, 2; |
| 45 |
like( |
| 46 |
$line, |
| 47 |
q{use Test2::Bundle::Extended -target => 'Test2::Harness::Util::File';}, |
| 48 |
"Can read file" |
| 49 |
); |
| 50 |
|
| 51 |
ok(dies { $two->read }, "read() dies on missing file"); |
| 52 |
|
| 53 |
close($fh); |
| 54 |
|
| 55 |
ok($fh = $one->fh, "Can generate an FH"); |
| 56 |
is($one->fh, $fh, "FH is remembered"); |
| 57 |
is($fh->blocking, 0, "FH is non-blocking"); |
| 58 |
|
| 59 |
close($fh); |
| 60 |
|
| 61 |
is($two->fh, undef, "return undef for missing file"); |
| 62 |
|
| 63 |
$one->set_done(1); |
| 64 |
is($one->done, 1, "can set done"); |
| 65 |
$one->reset; |
| 66 |
ok(!$one->{_fh}, "removed fh"); |
| 67 |
ok(!$one->done, "cleared done flag"); |
| 68 |
|
| 69 |
$two->reset; |
| 70 |
is($two->read_line, undef, "cannot read lines from missing file"); |
| 71 |
|
| 72 |
is( |
| 73 |
$one->read_line, |
| 74 |
"use Test2::Bundle::Extended -target => 'Test2::Harness::Util::File';\n", |
| 75 |
"Got first line" |
| 76 |
); |
| 77 |
|
| 78 |
while(my $l = $one->read_line) { 1 } |
| 79 |
|
| 80 |
is($one->read_line, undef, "no line to read yet"); |
| 81 |
$one->set_done(1); |
| 82 |
|
| 83 |
is( |
| 84 |
$one->read_line, |
| 85 |
"This line MUST be here, and MUST not end with a newline.", |
| 86 |
"Got final line with no terminator" |
| 87 |
); |
| 88 |
|
| 89 |
$one->reset; |
| 90 |
is( |
| 91 |
$one->read_line, |
| 92 |
"use Test2::Bundle::Extended -target => 'Test2::Harness::Util::File';\n", |
| 93 |
"Got first line again after reset" |
| 94 |
); |
| 95 |
|
| 96 |
#TODO: write (it is atomic) |
| 97 |
|
| 98 |
done_testing; |
| 99 |
|
| 100 |
__END__ |
| 101 |
|
| 102 |
This line MUST be here, and MUST not end with a newline. |