| 1 | package Test::More::Strict; | ||||||||||||
| 2 | |||||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 5 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | { | ||||||||||||
| 8 | # Nasty hack: install ourself in Test::Builder's ISA chain. | ||||||||||||
| 9 | 5 | my $builder = Test::More->builder; | |||||||||||
| 10 | 5 | our @ISA = ref $builder; | |||||||||||
| 11 | |||||||||||||
| 12 | # Bless builder into our package. | ||||||||||||
| 13 | 5 | bless Test::More->builder, __PACKAGE__; | |||||||||||
| 14 | } | ||||||||||||
| 15 | |||||||||||||
| 16 | 5 | my @OK_EVENT = qw( description ); | |||||||||||
| 17 | 5 | my %Handler = (); | |||||||||||
| 18 | |||||||||||||
| 19 | =head1 NAME | ||||||||||||
| 20 | |||||||||||||
| 21 | Test::More::Strict - Enforce policies on test results | ||||||||||||
| 22 | |||||||||||||
| 23 | =head1 VERSION | ||||||||||||
| 24 | |||||||||||||
| 25 | This document describes Test::More::Strict version 0.02 | ||||||||||||
| 26 | |||||||||||||
| 27 | =cut | ||||||||||||
| 28 | |||||||||||||
| 29 | 5 | our $VERSION = '0.02'; | |||||||||||
| 30 | |||||||||||||
| 31 | =head1 SYNOPSIS | ||||||||||||
| 32 | |||||||||||||
| 33 | # Enforce non-blank test description | ||||||||||||
| 34 | use Test::More::Strict description => sub { | ||||||||||||
| 35 | my $desc = shift; | ||||||||||||
| 36 | return defined $desc and $desc =~ /\S/; | ||||||||||||
| 37 | }; | ||||||||||||
| 38 | |||||||||||||
| 39 | =head1 DESCRIPTION | ||||||||||||
| 40 | |||||||||||||
| 41 | C<Test::More::Strict> allows policies for test results to be enforced. | ||||||||||||
| 42 | For example you may require that all tests have a non-blank description. | ||||||||||||
| 43 | You could achieve that like this: | ||||||||||||
| 44 | |||||||||||||
| 45 | # Enforce non-blank test description | ||||||||||||
| 46 | use Test::More::Strict description => sub { | ||||||||||||
| 47 | my $desc = shift; | ||||||||||||
| 48 | return defined $desc and $desc =~ /\S/; | ||||||||||||
| 49 | }; | ||||||||||||
| 50 | |||||||||||||
| 51 | In general you pass a number of key => coderef pairs on the use line. | ||||||||||||
| 52 | Currently the only recognised key is C<description>. The coderef is | ||||||||||||
| 53 | called with the test description as its first argument. It should return | ||||||||||||
| 54 | a true value if the description is OK otherwise false. | ||||||||||||
| 55 | |||||||||||||
| 56 | =head1 INTERFACE | ||||||||||||
| 57 | |||||||||||||
| 58 | =head2 C<< caller >> | ||||||||||||
| 59 | |||||||||||||
| 60 | Overridden from Test::Builder. Adjusts the stack depth to account for | ||||||||||||
| 61 | our intercept. | ||||||||||||
| 62 | |||||||||||||
| 63 | =cut | ||||||||||||
| 64 | |||||||||||||
| 65 | # Fix up caller | ||||||||||||
| 66 | 0 | ▶ | sub caller { | ||||||||||
|
Subroutine
caller: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 67 | 0 | my ( $self, $height ) = @_; | |||||||||||
| 68 | 0 | ▶ | $height ||= 0; | ||||||||||
|
Condition0%0/2
$height ||= 0
MC/DC0%0/1
$height ||= 0
$height
Truth table0%0/2
A
A= $height
| |||||||||||||
| 69 | 0 | return $self->SUPER::caller( $height + 2 ); | |||||||||||
| 70 | } | ||||||||||||
| 71 | |||||||||||||
| 72 | =head2 C<< ok >> | ||||||||||||
| 73 | |||||||||||||
| 74 | Overridden from Test::Builder. | ||||||||||||
| 75 | |||||||||||||
| 76 | =cut | ||||||||||||
| 77 | |||||||||||||
| 78 | 10 | ▶ | sub ok { | ||||||||||
|
Subroutine
ok: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 79 | 10 | my ( $self, $test, $description ) = @_; | |||||||||||
| 80 | 10 | return $self->SUPER::ok( | |||||||||||
| 81 | _and_with_handlers( 'description', $test, $description ), | ||||||||||||
| 82 | $description ); | ||||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | 10 | ▶ | sub _and_with_handlers { | ||||||||||
|
Subroutine
_and_with_handlers: called CC=5 SCAR=16.1
| |||||||||||||
| 86 | 10 | my ( $event, $ok, @args ) = @_; | |||||||||||
| 87 | 10 | ▶ | return $ok unless $ok; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | 9 | ▶ | for my $handler ( @{ $Handler{$event} || [] } ) { | ||||||||||
|
Condition100%2/2
$Handler{$event} || []
MC/DC100%1/1
$Handler{$event} || []
$Handler{$event}
Truth table100%2/2
A
A= $Handler{$event}
| |||||||||||||
| 89 | 5 | ▶ | return 0 unless $handler->( @args ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | } | ||||||||||||
| 91 | 8 | return $ok; | |||||||||||
| 92 | } | ||||||||||||
| 93 | |||||||||||||
| 94 | { | ||||||||||||
| 95 | 5 | my %OK_EVENT = map { $_ => 1 } @OK_EVENT; | |||||||||||
| 96 | |||||||||||||
| 97 | 4 | ▶ | sub import { | ||||||||||
|
Subroutine
import: called CC=5 SCAR=16.5
| |||||||||||||
| 98 | 4 | my $class = shift; | |||||||||||
| 99 | |||||||||||||
| 100 | 4 | ▶ | croak "Please supply a number of key => value pairs" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 101 | if @_ & 1; | ||||||||||||
| 102 | |||||||||||||
| 103 | 4 | ▶ | while ( my ( $event, $validator ) = splice @_, 0, 2 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | croak "$event is not a valid event name" | ||||||||||||
| 105 | 2 | ▶ | unless $OK_EVENT{$event}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 106 | 2 | ▶ | croak "Validator must be a code reference" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 107 | unless 'CODE' eq ref $validator; | ||||||||||||
| 108 | 2 | push @{ $Handler{$event} }, $validator; | |||||||||||
| 109 | } | ||||||||||||
| 110 | } | ||||||||||||
| 111 | } | ||||||||||||
| 112 | 5 | 1; | |||||||||||
| 113 | __END__ | ||||||||||||