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<
54 sub default_exports { @default_exports }
57 my %imported = map { $_ => 1 } @{ $_[0] || [] };
58 my @not_imported = @{ $_[1] || [] };
62 local $Test::Builder::Level = ($Test::Builder::Level || 0) + 1;
63 Test::More::skip($_[2] => @not_imported + @default_exports) if defined $_[2];
65 for (@not_imported, grep !$imported{$_}, @default_exports) {
66 Test::More::ok(!exists $this_stash->{$_}, "$_ was not imported");
68 for (grep $imported{$_}, @default_exports) {
69 my $code = get_subroutine($this_stash, $_);
70 Test::More::ok($code, "$_ was imported");
74 delete $this_stash->{$_} for @default_exports, keys %imported, @not_imported;
78 local $Test::Builder::Level = ($Test::Builder::Level || 0) + 1;
80 my $use_fallback = $ENV{PERL_TEST_LEANER_USES_TEST_MORE};
81 if ($use_fallback and $^V ge v5.8.4 and $^V le v5.8.5) {
82 Test::More::plan(skip_all
83 => 'goto may segfault randomly on perl 5.8.4 and 5.8.5');
85 Test::More::plan(tests => 9 * @default_exports + 8 + 3);
89 [ ], [ ], has_test_more_version('0.51')
91 : 'Test::More::plan exports stuff on Test::More <= 0.51'
94 local *Carp::carp = sub {
95 local $Carp::CarpLevel = ($Carp::CarpLevel || 0) + 1;
97 } unless has_exporter_version('5.565');
102 Test::Leaner->import(import => [ ]);
104 Test::More::is($@, '', 'empty import does not croak');
105 check_imports(\@default_exports);
111 Test::Leaner->import(import => [ 'nonexistent' ]);
113 my $class = $use_fallback ? 'Test::More' : 'Test::Leaner';
115 $@, qr/^"nonexistent" is not exported by the $class module/,
116 'import "nonexistent" croaks'
118 check_imports([ ], [ 'nonexistent' ]);
122 delete $this_stash->{use_ok} unless has_test_more_version('0.51');
125 Test::Leaner->import(import => [ 'use_ok' ]);
128 $@, qr/^"use_ok" is not exported by the Test::Leaner module/,
129 'import "use_ok" croaks'
131 check_imports([ ], [ 'use_ok' ]);
137 Test::Leaner->import(import => [ 'ok' ]);
139 Test::More::is($@, '', 'import "ok" does not croak');
140 check_imports([ 'ok' ], [ ]);
146 Test::Leaner->import(
147 import => [ qw<like unlike> ],
148 import => [ qw<diag note> ],
151 Test::More::is($@, '',
152 'import "like", "unlike", "diag" and "note" does not croak');
153 check_imports([ qw<like unlike diag note> ], [ ]);
159 Test::Leaner->import(import => [ '!fail' ]);
161 Test::More::is($@, '', 'import "!fail" does not croak');
162 check_imports([ grep $_ ne 'fail', @default_exports ], [ 'fail' ]);
167 Test::More::skip('Exporter 5.58 required to test negative imports'
168 => 1 + @default_exports) unless has_exporter_version('5.58');
171 Test::Leaner->import(import => [ 'pass' ], import => [ '!fail' ]);
173 Test::More::is($@, '', 'import "pass", "!fail" does not croak');
174 check_imports([ 'pass' ], [ ]);
179 Test::More::skip('Exporter 5.58 required to test negative imports'
180 => 1 + @default_exports) unless has_exporter_version('5.58');
183 Test::Leaner->import(import => [ 'fail' ], import => [ '!fail' ]);
185 Test::More::is($@, '', 'import "fail", "!fail" does not croak');
193 has_test_more_version
201 sub import { goto &Exporter::import }