]> git.vpit.fr Git - perl/modules/Test-Leaner.git/blob - t/03-fallback.t
Solve all test failures with combinations of old perl, Test::More and Exporter
[perl/modules/Test-Leaner.git] / t / 03-fallback.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 BEGIN {
7  if ($^V ge v5.8.4 and $^V le v5.8.5) {
8   require Test::More;
9   Test::More::plan(skip_all
10                        => 'goto may segfault randomly on perl 5.8.4 and 5.8.5');
11  }
12 }
13
14 BEGIN { $ENV{PERL_TEST_LEANER_USES_TEST_MORE} = 1 }
15
16 use Test::Leaner;
17
18 BEGIN {
19  my $loaded;
20  if ($INC{'Test/More.pm'}) {
21   $loaded = 1;
22  } else {
23   $loaded = 0;
24   require Test::More;
25   Test::More->import;
26  }
27  Test::More::plan(tests => 1 + 4 * 15 + 3 * 3 + 2 * 8);
28  Test::More::is($loaded, 1, 'Test::More has been loaded');
29 }
30
31 sub get_subroutine {
32  my ($stash, $name) = @_;
33
34  my $glob = $stash->{$name};
35  return undef unless $glob;
36
37  return *$glob{CODE};
38 }
39
40 sub has_module_version {
41  my ($module, $version) = @_;
42
43  local $@;
44  eval qq{
45   require $module;
46   "$module"->VERSION(\$version);
47   1;
48  }
49 }
50
51 sub has_test_more_version { has_module_version 'Test::More', @_ }
52
53 my $leaner_stash = \%Test::Leaner::;
54 my $more_stash   = \%Test::More::;
55 my $this_stash   = \%main::;
56
57 my @exported = qw<
58  plan
59  skip
60  done_testing
61  pass
62  fail
63  ok
64  is
65  isnt
66  like
67  unlike
68  cmp_ok
69  is_deeply
70  diag
71  note
72  BAIL_OUT
73 >;
74
75 for (@exported) {
76  my $more_variant     = get_subroutine($more_stash, $_);
77
78  my $leaner_variant   = get_subroutine($leaner_stash, $_);
79  Test::More::ok(defined $leaner_variant,
80                                        "Test::Leaner variant of $_ is defined");
81  my $imported_variant = get_subroutine($this_stash, $_);
82  Test::More::ok(defined $imported_variant, "imported variant of $_ is defined");
83
84  SKIP: {
85   Test::More::skip('Need leaner and imported variants to be defined' => 2)
86                    unless defined $leaner_variant
87                       and defined $imported_variant;
88
89   if (defined $more_variant) {
90    Test::More::is($leaner_variant, $more_variant,
91                   "Test::Leaner variant of $_ is Test::More variant");
92    Test::More::is($imported_variant, $more_variant,
93                   "imported variant of $_ is Test::More variant");
94   } else {
95    Test::More::is($imported_variant, $leaner_variant,
96                   "imported variant of $_ is Test::Leaner variant");
97    {
98     local $@;
99     eval { $leaner_variant->() };
100     Test::More::like($@, qr/^\Q$_\E is not implemented.*at \Q$0\E line \d+/,
101                          "Test::Leaner of $_ variant croaks");
102    }
103   }
104  }
105 }
106
107 my @only_in_test_leaner = qw<
108  tap_stream
109  diag_stream
110  THREADSAFE
111 >;
112
113 for (@only_in_test_leaner) {
114  Test::More::ok(exists $leaner_stash->{$_},
115                 "$_ still exists in Test::Leaner");
116  Test::More::ok(!exists $more_stash->{$_},
117                 "$_ was not imported into Test::More");
118  Test::More::ok(!exists $this_stash->{$_},
119                 "$_ was not imported into main");
120 }
121
122 SKIP:
123 {
124  Test::More::skip('Test::More::plan exports stuff on Test::More <= 0.51'
125                                  => 2 * 8) unless has_test_more_version('0.51');
126
127  my @only_in_test_more = qw<
128   use_ok
129   require_ok
130   can_ok
131   isa_ok
132   new_ok
133   subtest
134   explain
135   todo_skip
136  >;
137
138  for (@only_in_test_more) {
139   my $more_variant = get_subroutine($more_stash, $_);
140
141   SKIP: {
142    Test::More::skip("$_ is not implemented in this version of Test::More" => 2)
143                     unless defined $more_variant;
144
145    Test::More::ok(!exists $leaner_stash->{$_},
146                   "$_ was not imported into Test::Leaner");
147    Test::More::ok(!exists $this_stash->{$_},
148                   "$_ was not imported into main");
149   }
150  }
151 }