Author: ambs
Date: Wed Jan 11 12:36:52 2006
New Revision: 11100
Modified:
trunk/src/classes/file.pmc
trunk/t/pmc/file.t
Log:
Added is_file (-f in perl, I think) under File pmc
Added tests for is_file and is_dir
Modified: trunk/src/classes/file.pmc
==============================================================================
--- trunk/src/classes/file.pmc (original)
+++ trunk/src/classes/file.pmc Wed Jan 11 12:36:52 2006
@@ -79,6 +79,32 @@ Returns a true value (1) if the supplied
/*
+=item C<INTVAL is_dir(STRING* path)>
+
+Returns a true value (1) if the supplied path is a directory.
+
+=cut
+
+*/
+
+ METHOD INTVAL is_file(STRING *path) {
+ struct stat info;
+ char *cpath = string_to_cstring(interpreter, path);
+ int error = stat(cpath, &info);
+ if (error) {
+ char *errmsg = strerror(errno);
+ real_exception(interpreter, NULL, E_SystemError, errmsg);
+ }
+
+ if (S_ISREG(info.st_mode)) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+
+/*
+
=item C<INTVAL is_link(STRING* path)>
Returns a true value (1) if the supplied path is a link.
Modified: trunk/t/pmc/file.t
==============================================================================
--- trunk/t/pmc/file.t (original)
+++ trunk/t/pmc/file.t Wed Jan 11 12:36:52 2006
@@ -6,7 +6,7 @@ use strict;
use warnings;
use lib qw( . lib ../lib ../../lib );
use Test::More;
-use Parrot::Test tests => 1;
+use Parrot::Test tests => 2;
use Parrot::Config;
use Cwd;
use File::Spec;
@@ -30,28 +30,79 @@ Tests the C<File> PMC.
=cut
END {
+ # XXX - FIXME - Use Tempfir
+
# Clean up environment on exit
+ unlink "otpx" if -f "otpx";
rmdir "xpto" if -d "xpto";
- unlink "xpto" if -f "xpto";
}
-# test is_dir
+
mkdir "xpto" unless -d "xpto";
-pir_output_is(<<'CODE', <<"OUT", "Test is_dir in a directory");
+open X, ">otpx" or die $!;
+print X "xpto";
+close X;
+
+# test is_dir
+pir_output_is(<<'CODE', <<"OUT", "Test is_dir");
.sub main :main
$P1 = new .File
$S1 = "xpto"
$I1 = $P1."is_dir"($S1)
- if $I1 goto ok
+ if $I1 goto ok1
+ print "not "
+
+ok1:
+ print "ok 1\n"
+
+ $S1 = "otpx"
+ $I1 = $P1."is_dir"($S1)
+ $I1 = !$I1
+
+ if $I1 goto ok2
+ print "not "
+
+ok2:
+ print "ok 2\n"
+
+ end
+.end
+CODE
+ok 1
+ok 2
+OUT
+
+
+# test is_dir
+pir_output_is(<<'CODE', <<"OUT", "Test is_dir");
+.sub main :main
+ $P1 = new .File
+
+ $S1 = "xpto"
+ $I1 = $P1."is_file"($S1)
+ $I1 = !I1
+
+ if $I1 goto ok1
print "not "
-ok:
- print "ok\n"
+ok1:
+ print "ok 1\n"
+
+ $S1 = "otpx"
+ $I1 = $P1."is_file"($S1)
+
+ if $I1 goto ok2
+ print "not "
+
+ok2:
+ print "ok 2\n"
+
end
.end
CODE
-ok
+ok 1
+ok 2
OUT