]> git.vpit.fr Git - perl/modules/indirect.git/blob - t/lib/VPIT/TestHelpers.pm
42ff1897e3a869c6f79028347bc6f4784cad5425
[perl/modules/indirect.git] / t / lib / VPIT / TestHelpers.pm
1 package VPIT::TestHelpers;
2
3 use strict;
4 use warnings;
5
6 my %exports = (
7  load_or_skip => \&load_or_skip,
8  skip_all     => \&skip_all,
9 );
10
11 sub import {
12  my $pkg = caller;
13  while (my ($name, $code) = each %exports) {
14   no strict 'refs';
15   *{$pkg.'::'.$name} = $code;
16  }
17 }
18
19 my $test_sub = sub {
20  my $sub = shift;
21  my $stash;
22  if ($INC{'Test/Leaner.pm'}) {
23   $stash = \%Test::Leaner::;
24  } else {
25   require Test::More;
26   $stash = \%Test::More::;
27  }
28  my $glob = $stash->{$sub};
29  return $glob ? *$glob{CODE} : undef;
30 };
31
32 sub skip_all { $test_sub->('plan')->(skip_all => $_[0]) }
33
34 sub diag {
35  my $diag = $test_sub->('diag');
36  $diag->($_) for @_;
37 }
38
39 our $TODO;
40 local $TODO;
41
42 sub load_or_skip {
43  my ($pkg, $ver, $imports, $desc) = @_;
44  my $spec = $ver && $ver !~ /^[0._]*$/ ? "$pkg $ver" : $pkg;
45  local $@;
46  if (eval "use $spec (); 1") {
47   $ver = do { no strict 'refs'; ${"${pkg}::VERSION"} };
48   $ver = 'undef' unless defined $ver;
49   if ($imports) {
50    my @imports = @$imports;
51    my $caller  = (caller 0)[0];
52    local $@;
53    my $res = eval <<"IMPORTER";
54 package
55         $caller;
56 BEGIN { \$pkg->import(\@imports) }
57 1;
58 IMPORTER
59    skip_all "Could not import '@imports' from $pkg $ver: $@" unless $res;
60   }
61   diag "Using $pkg $ver";
62  } else {
63   (my $file = "$pkg.pm") =~ s{::}{/}g;
64   delete $INC{$file};
65   skip_all "$spec $desc";
66  }
67 }
68
69 package VPIT::TestHelpers::Guard;
70
71 sub new {
72  my ($class, $code) = @_;
73
74  bless { code => $code }, $class;
75 }
76
77 sub DESTROY { $_[0]->{code}->() }
78
79 1;