Implement Hash/Array ~~ Regex (with tests)
[p5sagit/p5-mst-13.2.git] / t / op / smartmatch.t
index 75c0ec0..eb14bf0 100644 (file)
@@ -13,6 +13,7 @@ use Tie::Hash;
 # Predeclare vars used in the tests:
 my @empty;
 my %empty;
+my @sparse; $sparse[2] = 2;
 
 my $deep1 = []; push @$deep1, \$deep1;
 my $deep2 = []; push @$deep2, \$deep2;
@@ -31,12 +32,14 @@ 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 { "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;
 
 my @keyandmore = qw(key and more);
@@ -166,7 +169,9 @@ __DATA__
 @      FALSE           $obj
 
 # object (overloaded or not) ~~ Any
-# TODO
+       $obj            qr/NoOverload/
+       $ov_obj         qr/^stringified$/
+       $ov_obj         "stringified"
 
 # ~~ Coderef
        sub{0}          sub { ref $_[0] eq "CODE" }
@@ -200,6 +205,8 @@ __DATA__
        qr//            \&bar
 !      [1]             \&foo
 !      {a=>1}          \&foo
+       $obj            sub { ref($_[0]) =~ /NoOverload/ }
+       $ov_obj         sub { ref($_[0]) =~ /WithOverload/ }
 # empty stuff matches, because the sub is never called:
        []              \&foo
        {}              \&foo
@@ -259,16 +266,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}
@@ -294,8 +300,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"]]
@@ -312,20 +318,24 @@ __DATA__
 =      \@nums                  @tied_nums
 =      @nums                   @tied_nums
 
+#  - an object
+!      $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(1foo 2bar)]
+       2               [qw(1.00 2.00)]
        2               [qw(foo 2)]
        2.0_0e+0        [qw(foo 2)]
 !      2               [qw(1foo bar2)]
@@ -334,6 +344,18 @@ __DATA__
 !      "2"             [qw(1foo 2bar)]
        "2bar"          [qw(1foo 2bar)]
 
+# - undef
+       undef           [1, 2, undef, 4]
+!      undef           [1, 2, [undef], 4]
+!      undef           @fooormore
+       undef           @sparse
+
+# - nested arrays and ~~ distributivity
+       11              [[11]]
+!      11              [[12]]
+       "foo"           [{foo => "bar"}]
+!      "bar"           [{foo => "bar"}]
+
 # Number against number
        2               2
        20              2_0