Fix test for overload in given() with smart match after last change
[p5sagit/p5-mst-13.2.git] / t / op / smartmatch.t
index fa66de9..8c48768 100644 (file)
@@ -6,9 +6,12 @@ BEGIN {
     require './test.pl';
 }
 use strict;
+use warnings;
+no warnings 'uninitialized';
 
 use Tie::Array;
 use Tie::Hash;
+use Tie::RefHash;
 
 # Predeclare vars used in the tests:
 my @empty;
@@ -32,15 +35,27 @@ tie my %tied_hash, 'Tie::StdHash';
 }
 
 {
-    package Test::Object::CopyOverload;
+    package Test::Object::WithOverload;
     sub new { bless { key => 'magic' } }
-    use overload '~~' => sub { my %hash = %{ $_[0] }; $_[1] eq $hash{key} };
+    use overload '~~' => sub {
+       my %hash = %{ $_[0] };
+       if ($_[2]) { # arguments reversed ?
+           return $_[1] eq reverse $hash{key};
+       }
+       else {
+           return $_[1] eq $hash{key};
+       }
+    };
     use overload '""' => sub { "stringified" };
+    use overload 'eq' => sub {"$_[0]" eq "$_[1]"};
 }
 
-our $ov_obj = Test::Object::CopyOverload->new;
+our $ov_obj = Test::Object::WithOverload->new;
 our $obj = Test::Object::NoOverload->new;
 
+tie my %refh, 'Tie::RefHash';
+$refh{$ov_obj} = 1;
+
 my @keyandmore = qw(key and more);
 my @fooormore = qw(foo or more);
 my %keyandmore = map { $_ => 0 } @keyandmore;
@@ -61,7 +76,13 @@ while (<DATA>) {
     my $tstr = "$left ~~ $right";
 
     test_again:
-    my $res = eval $tstr;
+    my $res;
+    if ($note =~ /NOWARNINGS/) {
+       $res = eval "no warnings; $tstr";
+    }
+    else {
+       $res = eval $tstr;
+    }
 
     chomp $@;
 
@@ -145,15 +166,15 @@ __DATA__
 
 # Any ~~ object overloaded
 !      \&fatal         $ov_obj
-       'magic'         $ov_obj
-!      'not magic'     $ov_obj
+       'cigam'         $ov_obj
+!      'cigam on'      $ov_obj
 !      $obj            $ov_obj
 !      undef           $ov_obj
 
 # regular object
 @      $obj            $obj
 @      $ov_obj         $obj
-@      \&fatal         $obj
+=@     \&fatal         $obj
 @      \&FALSE         $obj
 @      \&foo           $obj
 @      sub { 1 }       $obj
@@ -170,6 +191,9 @@ __DATA__
 # object (overloaded or not) ~~ Any
        $obj            qr/NoOverload/
        $ov_obj         qr/^stringified$/
+       "$ov_obj"       "stringified"
+       $ov_obj         'magic'
+!      $ov_obj         'not magic'
 
 # ~~ Coderef
        sub{0}          sub { ref $_[0] eq "CODE" }
@@ -204,7 +228,7 @@ __DATA__
 !      [1]             \&foo
 !      {a=>1}          \&foo
        $obj            sub { ref($_[0]) =~ /NoOverload/ }
-       $ov_obj         sub { ref($_[0]) =~ /CopyOverload/ }
+       $ov_obj         sub { ref($_[0]) =~ /WithOverload/ }
 # empty stuff matches, because the sub is never called:
        []              \&foo
        {}              \&foo
@@ -241,6 +265,11 @@ __DATA__
 =      %hash           %tied_hash
        %tied_hash      %tied_hash
 !=     {"a"=>"b"}      %tied_hash
+       $ov_obj         %refh
+!      "$ov_obj"       %refh
+       [$ov_obj]       %refh
+!      ["$ov_obj"]     %refh
+       %refh           %refh
 
 #  - an array ref
 #  (since this is symmetrical, tests as well hash~~array)
@@ -264,16 +293,15 @@ __DATA__
 =      \@fooormore     %fooormore
 
 #  - a regex
-# TODO those should be symmetrical
-       qr/^(fo[ox])$/          {foo => 1}
-       /^(fo[ox])$/            %fooormore
+=      qr/^(fo[ox])$/          {foo => 1}
+=      /^(fo[ox])$/            %fooormore
 =!     qr/[13579]$/            +{0..99}
-!      qr/a*/                  {}
+=!     qr/a*/                  {}
 =      qr/a*/                  {b=>2}
-       qr/B/i                  {b=>2}
-       /B/i                    {b=>2}
-!      qr/a+/                  {b=>2}
-       qr/^à/                 {"à"=>2}
+=      qr/B/i                  {b=>2}
+=      /B/i                    {b=>2}
+=!     qr/a+/                  {b=>2}
+=      qr/^à/                 {"à"=>2}
 
 #  - a scalar
        "foo"           +{foo => 1, bar => 2}
@@ -299,8 +327,8 @@ __DATA__
 #  - another array ref
        []                      []
 =!     []                      [1]
-!      [["foo"], ["bar"]]      [qr/o/, qr/a/]
-       [["foo"], ["bar"]]      [qr/ARRAY/, qr/ARRAY/]
+       [["foo"], ["bar"]]      [qr/o/, qr/a/]
+!      [["foo"], ["bar"]]      [qr/ARRAY/, qr/ARRAY/]
        ["foo", "bar"]          [qr/o/, qr/a/]
 !      [qr/o/, qr/a/]          ["foo", "bar"]
        ["foo", "bar"]          [["foo"], ["bar"]]
@@ -321,17 +349,13 @@ __DATA__
 !      $obj            @fooormore
        $obj            [sub{ref shift}]
 
-#  - works with lists instead of arrays
-       "foo"                   qw(foo bar)     TODO
-       "foo"                   ('foo','bar')   TODO
-
 #  - a regex
-       qr/x/           [qw(foo bar baz quux)]
-!      qr/y/           [qw(foo bar baz quux)]
-       /x/             [qw(foo bar baz quux)]
-!      /y/             [qw(foo bar baz quux)]
-       /FOO/i          @fooormore
-!      /bar/           @fooormore
+=      qr/x/           [qw(foo bar baz quux)]
+=!     qr/y/           [qw(foo bar baz quux)]
+=      /x/             [qw(foo bar baz quux)]
+=!     /y/             [qw(foo bar baz quux)]
+=      /FOO/i          @fooormore
+=!     /bar/           @fooormore
 
 # - a number
        2               [qw(1.00 2.00)]
@@ -367,7 +391,7 @@ __DATA__
 =      2               "2"
 =      2               "2.0"
 !      2               "2bananas"
-!=     2_3             "2_3"
+!=     2_3             "2_3"           NOWARNINGS
        FALSE           "0"
 
 # Regex against string
@@ -378,8 +402,6 @@ __DATA__
        12345           qr/3/
 !      12345           qr/7/
 
-# TODO ranges
-
 # array/hash against string
        @fooormore      "".\@fooormore
 !      @keyandmore     "".\@fooormore