X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=t%2F10-basic.t;h=70514c700a4884fffb1e5bd638d5441cf103e920;hb=4a3cf69401f1cdba7219cc23ede458c2615bbd7a;hp=7dc6308f8eb7393117ebb4a55643ea41b6ffe005;hpb=90a266f52eaecdd5e15d16fe4fa3b98cb184c67c;p=liburi-template-perl.git diff --git a/t/10-basic.t b/t/10-basic.t index 7dc6308..70514c7 100644 --- a/t/10-basic.t +++ b/t/10-basic.t @@ -1,31 +1,77 @@ use strict; use warnings; -use Test::More tests => 25; +use Test::More tests => 28; use_ok( 'URI::Template' ); -# fatal - no template provided +# new, empty template { - eval { URI::Template->new; }; - ok( $@ ); + my $template = URI::Template->new; + isa_ok( $template, 'URI::Template' ); + + { + my $result = $template->process(); + is( $result, '', 'process() for empty template' ); + isa_ok( $result, 'URI', 'return value from process() isa URI' ); + } } +# "0" as a template { - my $text = 'http://foo.com/{bar}/{baz}?q=%7B'; - my $template = URI::Template->new( $text ); + my $template = URI::Template->new( '0' ); isa_ok( $template, 'URI::Template' ); - is_deeply( [ sort $template->variables ], [ qw( bar baz ) ], 'variables()' ); - is( "$template", $text, 'as_string()' ); + is( $template->template, '0', 'template() is "0"' ); + + { + my $result = $template->process(); + is( $result, '0', 'process() for "0" template' ); + isa_ok( $result, 'URI', 'return value from process() isa URI' ); + } + + # set template back to empty + $template->template( '' ); + is( $template->template, '', 'template() is empty' ); + + { + my $result = $template->process(); + is( $result, '', 'process() for new empty template' ); + isa_ok( $result, 'URI', 'return value from process() isa URI' ); + } +} + +# Update template +{ + my $template = URI::Template->new; + is( "$template", '', 'stringify from empty' ); + + my $text = 'http://foo.com/{bar}/{baz}'; + $template->template($text); + + is( "$template", $text, 'stringify from updated template' ); { my $result = $template->process( bar => 'x', baz => 'y' ); - is( $result, 'http://foo.com/x/y?q=%7B', 'process()' ); + is( $result, 'http://foo.com/x/y', 'process() for updated template' ); + isa_ok( $result, 'URI', 'return value from process() isa URI' ); + } +} + +{ + my $text = 'http://foo.com/{bar}/{baz}?{foo}=%7B&{abr}=1'; + my $template = URI::Template->new( $text ); + isa_ok( $template, 'URI::Template' ); + is_deeply( [ $template->variables ], [ 'bar', 'baz', 'foo', 'abr' ], 'variables() in order of appearance' ); + is( "$template", $text, 'stringify' ); + + { + my $result = $template->process( bar => 'x', baz => 'y', foo => 'b', abr => 'a' ); + is( $result, 'http://foo.com/x/y?b=%7B&a=1', 'process()' ); isa_ok( $result, 'URI', 'return value from process() isa URI' ); } { - my $result = $template->process_to_string( bar => 'x', baz => 'y' ); - is( $result, 'http://foo.com/x/y?q=%7B', 'process_to_string()' ); + my $result = $template->process_to_string( bar => 'x', baz => 'y', foo => 'b', abr => 'a' ); + is( $result, 'http://foo.com/x/y?b=%7B&a=1', 'process_to_string()' ); ok( !ref $result, 'result is not a ref' ); } } @@ -43,7 +89,7 @@ use_ok( 'URI::Template' ); is( $result, 'http://foo.com/%7Bx%7D/', 'values are uri escaped' ); } { - my $result = $template->process( ); + my $result = $template->process(); is( $result, 'http://foo.com//', 'no value sent' ); } { @@ -52,74 +98,9 @@ use_ok( 'URI::Template' ); } } -# test from spec -{ - my %vals = ( - a => 'fred', - b => 'barney', - c => 'cheeseburger', - d => 'one two three', - e => '20% tricky', - f => '', - 20 => 'this-is-spinal-tap', - scheme => 'https', - p => 'quote=to+be+or+not+to+be', - q => 'hullo#world', - ); - - my @urls = ( - [ ( - 'http://example.org/page1#{a}', - 'http://example.org/page1#fred', - ) ], - [ ( - 'http://example.org/{a}/{b}/', - 'http://example.org/fred/barney/', - ) ], - [ ( - 'http://example.org/{a}{b}/', - 'http://example.org/fredbarney/', - ) ], - [ ( - 'http://example.com/order/{c}/{c}/{c}/', - 'http://example.com/order/cheeseburger/cheeseburger/cheeseburger/', - ) ], - [ ( - 'http://example.org/{d}', - 'http://example.org/one%20two%20three', - ) ], - [ ( - 'http://example.org/{e}', - 'http://example.org/20%25%20tricky', - ) ], - [ ( - 'http://example.com/{f}/', - 'http://example.com//', - ) ], - [ ( - '{scheme}://{20}.example.org?date={wilma}&option={a}', - 'https://this-is-spinal-tap.example.org?date=&option=fred', - ) ], - [ ( - 'http://example.org?{p}', - 'http://example.org?quote=to+be+or+not+to+be', - ) ], - [ ( - 'http://example.com/{q}', - 'http://example.com/hullo#world', - ) ], - ); - - for my $list ( @urls ) { - my $template = URI::Template->new( $list->[ 0 ] ); - my $result = $template->process( %vals ); - is( $result, $list->[ 1 ], 'escaped properly' ); - } -} - { my $template = URI::Template->new( 'http://foo.com/{z}/{z}/' ); - is_deeply( [ $template->variables ], [ 'z' ], 'unique vars' ); + is_deeply( [ sort $template->variables ], [ 'z' ], 'no duplicates in variables()' ); my $result = $template->process( 'z' => 'x' ); is( $result, 'http://foo.com/x/x/', 'multiple replaces' ); }