]> git.vpit.fr Git - perl/modules/Test-Leaner.git/blob - t/02-import-arg.t
Skip the first import argument test with Test::More 0.51 and lower
[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 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 my $this_stash = \%main::;
21
22 my @default_exports = qw<
23  plan
24  skip
25  done_testing
26  pass
27  fail
28  ok
29  is
30  isnt
31  like
32  unlike
33  cmp_ok
34  is_deeply
35  diag
36  note
37  BAIL_OUT
38 >;
39
40 sub check_imports {
41  my %imported     = map { $_ => 1 } @{ $_[0] || [] };
42  my @not_imported = @{ $_[1] || [] };
43
44 SKIP:
45  {
46   local $Test::Builder::Level = ($Test::Builder::Level || 0) + 1;
47   Test::More::skip($_[2] => @not_imported + @default_exports) if defined $_[2];
48
49   for (@not_imported, grep !$imported{$_}, @default_exports) {
50    Test::More::ok(!exists $this_stash->{$_}, "$_ was not imported");
51   }
52   for (grep $imported{$_}, @default_exports) {
53    my $code = get_subroutine($this_stash, $_);
54    Test::More::ok($code, "$_ was imported");
55   }
56  }
57
58  delete $this_stash->{$_} for @default_exports, keys %imported, @not_imported;
59 }
60
61 Test::More::plan(tests => 9 * @default_exports + 8 + 3);
62
63 check_imports([ ], [ ], $Test::More::VERSION > 0.51 ? undef : 'Test::More::plan exports stuff on Test::More <= 0.51');
64
65 {
66  local $@;
67  eval {
68   Test::Leaner->import(import => [ ]);
69  };
70  Test::More::is($@, '', 'empty import does not croak');
71  check_imports(\@default_exports);
72 }
73
74 {
75  local $@;
76  eval {
77   Test::Leaner->import(import => [ 'nonexistent' ]);
78  };
79  Test::More::like($@, qr/^"nonexistent" is not exported by the Test::Leaner module/, 'import "nonexistent" croaks');
80  check_imports([ ], [ 'nonexistent' ]);
81 }
82
83 {
84  local $@;
85  eval {
86   Test::Leaner->import(import => [ 'use_ok' ]);
87  };
88  Test::More::like($@, qr/^"use_ok" is not exported by the Test::Leaner module/, 'import "use_ok" croaks');
89  check_imports([ ], [ 'use_ok' ]);
90 }
91
92 {
93  local $@;
94  eval {
95   Test::Leaner->import(import => [ 'ok' ]);
96  };
97  Test::More::is($@, '', 'import "ok" does not croak');
98  check_imports([ 'ok' ], [ ]);
99 }
100
101 {
102  local $@;
103  eval {
104   Test::Leaner->import(
105    import => [ qw<like unlike> ],
106    import => [ qw<diag note> ],
107   );
108  };
109  Test::More::is($@, '', 'import "like", "unlike", "diag" and "note" does not croak');
110  check_imports([ qw<like unlike diag note> ], [ ]);
111 }
112
113 {
114  local $@;
115  eval {
116   Test::Leaner->import(import => [ '!fail' ]);
117  };
118  Test::More::is($@, '', 'import "!fail" does not croak');
119  check_imports([ grep $_ ne 'fail', @default_exports ], [ 'fail' ]);
120 }
121
122 {
123  local $@;
124  eval {
125   Test::Leaner->import(import => [ 'pass' ], import => [ '!fail' ]);
126  };
127  Test::More::is($@, '', 'import "pass", "!fail" does not croak');
128  check_imports([ 'pass' ], [ ]);
129 }
130
131 {
132  local $@;
133  eval {
134   Test::Leaner->import(import => [ 'fail' ], import => [ '!fail' ]);
135  };
136  Test::More::is($@, '', 'import "fail", "!fail" does not croak');
137  check_imports();
138 }