File: If.pm

package info (click to toggle)
libtest-if-perl 0.01-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 212 kB
  • sloc: perl: 1,833; sh: 22; makefile: 2
file content (101 lines) | stat: -rw-r--r-- 2,089 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package Test::If;

use 5.006;
use warnings;
use strict;

=head1 NAME

Test::If - Test only if ...

=head1 VERSION

Version 0.01

=cut

our $VERSION = '0.01';


=head1 SYNOPSIS

Generic usage

    use Test::More;
    use Test::If 'Test::Something', [ tests => N ];
    
    # Is equal to
    
    use Test::More;
    eval q{ use Test::Something; 1 } or plan skip_all => 'Module Test::Something required for this test';
    plan tests => N;

    
    use Test::More;
    use Test::If sub { $ENV{TEST_AUTHOR} }, [ tests => N ];
    
    # Is equal to
    
    use Test::More;
    $ENV{TEST_AUTHOR} or plan skip_all => "Test condition not met";
    plan tests => N;

You can also combine options and it is allowed to omit plan options, if it is run by loaded module or you want to load it manually

For example common C<pod-coverage.t>:

    use Test::More;
    use Test::If
        sub { $ENV{TEST_AUTHOR} },  # Checked first $ENV{TEST_AUTHOR}, otherwise skip
        'Test::Pod::Coverage 1.08', # Use Test::Pod::Coverage of at least version 1.08
        'Pod::Coverage 0.18',       # And want Pod::Coverage at least of version 0.18
    ;
    
    all_pod_coverage_ok();

If some of conditions will not be met, test will be skipped.

=cut

use Test::Builder ();
use Test::More ();

my $Test = Test::Builder->new;

sub import {
	my $me = shift;
	my $caller = caller;
	$Test->exported_to($caller);
	my $plan;$plan = pop if ref $_[-1] eq 'ARRAY';
	for my $check (@_) {
		if (ref $check eq 'CODE') {
			$check->() or return $Test->plan(skip_all => "Test condition not met");
		}
		else {
			if (eval qq{package main; use $check; 1}) {
				# Module loaded
			}
			else {
				return $Test->plan(skip_all => "Module $check required for this test");
			}
		}
	}
	$Test->plan( @$plan ) if $plan;
	# unless $Test->has_plan;
}


=head1 AUTHOR

Mons Anderson, C<< <mons at cpan.org> >>

=head1 COPYRIGHT & LICENSE

Copyright 2009 Mons Anderson, all rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

1; # End of Test::If