X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=t%2F12-suite.t;h=3a763be14795c3b2fbd9cc6e1633cee8c83a8f67;hb=1d94fcb2781945132b9a29e93744b6c168e647aa;hp=1acb5b5cadd857cb5aaafc089af190e5af3bc430;hpb=c4fd5064ccd0e0c568bec68ebe82f3daf6c235b6;p=liburi-template-perl.git diff --git a/t/12-suite.t b/t/12-suite.t index 1acb5b5..3a763be 100644 --- a/t/12-suite.t +++ b/t/12-suite.t @@ -2,31 +2,77 @@ use strict; use warnings; use Test::More; +use Scalar::Util (); BEGIN { eval "use JSON ();"; plan skip_all => "JSON required" if $@; + + eval { JSON->VERSION( 2 ) }; + plan skip_all => "JSON version 2 of greater required" if $@; + plan( 'no_plan' ); use_ok( 'URI::Template' ); } -my @files = glob( 't/data/*.json' ); +my @files = glob( $ENV{ UT_SPEC_GLOB } || 't/cases/*.json' ); for my $file ( @files ) { + next unless -e $file; + + # skip these tests for now + next if $file =~ m{negative}; + open( my $json, $file ); my $data = do { local $/; <$json> }; close( $json ); - eval { JSON->VERSION( 2 ) }; - my $suite = $@ ? JSON::jsonToObj( $data ) : JSON::from_json( $data ); - my $variables = $suite->{variables}; - - my $count = 0; - for my $test (@{ $suite->{tests} }) { - my $template = URI::Template->new( $test->{template} ); - my $result = $template->process( $variables ); - $count++; - is( $result, $test->{expected}, "${file} test ${count}" ); + my $suite = JSON->new->utf8( 1 )->decode( $data ); + + for my $name ( sort keys %$suite ) { + my $info = $suite->{ $name }; + my $vars = $info->{ variables }; + my $cases = $info->{ testcases }; + + note( sprintf( '%s [level %d]', $name, ( $info->{ level } || 4 ) ) ); + + for my $case ( @$cases ) { + my ( $input, $expect ) = @$case; + my $result; + eval { + my $template = URI::Template->new( $input ); + $result = $template->process_to_string( $vars ); + }; + + _check_result( $result, $expect, $input ); + } + } } +sub _check_result { + my ( $result, $expect, $input ) = @_; + + # boolean + if ( Scalar::Util::blessed( $expect ) ) { + ok( !defined $result, $input ); + } + + # list of possible results + elsif ( ref $expect ) { + my $ok = 0; + for my $e ( @$expect ) { + if ( $result eq $e ) { + $ok = 1; + last; + } + } + diag( "got: $result" ) if !$ok; + ok( $ok, $input ); + } + + # exact comparison + else { + is( $result, $expect, $input ); + } +}