Implement distributivity in $scalar ~~ @array
[p5sagit/p5-mst-13.2.git] / t / op / smartmatch.t
index a1c1a06..2c6e5f1 100644 (file)
@@ -11,6 +11,10 @@ use Tie::Array;
 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;
 
@@ -114,7 +118,7 @@ sub NOT_DEF() { undef }
 #   Values returned by a sub call
 __DATA__
 # Any ~~ undef
-!=     $ov_obj         undef
+!      $ov_obj         undef
 !      $obj            undef
 !      sub {}          undef
 !      %hash           undef
@@ -139,27 +143,31 @@ __DATA__
        &NOT_DEF        undef
 
 # Any ~~ object overloaded
-# object overloaded ~~ Any
-=!     $ov_obj         \&fatal
-=      $ov_obj         'magic'
-=!     $ov_obj         'not magic'
-=!     $ov_obj         $obj
+!      \&fatal         $ov_obj
+       'magic'         $ov_obj
+!      'not magic'     $ov_obj
+!      $obj            $ov_obj
+!      undef           $ov_obj
 
 # regular object
-@      $obj    $obj
-=@     $obj    \&fatal
-=@     $obj    \&FALSE
-=@     $obj    \&foo
-=@     $obj    sub { 1 }
-=@     $obj    sub { 0 }
-=@     $obj    %keyandmore
-=@     $obj    {"key" => 1}
-=@     $obj    @fooormore
-=@     $obj    ["key" => 1]
-=@     $obj    /key/
-=@     $obj    qr/key/
-=@     $obj    "key"
-=@     $obj    FALSE
+@      $obj            $obj
+@      $ov_obj         $obj
+@      \&fatal         $obj
+@      \&FALSE         $obj
+@      \&foo           $obj
+@      sub { 1 }       $obj
+@      sub { 0 }       $obj
+@      %keyandmore     $obj
+@      {"key" => 1}    $obj
+@      @fooormore      $obj
+@      ["key" => 1]    $obj
+@      /key/           $obj
+@      qr/key/         $obj
+@      "key"           $obj
+@      FALSE           $obj
+
+# object (overloaded or not) ~~ Any
+# TODO
 
 # ~~ Coderef
        sub{0}          sub { ref $_[0] eq "CODE" }
@@ -185,15 +193,21 @@ __DATA__
 !      0               sub{shift}
 !      undef           sub{shift}
        undef           sub{not shift}
+       NOT_DEF         sub{not shift}
+       &NOT_DEF        sub{not shift}
        FALSE           sub{not shift}
        [1]             \&bar
        {a=>1}          \&bar
        qr//            \&bar
 !      [1]             \&foo
 !      {a=>1}          \&foo
+       $obj            sub { ref $_[0] =~ /NoOverload/ }       TODO
+       $ov_obj         sub { ref $_[0] =~ /CopyOverload/ }     TODO
 # empty stuff matches, because the sub is never called:
        []              \&foo
        {}              \&foo
+       @empty          \&foo
+       %empty          \&foo
 !      qr//            \&foo
 !      undef           \&foo
        undef           \&bar
@@ -206,6 +220,8 @@ __DATA__
 # sub is not called on empty hashes / arrays
        []              \&fatal
        +{}             \&fatal
+       @empty          \&fatal
+       %empty          \&fatal
 
 # HASH ref against:
 #   - another hash ref
@@ -213,36 +229,63 @@ __DATA__
 =!     {}              {1 => 2}
        {1 => 2}        {1 => 2}
        {1 => 2}        {1 => 3}
-!      {1 => 2}        {2 => 3}
-       \%main::        {map {$_ => 'x'} keys %main::}
+=!     {1 => 2}        {2 => 3}
+=      \%main::        {map {$_ => 'x'} keys %main::}
 
 #  - tied hash ref
 =      \%hash          \%tied_hash
        \%tied_hash     \%tied_hash
