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