| 1 | package Test::If; | ||||||||||
| 2 | |||||||||||
| 3 | 6 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | =head1 NAME | ||||||||||
| 8 | |||||||||||
| 9 | Test::If - Test only if ... | ||||||||||
| 10 | |||||||||||
| 11 | =head1 VERSION | ||||||||||
| 12 | |||||||||||
| 13 | Version 0.01 | ||||||||||
| 14 | |||||||||||
| 15 | =cut | ||||||||||
| 16 | |||||||||||
| 17 | 6 | our $VERSION = '0.01'; | |||||||||
| 18 | |||||||||||
| 19 | |||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||
| 21 | |||||||||||
| 22 | Generic usage | ||||||||||
| 23 | |||||||||||
| 24 | use Test::More; | ||||||||||
| 25 | use Test::If 'Test::Something', [ tests => N ]; | ||||||||||
| 26 | |||||||||||
| 27 | # Is equal to | ||||||||||
| 28 | |||||||||||
| 29 | use Test::More; | ||||||||||
| 30 | eval q{ use Test::Something; 1 } or plan skip_all => 'Module Test::Something required for this test'; | ||||||||||
| 31 | plan tests => N; | ||||||||||
| 32 | |||||||||||
| 33 | |||||||||||
| 34 | use Test::More; | ||||||||||
| 35 | use Test::If sub { $ENV{TEST_AUTHOR} }, [ tests => N ]; | ||||||||||
| 36 | |||||||||||
| 37 | # Is equal to | ||||||||||
| 38 | |||||||||||
| 39 | use Test::More; | ||||||||||
| 40 | $ENV{TEST_AUTHOR} or plan skip_all => "Test condition not met"; | ||||||||||
| 41 | plan tests => N; | ||||||||||
| 42 | |||||||||||
| 43 | You can also combine options and it is allowed to omit plan options, if it is runned by loaded module or you want to load it manually | ||||||||||
| 44 | |||||||||||
| 45 | For example common C<pod-coverage.t>: | ||||||||||
| 46 | |||||||||||
| 47 | use Test::More; | ||||||||||
| 48 | use Test::If | ||||||||||
| 49 | sub { $ENV{TEST_AUTHOR} }, # Checked first $ENV{TEST_AUTHOR}, otherwise skip | ||||||||||
| 50 | 'Test::Pod::Coverage 1.08', # Use Test::Pod::Coverage of at least version 1.08 | ||||||||||
| 51 | 'Pod::Coverage 0.18', # And want Pod::Coverage at least of version 0.18 | ||||||||||
| 52 | ; | ||||||||||
| 53 | |||||||||||
| 54 | all_pod_coverage_ok(); | ||||||||||
| 55 | |||||||||||
| 56 | If some of conditions will not be met, test will be skipped. | ||||||||||
| 57 | |||||||||||
| 58 | =cut | ||||||||||
| 59 | |||||||||||
| 60 | 6 | ▶ | use Test::Builder (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 61 | 6 | ▶ | use Test::More (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 62 | |||||||||||
| 63 | 6 | my $Test = Test::Builder->new; | |||||||||
| 64 | |||||||||||
| 65 | 6 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=7 SCAR=19.5
| |||||||||||
| 66 | 6 | my $me = shift; | |||||||||
| 67 | 6 | my $caller = caller; | |||||||||
| 68 | 6 | $Test->exported_to($caller); | |||||||||
| 69 | 6 | ▶ | my $plan;$plan = pop if ref $_[-1] eq 'ARRAY'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 6 | for my $check (@_) { | |||||||||
| 71 | 7 | ▶ | if (ref $check eq 'CODE') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 4 | ▶ | $check->() or return $Test->plan(skip_all => "Test condition not met"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | } | ||||||||||
| 74 | else { | ||||||||||
| 75 | 3 | ▶ | if (eval qq{package main; use $check; 1}) { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||
| 76 | # Module loaded | ||||||||||
| 77 | } | ||||||||||
| 78 | else { | ||||||||||
| 79 | 1 | return $Test->plan(skip_all => "Module $check required for this test"); | |||||||||
| 80 | } | ||||||||||
| 81 | } | ||||||||||
| 82 | } | ||||||||||
| 83 | 3 | ▶ | $Test->plan( @$plan ) if $plan; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | # unless $Test->has_plan; | ||||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | |||||||||||
| 88 | =head1 AUTHOR | ||||||||||
| 89 | |||||||||||
| 90 | Mons Anderson, C<< <mons at cpan.org> >> | ||||||||||
| 91 | |||||||||||
| 92 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 93 | |||||||||||
| 94 | Copyright 2009 Mons Anderson, all rights reserved. | ||||||||||
| 95 | |||||||||||
| 96 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 97 | under the same terms as Perl itself. | ||||||||||
| 98 | |||||||||||
| 99 | =cut | ||||||||||
| 100 | |||||||||||
| 101 | 6 | 1; # End of Test::If | |||||||||