File Coverage

blib/lib/Test/More/Strict.pm
Criterion Covered Total %
statement 27 27 100.0
branch 7 12 58.3
condition 2 2 100.0
subroutine 7 7 100.0
pod 2 2 100.0
total 45 50 90.0


line stmt bran cond sub pod time code
1             package Test::More::Strict;
2              
3 3     3   94775 use warnings;
  3         8  
  3         261  
4 3     3   19 use strict;
  3         5  
  3         115  
5 3     3   18 use Carp;
  3         10  
  3         1953  
6              
7             {
8             # Nasty hack: install ourself in Test::Builder's ISA chain.
9             my $builder = Test::More->builder;
10             our @ISA = ref $builder;
11              
12             # Bless builder into our package.
13             bless Test::More->builder, __PACKAGE__;
14             }
15              
16             my @OK_EVENT = qw( description );
17             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             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 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. 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             sub caller {
67 17     17 1 5567 my ( $self, $height ) = @_;
68 17   100     47 $height ||= 0;
69 17         74 return $self->SUPER::caller( $height + 2 );
70             }
71              
72             =head2 C<< ok >>
73              
74             Overridden from Test::Builder.
75              
76             =cut
77              
78             sub ok {
79 5     5 1 996 my ( $self, $test, $description ) = @_;
80 5         18 return $self->SUPER::ok(
81             _and_with_handlers( 'description', $test, $description ),
82             $description );
83             }
84              
85             sub _and_with_handlers {
86 5     5   13 my ( $event, $ok, @args ) = @_;
87 5 50       19 return $ok unless $ok;
88 5 100       10 for my $handler ( @{ $Handler{$event} || [] } ) {
  5         32  
89 2 50       8 return 0 unless $handler->( @args );
90             }
91 5         51 return $ok;
92             }
93              
94             {
95             my %OK_EVENT = map { $_ => 1 } @OK_EVENT;
96              
97             sub import {
98 3     3   26 my $class = shift;
99              
100 3 50       17 croak "Please supply a number of key => value pairs"
101             if @_ & 1;
102              
103 3         2450 while ( my ( $event, $validator ) = splice @_, 0, 2 ) {
104 1 50       36 croak "$event is not a valid event name"
105             unless $OK_EVENT{$event};
106 1 50       4 croak "Validator must be a code reference"
107             unless 'CODE' eq ref $validator;
108 1         2 push @{ $Handler{$event} }, $validator;
  1         1847  
109             }
110             }
111             }
112             1;
113             __END__