]> git.vpit.fr Git - perl/modules/Test-Leaner.git/blob - t/02-import-arg.t
46f2cc175863edc914d31afe15e5ac5c8f4a50c4
[perl/modules/Test-Leaner.git] / t / 02-import-arg.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 BEGIN { delete $ENV{PERL_TEST_LEANER_USES_TEST_MORE} }
7
8 use Carp ();
9
10 use Test::Leaner ();
11 use Test::More   ();
12
13 sub get_subroutine {
14  my ($stash, $name) = @_;
15
16  my $glob = $stash->{$name};
17  return undef unless $glob;
18
19  return *$glob{CODE};
20 }
21
22 sub has_module_version {
23  my ($module, $version) = @_;
24
25  local $@;
26  eval qq{
27   require $module;
28   "$module"->VERSION(\$version);
29   1;
30  }
31 }
32
33 sub has_test_more_version { has_module_version 'Test::More', @_ }
34 sub has_exporter_version  { has_module_version 'Exporter',   @_ }
35
36 my $this_stash = \%main::;
37
38 my @default_exports = qw<
39  plan
40  skip
41  done_testing
42  pass
43  fail
44  ok
45  is
46  isnt
47  like
48  unlike
49  cmp_ok
50  is_deeply
51  diag
52  note
53  BAIL_OUT
54 >;
55
56 sub check_imports {
57  my %imported     = map { $_ => 1 } @{ $_[0] || [] };
58  my @not_imported = @{ $_[1] || [] };
59
60 SKIP:
61  {
62   local $Test::Builder::Level = ($Test::Builder::Level || 0) + 1;
63   Test::More::skip($_[2] => @not_imported + @default_exports) if defined $_[2];
64
65   for (@not_imported, grep !$imported{$_}, @default_exports) {
66    Test::More::ok(!exists $this_stash->{$_}, "$_ was not imported");
67   }
68   for (grep $imported{$_}, @default_exports) {
69    my $code = get_subroutine($this_stash, $_);
70    Test::More::ok($code, "$_ was imported");
71   }
72  }
73
74  delete $this_stash->{$_} for @default_exports, keys %imported, @not_imported;
75 }
76
77 Test::More::plan(tests => 9 * @default_exports + 8 + 3);
78
79 check_imports([ ], [ ], has_test_more_version('0.51') ? undef : 'Test::More::plan exports stuff on Test::More <= 0.51');
80
81 local *Carp::carp = sub {
82  local $Carp::CarpLevel = ($Carp::CarpLevel || 0) + 1;
83  Carp::croak(@_);
84 } unless has_exporter_version('5.565');
85
86 {
87  local $@;
88  eval {
89   Test::Leaner->import(import => [ ]);
90  };
91  Test::More::is($@, '', 'empty import does not croak');
92  check_imports(\@default_exports);
93 }
94
95 {
96  local $@;
97  eval {
98   Test::Leaner->import(import => [ 'nonexistent' ]);
99  };
100  Test::More::like($@, qr/^"nonexistent" is not exported by the Test::Leaner module/, 'import "nonexistent" croaks');
101  check_imports([ ], [ 'nonexistent' ]);
102 }
103
104 {
105  delete $this_stash->{use_ok} unless has_test_more_version('0.51');
106  local $@;
107  eval {
108   Test::Leaner->import(import => [ 'use_ok' ]);
109  };
110  Test::More::like($@, qr/^"use_ok" is not exported by the Test::Leaner module/, 'import "use_ok" croaks');
111  check_imports([ ], [ 'use_ok' ]);
112 }
113
114 {
115  local $@;
116  eval {
117   Test::Leaner->import(import => [ 'ok' ]);
118  };
119  Test::More::is($@, '', 'import "ok" does not croak');
120  check_imports([ 'ok' ], [ ]);
121 }
122
123 {
124  local $@;
125  eval {
126   Test::Leaner->import(
127    import => [ qw<like unlike> ],
128    import => [ qw<diag note> ],
129   );
130  };
131  Test::More::is($@, '', 'import "like", "unlike", "diag" and "note" does not croak');
132  check_imports([ qw<like unlike diag note> ], [ ]);
133 }
134
135 {
136  local $@;
137  eval {
138   Test::Leaner->import(import => [ '!fail' ]);
139  };
140  Test::More::is($@, '', 'import "!fail" does not croak');
141  check_imports([ grep $_ ne 'fail', @default_exports ], [ 'fail' ]);
142 }
143
144 SKIP:
145 {
146  Test::More::skip('Exporter 5.58 required to test negative imports'
147                    => 1 + @default_exports) unless has_exporter_version('5.58');
148  local $@;
149  eval {
150   Test::Leaner->import(import => [ 'pass' ], import => [ '!fail' ]);
151  };
152  Test::More::is($@, '', 'import "pass", "!fail" does not croak');
153  check_imports([ 'pass' ], [ ]);
154 }
155
156 SKIP:
157 {
158  Test::More::skip('Exporter 5.58 required to test negative imports'
159                    => 1 + @default_exports) unless has_exporter_version('5.58');
160  local $@;
161  eval {
162   Test::Leaner->import(import => [ 'fail' ], import => [ '!fail' ]);
163  };
164  Test::More::is($@, '', 'import "fail", "!fail" does not croak');
165  check_imports();
166 }