From: Vincent Pit Date: Fri, 8 Apr 2011 15:58:57 +0000 (+0200) Subject: Fix the handling of the "import" import argument X-Git-Tag: v0.03~12 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=7b03c18e358942c3388e3ba4d93319333a25b9cf;p=perl%2Fmodules%2FTest-Leaner.git Fix the handling of the "import" import argument And test it for Test::Leaner and for the Test::More fallback interface. --- diff --git a/MANIFEST b/MANIFEST index 7b33b2e..651d18a 100644 --- a/MANIFEST +++ b/MANIFEST @@ -7,7 +7,9 @@ lib/Test/Leaner.pm samples/bench.pl t/00-load.t t/01-import.t -t/02-fallback.t +t/02-import-arg.t +t/03-fallback.t +t/04-fallback-import-arg.t t/05-pass.t t/06-fail.t t/07-BAIL_OUT.t diff --git a/lib/Test/Leaner.pm b/lib/Test/Leaner.pm index 7253184..8e45f3c 100644 --- a/lib/Test/Leaner.pm +++ b/lib/Test/Leaner.pm @@ -165,14 +165,13 @@ if ($ENV{PERL_TEST_LEANER_USES_TEST_MORE}) { my $leaner_stash = \%Test::Leaner::; my $more_stash = \%Test::More::; - my %valid_imports; + my %stubbed; for (@EXPORT) { my $replacement = exists $more_stash->{$_} ? *{$more_stash->{$_}}{CODE} : undef; - if (defined $replacement) { - $valid_imports{$_} = 1; - } else { + unless (defined $replacement) { + $stubbed{$_}++; $replacement = sub { @_ = ("$_ is not implemented in this version of Test::More"); goto &croak; @@ -186,15 +185,15 @@ if ($ENV{PERL_TEST_LEANER_USES_TEST_MORE}) { shift; my @imports = &_handle_import_args; - @imports = @EXPORT unless @imports; + @imports = @EXPORT unless @imports; my @test_more_imports; for (@imports) { - if ($valid_imports{$_}) { - push @test_more_imports, $_; - } else { + if ($stubbed{$_}) { my $pkg = caller; no strict 'refs'; *{$pkg."::$_"} = $leaner_stash->{$_}; + } else { + push @test_more_imports, $_; } } diff --git a/t/02-import-arg.t b/t/02-import-arg.t new file mode 100644 index 0000000..fd6f0f9 --- /dev/null +++ b/t/02-import-arg.t @@ -0,0 +1,121 @@ +#!perl -T + +use strict; +use warnings; + +BEGIN { delete $ENV{PERL_TEST_LEANER_USES_TEST_MORE} } + +use Test::Leaner (); +use Test::More (); + +sub get_subroutine { + my ($stash, $name) = @_; + + my $glob = $stash->{$name}; + return undef unless $glob; + + return *$glob{CODE}; +} + +my $this_stash = \%main::; + +my @default_exports = qw< + plan + skip + done_testing + pass + fail + ok + is + isnt + like + unlike + cmp_ok + is_deeply + diag + note + BAIL_OUT +>; + +sub check_imports { + my %imported = map { $_ => 1 } @{ $_[0] || [] }; + my @not_imported = @{ $_[1] || [] }; + for (@not_imported, grep !$imported{$_}, @default_exports) { + Test::More::ok(!exists $this_stash->{$_}, "$_ was not imported"); + } + for (grep $imported{$_}, @default_exports) { + my $code = get_subroutine($this_stash, $_); + Test::More::ok($code, "$_ was imported"); + } + delete $this_stash->{$_} for @default_exports, keys %imported, @not_imported; +} + +Test::More::plan(tests => 8 * @default_exports + 7 + 2); + +check_imports(); + +{ + local $@; + eval { + Test::Leaner->import(import => [ ]); + }; + Test::More::is($@, '', 'empty import does not croak'); + check_imports(\@default_exports); +} + +{ + local $@; + eval { + Test::Leaner->import(import => [ 'nonexistent' ]); + }; + Test::More::like($@, qr/^"nonexistent" is not exported by the Test::Leaner module/, 'import "nonexistent" croaks'); + check_imports([ ], [ 'nonexistent' ]); +} + +{ + local $@; + eval { + Test::Leaner->import(import => [ 'ok' ]); + }; + Test::More::is($@, '', 'import "ok" does not croak'); + check_imports([ 'ok' ], [ ]); +} + +{ + local $@; + eval { + Test::Leaner->import( + import => [ qw ], + import => [ qw ], + ); + }; + Test::More::is($@, '', 'import "like", "unlike", "diag" and "note" does not croak'); + check_imports([ qw ], [ ]); +} + +{ + local $@; + eval { + Test::Leaner->import(import => [ '!fail' ]); + }; + Test::More::is($@, '', 'import "!fail" does not croak'); + check_imports([ grep $_ ne 'fail', @default_exports ], [ 'fail' ]); +} + +{ + local $@; + eval { + Test::Leaner->import(import => [ 'pass' ], import => [ '!fail' ]); + }; + Test::More::is($@, '', 'import "pass", "!fail" does not croak'); + check_imports([ 'pass' ], [ ]); +} + +{ + local $@; + eval { + Test::Leaner->import(import => [ 'fail' ], import => [ '!fail' ]); + }; + Test::More::is($@, '', 'import "fail", "!fail" does not croak'); + check_imports(); +} diff --git a/t/02-fallback.t b/t/03-fallback.t similarity index 100% rename from t/02-fallback.t rename to t/03-fallback.t diff --git a/t/04-fallback-import-arg.t b/t/04-fallback-import-arg.t new file mode 100644 index 0000000..ee2bbd5 --- /dev/null +++ b/t/04-fallback-import-arg.t @@ -0,0 +1,121 @@ +#!perl -T + +use strict; +use warnings; + +BEGIN { $ENV{PERL_TEST_LEANER_USES_TEST_MORE} = 1 } + +use Test::Leaner (); +use Test::More (); + +sub get_subroutine { + my ($stash, $name) = @_; + + my $glob = $stash->{$name}; + return undef unless $glob; + + return *$glob{CODE}; +} + +my $this_stash = \%main::; + +my @default_exports = qw< + plan + skip + done_testing + pass + fail + ok + is + isnt + like + unlike + cmp_ok + is_deeply + diag + note + BAIL_OUT +>; + +sub check_imports { + my %imported = map { $_ => 1 } @{ $_[0] || [] }; + my @not_imported = @{ $_[1] || [] }; + for (@not_imported, grep !$imported{$_}, @default_exports) { + Test::More::ok(!exists $this_stash->{$_}, "$_ was not imported"); + } + for (grep $imported{$_}, @default_exports) { + my $code = get_subroutine($this_stash, $_); + Test::More::ok($code, "$_ was imported"); + } + delete $this_stash->{$_} for @default_exports, keys %imported, @not_imported; +} + +Test::More::plan(tests => 8 * @default_exports + 7 + 2); + +check_imports(); + +{ + local $@; + eval { + Test::Leaner->import(import => [ ]); + }; + Test::More::is($@, '', 'empty import does not croak'); + check_imports(\@default_exports); +} + +{ + local $@; + eval { + Test::Leaner->import(import => [ 'nonexistent' ]); + }; + Test::More::like($@, qr/^"nonexistent" is not exported by the Test::More module/, 'import "nonexistent" croaks'); + check_imports([ ], [ 'nonexistent' ]); +} + +{ + local $@; + eval { + Test::Leaner->import(import => [ 'ok' ]); + }; + Test::More::is($@, '', 'import "ok" does not croak'); + check_imports([ 'ok' ], [ ]); +} + +{ + local $@; + eval { + Test::Leaner->import( + import => [ qw ], + import => [ qw ], + ); + }; + Test::More::is($@, '', 'import "like", "unlike", "diag" and "note" does not croak'); + check_imports([ qw ], [ ]); +} + +{ + local $@; + eval { + Test::Leaner->import(import => [ '!fail' ]); + }; + Test::More::is($@, '', 'import "!fail" does not croak'); + check_imports([ grep $_ ne 'fail', @default_exports ], [ 'fail' ]); +} + +{ + local $@; + eval { + Test::Leaner->import(import => [ 'pass' ], import => [ '!fail' ]); + }; + Test::More::is($@, '', 'import "pass", "!fail" does not croak'); + check_imports([ 'pass' ], [ ]); +} + +{ + local $@; + eval { + Test::Leaner->import(import => [ 'fail' ], import => [ '!fail' ]); + }; + Test::More::is($@, '', 'import "fail", "!fail" does not croak'); + check_imports(); +}