Integrate mainline
[p5sagit/p5-mst-13.2.git] / t / pragma / overload.t
index f673dce..d075062 100755 (executable)
@@ -2,7 +2,7 @@
 
 BEGIN {
     chdir 't' if -d 't';
-    unshift @INC, '../lib';
+    @INC = '../lib';
 }
 
 package Oscalar;
@@ -133,6 +133,7 @@ test ( $a eq "087");                # 29
 test ( $b eq "88");            # 30
 test (ref $a eq "Oscalar");    # 31
 
+undef $b;                      # Destroying updates tables too...
 
 eval q[package Oscalar; use overload ('++' => sub { $ {$_[0]} += 2; $_[0] } ) ];
 
@@ -417,7 +418,7 @@ EOF
   m'try it';
   s'first part'second part';
   s/yet another/tail here/;
-  tr/z-Z/z-Z/;
+  tr/A-Z/a-z/;
 }
 
 test($out, '_<foo>_');         # 117
@@ -425,7 +426,7 @@ test($out1, '_<f\'o\\o>_');         # 128
 test($out2, "_<a\a>_foo_<,\,>_");      # 129
 test("@q1", "foo q f'o\\\\o q a\\a qq ,\\, qq oups
  qq oups1
- q second part q tail here s z-Z tr z-Z tr");  # 130
+ q second part q tail here s A-Z tr a-z tr");  # 130
 test("@qr1", "b\\b qq .\\. qq try it q first part q yet another qq");  # 131
 test($res, 1);                 # 132
 test($a, "_<oups
@@ -493,7 +494,7 @@ test($c, "bareword");       # 135
   sub STORE { 
     my $obj = shift; 
     $#$obj = 1; 
-    @$obj->[0,1] = ('=', shift);
+    $obj->[1] = shift;
   }
 }
 
@@ -614,7 +615,7 @@ test($c, "bareword");       # 135
   sub STORE { 
     my $obj = shift; 
     $#$obj = 1; 
-    @$obj->[0,1] = ('=', shift);
+    $obj->[1] = shift;
   }
 }
 
@@ -759,7 +760,12 @@ else {
                    }, 'deref';
   # Hash:
   my @cont = sort %$deref;
-  test "@cont", '23 5 fake foo';       # 178
+  if ("\t" eq "\011") { # ascii
+      test "@cont", '23 5 fake foo';   # 178
+  } 
+  else {                # ebcdic alpha-numeric sort order
+      test "@cont", 'fake foo 23 5';   # 178
+  }
   my @keys = sort keys %$deref;
   test "@keys", 'fake foo';    # 179
   my @val = sort values %$deref;
@@ -914,14 +920,131 @@ test $bar->[3], 13;              # 206
 my $aaa;
 { my $bbbb = 0; $aaa = bless \$bbbb, B }
 
-test !$aaa, 1;
+test !$aaa, 1;                 # 207
 
 unless ($aaa) {
-  test 'ok', 'ok';
+  test 'ok', 'ok';             # 208
 } else {
-  test 'is not', 'ok';
+  test 'is not', 'ok';         # 208
+}
+
+# check that overload isn't done twice by join
+{ my $c = 0;
+  package Join;
+  use overload '""' => sub { $c++ };
+  my $x = join '', bless([]), 'pq', bless([]);
+  main::test $x, '0pq1';               # 209
+};
+
+# Test module-specific warning
+{
+    # check the Odd number of arguments for overload::constant warning
+    my $a = "" ;
+    local $SIG{__WARN__} = sub {$a = $_[0]} ;
+    $x = eval ' overload::constant "integer" ; ' ;
+    test($a eq "") ; # 210
+    use warnings 'overload' ;
+    $x = eval ' overload::constant "integer" ; ' ;
+    test($a =~ /^Odd number of arguments for overload::constant at/) ; # 211
+}
+
+{
+    # check the `$_[0]' is not an overloadable type warning
+    my $a = "" ;
+    local $SIG{__WARN__} = sub {$a = $_[0]} ;
+    $x = eval ' overload::constant "fred" => sub {} ; ' ;
+    test($a eq "") ; # 212
+    use warnings 'overload' ;
+    $x = eval ' overload::constant "fred" => sub {} ; ' ;
+    test($a =~ /^`fred' is not an overloadable type at/); # 213
+}
+
+{
+    # check the `$_[1]' is not a code reference warning
+    my $a = "" ;
+    local $SIG{__WARN__} = sub {$a = $_[0]} ;
+    $x = eval ' overload::constant "integer" => 1; ' ;
+    test($a eq "") ; # 214
+    use warnings 'overload' ;
+    $x = eval ' overload::constant "integer" => 1; ' ;
+    test($a =~ /^`1' is not a code reference at/); # 215
+}
+
+{
+  my $c = 0;
+  package ov_int1;
+  use overload '""'    => sub { 3+shift->[0] },
+               '0+'    => sub { 10+shift->[0] },
+               'int'   => sub { 100+shift->[0] };
+  sub new {my $p = shift; bless [shift], $p}
+
+  package ov_int2;
+  use overload '""'    => sub { 5+shift->[0] },
+               '0+'    => sub { 30+shift->[0] },
+               'int'   => sub { 'ov_int1'->new(1000+shift->[0]) };
+  sub new {my $p = shift; bless [shift], $p}
+
+  package noov_int;
+  use overload '""'    => sub { 2+shift->[0] },
+               '0+'    => sub { 9+shift->[0] };
+  sub new {my $p = shift; bless [shift], $p}
+
+  package main;
+
+  my $x = new noov_int 11;
+  my $int_x = int $x;
+  main::test("$int_x" eq 20);                  # 216
+  $x = new ov_int1 31;
+  $int_x = int $x;
+  main::test("$int_x" eq 131);                 # 217
+  $x = new ov_int2 51;
+  $int_x = int $x;
+  main::test("$int_x" eq 1054);                        # 218
 }
 
+# make sure that we don't inifinitely recurse
+{
+  my $c = 0;
+  package Recurse;
+  use overload '""'    => sub { shift },
+               '0+'    => sub { shift },
+               'bool'  => sub { shift },
+               fallback => 1;
+  my $x = bless([]);
+  main::test("$x" =~ /Recurse=ARRAY/);         # 219
+  main::test($x);                               # 220
+  main::test($x+0 =~ /Recurse=ARRAY/);         # 221
+}
+
+# BugID 20010422.003
+package Foo;
+
+use overload
+  'bool' => sub { return !$_[0]->is_zero() || undef; }
+;
+sub is_zero
+  {
+  my $self = shift;
+  return $self->{var} == 0;
+  }
+
+sub new
+  {
+  my $class = shift;
+  my $self =  {};
+  $self->{var} = shift;
+  bless $self,$class;
+  }
+
+package main;
+
+use strict;
+
+my $r = Foo->new(8);
+$r = Foo->new(0);
+
+test(($r || 0) == 0); # 222
 
 # Last test is:
-sub last {208}
+sub last {222}