]> git.vpit.fr Git - perl/modules/Test-Leaner.git/blobdiff - t/02-import-arg.t
Fix the handling of the "import" import argument
[perl/modules/Test-Leaner.git] / t / 02-import-arg.t
diff --git a/t/02-import-arg.t b/t/02-import-arg.t
new file mode 100644 (file)
index 0000000..fd6f0f9
--- /dev/null
@@ -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<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();
+}