X-Git-Url: http://erislabs.net/gitweb/?p=liburi-template-perl.git;a=blobdiff_plain;f=t%2F12-suite.t;h=cbe2869c4b9aef0114be5cd63149731369ead070;hp=83f0e995780a5ae8dfd5d2f17cc1f6d6f4e95a0a;hb=81c7f1b85cb98976fe96ad4f8a11037b2cb3de35;hpb=0bf9e38bd59ec2d30d98cdd5b1ff6659bc776761 diff --git a/t/12-suite.t b/t/12-suite.t index 83f0e99..cbe2869 100644 --- a/t/12-suite.t +++ b/t/12-suite.t @@ -2,6 +2,7 @@ use strict; use warnings; use Test::More; +use Scalar::Util (); BEGIN { eval "use JSON ();"; @@ -10,22 +11,65 @@ BEGIN { 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 $suite = JSON::jsonToObj( do { local $/; <$json> } ); + my $data = do { local $/; <$json> }; close( $json ); - my %variables = %{ $suite->{ variables } }; + eval { JSON->VERSION( 2 ) }; + my $suite = $@ ? JSON::jsonToObj( $data ) : JSON::from_json( $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 ); + } - 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}#${count}" ); } } +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 ); + } +}