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