1 package VPIT::TestHelpers;
7 load_or_skip => \&load_or_skip,
8 load_or_skip_all => \&load_or_skip_all,
9 skip_all => \&skip_all,
15 while (my ($name, $code) = each %exports) {
17 *{$pkg.'::'.$name} = $code;
25 if ($INC{'Test/Leaner.pm'}) {
26 $stash = \%Test::Leaner::;
29 $stash = \%Test::More::;
32 my $glob = $stash->{$sub};
33 return $glob ? *$glob{CODE} : undef;
36 sub skip { $test_sub->('skip')->(@_) }
38 sub skip_all { $test_sub->('plan')->(skip_all => $_[0]) }
41 my $diag = $test_sub->('diag');
49 my ($pkg, $ver, $imports) = @_;
51 my $spec = $ver && $ver !~ /^[0._]*$/ ? "$pkg $ver" : $pkg;
55 if (eval "use $spec (); 1") {
56 $ver = do { no strict 'refs'; ${"${pkg}::VERSION"} };
57 $ver = 'undef' unless defined $ver;
60 my @imports = @$imports;
61 my $caller = (caller 1)[0];
63 my $res = eval <<"IMPORTER";
66 BEGIN { \$pkg->import(\@imports) }
69 $err = "Could not import '@imports' from $pkg $ver: $@" unless $res;
72 (my $file = "$pkg.pm") =~ s{::}{/}g;
74 $err = "Could not load $spec";
78 return wantarray ? (0, $err) : 0;
80 diag "Using $pkg $ver";
86 my ($pkg, $ver, $imports, $tests) = @_;
88 die 'You must specify how many tests to skip' unless defined $tests;
90 my ($loaded, $err) = load($pkg, $ver, $imports);
91 skip $err => $tests unless $loaded;
96 sub load_or_skip_all {
97 my ($pkg, $ver, $imports) = @_;
99 my ($loaded, $err) = load($pkg, $ver, $imports);
100 skip_all $err unless $loaded;
105 package VPIT::TestHelpers::Guard;
108 my ($class, $code) = @_;
110 bless { code => $code }, $class;
113 sub DESTROY { $_[0]->{code}->() }