Imported Upstream version 0.14.01
[liburi-template-perl.git] / t / 10-basic.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 15;
5
6 use_ok( 'URI::Template' );
7
8 # fatal - no template provided
9 {
10     eval { URI::Template->new; };
11     ok( $@ );
12 }
13
14 {
15     my $text     = 'http://foo.com/{bar}/{baz}?q=%7B';
16     my $template = URI::Template->new( $text );
17     isa_ok( $template, 'URI::Template' );
18     is_deeply( [ sort $template->variables ], [ 'bar', 'baz' ], 'variables()' );
19     is( "$template", $text, 'stringify' );
20
21     {
22         my $result = $template->process( bar => 'x', baz => 'y' );
23         is( $result, 'http://foo.com/x/y?q=%7B', 'process()' );
24         isa_ok( $result, 'URI', 'return value from process() isa URI' );
25     }
26     {
27         my $result = $template->process_to_string( bar => 'x', baz => 'y' );
28         is( $result, 'http://foo.com/x/y?q=%7B', 'process_to_string()' );
29         ok( !ref $result, 'result is not a ref' );
30     }
31 }
32
33 {
34     my $template = URI::Template->new( 'http://foo.com/{z(}/' );
35     my $result = $template->process( 'z(' => 'x' );
36     is( $result, 'http://foo.com/x/', 'potential regex issue escaped' );
37 }
38
39 {
40     my $template = URI::Template->new( 'http://foo.com/{z}/' );
41     {
42         my $result = $template->process( 'z' => '{x}' );
43         is( $result, 'http://foo.com/%7Bx%7D/', 'values are uri escaped' );
44     }
45     {
46         my $result = $template->process();
47         is( $result, 'http://foo.com//', 'no value sent' );
48     }
49     {
50         my $result = $template->process( 'y' => '1' );
51         is( $result, 'http://foo.com//', 'no valid keys used' );
52     }
53 }
54
55 {
56     my $template = URI::Template->new( 'http://foo.com/{z}/{z}/' );
57     is_deeply( [ sort $template->variables ], [ 'z' ], 'no duplicates in variables()' );
58     my $result = $template->process( 'z' => 'x' );
59     is( $result, 'http://foo.com/x/x/', 'multiple replaces' );
60 }
61