Imported Upstream version 0.17
[liburi-template-perl.git] / t / 12-suite.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Scalar::Util ();
6
7 BEGIN {
8     eval "use JSON ();";
9     plan skip_all => "JSON required" if $@;
10
11     eval { JSON->VERSION( 2 ) };
12     plan skip_all => "JSON version 2 of greater required" if $@;
13
14     plan( 'no_plan' );
15     use_ok( 'URI::Template' );
16 }
17
18 my @files = glob( $ENV{ UT_SPEC_GLOB } || 't/cases/*.json' );
19
20 for my $file ( @files ) {
21     next unless -e $file;
22
23     # skip these tests for now
24     next if $file =~ m{negative};
25
26     open( my $json, $file );
27     my $data = do { local $/; <$json> };
28     close( $json );
29
30     my $suite = JSON->new->utf8( 1 )->decode( $data );
31
32     for my $name ( sort keys %$suite ) {
33         my $info  = $suite->{ $name };
34         my $vars  = $info->{ variables };
35         my $cases = $info->{ testcases };
36
37         note( sprintf( '%s [level %d]', $name, ( $info->{ level } || 4 ) ) );
38
39         for my $case ( @$cases ) {
40             my ( $input, $expect ) = @$case;
41             my $result;
42             eval {
43                 my $template = URI::Template->new( $input );
44                 $result = $template->process_to_string( $vars );
45             };
46
47             _check_result( $result, $expect, $input );
48         }
49
50     }
51 }
52
53 sub _check_result {
54     my ( $result, $expect, $input ) = @_;
55
56     # boolean
57     if ( Scalar::Util::blessed( $expect ) ) {
58         ok( !defined $result, $input );
59     }
60
61     # list of possible results
62     elsif ( ref $expect ) {
63         my $ok = 0;
64         for my $e ( @$expect ) {
65             if ( $result eq $e ) {
66                 $ok = 1;
67                 last;
68             }
69         }
70         diag( "got: $result" ) if !$ok;
71         ok( $ok, $input );
72     }
73
74     # exact comparison
75     else {
76         is( $result, $expect, $input );
77     }
78 }