And test it for Test::Leaner and for the Test::More fallback interface.
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
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;
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, $_;
}
}
--- /dev/null
+#!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<like unlike> ],
+ import => [ qw<diag note> ],
+ );
+ };
+ Test::More::is($@, '', 'import "like", "unlike", "diag" and "note" does not croak');
+ check_imports([ qw<like unlike diag note> ], [ ]);
+}
+
+{
+ 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();
+}
--- /dev/null
+#!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<like unlike> ],
+ import => [ qw<diag note> ],
+ );
+ };
+ Test::More::is($@, '', 'import "like", "unlike", "diag" and "note" does not croak');
+ check_imports([ qw<like unlike diag note> ], [ ]);
+}
+
+{
+ 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();
+}