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