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