]> git.vpit.fr Git - perl/modules/Test-Leaner.git/blob - t/04-fallback-import-arg.t
ed9ad43c83f681521e8c023055733721f35f3ad1
[perl/modules/Test-Leaner.git] / t / 04-fallback-import-arg.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 BEGIN { $ENV{PERL_TEST_LEANER_USES_TEST_MORE} = 1 }
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 if ($^V ge v5.8.4 and $^V le v5.8.5) {
78  Test::More::plan(skip_all
79                        => 'goto may segfault randomly on perl 5.8.4 and 5.8.5');
80 } else {
81  Test::More::plan(tests => 9 * @default_exports + 8 + 3);
82 }
83
84 check_imports([ ], [ ], has_test_more_version('0.51') ? undef : 'Test::More::plan exports stuff on Test::More <= 0.51');
85
86 local *Carp::carp = sub {
87  local $Carp::CarpLevel = ($Carp::CarpLevel || 0) + 1;
88  Carp::croak(@_);
89 } unless has_exporter_version('5.565');
90
91 {
92  local $@;
93  eval {
94   Test::Leaner->import(import => [ ]);
95  };
96  Test::More::is($@, '', 'empty import does not croak');
97  check_imports(\@default_exports);
98 }
99
100 {
101  local $@;
102  eval {
103   Test::Leaner->import(import => [ 'nonexistent' ]);
104  };
105  Test::More::like($@, qr/^"nonexistent" is not exported by the Test::More module/, 'import "nonexistent" croaks');
106  check_imports([ ], [ 'nonexistent' ]);
107 }
108
109 {
110  delete $this_stash->{use_ok} unless has_test_more_version('0.51');
111  local $@;
112  eval {
113   Test::Leaner->import(import => [ 'use_ok' ]);
114  };
115  Test::More::like($@, qr/^"use_ok" is not exported by the Test::Leaner module/, 'import "use_ok" croaks');
116  check_imports([ ], [ 'use_ok' ]);
117 }
118
119 {
120  local $@;
121  eval {
122   Test::Leaner->import(import => [ 'ok' ]);
123  };
124  Test::More::is($@, '', 'import "ok" does not croak');
125  check_imports([ 'ok' ], [ ]);
126 }
127
128 {
129  local $@;
130  eval {
131   Test::Leaner->import(
132    import => [ qw<like unlike> ],
133    import => [ qw<diag note> ],
134   );
135  };
136  Test::More::is($@, '', 'import "like", "unlike", "diag" and "note" does not croak');
137  check_imports([ qw<like unlike diag note> ], [ ]);
138 }
139
140 {
141  local $@;
142  eval {
143   Test::Leaner->import(import => [ '!fail' ]);
144  };
145  Test::More::is($@, '', 'import "!fail" does not croak');
146  check_imports([ grep $_ ne 'fail', @default_exports ], [ 'fail' ]);
147 }
148
149 SKIP:
150 {
151  Test::More::skip('Exporter 5.58 required to test negative imports'
152                    => 1 + @default_exports) unless has_exporter_version('5.58');
153  local $@;
154  eval {
155   Test::Leaner->import(import => [ 'pass' ], import => [ '!fail' ]);
156  };
157  Test::More::is($@, '', 'import "pass", "!fail" does not croak');
158  check_imports([ 'pass' ], [ ]);
159 }
160
161 SKIP:
162 {
163  Test::More::skip('Exporter 5.58 required to test negative imports'
164                    => 1 + @default_exports) unless has_exporter_version('5.58');
165  local $@;
166  eval {
167   Test::Leaner->import(import => [ 'fail' ], import => [ '!fail' ]);
168  };
169  Test::More::is($@, '', 'import "fail", "!fail" does not croak');
170  check_imports();
171 }