File: file_object.t

package info (click to toggle)
libfile-slurp-perl 9999.32-2.1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 412 kB
  • sloc: perl: 650; makefile: 39
file content (68 lines) | stat: -rw-r--r-- 1,977 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use strict;
use warnings;

use File::Basename ();
use File::Spec ();
use lib File::Spec->catdir(File::Spec->rel2abs(File::Basename::dirname(__FILE__)), 'lib');
use FileSlurpTest qw(temp_file_path trap_function);
use File::Slurp qw(read_file write_file);
use Scalar::Util qw(blessed);
use Test::More ;

plan tests => 12;

# the following mimics the parts from Path::Class causing
# problems with File::Slurp
{
    package FileObject;
    use strict;
    use warnings;
    use overload
        q[""] => \&stringify, fallback => 1;

    sub new {
        return bless { path => $_[1] }, $_[0]
    }

    sub stringify {
        return $_[0]->{path}
    }
}

my $path = temp_file_path();
my $data = "random junk\n";

# create an object
my $obj = FileObject->new($path);
isa_ok($obj, 'FileObject');
is("$obj", $path, "check that the object correctly stringifies");
ok(!($obj && ref($obj) && blessed($obj) && $obj->isa('GLOB')), "FileObject isn't a glob");
ok(!($obj && ref($obj) && blessed($obj) && $obj->isa('IO')), "FileObject isn't an IO");

my $io = IO::Handle->new();
isa_ok($io, 'IO::Handle');
ok($io && ref($io) && blessed($io) && $io->isa('GLOB'), "IO::Handle is a glob");
ok(!($io && ref($io) && blessed($io) && $io->isa('IO')), "IO::Handle isn't an IO");

SKIP: {
    open(FH, '<', $0) or skip 3, "Can't open $0: $!";
    my $fh = *FH{IO};
    my $glob = *FH{GLOB};
    ok($fh && ref($fh) && blessed($fh) && $fh->isa('IO'), '$fh is an IO');
    ok(!($glob && ref($glob) && blessed($glob) && $glob->isa('GLOB')), '$glob is an GLOB');
}

SKIP: {
    # write something to that file
    open(FILE, '>', "$obj") or skip 4, "can't write to '$path': $!";
    print FILE $data;
    close(FILE);

    # pass it to read_file()
    my ($res, $warn, $err) = trap_function(\&read_file, $obj);
    is($res, $data, "read_file: file object: right content");
    ok(!$warn, "read_file: file object: no warnings!");
    ok(!$err, "read_file: file object: no exceptions!");
}

unlink $path;