Imported Upstream version 0.20
[liburi-template-perl.git] / t / 10-basic.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 28;
5
6 use_ok( 'URI::Template' );
7
8 #   new, empty template
9 {
10     my $template = URI::Template->new;
11     isa_ok( $template, 'URI::Template' );
12
13     {
14         my $result = $template->process();
15         is( $result, '', 'process() for empty template' );
16         isa_ok( $result, 'URI', 'return value from process() isa URI' );
17     }
18 }
19
20 #   "0" as a template
21 {
22     my $template = URI::Template->new( '0' );
23     isa_ok( $template, 'URI::Template' );
24     is( $template->template, '0', 'template() is "0"' );
25
26     {
27         my $result = $template->process();
28         is( $result, '0', 'process() for "0" template' );
29         isa_ok( $result, 'URI', 'return value from process() isa URI' );
30     }
31
32     # set template back to empty
33     $template->template( '' );
34     is( $template->template, '', 'template() is empty' );
35
36     {
37         my $result = $template->process();
38         is( $result, '', 'process() for new empty template' );
39         isa_ok( $result, 'URI', 'return value from process() isa URI' );
40     }
41 }
42
43 #   Update template
44 {
45     my $template = URI::Template->new;
46     is( "$template", '', 'stringify from empty' );
47
48     my $text = 'http://foo.com/{bar}/{baz}';
49     $template->template($text);
50
51     is( "$template", $text, 'stringify from updated template' );
52
53     {
54         my $result = $template->process( bar => 'x', baz => 'y' );
55         is( $result, 'http://foo.com/x/y', 'process() for updated template' );
56         isa_ok( $result, 'URI', 'return value from process() isa URI' );
57     }
58 }
59
60 {
61     my $text     = 'http://foo.com/{bar}/{baz}?q=%7B';
62     my $template = URI::Template->new( $text );
63     isa_ok( $template, 'URI::Template' );
64     is_deeply( [ sort $template->variables ], [ 'bar', 'baz' ], 'variables()' );
65     is( "$template", $text, 'stringify' );
66
67     {
68         my $result = $template->process( bar => 'x', baz => 'y' );
69         is( $result, 'http://foo.com/x/y?q=%7B', 'process()' );
70         isa_ok( $result, 'URI', 'return value from process() isa URI' );
71     }
72     {
73         my $result = $template->process_to_string( bar => 'x', baz => 'y' );
74         is( $result, 'http://foo.com/x/y?q=%7B', 'process_to_string()' );
75         ok( !ref $result, 'result is not a ref' );
76     }
77 }
78
79 {
80     my $template = URI::Template->new( 'http://foo.com/{z(}/' );
81     my $result = $template->process( 'z(' => 'x' );
82     is( $result, 'http://foo.com/x/', 'potential regex issue escaped' );
83 }
84
85 {
86     my $template = URI::Template->new( 'http://foo.com/{z}/' );
87     {
88         my $result = $template->process( 'z' => '{x}' );
89         is( $result, 'http://foo.com/%7Bx%7D/', 'values are uri escaped' );
90     }
91     {
92         my $result = $template->process();
93         is( $result, 'http://foo.com//', 'no value sent' );
94     }
95     {
96         my $result = $template->process( 'y' => '1' );
97         is( $result, 'http://foo.com//', 'no valid keys used' );
98     }
99 }
100
101 {
102     my $template = URI::Template->new( 'http://foo.com/{z}/{z}/' );
103     is_deeply( [ sort $template->variables ], [ 'z' ], 'no duplicates in variables()' );
104     my $result = $template->process( 'z' => 'x' );
105     is( $result, 'http://foo.com/x/x/', 'multiple replaces' );
106 }
107