Moose/AttributeRegistry.pm

filter: Statementstmt 86.4% 89 / 103 Branchbran 65.3% 34 / 52 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 87.5% 14 / 16 Podpod 100% 7 / 7 totaltotal 78.1% 147 / 188 CC 34 SCAR 18.8
CC
34
Cov
78%
tags
12.0
_superclass_tags
11.7
add_tags
6.3
CRAP 6.5
↑ Summary Moose/Role/Executor.pm »
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
BranchTrueFalseTotal
if (defined $plan) 6 2 8
23 6 $plan =~ s/\D//g;
24 6 ▶ undef $plan unless $plan =~ /\d/; # no_plan
Branch: 1 branch
BranchTrueFalseTotal
unless $plan =~ /\d/ 0 6 6
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
BranchTrueFalseTotal
if (&any(sub { /^[-+]/; } , @tags_copy)) 1 24 25
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
BranchTrueFalseTotal
unless ($tag =~ /^\w+$/) 0 30 30
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
BranchTrueFalseTotal
if (exists $BY_METHOD{"tags"}{$method} and exists $BY_METHOD{"tags"}{$method}{$test_class}) 0 25 25
Condition66%2/3
exists $BY_METHOD{"tags"}{$method} and exists $BY_METHOD{"tags"}{$method}{$test_class}
!ll&&!rl&&r
2320
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}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless defined $test_class 0 2 2
86 2 ▶ croak('no tag specified') if not defined $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag 0 2 2
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
BranchTrueFalseTotal
unless $method =~ /test_/ 25 21 46
92 21 ▶ return 1 if $class->method_has_tag( $test_class, $method, $tag );
Branch: 1 branch
BranchTrueFalseTotal
if $class->method_has_tag($test_class, $method, $tag) 1 20 21
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
BranchTrueFalseTotal
unless defined $test_class 0 423 423
101 423 ▶ croak('no method specified') if not defined $method;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method 0 423 423
102 423 ▶ croak('no tag specified') if not defined $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag 0 423 423
103  
104 # avoid auto-vivication
105 423 ▶ return if not exists $BY_METHOD{tags}{$method};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BY_METHOD{"tags"}{$method} 320 103 423
106  
107 103 ▶ if ( not exists $BY_METHOD{tags}{$method}{$test_class} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $BY_METHOD{"tags"}{$method}{$test_class}) 4 99 103
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
BranchTrueFalseTotal
unless defined $test_class 0 5 5
122 5 ▶ croak('no method specified') if not defined $method;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method 0 5 5
123  
124 5 ▶ return {} if not exists $BY_METHOD{tags}{$method};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BY_METHOD{"tags"}{$method} 0 5 5
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
BranchTrueFalseTotal
if $test_class_meta->can("find_next_method_by_name") 5 0 5
130 if $test_class_meta->can('find_next_method_by_name');
131  
132 5 ▶ if ( !$method_meta ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method_meta) 1 4 5
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
BranchTrueFalseTotal
if ($orig and $mm->package_name ne $orig->package_name) 1 0 1
Condition33%1/3
$orig and $mm->package_name ne $orig->package_name
!ll&&!rl&&r
001
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless $method_meta 0 5 5
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
BranchTrueFalseTotal
if (exists $BY_METHOD{"tags"}{$method}{$super_test_class}) 5 0 5
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
BranchTrueFalseTotal
unless defined $test_class 0 1 1
164 1 ▶ croak('no method specified') if not defined $method;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method 0 1 1
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
BranchTrueFalseTotal
if ($direction eq '+') { } 1 1 2
elsif ($direction eq '-') { } 1 0 1
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__
↑ Summary Moose/Role/Executor.pm »