]> git.vpit.fr Git - perl/modules/Test-Leaner.git/blob - t/lib/Test/Leaner/TestImport.pm
30f786c71380d5a09decd8a16f635d606b88c495
[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 default_exports { @default_exports }
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 sub test_import_arg {
78  local $Test::Builder::Level = ($Test::Builder::Level || 0) + 1;
79
80  my $use_fallback = $ENV{PERL_TEST_LEANER_USES_TEST_MORE};
81  if ($use_fallback and "$]" >= 5.008004 and "$]" <= 5.008005) {
82   Test::More::plan(skip_all
83                        => 'goto may segfault randomly on perl 5.8.4 and 5.8.5');
84  } else {
85   Test::More::plan(tests => 9 * @default_exports + 8 + 3);
86  }
87
88  check_imports(
89   [ ], [ ], has_test_more_version('0.51')
90                        ? undef
91                        : 'Test::More::plan exports stuff on Test::More <= 0.51'
92  );
93
94  local *Carp::carp = sub {
95   local $Carp::CarpLevel = ($Carp::CarpLevel || 0) + 1;
96   Carp::croak(@_);
97  } unless has_exporter_version('5.565');
98
99  {
100   local $@;
101   eval {
102    Test::Leaner->import(import => [ ]);
103   };
104   Test::More::is($@, '', 'empty import does not croak');
105   check_imports(\@default_exports);
106  }
107
108  {
109   local $@;
110   eval {
111    Test::Leaner->import(import => [ 'nonexistent' ]);
112   };
113   my $class = $use_fallback ? 'Test::More' : 'Test::Leaner';
114   Test::More::like(
115    $@, qr/^"nonexistent" is not exported by the $class module/,
116    'import "nonexistent" croaks'
117   );
118   check_imports([ ], [ 'nonexistent' ]);
119  }
120
121  {
122   delete $this_stash->{use_ok} unless has_test_more_version('0.51');
123   local $@;
124   eval {
125    Test::Leaner->import(import => [ 'use_ok' ]);
126   };
127   Test::More::like(
128    $@, qr/^"use_ok" is not exported by the Test::Leaner module/,
129    'import "use_ok" croaks'
130   );
131   check_imports([ ], [ 'use_ok' ]);
132  }
133
134  {
135   local $@;
136   eval {
137    Test::Leaner->import(import => [ 'ok' ]);
138   };
139   Test::More::is($@, '', 'import "ok" does not croak');
140   check_imports([ 'ok' ], [ ]);
141  }
142
143  {
144   local $@;
145   eval {
146    Test::Leaner->import(
147     import => [ qw<like unlike> ],
148     import => [ qw<diag note> ],
149    );
150   };
151   Test::More::is($@, '',
152                  'import "like", "unlike", "diag" and "note" does not croak');
153   check_imports([ qw<like unlike diag note> ], [ ]);
154  }
155
156  {
157   local $@;
158   eval {
159    Test::Leaner->import(import => [ '!fail' ]);
160   };
161   Test::More::is($@, '', 'import "!fail" does not croak');
162   check_imports([ grep $_ ne 'fail', @default_exports ], [ 'fail' ]);
163  }
164
165  SKIP:
166  {
167   Test::More::skip('Exporter 5.58 required to test negative imports'
168                    => 1 + @default_exports) unless has_exporter_version('5.58');
169   local $@;
170   eval {
171    Test::Leaner->import(import => [ 'pass' ], import => [ '!fail' ]);
172   };
173   Test::More::is($@, '', 'import "pass", "!fail" does not croak');
174   check_imports([ 'pass' ], [ ]);
175  }
176
177  SKIP:
178  {
179   Test::More::skip('Exporter 5.58 required to test negative imports'
180                    => 1 + @default_exports) unless has_exporter_version('5.58');
181   local $@;
182   eval {
183    Test::Leaner->import(import => [ 'fail' ], import => [ '!fail' ]);
184   };
185   Test::More::is($@, '', 'import "fail", "!fail" does not croak');
186   check_imports();
187  }
188 }
189
190 our @EXPORT_OK = qw<
191  get_subroutine
192  has_module_version
193  has_test_more_version
194  has_exporter_version
195  default_exports
196  test_import_arg
197 >;
198
199 use Exporter ();
200
201 sub import { goto &Exporter::import }
202
203 1;