summaryrefslogtreecommitdiff
path: root/t/op/smartmatch.t
blob: 40867e364945564d807d70130a0066a2c79f2db5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!./perl

BEGIN {
    chdir 't' if -d 't';
    require './test.pl';
    set_up_inc('../lib');
}
use strict;
use warnings;
no warnings qw(uninitialized experimental::smartmatch);

my @notov = (
    undef,
    0,
    1,
    "",
    "abc",
    *foo,
    ${qr/./},
    \undef,
    \0,
    \1,
    \"",
    \"abc",
    \*foo,
    [],
    {},
    sub { 1 },
    \*STDIN,
    bless({}, "NotOverloaded"),
);

package MatchAbc { use overload "~~" => sub { $_[1] eq "abc" }, fallback => 1; }
my $matchabc = bless({}, "MatchAbc");
my $regexpabc = qr/\Aabc\z/;

plan tests => (2+@notov)*@notov + 4*(2+@notov) + 7;

foreach my $matcher (@notov) {
    foreach my $matchee ($matchabc, $regexpabc, @notov) {
	my $res = eval { $matchee ~~ $matcher };
	like $@, qr/\ACannot smart match without a matcher object /;
    }
}
foreach my $matchee ($matchabc, $regexpabc, @notov) {
    my $res = eval { $matchee ~~ $matchabc };
    is $@, "";
    is $res, $matchee eq "abc";
    $res = eval { $matchee ~~ $regexpabc };
    is $@, "";
    is $res, $matchee eq "abc";
}

ok "abc" ~~ qr/\Aabc/;
ok "abcd" ~~ qr/\Aabc/;
ok !("xabc" ~~ qr/\Aabc/);

package MatchRef { use overload "~~" => sub { ref($_[1]) }; }
my $matchref = bless({}, "MatchRef");
package MatchThree { use overload "~~" => sub { !ref($_[1]) && $_[1] == 3 }; }
my $matchthree = bless({}, "MatchThree");

my @a = qw(x y z);
ok @a ~~ $matchthree;
ok !(@a ~~ $matchref);
my %h = qw(a b c d);
ok !(%h ~~ $matchref);
my $res = eval { "abc" ~~ %$matchabc };
like $@, qr/\ACannot smart match without a matcher object /;

1;