initial checkin of 0.04
[liburi-template-perl.git] / t / 10-basic.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 13;
5
6 use_ok( 'URI::Template' );
7
8 {
9     my $text     = 'http://foo.com/{bar}/{baz}?q=%7B';
10     my $template = URI::Template->new( $text );
11     isa_ok( $template, 'URI::Template' );
12     is_deeply( [ $template->variables ], [ qw( bar baz ) ], 'variables()' );
13     is( "$template", $text, 'as_string()' );
14
15     {
16         my $result = $template->process( bar => 'x', baz => 'y' );
17         is( $result, 'http://foo.com/x/y?q=%7B', 'process()' );
18         isa_ok( $result, 'URI', 'return value from process() isa URI' );
19     }
20     {
21         my $result = $template->process_to_string( bar => 'x', baz => 'y' );
22         is( $result, 'http://foo.com/x/y?q=%7B', 'process_to_string()' );
23         ok( !ref $result, 'result is not a ref' );
24     }
25 }
26
27 {
28     my $template = URI::Template->new( 'http://foo.com/{z(}/' );
29     my $result = $template->process( 'z(' => 'x' );
30     is( $result, 'http://foo.com/x/', 'potential regex issue escaped' );
31 }
32
33 {
34     my $template = URI::Template->new( 'http://foo.com/{z}/' );
35     {
36         my $result = $template->process( 'z' => '{x}' );
37         is( $result, 'http://foo.com/%7Bx%7D/', 'values are uri escaped' );
38     }
39     {
40         my $result = $template->process( );
41         is( $result, 'http://foo.com//', 'no value sent' );
42     }
43 }
44
45 {
46     my $template = URI::Template->new( 'http://foo.com/{z}/{z}/' );
47     is_deeply( [ $template->variables ], [ 'z' ], 'unique vars' );
48     my $result = $template->process( 'z' => 'x' );
49     is( $result, 'http://foo.com/x/x/', 'multiple replaces' );
50 }
51