blib/lib/Catmandu/Fix/Condition/SimpleCompareTest.pm

filter: Statementstmt 98.3% 58 / 59 Branchbran 50.0% 2 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 94.5% 69 / 73 CC 3 SCAR 4.4
CC
3
Cov
95%
emit
3.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.5
« blib/lib/Catmandu/Fix/Parser.pm ↑ Summary blib/lib/Catmandu/Fix/Inlineable.pm »
1 package Catmandu::Fix::Condition::SimpleCompareTest;
2  
3 1 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '1.0201_02';
6  
7 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 with 'Catmandu::Fix::Condition';
11  
12 1 requires 'path';
13 1 requires 'path2';
14 1 requires 'emit_test';
15  
16 sub emit {
17 1 ▶ my ($self, $fixer, $label) = @_;
Subroutine emit: called CC=3 SCAR=11.0
Pod undocumented
18  
19 1 my $path = $fixer->split_path($self->path);
20 1 my $key = pop @$path;
21  
22 1 my $path2 = $fixer->split_path($self->path2);
23 1 my $key2 = pop @$path2;
24  
25 1 my $pass_fixes = $self->pass_fixes;
26 1 my $fail_fixes = $self->fail_fixes;
27  
28 1 my $fail_label;
29 my $fail_block = $fixer->emit_block(
30 sub {
31 1 ▶ $fail_label = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
32 1 $fixer->emit_fixes($fail_fixes);
33 }
34 1 );
35  
36 1 my $perl = "no if ($] >= 5.018), 'warnings' => 'experimental';";
37  
38 1 my $has_match_var = $fixer->generate_var;
39 1 $perl .= $fixer->emit_declare_vars($has_match_var, '0');
40  
41 1 my $vals_1 = $fixer->generate_var;
42 1 $perl .= $fixer->emit_declare_vars($vals_1, '{}');
43  
44 $perl .= $fixer->emit_walk_path(
45 $fixer->var,
46 $path,
47 sub {
48 1 ▶ my $var = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
49 $fixer->emit_get_key(
50 $var, $key,
51 sub {
52 1 ▶ my $var = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
53 1 my $perl = "${has_match_var} ||= 1;";
54 1 $perl .= "${vals_1} = ${var};";
55 1 $perl;
56 }
57 1 );
58 }
59 1 );
60  
61 1 my $vals_2 = $fixer->generate_var;
62 1 $perl .= $fixer->emit_declare_vars($vals_2, '{}');
63  
64 $perl .= $fixer->emit_walk_path(
65 $fixer->var,
66 $path2,
67 sub {
68 1 ▶ my $var = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
69 $fixer->emit_get_key(
70 $var, $key2,
71 sub {
72 1 ▶ my $var = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
73 1 my $perl = "${has_match_var} ||= 1;";
74 1 $perl .= "${vals_2} = ${var};";
75 1 $perl;
76 }
77 1 );
78 }
79 1 );
80  
81 1 $perl .= "unless (" . $self->emit_test($vals_1, $vals_2, $fixer) . ") {";
82 1 ▶ if (@$fail_fixes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$fail_fixes) { } 1 0 1
83 1 $perl .= "goto ${fail_label};";
84 }
85 else {
86 0 $perl .= "last ${label};";
87 }
88 1 $perl .= "}";
89  
90 1 $perl .= "if (${has_match_var}) {";
91  
92 1 $perl .= $fixer->emit_fixes($pass_fixes);
93  
94 1 $perl .= "last ${label};";
95 1 $perl .= "}";
96  
97 1 ▶ if (@$fail_fixes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$fail_fixes) 1 0 1
98 1 $perl .= $fail_block;
99 }
100  
101 1 $perl;
102 }
103  
104 1 1;
105  
106 __END__
107  
108 =pod
109  
110 =head1 NAME
111  
112 Catmandu::Fix::Condition::SimpleCompareTest - Base class to ease the construction of compare conditionals
113  
114 =head1 SYNOPSIS
115  
116 package Catmandu::Fix::Condition::has_equal_type
117  
118 use Catmandu::Sane;
119 use Moo;
120 use Catmandu::Fix::Has;
121  
122 has path => (fix_arg => 1);
123 has path2 => (fix_arg => 1);
124  
125 with 'Catmandu::Fix::Condition::SimpleCompareTest';
126  
127 sub emit_test {
128 my ($self, $var, $var2, $fixer) = @_;
129 "is_value(${var}) && is_value(${var2}) && ref ${var} eq ref ${var2}";
130 }
131  
132 1;
133  
134 # Now you can write in your fixes
135 has_equal_type(my_field_1,my_field_2) # True when my_field_1 and my_field_2 have
136 # the same refence type (both scalas, arrays, hashes)
137  
138 =head1 SEE ALSO
139  
140 L<Catmandu::Fix::Condition::SimpleAllTest>,
141 L<Catmandu::Fix::Condition::SimpleAnyTest>
142  
143 =cut
« blib/lib/Catmandu/Fix/Parser.pm ↑ Summary blib/lib/Catmandu/Fix/Inlineable.pm »