From: Vincent Pit Date: Thu, 19 Mar 2015 18:06:04 +0000 (-0300) Subject: Add optional features support X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=7bb937cb8a28af2e5de0d7d23e1e946cb5892536;p=perl%2Fmodules%2FVPIT-TestHelpers.git Add optional features support --- diff --git a/lib/VPIT/TestHelpers.pm b/lib/VPIT/TestHelpers.pm index 023a873..f448074 100644 --- a/lib/VPIT/TestHelpers.pm +++ b/lib/VPIT/TestHelpers.pm @@ -16,17 +16,42 @@ sub export_to_pkg { return 1; } -my %exports = ( +my %default_exports = ( load_or_skip => \&load_or_skip, load_or_skip_all => \&load_or_skip_all, run_perl => \&run_perl, skip_all => \&skip_all, ); +my %features = (); + sub import { - my $pkg = caller; + shift; + my @opts = @_; + + my %exports = %default_exports; + + for (my $i = 0; $i <= $#opts; ++$i) { + my $feature = $opts[$i]; + next unless defined $feature; + + my $args; + if ($i < $#opts and defined $opts[$i+1] and ref $opts[$i+1] eq 'ARRAY') { + ++$i; + $args = $opts[$i]; + } else { + $args = [ ]; + } + + my $handler = $features{$feature}; + die "Unknown feature '$feature'" unless defined $handler; + + my %syms = $handler->(@$args); + + $exports{$_} = $syms{$_} for sort keys %syms; + } - export_to_pkg \%exports => $pkg; + export_to_pkg \%exports => scalar caller; } my $test_sub = sub {