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