4 use Test::More tests => 28;
6 use_ok( 'URI::Template' );
10 my $template = URI::Template->new;
11 isa_ok( $template, 'URI::Template' );
14 my $result = $template->process();
15 is( $result, '', 'process() for empty template' );
16 isa_ok( $result, 'URI', 'return value from process() isa URI' );
22 my $template = URI::Template->new( '0' );
23 isa_ok( $template, 'URI::Template' );
24 is( $template->template, '0', 'template() is "0"' );
27 my $result = $template->process();
28 is( $result, '0', 'process() for "0" template' );
29 isa_ok( $result, 'URI', 'return value from process() isa URI' );
32 # set template back to empty
33 $template->template( '' );
34 is( $template->template, '', 'template() is empty' );
37 my $result = $template->process();
38 is( $result, '', 'process() for new empty template' );
39 isa_ok( $result, 'URI', 'return value from process() isa URI' );
45 my $template = URI::Template->new;
46 is( "$template", '', 'stringify from empty' );
48 my $text = 'http://foo.com/{bar}/{baz}';
49 $template->template($text);
51 is( "$template", $text, 'stringify from updated template' );
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' );
61 my $text = 'http://foo.com/{bar}/{baz}?{foo}=%7B&{abr}=1';
62 my $template = URI::Template->new( $text );
63 isa_ok( $template, 'URI::Template' );
64 is_deeply( [ $template->variables ], [ 'bar', 'baz', 'foo', 'abr' ], 'variables() in order of appearance' );
65 is( "$template", $text, 'stringify' );
68 my $result = $template->process( bar => 'x', baz => 'y', foo => 'b', abr => 'a' );
69 is( $result, 'http://foo.com/x/y?b=%7B&a=1', 'process()' );
70 isa_ok( $result, 'URI', 'return value from process() isa URI' );
73 my $result = $template->process_to_string( bar => 'x', baz => 'y', foo => 'b', abr => 'a' );
74 is( $result, 'http://foo.com/x/y?b=%7B&a=1', 'process_to_string()' );
75 ok( !ref $result, 'result is not a ref' );
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' );
86 my $template = URI::Template->new( 'http://foo.com/{z}/' );
88 my $result = $template->process( 'z' => '{x}' );
89 is( $result, 'http://foo.com/%7Bx%7D/', 'values are uri escaped' );
92 my $result = $template->process();
93 is( $result, 'http://foo.com//', 'no value sent' );
96 my $result = $template->process( 'y' => '1' );
97 is( $result, 'http://foo.com//', 'no valid keys used' );
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' );