From: Vincent Pit Date: Mon, 18 Apr 2011 23:47:39 +0000 (+0200) Subject: Factor t/02-import-arg.t and t/04-fallback-import-arg.t into the same file X-Git-Tag: v0.03~4 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=00cdc2b34a6be344a34ca4ac2f56f0238a88b195;p=perl%2Fmodules%2FTest-Leaner.git Factor t/02-import-arg.t and t/04-fallback-import-arg.t into the same file --- diff --git a/MANIFEST b/MANIFEST index 651d18a..b438d69 100644 --- a/MANIFEST +++ b/MANIFEST @@ -36,3 +36,4 @@ t/92-pod-coverage.t t/95-portability-files.t t/99-kwalitee.t t/lib/Test/Leaner/TestHelper.pm +t/lib/Test/Leaner/TestImport.pm diff --git a/t/02-import-arg.t b/t/02-import-arg.t index 46f2cc1..6470c83 100644 --- a/t/02-import-arg.t +++ b/t/02-import-arg.t @@ -5,162 +5,7 @@ use warnings; BEGIN { delete $ENV{PERL_TEST_LEANER_USES_TEST_MORE} } -use Carp (); +use lib 't/lib'; +use Test::Leaner::TestImport; -use Test::Leaner (); -use Test::More (); - -sub get_subroutine { - my ($stash, $name) = @_; - - my $glob = $stash->{$name}; - return undef unless $glob; - - return *$glob{CODE}; -} - -sub has_module_version { - my ($module, $version) = @_; - - local $@; - eval qq{ - require $module; - "$module"->VERSION(\$version); - 1; - } -} - -sub has_test_more_version { has_module_version 'Test::More', @_ } -sub has_exporter_version { has_module_version 'Exporter', @_ } - -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] || [] }; - -SKIP: - { - local $Test::Builder::Level = ($Test::Builder::Level || 0) + 1; - Test::More::skip($_[2] => @not_imported + @default_exports) if defined $_[2]; - - 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 => 9 * @default_exports + 8 + 3); - -check_imports([ ], [ ], has_test_more_version('0.51') ? undef : 'Test::More::plan exports stuff on Test::More <= 0.51'); - -local *Carp::carp = sub { - local $Carp::CarpLevel = ($Carp::CarpLevel || 0) + 1; - Carp::croak(@_); -} unless has_exporter_version('5.565'); - -{ - 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' ]); -} - -{ - delete $this_stash->{use_ok} unless has_test_more_version('0.51'); - local $@; - eval { - Test::Leaner->import(import => [ 'use_ok' ]); - }; - Test::More::like($@, qr/^"use_ok" is not exported by the Test::Leaner module/, 'import "use_ok" croaks'); - check_imports([ ], [ 'use_ok' ]); -} - -{ - 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' ]); -} - -SKIP: -{ - Test::More::skip('Exporter 5.58 required to test negative imports' - => 1 + @default_exports) unless has_exporter_version('5.58'); - local $@; - eval { - Test::Leaner->import(import => [ 'pass' ], import => [ '!fail' ]); - }; - Test::More::is($@, '', 'import "pass", "!fail" does not croak'); - check_imports([ 'pass' ], [ ]); -} - -SKIP: -{ - Test::More::skip('Exporter 5.58 required to test negative imports' - => 1 + @default_exports) unless has_exporter_version('5.58'); - local $@; - eval { - Test::Leaner->import(import => [ 'fail' ], import => [ '!fail' ]); - }; - Test::More::is($@, '', 'import "fail", "!fail" does not croak'); - check_imports(); -} +test_import_arg; diff --git a/t/04-fallback-import-arg.t b/t/04-fallback-import-arg.t index ed9ad43..8e676f9 100644 --- a/t/04-fallback-import-arg.t +++ b/t/04-fallback-import-arg.t @@ -5,167 +5,7 @@ use warnings; BEGIN { $ENV{PERL_TEST_LEANER_USES_TEST_MORE} = 1 } -use Carp (); +use lib 't/lib'; +use Test::Leaner::TestImport; -use Test::Leaner (); -use Test::More (); - -sub get_subroutine { - my ($stash, $name) = @_; - - my $glob = $stash->{$name}; - return undef unless $glob; - - return *$glob{CODE}; -} - -sub has_module_version { - my ($module, $version) = @_; - - local $@; - eval qq{ - require $module; - "$module"->VERSION(\$version); - 1; - } -} - -sub has_test_more_version { has_module_version 'Test::More', @_ } -sub has_exporter_version { has_module_version 'Exporter', @_ } - -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] || [] }; - -SKIP: - { - local $Test::Builder::Level = ($Test::Builder::Level || 0) + 1; - Test::More::skip($_[2] => @not_imported + @default_exports) if defined $_[2]; - - 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; -} - -if ($^V ge v5.8.4 and $^V le v5.8.5) { - Test::More::plan(skip_all - => 'goto may segfault randomly on perl 5.8.4 and 5.8.5'); -} else { - Test::More::plan(tests => 9 * @default_exports + 8 + 3); -} - -check_imports([ ], [ ], has_test_more_version('0.51') ? undef : 'Test::More::plan exports stuff on Test::More <= 0.51'); - -local *Carp::carp = sub { - local $Carp::CarpLevel = ($Carp::CarpLevel || 0) + 1; - Carp::croak(@_); -} unless has_exporter_version('5.565'); - -{ - 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' ]); -} - -{ - delete $this_stash->{use_ok} unless has_test_more_version('0.51'); - local $@; - eval { - Test::Leaner->import(import => [ 'use_ok' ]); - }; - Test::More::like($@, qr/^"use_ok" is not exported by the Test::Leaner module/, 'import "use_ok" croaks'); - check_imports([ ], [ 'use_ok' ]); -} - -{ - 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' ]); -} - -SKIP: -{ - Test::More::skip('Exporter 5.58 required to test negative imports' - => 1 + @default_exports) unless has_exporter_version('5.58'); - local $@; - eval { - Test::Leaner->import(import => [ 'pass' ], import => [ '!fail' ]); - }; - Test::More::is($@, '', 'import "pass", "!fail" does not croak'); - check_imports([ 'pass' ], [ ]); -} - -SKIP: -{ - Test::More::skip('Exporter 5.58 required to test negative imports' - => 1 + @default_exports) unless has_exporter_version('5.58'); - local $@; - eval { - Test::Leaner->import(import => [ 'fail' ], import => [ '!fail' ]); - }; - Test::More::is($@, '', 'import "fail", "!fail" does not croak'); - check_imports(); -} +test_import_arg; diff --git a/t/lib/Test/Leaner/TestImport.pm b/t/lib/Test/Leaner/TestImport.pm new file mode 100644 index 0000000..fd70030 --- /dev/null +++ b/t/lib/Test/Leaner/TestImport.pm @@ -0,0 +1,196 @@ +package Test::Leaner::TestImport; + +use strict; +use warnings; + +use Carp (); + +use Test::Leaner (); +use Test::More (); + +sub get_subroutine { + my ($stash, $name) = @_; + + my $glob = $stash->{$name}; + return undef unless $glob; + + return *$glob{CODE}; +} + +sub has_module_version { + my ($module, $version) = @_; + + local $@; + eval qq{ + require $module; + "$module"->VERSION(\$version); + 1; + } +} + +sub has_test_more_version { has_module_version 'Test::More', @_ } +sub has_exporter_version { has_module_version 'Exporter', @_ } + +my $this_stash = \%Test::Leaner::TestImport::; + +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] || [] }; + +SKIP: + { + local $Test::Builder::Level = ($Test::Builder::Level || 0) + 1; + Test::More::skip($_[2] => @not_imported + @default_exports) if defined $_[2]; + + 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; +} + +sub test_import_arg { + local $Test::Builder::Level = ($Test::Builder::Level || 0) + 1; + + my $use_fallback = $ENV{PERL_TEST_LEANER_USES_TEST_MORE}; + if ($use_fallback and $^V ge v5.8.4 and $^V le v5.8.5) { + Test::More::plan(skip_all + => 'goto may segfault randomly on perl 5.8.4 and 5.8.5'); + } else { + Test::More::plan(tests => 9 * @default_exports + 8 + 3); + } + + check_imports( + [ ], [ ], has_test_more_version('0.51') + ? undef + : 'Test::More::plan exports stuff on Test::More <= 0.51' + ); + + local *Carp::carp = sub { + local $Carp::CarpLevel = ($Carp::CarpLevel || 0) + 1; + Carp::croak(@_); + } unless has_exporter_version('5.565'); + + { + 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' ]); + }; + my $class = $use_fallback ? 'Test::More' : 'Test::Leaner'; + Test::More::like( + $@, qr/^"nonexistent" is not exported by the $class module/, + 'import "nonexistent" croaks' + ); + check_imports([ ], [ 'nonexistent' ]); + } + + { + delete $this_stash->{use_ok} unless has_test_more_version('0.51'); + local $@; + eval { + Test::Leaner->import(import => [ 'use_ok' ]); + }; + Test::More::like( + $@, qr/^"use_ok" is not exported by the Test::Leaner module/, + 'import "use_ok" croaks' + ); + check_imports([ ], [ 'use_ok' ]); + } + + { + 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' ]); + } + + SKIP: + { + Test::More::skip('Exporter 5.58 required to test negative imports' + => 1 + @default_exports) unless has_exporter_version('5.58'); + local $@; + eval { + Test::Leaner->import(import => [ 'pass' ], import => [ '!fail' ]); + }; + Test::More::is($@, '', 'import "pass", "!fail" does not croak'); + check_imports([ 'pass' ], [ ]); + } + + SKIP: + { + Test::More::skip('Exporter 5.58 required to test negative imports' + => 1 + @default_exports) unless has_exporter_version('5.58'); + local $@; + eval { + Test::Leaner->import(import => [ 'fail' ], import => [ '!fail' ]); + }; + Test::More::is($@, '', 'import "fail", "!fail" does not croak'); + check_imports(); + } +} + +our @EXPORT = qw< + test_import_arg +>; + +use Exporter (); + +sub import { goto &Exporter::import } + +1;