CLI/TestCaseSet.pm

filter: Statementstmt 16.2% 12 / 74 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 44.4% 4 / 9 Podpod 75.0% 3 / 4 totaltotal 17.4% 19 / 109 CC 18 SCAR 38.8
CC
18
Cov
12%
_get_schema_terms
90.0
apply_modifier
56.0
new
12.0
CRAP 48.3
↑ Summary CLI.pm »
1 package Zonemaster::CLI::TestCaseSet;
2 1 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Zonemaster::CLI::TestCaseSet - Manage and modify Zonemaster test case selections
11  
12 =head1 SYNOPSIS
13  
14 use Zonemaster::CLI::TestCaseSet;
15  
16 # Define the names of the available test modules and their test cases
17 my $schema = {
18 alpha => [qw( alpha01 alpha02 alpha03 )],
19 beta => [qw( beta01 beta02 )],
20 };
21  
22 # Construct an initial selection of test cases
23 my $selection = Zonemaster::CLI::TestCaseSet->new(
24 [qw( alpha01 alpha02 alpha03 beta01 )],
25 $schema,
26 );
27  
28 # Parse and apply a modifier expression
29 my @modifiers = Zonemaster::CLI::TestCaseSet->parse_modifier_expr( '-alpha+alpha02' );
30 while ( @modifiers ) {
31 my ( $op, $term ) = splice @modifiers, 0, 2;
32 $selection->apply_modifier( $op, $term )
33 or die "Error: Unrecognized term '$term'.\n";
34 }
35  
36 # Output final test case selection
37 print join( ' ', $selection->to_list ); # alpha02 beta01
38  
39 =head1 DESCRIPTION
40  
41 Zonemaster::CLI::TestCaseSet represents a mutable selection of test cases,
42 together with an immutable schema defining available test modules and their
43 associated test cases.
44  
45 The schema is defined as a mapping of test module names to their associated test
46 case names.
47  
48 The selection can be adjusted using modifier expressions.
49  
50 =head2 MODIFIER EXPRESSIONS
51  
52 A modifier expression describes a change to the current selection.
53 Expressions combine terms using operators, e.g., C<'-alpha+alpha02'>.
54  
55 These operators are supported:
56  
57 =over 4
58  
59 =item C<'+'> (union)
60  
61 Add test cases to the current selection.
62 The set of test cases to add is the expansion of C<$term>.
63  
64 =item C<'-'> (difference)
65  
66 Remove test cases from the current selection.
67 The set of test cases to remove is the expansion of C<$term>.
68  
69 =item C<''> (replace)
70  
71 Replace the current selection.
72 The new selection is the set of test cases expanded from C<$term>.
73  
74 =back
75  
76 Terms expand into sets of test cases in one of three ways:
77  
78 =over 4
79  
80 =item C<all>
81  
82 Expands to all available test cases defined by the schema.
83  
84 =item Test module name
85  
86 Expands to all test cases associated with the test module.
87  
88 =item Test case name
89  
90 Expands directly to the specified test case itself.
91 Test cases may be specified plainly (e.g., C<Case10>) or fully qualified
92 (module/testcase, e.g., C<Case/Case10>).
93  
94 =back
95  
96 Term matching is case-insensitive.
97  
98 =cut
99  
100 =head1 CONSTRUCTORS
101  
102 =head2 new( $selection, $schema )
103  
104 Construct a new TestCaseSet object.
105  
106 =over 4
107  
108 =item C<$selection> (arrayref)
109  
110 Initial selection of test case names.
111  
112 =item C<$schema> (hashref)
113  
114 A hash mapping test module names to arrays of their associated test case names.
115  
116 =back
117  
118 Dies if:
119 - Any test case name in C<$schema> is repeated.
120 - C<$selection> contains names not found in C<$schema>.
121  
122 =cut
123  
124 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
125 0 my ( $class, $selection, $schema ) = @_;
126  
127 0 my %cases = map { lc $_ => 1 } map { @{$_} } values %$schema;
128 0 for my $case ( @$selection ) {
129 0 ▶ if ( !exists $cases{ lc $case } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $cases{lc $case}) 0 0 0
130 0 croak "Unrecognized initial test case '$case'";
131 }
132 }
133  
134 my $obj = {
135 0 _selection => { map { lc $_ => 1 } @$selection },
136 _terms => _get_schema_terms( $schema ),
137 };
138  
139 0 bless $obj, $class;
140  
141 0 return $obj;
142 }
143  
144 =head1 CLASS METHODS
145  
146 parse_modifier_expr( $modifier_expr )
147  
148 Parse a string containing a modifier expression and returns a list of
149 alternating operators and terms.
150  
151 The returned list always starts with an operator.
152  
153 For example, parsing C<'-alpha+beta02'> returns:
154  
155 ('-', 'alpha', '+', 'beta02')
156  
157 =cut
158  
159 0 ▶sub parse_modifier_expr {
Subroutine parse_modifier_expr: not called CC=2 SCAR=17.9
Pod undocumented
160 0 my ( $class, $modifier_expr ) = @_;
161  
162 0 my @modifiers;
163 0 for my $op_and_term ( split /(?=[+-])/, $modifier_expr ) {
164 0 $op_and_term =~ /([+-]?)(.*)/;
165 0 my ( $op, $term ) = ( $1, $2 );
166  
167 0 push @modifiers, ( $op, $term );
168 }
169  
170 0 return @modifiers;
171 }
172  
173 =head1 INSTANCE METHODS
174  
175 =head2 apply_modifier( $operator, $term )
176  
177 Update the selection using the given operator and term.
178  
179 Returns true if successful, or false if the term could not be expanded based on
180 the schema.
181  
182 Dies if the operator is invalid.
183  
184 =head3 Example:
185  
186 $selection->apply_modifier('+', 'beta')
187 or die "Unrecognized term";
188  
189 =cut
190  
191 0 ▶sub apply_modifier {
Subroutine apply_modifier: not called CC=7 SCAR=40.3
Pod documented
192 0 my ( $self, $op, $term ) = @_;
193  
194 0 my $cases_ref = $self->{_terms}{ lc $term };
195  
196 0 ▶ if ( !defined $cases_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cases_ref) 0 0 0
197 0 return 0;
198 }
199  
200 0 ▶ if ( $op eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "") 0 0 0
201 0 $self->{_selection} = {};
202 0 $op = '+';
203 }
204  
205 0 ▶ if ( $op eq '-' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq '-') { } 0 0 0
elsif ($op eq '+') { } 0 0 0
206 0 for my $case ( @$cases_ref ) {
207 0 delete $self->{_selection}{$case};
208 }
209 }
210 elsif ( $op eq '+' ) {
211 0 for my $case ( @$cases_ref ) {
212 0 $self->{_selection}{$case} = 1;
213 }
214 }
215 else {
216 0 croak "Unrecognized operator '$op'";
217 }
218  
219 0 return 1;
220 } ## end sub apply_modifier
221  
222 =head2 to_list
223  
224 Return a lowercase list of the currently selected test case names.
225  
226 =cut
227  
228 0 ▶sub to_list {
Subroutine to_list: not called CC=1 SCAR=6.9
Pod documented
229 0 my ( $self ) = @_;
230  
231 0 return sort keys %{ $self->{_selection} };
232 }
233  
234 0 ▶sub _get_schema_terms {
Subroutine _get_schema_terms: not called CC=9 SCAR=45.0
235 0 my ( $schema ) = @_;
236  
237 0 my $terms = {};
238 0 $terms->{all} = [];
239  
240 0 for my $module ( keys %$schema ) {
241 0 ▶ if ( lc $module eq 'all' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $module eq "all") 0 0 0
242 0 croak "test module name must not be 'all'";
243 }
244 0 ▶ if ( $module =~ qr{/} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module =~ qr"/"u) 0 0 0
245 0 croak "test module name contains forbidden character '/': '$module'";
246 }
247 0 ▶ if ( exists $terms->{ lc $module } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$terms{lc $module}) 0 0 0
248 0 croak "found test module with same name as another test case or test module: '$module'";
249 }
250 0 $terms->{ lc $module } = [];
251 0 for my $case ( @{ $schema->{$module} } ) {
252 0 ▶ if ( lc $case eq 'all' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $case eq "all") 0 0 0
253 0 croak "test case name must not be 'all'";
254 }
255 0 ▶ if ( $case =~ qr{/} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($case =~ qr"/"u) 0 0 0
256 0 croak "test case name contains forbidden character '/': '$case'";
257 }
258 0 ▶ if ( exists $terms->{ lc $case } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$terms{lc $case}) 0 0 0
259 0 croak "found test case with same name as another test case or test module: '$case'";
260 }
261 0 $terms->{ lc $case } = [$case];
262 0 $terms->{ lc "$module/$case" } = [$case];
263 0 push @{ $terms->{ lc $module } }, $case;
264 0 push @{ $terms->{all} }, $case;
265 }
266 } ## end for my $module ( keys %$schema)
267  
268 0 return $terms;
269 } ## end sub _get_schema_terms
270  
271 1 1;
↑ Summary CLI.pm »