| 1 | package Test::Class::Moose::AttributeRegistry; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | ## ABSTRACT: Global registry of tags by class and method. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 33 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 33 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 33 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 33 | our $VERSION = '1.00'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 33 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 33 | ▶ | use Class::MOP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 33 | ▶ | use List::SomeUtils qw( any uniq ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 33 | my %BY_METHOD = ( | |||||||||||||||||||||||||||
| 16 | tags => {}, # {$method}{$test_class} | ||||||||||||||||||||||||||||
| 17 | plans => {}, | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 8 | ▶ | sub add_plan { | ||||||||||||||||||||||||||
|
Subroutine
add_plan: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 8 | my ( undef, $test_class, $method, $plan ) = @_; | |||||||||||||||||||||||||||
| 22 | 8 | ▶ | if ( defined $plan ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 6 | $plan =~ s/\D//g; | |||||||||||||||||||||||||||
| 24 | 6 | ▶ | undef $plan unless $plan =~ /\d/; # no_plan | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | 8 | $BY_METHOD{plans}{$method}{$test_class} = $plan; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub get_plan { | ||||||||||||||||||||||||||
|
Subroutine
get_plan: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 0 | my ( undef, $test_class, $method ) = @_; | |||||||||||||||||||||||||||
| 31 | 0 | return $BY_METHOD{plans}{$method}{$test_class}; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 4382 | ▶ | sub has_test_attribute { | ||||||||||||||||||||||||||
|
Subroutine
has_test_attribute: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 4382 | my ( undef, $test_class, $method ) = @_; | |||||||||||||||||||||||||||
| 36 | 4382 | return exists $BY_METHOD{plans}{$method}{$test_class}; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 25 | ▶ | sub add_tags { | ||||||||||||||||||||||||||
|
Subroutine
add_tags: called CC=6 SCAR=18.4
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 25 | my ( $class, $test_class, $method, $tags ) = @_; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 25 | my @tags_copy = @{$tags}; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # check for additions or deletions to the inherited tag list | ||||||||||||||||||||||||||||
| 45 | 25 | ▶ | if ( any {/^[-+]/} @tags_copy ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 1 | @tags_copy = $class->_augment_tags( $test_class, $method, $tags ); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 25 | foreach my $tag (@tags_copy) { | |||||||||||||||||||||||||||
| 50 | 30 | ▶ | if ( $tag !~ /^\w+$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | die "tags must be alphanumeric\n"; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # dedupe tags | ||||||||||||||||||||||||||||
| 56 | 25 | my %tags = map { $_ => 1 } @tags_copy; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 25 | ▶ | if ( exists $BY_METHOD{tags}{$method} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $BY_METHOD{"tags"}{$method} and exists $BY_METHOD{"tags"}{$method}{$test_class}
MC/DC0%0/2
exists $BY_METHOD{"tags"}{$method} and exists $BY_METHOD{"tags"}{$method}{$test_class}
exists $BY_METHOD{"tags"}{$method}
exists $BY_METHOD{"tags"}{$method}{$test_class}
Truth table66%2/3
A and B
A= exists $BY_METHOD{"tags"}{$method}B= exists $BY_METHOD{"tags"}{$method}{$test_class}
| |||||||||||||||||||||||||||||
| 59 | && exists $BY_METHOD{tags}{$method}{$test_class} ) | ||||||||||||||||||||||||||||
| 60 | { | ||||||||||||||||||||||||||||
| 61 | 0 | die | |||||||||||||||||||||||||||
| 62 | "tags for $test_class->$method already exists, method redefinition perhaps?\n"; | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 25 | $BY_METHOD{tags}{$method}{$test_class} = \%tags; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 25 | return; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub tags { | ||||||||||||||||||||||||||
|
Subroutine
tags: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 0 | my @tags; | |||||||||||||||||||||||||||
| 72 | 0 | for my $method ( keys %BY_METHOD ) { | |||||||||||||||||||||||||||
| 73 | 0 | for my $test_class ( keys %{ $BY_METHOD{tags}{$method} } ) { | |||||||||||||||||||||||||||
| 74 | 0 | push @tags, keys %{ $BY_METHOD{tags}{$method}{$test_class} }; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | @tags = sort uniq(@tags); | |||||||||||||||||||||||||||
| 79 | 0 | return @tags; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 2 | ▶ | sub class_has_tag { | ||||||||||||||||||||||||||
|
Subroutine
class_has_tag: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 2 | my ( $class, $test_class, $tag ) = @_; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 2 | ▶ | croak('no class specified') if not defined $test_class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 2 | ▶ | croak('no tag specified') if not defined $tag; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # XXX a naïve implementation, but it does the job for now. | ||||||||||||||||||||||||||||
| 89 | 2 | my $test_class_meta = Class::MOP::Class->initialize($test_class); | |||||||||||||||||||||||||||
| 90 | 2 | foreach my $method ( $test_class_meta->get_all_method_names ) { | |||||||||||||||||||||||||||
| 91 | 46 | ▶ | next unless $method =~ /test_/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 21 | ▶ | return 1 if $class->method_has_tag( $test_class, $method, $tag ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 1 | return; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 423 | ▶ | sub method_has_tag { | ||||||||||||||||||||||||||
|
Subroutine
method_has_tag: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 423 | my ( $class, $test_class, $method, $tag ) = @_; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 423 | ▶ | croak('no class specified') if not defined $test_class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 423 | ▶ | croak('no method specified') if not defined $method; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 423 | ▶ | croak('no tag specified') if not defined $tag; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # avoid auto-vivication | ||||||||||||||||||||||||||||
| 105 | 423 | ▶ | return if not exists $BY_METHOD{tags}{$method}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 103 | ▶ | if ( not exists $BY_METHOD{tags}{$method}{$test_class} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # If this method has no tag data at all, then inherit the tags from | ||||||||||||||||||||||||||||
| 110 | # from the superclass | ||||||||||||||||||||||||||||
| 111 | 4 | $BY_METHOD{tags}{$method}{$test_class} | |||||||||||||||||||||||||||
| 112 | = $class->_superclass_tags( $test_class, $method ); | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 103 | return exists $BY_METHOD{tags}{$method}{$test_class}{$tag}; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 5 | ▶ | sub _superclass_tags { | ||||||||||||||||||||||||||
|
Subroutine
_superclass_tags: called CC=10 SCAR=24.6
| |||||||||||||||||||||||||||||
| 119 | 5 | my ( $class, $test_class, $method ) = @_; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 5 | ▶ | croak('no class specified') if not defined $test_class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 5 | ▶ | croak('no method specified') if not defined $method; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 5 | ▶ | return {} if not exists $BY_METHOD{tags}{$method}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 5 | my $test_class_meta = Class::MOP::Class->initialize($test_class); | |||||||||||||||||||||||||||
| 127 | 5 | my $method_meta; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 5 | ▶ | $method_meta = $test_class_meta->find_next_method_by_name($method) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | if $test_class_meta->can('find_next_method_by_name'); | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 5 | ▶ | if ( !$method_meta ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | #Might be a from a role or this class | ||||||||||||||||||||||||||||
| 135 | 1 | my $mm = $test_class_meta->find_method_by_name($method); | |||||||||||||||||||||||||||
| 136 | 1 | my $orig = $mm->original_method; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 1 | ▶ | if ( $orig && ( $mm->package_name ne $orig->package_name ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$orig and $mm->package_name ne $orig->package_name
MC/DC0%0/2
$orig and $mm->package_name ne $orig->package_name
$orig
$mm->package_name ne $orig->package_name
Truth table33%1/3
A and B
A= $origB= $mm->package_name ne $orig->package_name
| |||||||||||||||||||||||||||||
| 139 | 1 | $method_meta = $orig; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # no method, so no tags to inherit | ||||||||||||||||||||||||||||
| 144 | 5 | ▶ | return {} if not $method_meta; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 5 | my $super_test_class = $method_meta->package_name(); | |||||||||||||||||||||||||||
| 147 | 5 | ▶ | if ( exists $BY_METHOD{tags}{$method}{$super_test_class} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # shallow copy the superclass method's tags, because it's possible to | ||||||||||||||||||||||||||||
| 150 | # change add/remove items from the subclass's list later | ||||||||||||||||||||||||||||
| 151 | 7 | my %tags = map { $_ => 1 } | |||||||||||||||||||||||||||
| 152 | 5 | keys %{ $BY_METHOD{tags}{$method}{$super_test_class} }; | |||||||||||||||||||||||||||
| 153 | 5 | return \%tags; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # nothing defined at this level, recurse | ||||||||||||||||||||||||||||
| 157 | 0 | return $class->_superclass_tags( $super_test_class, $method ); | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 1 | ▶ | sub _augment_tags { | ||||||||||||||||||||||||||
|
Subroutine
_augment_tags: called CC=6 SCAR=18.3
| |||||||||||||||||||||||||||||
| 161 | 1 | my ( $class, $test_class, $method, $tags ) = @_; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 1 | ▶ | croak('no class specified') if not defined $test_class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 1 | ▶ | croak('no method specified') if not defined $method; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # Get the base list from the superclass | ||||||||||||||||||||||||||||
| 167 | 1 | my $tag_list = $class->_superclass_tags( $test_class, $method ); | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 1 | for my $tag_definition ( @{$tags} ) { | |||||||||||||||||||||||||||
| 170 | 2 | my $direction = substr( $tag_definition, 0, 1 ); | |||||||||||||||||||||||||||
| 171 | 2 | my $tag = substr( $tag_definition, 1 ); | |||||||||||||||||||||||||||
| 172 | 2 | ▶ | if ( $direction eq '+' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 173 | 1 | $tag_list->{$tag} = 1; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | elsif ( $direction eq '-' ) { | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | # die here if the tag wasn't inherited? | ||||||||||||||||||||||||||||
| 178 | 1 | delete $tag_list->{$tag}; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | else { | ||||||||||||||||||||||||||||
| 181 | 0 | die | |||||||||||||||||||||||||||
| 182 | "$test_class->$method attempting to override and modify tags, did you forget a '+'?\n"; | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 1 | return keys %{$tag_list}; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 33 | 1; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | __END__ | ||||||||||||||||||||||||||||