35e8b23d271576e2be4e84aafbe879ec8820f11f
[pkg-perl] / deb-src / libperl-critic-perl / libperl-critic-perl-1.088 / lib / Perl / Critic / Policy / TestingAndDebugging / RequireTestLabels.pm
1 ##############################################################################
2 #      $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/TestingAndDebugging/RequireTestLabels.pm $
3 #     $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
4 #   $Author: clonezone $
5 # $Revision: 2489 $
6 ##############################################################################
7
8 package Perl::Critic::Policy::TestingAndDebugging::RequireTestLabels;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13 use Readonly;
14
15 use List::MoreUtils qw(any);
16 use Perl::Critic::Utils qw{
17     :characters :severities :data_conversion :classification :ppi
18 };
19 use base 'Perl::Critic::Policy';
20
21 our $VERSION = '1.088';
22
23 Readonly::Hash my %LABEL_ARG_POS => (
24    ok        => 1,
25    is        => 2,
26    isnt      => 2,
27    like      => 2,
28    unlike    => 2,
29    cmp_ok    => 3,
30    is_deeply => 2,
31    pass      => 0,
32    fail      => 0,
33 );
34
35 #-----------------------------------------------------------------------------
36
37 Readonly::Scalar my $DESC => q{Test without a label};
38 Readonly::Scalar my $EXPL => q{Add a label argument to all Test::More functions};
39
40 #-----------------------------------------------------------------------------
41
42 sub supported_parameters {
43     return (
44         {
45             name            => 'modules',
46             description     => 'The additional modules to require labels for.',
47             default_string  => $EMPTY,
48             behavior        => 'string list',
49             list_always_present_values => [ qw( Test::More ) ],
50         },
51     );
52 }
53
54 sub default_severity { return $SEVERITY_MEDIUM             }
55 sub default_themes   { return qw( core maintenance tests ) }
56 sub applies_to       { return 'PPI::Token::Word'           }
57
58 #-----------------------------------------------------------------------------
59
60 sub violates {
61     my ($self, $elem, $doc) = @_;
62
63     my $arg_index = $LABEL_ARG_POS{$elem};
64     return if not defined $arg_index;
65     return if not is_function_call($elem);
66     return if not $self->_has_test_more($doc);
67
68     # Does the function call have enough arguments?
69     my @args = parse_arg_list($elem);
70     return if ( @args > $arg_index );
71
72     return $self->violation( $DESC, $EXPL, $elem );
73 }
74
75 #-----------------------------------------------------------------------------
76
77 sub _has_test_more {
78     my ( $self, $doc ) = @_;
79
80     # TODO: This method gets called every time violates() is invoked,
81     # but it only needs to happen once per document.  Perhaps this
82     # policy should just apply to PPI::Document, and then do its own
83     # search for for method calls.  Since Perl::Critic::Document is
84     # optimized, this should be pretty fast.
85
86     my $includes = $doc->find('PPI::Statement::Include');
87     return if not $includes;
88     return any { exists $self->{_modules}->{$_->module()} }
89         @{ $includes };
90 }
91
92 1;
93
94 #-----------------------------------------------------------------------------
95
96 __END__
97
98 =pod
99
100 =head1 NAME
101
102 Perl::Critic::Policy::TestingAndDebugging::RequireTestLabels - Tests should all have labels.
103
104 =head1 AFFILIATION
105
106 This Policy is part of the core L<Perl::Critic> distribution.
107
108
109 =head1 DESCRIPTION
110
111 Most Perl modules with regression tests use L<Test::More> as
112 infrastructure for writing and running those tests.  It has an easy,
113 procedural syntax for writing comparisons of results to expectations.
114
115 Most of the Test::More functions allow the programmer to add an
116 optional label that describes what each test is trying to judge.  When
117 a test goes wrong, these labels are very useful for quickly
118 determining where the problem originated.
119
120 This policy enforces that all Test::More functions have labels where
121 applicable.  This only applies to code that has a C<use Test::More> or
122 C<require Test::More> declaration (see below to add more test modules
123 to the list).
124
125 =head1 CONFIGURATION
126
127 A list of additional modules to require label parameters be passed to
128 their methods can be specified with the C<modules> option.  The list
129 must consist of whitespace-delimited, fully-qualified module names.
130 For example:
131
132  [TestingAndDebugging::RequireTestLabels]
133  modules = My::Test::SubClass  Some::Other::Module
134
135 The module list always implicitly includes L<Test::More>.
136
137 =head1 AUTHOR
138
139 Chris Dolan <cdolan@cpan.org>
140
141 =head1 COPYRIGHT
142
143 Copyright (C) 2006 Chris Dolan.  All rights reserved.
144
145 This program is free software; you can redistribute it and/or modify
146 it under the same terms as Perl itself.
147
148 =cut
149
150 # Local Variables:
151 #   mode: cperl
152 #   cperl-indent-level: 4
153 #   fill-column: 78
154 #   indent-tabs-mode: nil
155 #   c-indentation-style: bsd
156 # End:
157 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :