From cc83588695d8d1c7c1db2b11dac3225c0b92541f Mon Sep 17 00:00:00 2001 From: vti Date: Sun, 3 Jan 2016 17:27:37 +0100 Subject: [PATCH] ref constraint tests --- t/match-with-constraints.t | 54 +++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/t/match-with-constraints.t b/t/match-with-constraints.t index b4ba0f8..3b3b64a 100644 --- a/t/match-with-constraints.t +++ b/t/match-with-constraints.t @@ -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; @@ -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;