Make C<undef ~~ 0> and C<undef ~~ ""> not match (like in 5.10.0)
[p5sagit/p5-mst-13.2.git] / t / op / smartmatch.t
index 5dfebbd..cb0e656 100644 (file)
@@ -35,15 +35,31 @@ tie my %tied_hash, 'Tie::StdHash';
 }
 
 {
+    package Test::Object::StringOverload;
+    use overload '""' => sub { "object" }, fallback => 1;
+    sub new { bless { key => 1 } }
+}
+
+{
     package Test::Object::WithOverload;
-    sub new { bless { key => 'magic' } }
-    use overload '~~' => sub { my %hash = %{ $_[0] }; $_[1] eq $hash{key} };
+    sub new { bless { key => ($_[1] // 'magic') } }
+    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::WithOverload->new;
+our $ov_obj_2 = Test::Object::WithOverload->new("object");
 our $obj = Test::Object::NoOverload->new;
+our $str_obj = Test::Object::StringOverload->new;
 
 tie my %refh, 'Tie::RefHash';
 $refh{$ov_obj} = 1;
@@ -54,7 +70,7 @@ my %keyandmore = map { $_ => 0 } @keyandmore;
 my %fooormore = map { $_ => 0 } @fooormore;
 
 # Load and run the tests
-plan "no_plan";
+plan tests => 322;
 
 while (<DATA>) {
     next if /^#/ || !/\S/;
@@ -158,15 +174,19 @@ __DATA__
 
 # Any ~~ object overloaded
 !      \&fatal         $ov_obj
-       'magic'         $ov_obj
-!      'not magic'     $ov_obj
+       'cigam'         $ov_obj
+!      'cigam on'      $ov_obj
+!      ['cigam']       $ov_obj
+!      ['stringified'] $ov_obj
+!      { cigam => 1 }  $ov_obj
+!      { stringified => 1 }    $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
@@ -180,10 +200,34 @@ __DATA__
 @      "key"           $obj
 @      FALSE           $obj
 
+# regular object with "" overload
+@      $obj            $str_obj
+=@     \&fatal         $str_obj
+@      \&FALSE         $str_obj
+@      \&foo           $str_obj
+@      sub { 1 }       $str_obj
+@      sub { 0 }       $str_obj
+@      %keyandmore     $str_obj
+@      {"object" => 1} $str_obj
+@      @fooormore      $str_obj
+@      ["object" => 1] $str_obj
+@      /object/        $str_obj
+@      qr/object/      $str_obj
+@      "object"        $str_obj
+@      FALSE           $str_obj
+# Those will treat the $str_obj as a string because of fallback:
+!      $ov_obj         $str_obj
+       $ov_obj_2       $str_obj
+
 # object (overloaded or not) ~~ Any
        $obj            qr/NoOverload/
        $ov_obj         qr/^stringified$/
-       $ov_obj         "stringified"
+=      "$ov_obj"       "stringified"
+=      "$str_obj"      "object"
+!=     $ov_obj         "stringified"
+       $str_obj        "object"
+       $ov_obj         'magic'
+!      $ov_obj         'not magic'
 
 # ~~ Coderef
        sub{0}          sub { ref $_[0] eq "CODE" }
@@ -238,6 +282,9 @@ __DATA__
        +{}             \&fatal
        @empty          \&fatal
        %empty          \&fatal
+# sub is not special on the left
+       sub {0}         qr/^CODE/
+       sub {0}         sub { ref shift eq "CODE" }
 
 # HASH ref against:
 #   - another hash ref
@@ -324,7 +371,7 @@ __DATA__
        ["foo", "bar"]          [["foo"], ["bar"]]
 !      ["foo", "bar"]          [qr/o/, "foo"]
        ["foo", undef, "bar"]   [qr/o/, undef, "bar"]
-       ["foo", undef, "bar"]   [qr/o/, "",    "bar"]
+!      ["foo", undef, "bar"]   [qr/o/, "",    "bar"]
 !      ["foo", "", "bar"]      [qr/o/, undef, "bar"]
        $deep1                  $deep1
        @$deep1                 @$deep1
@@ -362,6 +409,11 @@ __DATA__
 !      undef           [1, 2, [undef], 4]
 !      undef           @fooormore
        undef           @sparse
+       undef           [undef]
+!      0               [undef]
+!      ""              [undef]
+!      undef           [0]
+!      undef           [""]
 
 # - nested arrays and ~~ distributivity
        11              [[11]]
@@ -375,7 +427,8 @@ __DATA__
 !      2               3
        0               FALSE
        3-2             TRUE
-       undef           0
+!      undef           0
+!      (my $u)         0
 
 # Number against string
 =      2               "2"
@@ -383,6 +436,8 @@ __DATA__
 !      2               "2bananas"
 !=     2_3             "2_3"           NOWARNINGS
        FALSE           "0"
+!      undef           "0"
+!      undef           ""
 
 # Regex against string
        "x"             qr/x/