4d6f404c8ddcf8d812dbb7149d26a1d6e408109b
[liburi-template-perl.git] / t / 10-basic.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 21;
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 #   Update template
21 {
22     my $template = URI::Template->new;
23     is( "$template", '', 'stringify from empty' );
24
25     my $text = 'http://foo.com/{bar}/{baz}';
26     $template->template($text);
27
28     is( "$template", $text, 'stringify from updated template' );
29
30     {
31         my $result = $template->process( bar => 'x', baz => 'y' );
32         is( $result, 'http://foo.com/x/y', 'process() for updated template' );
33         isa_ok( $result, 'URI', 'return value from process() isa URI' );
34     }
35 }
36
37 {
38     my $text     = 'http://foo.com/{bar}/{baz}?q=%7B';
39     my $template = URI::Template->new( $text );
40     isa_ok( $template, 'URI::Template' );
41     is_deeply( [ sort $template->variables ], [ 'bar', 'baz' ], 'variables()' );
42     is( "$template", $text, 'stringify' );
43
44     {
45         my $result = $template->process( bar => 'x', baz => 'y' );
46         is( $result, 'http://foo.com/x/y?q=%7B', 'process()' );
47         isa_ok( $result, 'URI', 'return value from process() isa URI' );
48     }
49     {
50         my $result = $template->process_to_string( bar => 'x', baz => 'y' );
51         is( $result, 'http://foo.com/x/y?q=%7B', 'process_to_string()' );
52         ok( !ref $result, 'result is not a ref' );
53     }
54 }
55
56 {
57     my $template = URI::Template->new( 'http://foo.com/{z(}/' );
58     my $result = $template->process( 'z(' => 'x' );
59     is( $result, 'http://foo.com/x/', 'potential regex issue escaped' );
60 }
61
62 {
63     my $template = URI::Template->new( 'http://foo.com/{z}/' );
64     {
65         my $result = $template->process( 'z' => '{x}' );
66         is( $result, 'http://foo.com/%7Bx%7D/', 'values are uri escaped' );
67     }
68     {
69         my $result = $template->process();
70         is( $result, 'http://foo.com//', 'no value sent' );
71     }
72     {
73         my $result = $template->process( 'y' => '1' );
74         is( $result, 'http://foo.com//', 'no valid keys used' );
75     }
76 }
77
78 {
79     my $template = URI::Template->new( 'http://foo.com/{z}/{z}/' );
80     is_deeply( [ sort $template->variables ], [ 'z' ], 'no duplicates in variables()' );
81     my $result = $template->process( 'z' => 'x' );
82     is( $result, 'http://foo.com/x/x/', 'multiple replaces' );
83 }
84