]> git.vpit.fr Git - perl/modules/Test-Leaner.git/blob - t/04-fallback-import-arg.t
Croak when importing symbols from Test::More that aren't in Test::Leaner
[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 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  for (@not_imported, grep !$imported{$_}, @default_exports) {
44   Test::More::ok(!exists $this_stash->{$_}, "$_ was not imported");
45  }
46  for (grep $imported{$_}, @default_exports) {
47   my $code = get_subroutine($this_stash, $_);
48   Test::More::ok($code, "$_ was imported");
49  }
50  delete $this_stash->{$_} for @default_exports, keys %imported, @not_imported;
51 }
52
53 Test::More::plan(tests => 9 * @default_exports + 8 + 3);
54
55 check_imports();
56
57 {
58  local $@;
59  eval {
60   Test::Leaner->import(import => [ ]);
61  };
62  Test::More::is($@, '', 'empty import does not croak');
63  check_imports(\@default_exports);
64 }
65
66 {
67  local $@;
68  eval {
69   Test::Leaner->import(import => [ 'nonexistent' ]);
70  };
71  Test::More::like($@, qr/^"nonexistent" is not exported by the Test::More module/, 'import "nonexistent" croaks');
72  check_imports([ ], [ 'nonexistent' ]);
73 }
74
75 {
76  local $@;
77  eval {
78   Test::Leaner->import(import => [ 'use_ok' ]);
79  };
80  Test::More::like($@, qr/^"use_ok" is not exported by the Test::Leaner module/, 'import "use_ok" croaks');
81  check_imports([ ], [ 'use_ok' ]);
82 }
83
84 {
85  local $@;
86  eval {
87   Test::Leaner->import(import => [ 'ok' ]);
88  };
89  Test::More::is($@, '', 'import "ok" does not croak');
90  check_imports([ 'ok' ], [ ]);
91 }
92
93 {
94  local $@;
95  eval {
96   Test::Leaner->import(
97    import => [ qw<like unlike> ],
98    import => [ qw<diag note> ],
99   );
100  };
101  Test::More::is($@, '', 'import "like", "unlike", "diag" and "note" does not croak');
102  check_imports([ qw<like unlike diag note> ], [ ]);
103 }
104
105 {
106  local $@;
107  eval {
108   Test::Leaner->import(import => [ '!fail' ]);
109  };
110  Test::More::is($@, '', 'import "!fail" does not croak');
111  check_imports([ grep $_ ne 'fail', @default_exports ], [ 'fail' ]);
112 }
113
114 {
115  local $@;
116  eval {
117   Test::Leaner->import(import => [ 'pass' ], import => [ '!fail' ]);
118  };
119  Test::More::is($@, '', 'import "pass", "!fail" does not croak');
120  check_imports([ 'pass' ], [ ]);
121 }
122
123 {
124  local $@;
125  eval {
126   Test::Leaner->import(import => [ 'fail' ], import => [ '!fail' ]);
127  };
128  Test::More::is($@, '', 'import "fail", "!fail" does not croak');
129  check_imports();
130 }