From: Robert Sedlacek Date: Tue, 20 Dec 2011 23:03:23 +0000 (+0100) Subject: more complete error handling and error testing X-Git-Tag: v1.000000~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=203d81fc3c1966d9a28a96987ccf49c2b7bfde97;p=p5sagit%2FPackage-Variant.git more complete error handling and error testing --- diff --git a/lib/Package/Variant.pm b/lib/Package/Variant.pm index bd39d4f..977f76c 100644 --- a/lib/Package/Variant.pm +++ b/lib/Package/Variant.pm @@ -9,26 +9,36 @@ my $sanitize_importing = sub { my ($me, $spec) = @_; return [] unless defined $spec; - return [map { - my $import_args = $spec->{$_}; - croak sprintf q{Import argument list for '%s' is not an array ref}, - $_, - unless ref($import_args) and ref($import_args) eq 'ARRAY'; - [$_ => $import_args]; - } keys %$spec] - if ref $spec eq 'HASH'; - croak q{The 'importing' option has to be either a hash or array ref} - unless ref $spec eq 'ARRAY'; - my @specced = @$spec; + my @specced = + not(ref $spec) + ? ($spec) + : (ref($spec) eq 'ARRAY') + ? (@$spec) + : (ref($spec) eq 'HASH') + ? (map { + croak qq{The import argument list for '$_' is not an array ref} + unless ref($spec->{$_}) eq 'ARRAY'; + ($_ => $spec->{$_}); + } sort keys %$spec) + : croak q{The 'importing' option has to be either a hash or array ref}; my @imports; + my $arg_count = 1; while (@specced) { my $key = shift @specced; - push @imports, [ - $key, - (ref($specced[0]) and ref($specced[0]) eq 'ARRAY') - ? shift(@specced) - : [], - ]; + croak qq{Value $arg_count in 'importing' is not a package string}, + $arg_count + unless defined($key) and not(ref $key); + $arg_count++; + my $import_args = + (not(@specced) or (defined($specced[0]) and not ref($specced[0]))) + ? [] + : (ref($specced[0]) eq 'ARRAY') + ? do { $arg_count++; shift @specced } + : croak( + qq{Value $arg_count for package '$key' in 'importing' is not} + . qq{ a package string or array ref} + ); + push @imports, [$key, $import_args]; } return \@imports; }; diff --git a/t/01simple.t b/t/01simple.t index e41d27f..2a060cb 100644 --- a/t/01simple.t +++ b/t/01simple.t @@ -85,6 +85,21 @@ TestArrayImports(23); is_deeply [@imported], [qw( TestImportableA TestImportableB )], 'multiple imports in the right order'; +BEGIN { + package TestSingleImport; + use Package::Variant importing => 'TestImportableA'; + sub make_variant { } + $INC{'TestSingleImport.pm'} = __FILE__; +} + +@imported = (); + +use TestSingleImport; +TestSingleImport(23); + +is_deeply [@imported], [qw( TestImportableA )], + 'scalar import works'; + like exception { Package::Variant->import( importing => \'foo', subs => [qw( foo )], @@ -95,6 +110,18 @@ like exception { Package::Variant->import( importing => { foo => \'bar' }, subs => [qw( bar )], ); -}, qr/import.+argument.+not.+array/i, 'invalid import argument list'; +}, qr/import.+argument.+foo.+not.+array/i, 'invalid import argument list'; + +like exception { + Package::Variant->import( + importing => [ foo => ['bar'], ['bam'], subs => [qw( bar )] ], + ); +}, qr/value.+3.+importing.+not.+string/i, 'importing array invalid key'; + +like exception { + Package::Variant->import( + importing => [ foo => \'bam', subs => [qw( bar )] ], + ); +}, qr/value.+2.+foo.+importing.+array/i, 'importing array invalid list'; done_testing;