* t/oo/composition.t, t/oo/mro-c3.t, t/op/calling.t:
[parrot.git] / t / pmc / file.t
blobe4111b216380074ec7d8201cef7a9b49404b2675
1 #! perl
2 # Copyright (C) 2001-2006, The Perl Foundation.
3 # $Id$
5 use strict;
6 use warnings;
7 use lib qw( . lib ../lib ../../lib );
9 use Test::More;
10 use Parrot::Test tests => 7;
12 use Parrot::Config;
14 use Cwd;
15 use File::Temp;
16 use File::Spec::Functions;
18 my $tempdir = File::Temp::tempdir( CLEANUP => 1 );
20 our ( $MSWin32, $cygwin );
21 $MSWin32 = 1 if $^O =~ m!MSWin32!;
22 $cygwin  = 1 if $^O =~ m!cygwin!;
24 =head1 NAME
26 t/pmc/file.t - Files functions
28 =head1 SYNOPSIS
30     % prove t/pmc/file.t
32 =head1 DESCRIPTION
34 Tests the C<File> PMC.
36 =cut
38 my $xpto = catdir( $tempdir, 'xpto' );
39 mkdir $xpto unless -d $xpto;
41 my $otpx = catfile( $xpto, 'otpx' );
43 open my $fh, '>', $otpx or die $!;
44 print $fh 'xpto';
45 close $fh;
47 # test is_dir
48 pir_output_is( <<"CODE", <<"OUT", "Test is_dir" );
49 .sub main :main
50         \$P1 = new 'File'
52         \$S1 = '$xpto'
53         \$I1 = \$P1."is_dir"(\$S1)
55         if \$I1 goto ok1
56         print "not "
58 ok1:
59         print "ok 1\\n"
61         \$S1 = '$otpx'
62         \$I1 = \$P1."is_dir"(\$S1)
63         \$I1 = !\$I1
65         if \$I1 goto ok2
66         print "not "
68 ok2:
69         print "ok 2\\n"
71         end
72 .end
73 CODE
74 ok 1
75 ok 2
76 OUT
78 # test is_file
79 pir_output_is( <<"CODE", <<"OUT", "Test is_file" );
80 .sub main :main
81         \$P1 = new 'File'
83         \$S1 = '$xpto'
84         \$I1 = \$P1."is_file"(\$S1)
85         \$I1 = !\$I1
87         if \$I1 goto ok1
88         print "not "
90 ok1:
91         print "ok 1\\n"
93         \$S1 = '$otpx'
94         \$I1 = \$P1."is_file"(\$S1)
96         if \$I1 goto ok2
97         print "not "
99 ok2:
100         print "ok 2\\n"
102         end
103 .end
104 CODE
105 ok 1
106 ok 2
109 SKIP: {
110     skip "Links not available under Windows", 1 if $MSWin32;
112     my $lotpx = catfile( $xpto, 'lotpx' );
113     symlink $otpx, $lotpx;
115     # test is_link
116     pir_output_is( <<"CODE", <<"OUT", "Test is_link with links to files" );
117 .sub main :main
118         \$P1 = new 'File'
120         \$S1 = '$lotpx'
121         \$I1 = \$P1."is_link"(\$S1)
123         if \$I1 goto ok1
124         print "not "
125 ok1:
126         print "ok 1\\n"
128         \$S1 = '$otpx'
129         \$I1 = \$P1."is_link"(\$S1)
130         \$I1 = !\$I1
131         if \$I1 goto ok2
132         print "not "
133 ok2:
134         print "ok 2\\n"
135         end
136 .end
137 CODE
138 ok 1
139 ok 2
144 SKIP: {
145     skip "Links not available under Windows", 1 if $MSWin32;
147     my $xptol = catdir( $xpto, 'xptol' );
148     symlink $xpto, $xptol;
150     # test is_link
151     pir_output_is( <<"CODE", <<"OUT", "Test is_link with links to directories" );
152 .sub main :main
153         \$P1 = new 'File'
155         \$S1 = '$xptol'
156         \$I1 = \$P1."is_link"(\$S1)
158         if \$I1 goto ok1
159         print "not "
160 ok1:
161         print "ok 1\\n"
163         \$S1 = '$xpto'
164         \$I1 = \$P1."is_link"(\$S1)
165         \$I1 = !\$I1
166         if \$I1 goto ok2
167         print "not "
168 ok2:
169         print "ok 2\\n"
170         end
171 .end
172 CODE
173 ok 1
174 ok 2
178 my $otpxcopy = catdir( $xpto, 'otpxcopy' );
180 # test copy
181 pir_output_is( <<"CODE", <<"OUT", "Test copy for files" );
182 .sub main :main
183        \$S1 = '$otpx'
184        \$S2 = '$otpxcopy'
186        \$P1 = new 'File'
187        \$P2 = new 'OS'
189        \$P1."copy"(\$S1,\$S2)
190        print "ok\\n"
192        \$P3 = \$P2."stat"(\$S1)
193        \$P4 = \$P2."stat"(\$S2)
195        \$I1 = \$P3[7]
196        \$I2 = \$P4[7]
198        if \$I1 == \$I2 goto ok
199        print "not "
201        print "ok\\n"
203        end
204 .end
205 CODE
210 # test rename
211 SKIP: {
212     skip 'file exists', 1 if $MSWin32;
214     pir_output_is( <<"CODE", <<"OUT", "Test rename for files" );
215 .sub main :main
216        \$S1 = '$otpx'
217        \$S2 = '$otpxcopy'
219        \$P1 = new 'File'
220        \$P2 = new 'OS'
222        \$P3 = \$P2."stat"(\$S1)
223        \$I1 = \$P3[7]
225        \$P1."rename"(\$S1,\$S2)
226        print "ok\\n"
228        \$P4 = \$P2."stat"(\$S2)
229        \$I2 = \$P4[7]
231        if \$I1 == \$I2 goto ok
232        print "not "
234        print "ok\\n"
236        end
237 .end
238 CODE
244 my $bad_file = catfile( $xpto, 'not a file' );
246 # test exists
247 pir_output_is( <<"CODE", <<"OUT", "Test rename for files" );
248 .sub main :main
249        \$P1 = new 'File'
250        \$I1 = \$P1.'exists'( '$otpxcopy' )
252        if \$I1 goto file_exists
253        print "not "
255   file_exists:
256        print "ok 1 - file exists\\n"
258        \$I1 = \$P1.'exists'( '$xpto' )
260        if \$I1 goto dir_exists
261        print "not "
263   dir_exists:
264        print "ok 2 - directory exists\\n"
266        \$I1 = \$P1.'exists'( '$bad_file' )
268        if \$I1 == 0 goto file_does_not_exist
269        print "not "
271   file_does_not_exist:
272        print "ok 3 - file does not exist\\n"
274        end
275 .end
276 CODE
277 ok 1 - file exists
278 ok 2 - directory exists
279 ok 3 - file does not exist
282 # Local Variables:
283 #   mode: cperl
284 #   cperl-indent-level: 4
285 #   fill-column: 100
286 # End:
287 # vim: expandtab shiftwidth=4: