summaryrefslogtreecommitdiffstats
path: root/t/004-submatching.t
blob: 3afd43689e704fd665e79e6cc3876655658c9066 (plain) (blame)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 6;

package Bar;
use Moose;
with 'MooseX::Role::Matcher';

has [qw/a b c/] => (
    is       => 'rw',
    required => 1,
);

package Baz;
use Moose;
with 'MooseX::Role::Matcher';

has [qw/a b c/] => (
    is       => 'rw',
    required => 1,
);

package Foo;
use Moose;
with 'MooseX::Role::Matcher';

has bar => (
    is       => 'ro',
    isa      => 'Bar',
    required => 1,
);

has baz => (
    is       => 'ro',
    isa      => 'Baz',
    required => 1,
);

has [qw/a b c/] => (
    is       => 'ro',
    required => 1,
);

package main;
{
    my $bar = Bar->new(a => 1, b => 2, c => 3);
    my $baz = Baz->new(a => 'chess', b => 'go', c => 'nethack');
    my $foo = Foo->new(bar => $bar, baz => $baz,
                       a => 'foo', b => 'bar', c => 'baz');
    ok($foo->match(bar => {
                       a => 1,
                   }),
       'simple submatching works');
    ok($foo->match(bar => {
                       b    => 2,
                       '!c' => sub { $_ > 5 },
                   }),
       'simple submatching works');
    ok($foo->match(a => 'foo',
                   bar => {
                       b => qr/\d/,
                       a => sub { length == 1 },
                   },
                   baz => {
                       c => 'nethack',
                       a => qr/^c.*s$/
                   },
       ),
       'simple submatching works');
}
{
    my $bar = Bar->new(a => 4, b => 5, c => 6);
    my $baz = Baz->new(a => $bar, b => 'quux', c => 'cribbage');
    my $foo = Foo->new(a => 3.14, b => 2.72, c => 1.61,
                       bar => $bar, baz => $baz);
    ok($foo->match(a   => 3.14,
                   bar => {
                       a => 4,
                   },
                   baz => {
                       b => sub { length == 4 },
                       a => {
                           b => 5,
                           c => qr/\d/,
                       },
                   }),
       'deeper submatching works');
}
{
    my $bar = Bar->new(a => 7, b => 'tmp', c => 9);
    my $baz = Baz->new(a => $bar, b => 'quuux', c => 'tmp');
    my $foo = Foo->new(a => 256, b => 65536, c => 4294967296,
                       bar => $bar, baz => $baz);
    $bar->b($foo);
    $baz->c($baz);
    ok($foo->match(baz => {
                       c => {
                           c => {
                               c => {
                                   c => {
                                       b => 'quuux',
                                   },
                               },
                           },
                       },
                   }),
       'cyclical submatching works');
    ok($foo->match('!b' => sub { $_ % 2 },
                   bar  => {
                       a => 7,
                       b => {
                           a => qr/\d+/,
                           baz => {
                               c => {
                                   a => {
                                       a => 7,
                                       b => $foo
                                   },
                               },
                           },
                       },
                   }),
       'cyclical submatching works');
}