Imported upstream version 0.10
[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(
19         [ sort $template->variables ],
20         [ qw( bar baz ) ],
21         'variables()'
22     );
23     is( "$template", $text, 'as_string()' );
24
25     {
26         my $result = $template->process( bar => 'x', baz => 'y' );
27         is( $result, 'http://foo.com/x/y?q=%7B', 'process()' );
28         isa_ok( $result, 'URI', 'return value from process() isa URI' );
29     }
30     {
31         my $result = $template->process_to_string( bar => 'x', baz => 'y' );
32         is( $result, 'http://foo.com/x/y?q=%7B', 'process_to_string()' );
33         ok( !ref $result, 'result is not a ref' );
34     }
35 }
36
37 {
38     my $template = URI::Template->new( 'http://foo.com/{z(}/' );
39     my $result = $template->process( 'z(' => 'x' );
40     is( $result, 'http://foo.com/x/', 'potential regex issue escaped' );
41 }
42
43 {
44     my $template = URI::Template->new( 'http://foo.com/{z}/' );
45     {
46         my $result = $template->process( 'z' => '{x}' );
47         is( $result, 'http://foo.com/%7Bx%7D/', 'values are uri escaped' );
48     }
49     {
50         my $result = $template->process();
51         is( $result, 'http://foo.com//', 'no value sent' );
52     }
53     {
54         my $result = $template->process( 'y' => '1' );
55         is( $result, 'http://foo.com//', 'no valid keys used' );
56     }
57 }
58
59 {
60     my $template = URI::Template->new( 'http://foo.com/{z}/{z}/' );
61     is_deeply( [ $template->variables ], [ 'z' ], 'unique vars' );
62     my $result = $template->process( 'z' => 'x' );
63     is( $result, 'http://foo.com/x/x/', 'multiple replaces' );
64 }
65