summaryrefslogtreecommitdiffstats
path: root/t/001-basic-matching.t
blob: e45b43f0dc2f43c15411660a505044ebf56ad74f (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
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 8;

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

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

package main;
my $foo = Foo->new(a => 'foo', b => 'bar', c => 'baz');
ok($foo->match(a => 'foo', b => 'bar', c => 'baz'),
   'string matching works');
ok($foo->match(a => qr/o/),
   'regex matching works');
ok($foo->match(b => sub { length == 3 }),
   'subroutine matching works');
ok($foo->match(a => 'foo', b => qr/a/, c => sub { substr($_, 2) eq 'z' }),
   'combined matching works');
$foo = Foo->new(a => 'foo');
ok($foo->match(a => 'foo', b => undef),
   'matching against undef works');
ok($foo->match(a => 'foo', d => undef),
   'matching against undef works even when the method doesn\'t exist');
ok(!$foo->match(a => undef),
   'matching undef against a method with a value fails');
ok(!$foo->match(b => 'foo'),
   'matching against a method with an undef value fails');