Skip to content

Commit

Permalink
ref constraint tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vti committed Jan 3, 2016
1 parent a82b864 commit cc83588
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions t/match-with-constraints.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,45 @@ use Test::More;

use Routes::Tiny;

my $r = Routes::Tiny->new;
subtest 'not match when contstraint fails' => sub {
my $r = Routes::Tiny->new;

$r->add_route(
'/articles/:id',
name => 'article',
constraints => {id => qr/\d+/}
);

$r->add_route(
'/articles/:id',
name => 'article',
constraints => {id => qr/\d+/}
);
my $m = $r->match('articles/abc');
ok(!$m);
};

subtest 'match when contstraint is ok' => sub {
my $r = Routes::Tiny->new;

$r->add_route(
'/articles/:id',
name => 'article',
constraints => {id => qr/\d+/}
);

my $m = $r->match('articles/abc');
ok(!$m);
my $m = $r->match('articles/123');
is_deeply($m->params, {id => 123});
is($r->build_path('article', id => 123), '/articles/123');
};

$m = $r->match('articles/123');
is_deeply($m->params, {id => 123});
is($r->build_path('article', id => 123), '/articles/123');
subtest 'throws when building path with not passing constraint' => sub {
my $r = Routes::Tiny->new;

eval { $r->build_path('article'); };
ok($@ =~ qr/Required param 'id' was not passed when building a path/);
$r->add_route(
'/articles/:id',
name => 'article',
constraints => {id => qr/\d+/}
);

eval { $r->build_path('article', id => 'abc'); };
ok($@ =~ qr/Param 'id' fails a constraint/);
eval { $r->build_path('article', id => 'abc'); };
ok($@ =~ qr/Param 'id' fails a constraint/);
};

subtest 'contraint as array' => sub {
my $r = Routes::Tiny->new;
Expand All @@ -36,7 +55,10 @@ subtest 'contraint as array' => sub {
);

ok $r->match('/articles/1');
ok!$r->match('/articles/a');
ok !$r->match('/articles/a');

eval { $r->build_path('article', id => 'abc'); };
ok($@ =~ qr/Param 'id' fails a constraint/);
};

done_testing;

0 comments on commit cc83588

Please sign in to comment.