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) $
6 ##############################################################################
8 package Perl::Critic::Policy::TestingAndDebugging::RequireTestLabels;
15 use List::MoreUtils qw(any);
16 use Perl::Critic::Utils qw{
17 :characters :severities :data_conversion :classification :ppi
19 use base 'Perl::Critic::Policy';
21 our $VERSION = '1.088';
23 Readonly::Hash my %LABEL_ARG_POS => (
35 #-----------------------------------------------------------------------------
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};
40 #-----------------------------------------------------------------------------
42 sub supported_parameters {
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 ) ],
54 sub default_severity { return $SEVERITY_MEDIUM }
55 sub default_themes { return qw( core maintenance tests ) }
56 sub applies_to { return 'PPI::Token::Word' }
58 #-----------------------------------------------------------------------------
61 my ($self, $elem, $doc) = @_;
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);
68 # Does the function call have enough arguments?
69 my @args = parse_arg_list($elem);
70 return if ( @args > $arg_index );
72 return $self->violation( $DESC, $EXPL, $elem );
75 #-----------------------------------------------------------------------------
78 my ( $self, $doc ) = @_;
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.
86 my $includes = $doc->find('PPI::Statement::Include');
87 return if not $includes;
88 return any { exists $self->{_modules}->{$_->module()} }
94 #-----------------------------------------------------------------------------
102 Perl::Critic::Policy::TestingAndDebugging::RequireTestLabels - Tests should all have labels.
106 This Policy is part of the core L<Perl::Critic> distribution.
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.
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.
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
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.
132 [TestingAndDebugging::RequireTestLabels]
133 modules = My::Test::SubClass Some::Other::Module
135 The module list always implicitly includes L<Test::More>.
139 Chris Dolan <cdolan@cpan.org>
143 Copyright (C) 2006 Chris Dolan. All rights reserved.
145 This program is free software; you can redistribute it and/or modify
146 it under the same terms as Perl itself.
152 # cperl-indent-level: 4
154 # indent-tabs-mode: nil
155 # c-indentation-style: bsd
157 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :