new
[urisagit/Perl-Docs.git] / t / file_object.t
1 #!perl
2 use strict;
3 use Test::More;
4 use File::Slurp;
5
6 use IO::Handle ;
7
8 use UNIVERSAL ;
9
10 plan tests => 4;
11
12 my $path = "data.txt";
13 my $data = "random junk\n";
14
15 # create an object
16 my $obj = FileObject->new($path);
17 isa_ok( $obj, 'FileObject' );
18 is( "$obj", $path, "check that the object correctly stringifies" );
19
20 my $is_glob = eval{ $obj->isa( 'GLOB' ) } ;
21 #print "GLOB $is_glob\n" ;
22
23 my $is_io = eval{ $obj->isa( 'IO' ) } ;
24 #print "IO $is_io\n" ;
25
26 my $io = IO::Handle->new() ;
27 #print "IO2: $io\n" ;
28
29 my $is_io2 = eval{ $io->isa( 'GLOB' ) } ;
30 #print "IO2 $is_io2\n" ;
31
32 open( FH, "<$0" ) or die "can't open $0: $!" ;
33
34 my $io3 = *FH{IO} ;
35 #print "IO3: $io3\n" ;
36
37 my $is_io3 = eval{ $io3->isa( 'IO' ) } ;
38 #print "IO3 $is_io3\n" ;
39
40 my $io4 = *FH{GLOB} ;
41 #print "IO4: $io4\n" ;
42
43 my $is_io4 = eval{ $io4->isa( 'GLOB' ) } ;
44 #print "IO4 $is_io4\n" ;
45
46
47 SKIP: {
48     # write something to that file
49     open(FILE, ">$obj") or skip 4, "can't write to '$path': $!";
50     print FILE $data;
51     close(FILE);
52
53     # pass it to read_file()
54     my $content = eval { read_file($obj) };
55     is( $@, '', "passing an object to read_file()" );
56     is( $content, $data, "checking that the content matches the data" );
57 }
58
59 unlink $path;
60
61
62 # the following mimics the parts from Path::Class causing 
63 # problems with File::Slurp
64 package FileObject;
65 use overload
66     q[""] => \&stringify, fallback => 1;
67
68 sub new {
69     return bless { path => $_[1] }, $_[0]
70 }
71
72 sub stringify {
73     return $_[0]->{path}
74 }
75