lib/Perl/ToPerl6/TransformerParameter/Behavior/Enumeration.pm

filter: Statementstmt 88.7% 55 / 62 Branchbran 90.0% 18 / 20 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 90.0% 9 / 10 Podpod 100% 2 / 2 totaltotal 88.3% 91 / 103 CC 13 SCAR 13.4
CC
13
Cov
89%
generate_parameter_description
6.0
__ANON__
5.0
initialize_parameter
5.0
CRAP 3.8
« lib/Perl/ToPerl6/Transformer/References/RewriteDereferences.pm ↑ Summary lib/Perl/ToPerl6/Transformer/RewriteSpecialLiterals.pm »
1 package Perl::ToPerl6::TransformerParameter::Behavior::Enumeration;
2  
3 23 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 use Perl::ToPerl6::Exception::Fatal::TransformerDefinition
8 23 ▶ qw{ &throw_transformer_definition };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 23 ▶use Perl::ToPerl6::Utils qw{ :characters &words_from_string &hashify };
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 23 ▶use base qw{ Perl::ToPerl6::TransformerParameter::Behavior };
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 #-----------------------------------------------------------------------------
14  
15 8 ▶sub initialize_parameter {
Subroutine initialize_parameter: called CC=5 SCAR=16.1
Pod documented
16 8 my ($self, $parameter, $specification) = @_;
17  
18 my $valid_values = $specification->{enumeration_values}
19 8 ▶ or throw_transformer_definition(
Branch: 1 branch
BranchTrueFalseTotal
my $valid_values = $specification->{"enumeration_values"} or throw_transformer_definition('No enumeration_values given for ' . $parameter->get_name . $Perl::ToPerl6::TransformerParameter::Behavior::Enumeration::PERIOD) 7 1 8
20 'No enumeration_values given for '
21 . $parameter->get_name()
22 . $PERIOD);
23 7 ▶ ref $valid_values eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $valid_values eq "ARRAY" or throw_transformer_definition('The value given for enumeration_values for ' . $parameter->get_name . ' is not an array reference.') 6 1 7
24 or throw_transformer_definition(
25 'The value given for enumeration_values for '
26 . $parameter->get_name()
27 . ' is not an array reference.');
28 6 ▶ scalar @{$valid_values} > 1
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$valid_values;} > 1 or throw_transformer_definition('There were not at least two valid values given for enumeration_values for ' . $parameter->get_name . $Perl::ToPerl6::TransformerParameter::Behavior::Enumeration::PERIOD) 4 2 6
29 or throw_transformer_definition(
30 'There were not at least two valid values given for'
31 . ' enumeration_values for '
32 . $parameter->get_name()
33 . $PERIOD);
34  
35 # Unfortunately, this has to be a reference, rather than a regular hash,
36 # due to a problem in Devel::Cycle
37 # (http://rt.cpan.org/Ticket/Display.html?id=25360) which causes
38 # t/92_memory_leaks.t to fall over.
39 4 my $value_lookup = { hashify( @{$valid_values} ) };
40 4 $parameter->_get_behavior_values()->{enumeration_values} = $value_lookup;
41  
42 my $allow_multiple_values =
43 4 $specification->{enumeration_allow_multiple_values};
44  
45 4 ▶ if ($allow_multiple_values) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_multiple_values) { } 2 2 4
46 $parameter->_set_parser(
47 7 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
48 # Normally bad thing, obscuring a variable in a outer scope
49 # with a variable with the same name is being done here in
50 # order to remain consistent with the parser function interface.
51 7 my ($transformer, $parameter, $config_string) = @_;
52  
53 7 my @potential_values;
54 7 my $value_string = $parameter->get_default_string();
55  
56 7 ▶ if (defined $config_string) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config_string) 5 2 7
57 5 $value_string = $config_string;
58 }
59  
60 7 ▶ if ( defined $value_string ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value_string) 6 1 7
61 6 @potential_values = words_from_string($value_string);
62  
63 my @bad_values =
64 6 grep { not exists $value_lookup->{$_} } @potential_values;
65 6 ▶ if (@bad_values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bad_values) 1 5 6
66 $transformer->throw_parameter_value_exception(
67 $parameter->get_name(),
68 $value_string,
69 undef,
70 q{contains invalid values: }
71 . join (q{, }, @bad_values)
72 . q{. Allowed values are: }
73 1 . join (q{, }, sort keys %{$value_lookup})
74 . qq{.\n},
75 );
76 }
77 }
78  
79 6 my %actual_values = hashify(@potential_values);
80  
81 6 $transformer->__set_parameter_value($parameter, \%actual_values);
82  
83 6 return;
84 }
85 2 );
86 } else {
87 $parameter->_set_parser(
88 5 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
89 # Normally bad thing, obscuring a variable in a outer scope
90 # with a variable with the same name is being done here in
91 # order to remain consistent with the parser function interface.
92 5 my ($transformer, $parameter, $config_string) = @_;
93  
94 5 my $value_string = $parameter->get_default_string();
95  
96 5 ▶ if (defined $config_string) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config_string) 3 2 5
97 3 $value_string = $config_string;
98 }
99  
100 5 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value_string and $Perl::ToPerl6::TransformerParameter::Behavior::Enumeration::EMPTY ne $value_string and not defined $value_lookup->{$value_string}) 1 4 5
Condition83%5/6
defined $value_string and $Perl::ToPerl6::TransformerParameter::Behavior::Enumeration::EMPTY ne $value_string and not defined $value_lookup->{$value_string}
!ll&&!rl&&r
131
defined $value_string and $Perl::ToPerl6::TransformerParameter::Behavior::Enumeration::EMPTY ne $value_string
!ll&&!rl&&r
104
MC/DC66%2/3
defined $value_string and $Perl::ToPerl6::TransformerParameter::Behavior::Enumeration::EMPTY ne $value_string and not defined $value_lookup->{$value_string}
defined $value_string $Perl::ToPerl6::TransformerParameter::Behavior::Enumeration::EMPTY ne $value_string not defined $value_lookup->{$value_string}
Truth table75%3/4
A and B and not(C)
A= defined $value_stringB= $Perl::ToPerl6::TransformerParameter::Behavior::Enumeration::EMPTY ne $value_stringC= not defined $value_lookup->{$value_string}
ABCresult
0XX0
10X0
1100
1111
101 defined $value_string
102 and $EMPTY ne $value_string
103 and not defined $value_lookup->{$value_string}
104 ) {
105 $transformer->throw_parameter_value_exception(
106 $parameter->get_name(),
107 $value_string,
108 undef,
109 q{is not one of the allowed values: }
110 1 . join (q{, }, sort keys %{$value_lookup})
111 . qq{.\n},
112 );
113 }
114  
115 4 $transformer->__set_parameter_value($parameter, $value_string);
116  
117 4 return;
118 }
119 2 );
120 }
121  
122 4 return;
123 }
124  
125 #-----------------------------------------------------------------------------
126  
127 0 ▶sub generate_parameter_description {
Subroutine generate_parameter_description: not called CC=2 SCAR=17.9
Pod documented
128 0 my ($self, $parameter) = @_;
129  
130 0 my $description = $parameter->_get_description_with_trailing_period();
131 0 ▶ if ( $description ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($description) 0 0 0
132 0 $description .= qq{\n};
133 }
134  
135 0 my %values = %{$parameter->_get_behavior_values()->{enumeration_values}};
136 return
137 0 $description
138 . 'Valid values: '
139 . join (', ', sort keys %values)
140 . $PERIOD;
141 }
142  
143 #-----------------------------------------------------------------------------
144  
145 23 1;
146  
147 __END__
« lib/Perl/ToPerl6/Transformer/References/RewriteDereferences.pm ↑ Summary lib/Perl/ToPerl6/Transformer/RewriteSpecialLiterals.pm »