+!=     {"a"=>"b"}      \%tied_hash
+=      %hash           %tied_hash
+       %tied_hash      %tied_hash
+!=     {"a"=>"b"}      %tied_hash
 
 #  - an array ref
-       [keys %main::]  \%::
-!      []              \%::
-!      [""]            {}
-!      []              {}
-       [undef]         {"" => 1}
-       [""]            {"" => 1}
-       ["foo"]         { foo => 1 }
-       ["foo", "bar"]  { foo => 1 }
-       ["foo", "bar"]  \%hash
-       ["foo"]         \%hash
-!      ["quux"]        \%hash
-       [qw(foo quux)]  \%hash
+#  (since this is symmetrical, tests as well hash~~array)
+=      [keys %main::]  \%::
+=      [qw[STDIN STDOUT]]      \%::
+=!     []              \%::
+=!     [""]            {}
+=!     []              {}
+=!     @empty          {}
+=      [undef]         {"" => 1}
+=      [""]            {"" => 1}
+=      ["foo"]         { foo => 1 }
+=      ["foo", "bar"]  { foo => 1 }
+=      ["foo", "bar"]  \%hash
+=      ["foo"]         \%hash
+=!     ["quux"]        \%hash
+=      [qw(foo quux)]  \%hash
+=      @fooormore      { foo => 1, or => 2, more => 3 }
+=      @fooormore      %fooormore
+=      @fooormore      \%fooormore
+=      \@fooormore     %fooormore
 
 #  - a regex
+# TODO those should be symmetrical
        qr/^(fo[ox])$/          {foo => 1}
-!      qr/[13579]$/            +{0..99}
+       /^(fo[ox])$/            %fooormore
+=!     qr/[13579]$/            +{0..99}
 !      qr/a*/                  {}
-       qr/a*/                  {b=>2}
+=      qr/a*/                  {b=>2}
+       qr/B/i                  {b=>2}
+       /B/i                    {b=>2}
+!      qr/a+/                  {b=>2}
+       qr/^à/                 {"à"=>2}
 
-#  - a string
+#  - a scalar
        "foo"           +{foo => 1, bar => 2}
+       "foo"           %fooormore
 !      "baz"           +{foo => 1, bar => 2}
+!      "boz"           %fooormore
+!      1               +{foo => 1, bar => 2}
+!      1               %fooormore
+       1               { 1 => 3 }
+       1.0             { 1 => 3 }
+!      "1.0"           { 1 => 3 }
+!      "1.0"           { 1.0 => 3 }
+       "1.0"           { "1.0" => 3 }
+       "à"            { "à" => "À" }
 
 #  - undef
 !      undef           { hop => 'zouu' }
@@ -257,24 +300,39 @@ __DATA__
 !      [["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"]]
 !      ["foo", "bar"]          [qr/o/, "foo"]
        ["foo", undef, "bar"]   [qr/o/, undef, "bar"]
        ["foo", undef, "bar"]   [qr/o/, "",    "bar"]
 !      ["foo", "", "bar"]      [qr/o/, undef, "bar"]
        $deep1                  $deep1
+       @$deep1                 @$deep1
 !      $deep1                  $deep2
 
-       \@nums                  \@tied_nums
+=      \@nums                  \@tied_nums
+=      @nums                   \@tied_nums
+=      \@nums                  @tied_nums
+=      @nums                   @tied_nums
+
+#  - an object
+!      $obj            @fooormore
+       $obj            [sub{ref shift}]        TODO
+
+#  - 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
 
 # - 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)]
@@ -283,6 +341,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
@@ -306,7 +376,9 @@ __DATA__
        12345           qr/3/
 !      12345           qr/7/
 
-# array against string
+# TODO ranges
+
+# array/hash against string
        @fooormore      "".\@fooormore
 !      @keyandmore     "".\@fooormore
        %fooormore      "".\%fooormore