Fix test for overload in given() with smart match after last change
[p5sagit/p5-mst-13.2.git] / t / op / filetest.t
index 97ce3f9..4678f92 100755 (executable)
@@ -10,7 +10,7 @@ BEGIN {
 }
 
 use Config;
-plan(tests => 28);
+plan(tests => 28 + 27*14);
 
 ok( -d 'op' );
 ok( -f 'TEST' );
@@ -80,13 +80,14 @@ ok( (-s -f 'TEST' > 1), "-s returns real size" );
 ok( -f -s 'TEST' == 1 );
 
 # now with an empty file
-open my $fh, ">", "foo";
+my $tempfile = tempfile();
+open my $fh, ">", $tempfile;
 close $fh;
-ok( -f "foo" );
-is( -s "foo", 0 );
-is( -f -s "foo", 0 );
-is( -s -f "foo", 0 );
-unlink "foo";
+ok( -f $tempfile );
+is( -s $tempfile, 0 );
+is( -f -s $tempfile, 0 );
+is( -s -f $tempfile, 0 );
+unlink $tempfile;
 
 # test that _ is a bareword after filetest operators
 
@@ -94,3 +95,102 @@ unlink "foo";
 ok( -f _ );
 sub _ { "this is not a file name" }
 ok( -f _ );
+
+my $over;
+{
+    package OverFtest;
+
+    use overload 
+        -X => sub { 
+            $over = [overload::StrVal($_[0]), $_[1]];
+            "-$_[1]"; 
+        };
+}
+{
+    package OverString;
+
+    # No fallback. -X should fall back to string overload even without
+    # it.
+    use overload q/""/ => sub { $over = 1; "TEST" };
+}
+{
+    package OverBoth;
+
+    use overload
+        q/""/   => sub { "TEST" },
+        -X      => sub { "-$_[1]" };
+}
+{
+    package OverNeither;
+
+    # Need fallback. Previous versions of perl required 'fallback' to do
+    # -X operations on an object with no "" overload.
+    use overload 
+        '+' => sub { 1 },
+        fallback => 1;
+}
+
+my $ft = bless [], "OverFtest";
+my $ftstr = overload::StrVal($ft);
+my $str = bless [], "OverString";
+my $both = bless [], "OverBoth";
+my $neither = bless [], "OverNeither";
+my $nstr = overload::StrVal($neither);
+
+open my $gv, "<", "TEST";
+bless $gv, "OverString";
+open my $io, "<", "TEST";
+$io = *{$io}{IO};
+bless $io, "OverString";
+
+eval { require Fcntl };
+
+for my $op (split //, "rwxoRWXOezsfdlpSbctugkTMBAC") {
+    $over = [];
+    ok( my $rv = eval "-$op \$ft",  "overloaded -$op succeeds" )
+        or diag( $@ );
+    is( $over->[0], $ftstr,         "correct object for overloaded -$op" );
+    is( $over->[1], $op,            "correct op for overloaded -$op" );
+    is( $rv,        "-$op",         "correct return value for overloaded -$op");
+
+    my ($exp, $is) = (1, "is");
+    if (
+        $op eq "u" and not eval { Fcntl::S_ISUID() } or
+        $op eq "g" and not eval { Fcntl::S_ISGID() } or
+        $op eq "k" and not eval { Fcntl::S_ISVTX() }
+    ) {
+        ($exp, $is) = (0, "not");
+    }
+
+    $over = 0;
+    $rv = eval "-$op \$str";
+    ok( !$@,                        "-$op succeeds with string overloading" )
+        or diag( $@ );
+    is( $rv, eval "-$op 'TEST'",    "correct -$op on string overload" );
+    is( $over,      $exp,           "string overload $is called for -$op" );
+
+    ($exp, $is) = $op eq "l" ? (1, "is") : (0, "not");
+
+    $over = 0;
+    eval "-$op \$gv";
+    is( $over,      $exp,   "string overload $is called for -$op on GLOB" );
+
+    # IO refs always get string overload called. This might be a bug.
+    $op eq "t" || $op eq "T" || $op eq "B"
+        and ($exp, $is) = (1, "is");
+
+    $over = 0;
+    eval "-$op \$io";
+    is( $over,      $exp,   "string overload $is called for -$op on IO");
+
+    $rv = eval "-$op \$both";
+    is( $rv,        "-$op",         "correct -$op on string/-X overload" );
+
+    $rv = eval "-$op \$neither";
+    ok( !$@,                        "-$op succeeds with random overloading" )
+        or diag( $@ );
+    is( $rv, eval "-$op \$nstr",    "correct -$op with random overloading" );
+
+    is( eval "-r -$op \$ft", "-r",      "stacked overloaded -$op" );
+    is( eval "-$op -r \$ft", "-$op",    "overloaded stacked -$op" );
+}