1 package Test::Leaner::TestImport;
12 my ($stash, $name) = @_;
14 my $glob = $stash->{$name};
15 return undef unless $glob;
20 sub has_module_version {
21 my ($module, $version) = @_;
26 "$module"->VERSION(\$version);
31 sub has_test_more_version { has_module_version 'Test::More', @_ }
32 sub has_exporter_version { has_module_version 'Exporter', @_ }
34 my $this_stash = \%Test::Leaner::TestImport::;
36 my @default_exports = qw<
55 my %imported = map { $_ => 1 } @{ $_[0] || [] };
56 my @not_imported = @{ $_[1] || [] };
60 local $Test::Builder::Level = ($Test::Builder::Level || 0) + 1;
61 Test::More::skip($_[2] => @not_imported + @default_exports) if defined $_[2];
63 for (@not_imported, grep !$imported{$_}, @default_exports) {
64 Test::More::ok(!exists $this_stash->{$_}, "$_ was not imported");
66 for (grep $imported{$_}, @default_exports) {
67 my $code = get_subroutine($this_stash, $_);
68 Test::More::ok($code, "$_ was imported");
72 delete $this_stash->{$_} for @default_exports, keys %imported, @not_imported;
76 local $Test::Builder::Level = ($Test::Builder::Level || 0) + 1;
78 my $use_fallback = $ENV{PERL_TEST_LEANER_USES_TEST_MORE};
79 if ($use_fallback and $^V ge v5.8.4 and $^V le v5.8.5) {
80 Test::More::plan(skip_all
81 => 'goto may segfault randomly on perl 5.8.4 and 5.8.5');
83 Test::More::plan(tests => 9 * @default_exports + 8 + 3);
87 [ ], [ ], has_test_more_version('0.51')
89 : 'Test::More::plan exports stuff on Test::More <= 0.51'
92 local *Carp::carp = sub {
93 local $Carp::CarpLevel = ($Carp::CarpLevel || 0) + 1;
95 } unless has_exporter_version('5.565');
100 Test::Leaner->import(import => [ ]);
102 Test::More::is($@, '', 'empty import does not croak');
103 check_imports(\@default_exports);
109 Test::Leaner->import(import => [ 'nonexistent' ]);
111 my $class = $use_fallback ? 'Test::More' : 'Test::Leaner';
113 $@, qr/^"nonexistent" is not exported by the $class module/,
114 'import "nonexistent" croaks'
116 check_imports([ ], [ 'nonexistent' ]);
120 delete $this_stash->{use_ok} unless has_test_more_version('0.51');
123 Test::Leaner->import(import => [ 'use_ok' ]);
126 $@, qr/^"use_ok" is not exported by the Test::Leaner module/,
127 'import "use_ok" croaks'
129 check_imports([ ], [ 'use_ok' ]);
135 Test::Leaner->import(import => [ 'ok' ]);
137 Test::More::is($@, '', 'import "ok" does not croak');
138 check_imports([ 'ok' ], [ ]);
144 Test::Leaner->import(
145 import => [ qw<like unlike> ],
146 import => [ qw<diag note> ],
149 Test::More::is($@, '',
150 'import "like", "unlike", "diag" and "note" does not croak');
151 check_imports([ qw<like unlike diag note> ], [ ]);
157 Test::Leaner->import(import => [ '!fail' ]);
159 Test::More::is($@, '', 'import "!fail" does not croak');
160 check_imports([ grep $_ ne 'fail', @default_exports ], [ 'fail' ]);
165 Test::More::skip('Exporter 5.58 required to test negative imports'
166 => 1 + @default_exports) unless has_exporter_version('5.58');
169 Test::Leaner->import(import => [ 'pass' ], import => [ '!fail' ]);
171 Test::More::is($@, '', 'import "pass", "!fail" does not croak');
172 check_imports([ 'pass' ], [ ]);
177 Test::More::skip('Exporter 5.58 required to test negative imports'
178 => 1 + @default_exports) unless has_exporter_version('5.58');
181 Test::Leaner->import(import => [ 'fail' ], import => [ '!fail' ]);
183 Test::More::is($@, '', 'import "fail", "!fail" does not croak');
194 sub import { goto &Exporter::import }