Test/Form/Widget/Menu.pm

filter: Statementstmt 78.3% 87 / 111 Branchbran 64.2% 18 / 28 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 79.1% 19 / 24 Podpod 93.7% 15 / 16 totaltotal 73.5% 142 / 193 CC 19 SCAR 18.3
CC
19
Cov
73%
_parse_options
15.5
set_selected
7.2
reset_state
6.0
CRAP 6.2
« Test/Form/Group.pm ↑ Summary Test/Page/HTML.pm »
1 package CGI::Test::Form::Widget::Menu;
2 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 ##################################################################
5 # $Id: Menu.pm 411 2011-09-26 11:19:30Z nohuhu@nohuhu.org $
6 # $Name: cgi-test_0-104_t1 $
7 ##################################################################
8 #
9 # Copyright (c) 2001, Raphael Manfredi
10 #
11 # You may redistribute only under the terms of the Artistic License,
12 # as specified in the README file that comes with the distribution.
13 #
14  
15 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 #
18 # This class models a FORM menu (either a popup or a scrollable list).
19 #
20  
21 14 ▶use base qw(CGI::Test::Form::Widget);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 14 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 ############################################################
26 #
27 # ->_parse_options
28 #
29 # Parse <OPTION> items held within the <SELECT> node.
30 # We ignore <OPTGROUP> items, since those are only there for grouping options,
31 # and cannot be individually selected as such.
32 #
33 # The following attributes are used to record the options:
34 #
35 # option_labels listref of option labels, in the order they appear
36 # option_values listref of option values, in the order they appear
37 # known_values hashref, recording valid *values*
38 # selected hashref, recording selected *values*
39 # selected_count amount of selected items
40 #
41 ############################################################
42 sub _parse_options
43 32 ▶{
Subroutine _parse_options: called CC=11 SCAR=27.4
44 32 my $this = shift;
45 32 my ($node) = shift;
46  
47 32 my $labels = $this->{option_labels} = [];
48 32 my $values = $this->{option_values} = [];
49 32 my $selected = $this->{selected} = {};
50 32 my $known = $this->{known_values} = {};
51 32 my $count = 0;
52 32 my %seen;
53  
54 32 ▶ my @nodes = $node->look_down(sub {1});
Subroutine __ANON__: called CC=1 SCAR=0.0
55 32 shift @nodes; # first node is the <SELECT> itself
56  
57 32 foreach my $opt (@nodes)
58 {
59 272 ▶ next if $opt->tag() eq "optgroup";
Branch: 1 branch
BranchTrueFalseTotal
if $opt->tag eq "optgroup" 0 272 272
60 272 ▶ unless ($opt->tag() eq "option")
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt->tag eq "option") 0 272 272
61 {
62 0 warn "ignoring non-option tag '%s' within SELECT",
63 uc($opt->tag());
64 0 next;
65 }
66  
67 #
68 # The option label is normally the content of the <OPTION> tag.
69 # However, if there is a LABEL= within the tag, it should be used
70 # in preference to the option content, says the W3C's norm.
71 #
72  
73 272 ▶ my $label = $opt->attr("label") || $opt->as_text();
Condition33%1/3
$opt->attr('label') || $opt->as_text
l!l&&r!l&&!r
02720
MC/DC0%0/2
$opt->attr('label') || $opt->as_text
$opt->attr('label') $opt->as_text
Truth table33%1/3
A || B
A= $opt->attr('label')B= $opt->as_text
ABresult
1X1
011
000
74 272 my $is_selected = $opt->attr("selected");
75 272 my $value = $opt->attr("value");
76  
77 272 ▶ unless (defined $value)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 272 272
78 {
79 0 warn "ignoring OPTION tag with no value: %s", $opt->starttag();
80 0 next;
81 }
82  
83 #
84 # It is not really an error to have duplicate values, but is it
85 # a good interface style? The user will be faced with multiple
86 # labels to choose from, some of them being handled in the same way
87 # since they bear the same value... Tough choice... Let's warn!
88 #
89  
90 warn "duplicate value '%s' in OPTION for SELECT NAME=\"%s\"",
91 $value, $this->name
92 272 ▶ if $seen{$value}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$value}++ 0 272 272
93  
94 272 push @$labels, $label;
95 272 push @$values, $value;
96 272 $known->{$value}++; # help them spot dups
97 272 ▶ if ($is_selected)
Branch: 1 branch
BranchTrueFalseTotal
if ($is_selected) 34 238 272
98 {
99 34 $selected->{$value}++;
100 34 $count++;
101 }
102 }
103  
104 #
105 # A popup menu needs to have at least one item selected. We're the
106 # user agent, and we get to choose which item we'll select implicitely.
107 # Use the first listed value, if any.
108 #
109  
110 32 ▶ if ($count == 0 && $this->is_popup() && @$values)
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 0 and $this->is_popup and @$values) 0 32 32
Condition33%2/6
$count == 0 and $this->is_popup and @$values
!ll&&!rl&&r
3200
$count == 0 and $this->is_popup
!ll&&!rl&&r
3200
MC/DC0%0/3
$count == 0 and $this->is_popup and @$values
$count == 0 $this->is_popup @$values
Truth table25%1/4
A and B and C
A= $count == 0B= $this->is_popupC= @$values
ABCresult
0XX0
10X0
1100
1111
111 {
112 0 my $first = $values->[ 0 ];
113 0 $selected->{$first}++;
114 0 $count++;
115 0 warn "implicitely selecting OPTION '%s' for SELECT NAME=\"%s\"",
116 $first, $this->name();
117 }
118  
119 32 $this->{selected_count} = $count;
120  
121 32 return;
122 }
123  
124 ############################################################
125 #
126 # ->_is_successful -- defined
127 #
128 # Is the enabled widget "successful", according to W3C's specs?
129 # Any menu with at least one selected item is.
130 #
131 ############################################################
132 sub _is_successful
133 38 ▶{
Subroutine _is_successful: called CC=1 SCAR=0.0
134 38 my $this = shift;
135 38 return $this->selected_count > 0;
136 }
137  
138 ############################################################
139 #
140 # ->submit_tuples -- redefined
141 #
142 # Returns list of (name => value) tuples that should be part of the
143 # submitted form data.
144 #
145 ############################################################
146 sub submit_tuples
147 19 ▶{
Subroutine submit_tuples: called CC=1 SCAR=0.0
Pod documented
148 19 my $this = shift;
149  
150 19 my $name = $this->name();
151 19 my $selected = $this->selected();
152  
153 my @tuples =
154 19 map {$name => $_} grep {$selected->{$_}} @{$this->option_values()};
155  
156 19 return @tuples;
157 }
158  
159 #
160 # Attribute access
161 #
162 ############################################################
163 sub multiple
164 28 ▶{
Subroutine multiple: called CC=1 SCAR=0.0
Pod documented
165 28 my $this = shift;
166 28 return $this->{multiple};
167 } # Set by Menu::List
168  
169 ############################################################
170 sub option_labels
171 0 ▶{
Subroutine option_labels: not called CC=1 SCAR=6.9
Pod documented
172 0 my $this = shift;
173 0 return $this->{option_labels};
174 }
175 ############################################################
176 sub option_values
177 21 ▶{
Subroutine option_values: called CC=1 SCAR=0.0
Pod documented
178 21 my $this = shift;
179 21 return $this->{option_values};
180 }
181 ############################################################
182 sub known_values
183 66 ▶{
Subroutine known_values: called CC=1 SCAR=0.0
Pod documented
184 66 my $this = shift;
185 66 return $this->{known_values};
186 }
187 ############################################################
188 sub selected
189 104 ▶{
Subroutine selected: called CC=1 SCAR=0.0
Pod documented
190 104 my $this = shift;
191 104 return $this->{selected};
192 }
193 ############################################################
194 sub selected_count
195 40 ▶{
Subroutine selected_count: called CC=1 SCAR=0.0
Pod documented
196 40 my $this = shift;
197 40 return $this->{selected_count};
198 }
199 ############################################################
200 sub old_selected
201 0 ▶{
Subroutine old_selected: not called CC=1 SCAR=6.9
Pod undocumented
202 0 my $this = shift;
203 0 return $this->{old_selected};
204 }
205  
206 #
207 # Selection shortcuts
208 #
209  
210 ############################################################
211 sub select
212 22 ▶{
Subroutine select: called CC=1 SCAR=0.0
Pod documented
213 22 my $this = shift;
214 22 my $item = shift;
215 22 $this->set_selected($item, 1);
216 }
217 ############################################################
218 sub unselect
219 6 ▶{
Subroutine unselect: called CC=1 SCAR=0.0
Pod documented
220 6 my $this = shift;
221 6 my $item = shift;
222 6 $this->set_selected($item, 0);
223 }
224  
225 #
226 # Global widget predicates
227 #
228  
229 ############################################################
230 sub is_read_only
231 0 ▶{
Subroutine is_read_only: not called CC=1 SCAR=6.9
Pod documented
232 0 return 1;
233 }
234  
235 #
236 # High-level classification predicates
237 #
238  
239 ############################################################
240 sub is_menu
241 2 ▶{
Subroutine is_menu: called CC=1 SCAR=0.0
Pod documented
242 2 return 1;
243 }
244  
245 #
246 # Predicates for menus
247 #
248  
249 ############################################################
250 sub is_popup
251 0 ▶{
Subroutine is_popup: not called CC=1 SCAR=6.9
Pod documented
252 0 confess "deferred";
253 }
254  
255 ############################################################
256 #
257 # ->is_selected
258 #
259 # Checks whether given value is selected.
260 #
261 ############################################################
262 sub is_selected
263 38 ▶{
Subroutine is_selected: called CC=2 SCAR=7.9
Pod documented
264 38 my $this = shift;
265 38 my ($value) = @_;
266  
267 38 ▶ unless ($this->known_values->{$value})
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->known_values->{$value}) 0 38 38
268 {
269 0 carp "unknown value \"%s\" in $this", $value;
270 0 return 0;
271 }
272  
273 38 return exists $this->selected->{$value};
274 }
275  
276 ############################################################
277 #
278 # ->set_selected
279 #
280 # Change "selected" status for a menu value.
281 #
282 ############################################################
283 sub set_selected
284 28 ▶{
Subroutine set_selected: called CC=7 SCAR=19.7
Pod documented
285 28 my $this = shift;
286 28 my ($value, $state) = @_;
287  
288 28 ▶ unless ($this->known_values->{$value})
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->known_values->{$value}) 0 28 28
289 {
290 0 carp "unknown value \"%s\" in $this", $value;
291 0 return;
292 }
293  
294 28 my $is_selected = $this->is_selected($value);
295 28 ▶ return if !$is_selected == !$state; # No change // WTF? -nohuhu
Branch: 1 branch
BranchTrueFalseTotal
if !$is_selected == !$state 0 28 28
296  
297 #
298 # Save selected status for all the values the first time a change is made.
299 #
300  
301 $this->{old_selected} = dclone $this->{selected}
302 28 ▶ unless exists $this->{old_selected};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $this->{"old_selected"} 16 12 28
303  
304 #
305 # If multiple selection is not authorized, clear the selection list.
306 #
307  
308 28 my $selected = $this->selected();
309 28 ▶ %$selected = () unless $this->multiple();
Branch: 1 branch
BranchTrueFalseTotal
unless $this->multiple 6 22 28
310  
311 28 ▶ $selected->{$value} = 1 if $state;
Branch: 1 branch
BranchTrueFalseTotal
if $state 22 6 28
312 28 ▶ delete $selected->{$value} unless $state;
Branch: 1 branch
BranchTrueFalseTotal
unless $state 6 22 28
313 28 $this->{selected_count} = scalar keys %$selected;
314  
315 28 return;
316 }
317  
318 ############################################################
319 #
320 # ->reset_state
321 #
322 # Called when a "Reset" button is pressed to restore the value the widget
323 # had upon form entry.
324 #
325 ############################################################
326 sub reset_state
327 0 ▶{
Subroutine reset_state: not called CC=2 SCAR=17.9
Pod documented
328 0 my $this = shift;
329  
330 0 ▶ return unless exists $this->{old_selected};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $this->{"old_selected"} 0 0 0
331 0 $this->{selected} = delete $this->{old_selected};
332 0 $this->{selected_count} = scalar keys %{$this->selected()};
333  
334 0 return;
335 }
336  
337 14 1;
338  
339 =head1 NAME
340  
341 CGI::Test::Form::Widget::Menu - Abstract representation of a menu
342  
343 =head1 SYNOPSIS
344  
345 # Inherits from CGI::Test::Form::Widget
346  
347 =head1 DESCRIPTION
348  
349 This class is the abstract representation of a menu from which one can choose
350 one or several items, i.e. either a popup menu or a scrollable list
351 (with possibly multiple selections).
352  
353 There is an interface to query the selected items, get at the presented
354 labels and associated values, and naturally C<select()> or C<unselect()>
355 items.
356  
357 =head1 INTERFACE
358  
359 The interface is the same as the one described in L<CGI::Test::Form::Widget>,
360 with the following additions:
361  
362 =head2 Attributes
363  
364 =over 4
365  
366 =item C<known_values>
367  
368 An hash reference, recording valid menu values, as tuples
369 (I<value> => I<count>), with I<count> set to the number of times the same
370 value is re-used amongst the proposed options.
371  
372 =item C<multiple>
373  
374 Whether menu allows multiple selections.
375  
376 =item C<option_labels>
377  
378 A list reference, providing the labels to choose from, in the order in which
379 they appear. The retained labels are either the content of the <OPTION>
380 elements, or the value of their C<label> attribute, when specified.
381  
382 =item C<option_values>
383  
384 A list reference, providing the underlying values that the user chooses from
385 when he selects labels, in the order in which they appear in the menu.
386  
387 =item C<selected>
388  
389 An hash reference, whose keys are the selected values.
390  
391 =item C<selected_count>
392  
393 The amount of currently selected items.
394  
395 =back
396  
397 =head2 Attribute Setting
398  
399 =over 4
400  
401 =item C<select> I<value>
402  
403 Mark the option I<value> as selected. If C<multiple> is false, any
404 previously selected value is automatically unselected.
405  
406 Note that this takes a I<value>, not a I<label>.
407  
408 =item C<unselect> I<value>
409  
410 Unselect an option I<value>. It is not possible to do that on a popup
411 menu: you must C<select> another item to unselect any previously selected one.
412  
413 =back
414  
415 =head2 Menu Probing
416  
417 =over 4
418  
419 =item C<is_selected> I<value>
420  
421 Test whether an option I<value> is currently selected or not. This is
422 not testing a label, but a value, which is what the script will get back
423 eventually: labels are there for human consumption only.
424  
425 =back
426  
427 =head2 Widget Classification Predicates
428  
429 There is an additional predicate to distinguish between a popup menu (single
430 selection mandatory) from a scrolling list (multiple selection allowed, and
431 may select nothing).
432  
433 =over 4
434  
435 =item C<is_popup>
436  
437 Returns I<true> for a popup menu.
438  
439 =back
440  
441 =head2 Miscellaneous Features
442  
443 Although documented, those features are more targetted for
444 internal use...
445  
446 =over 4
447  
448 =item C<set_selected> I<value>, I<flag>
449  
450 Change the selection status of an option I<value>.
451  
452 You should use the C<select> and C<unselect> convenience routines instead
453 of calling this feature.
454  
455 =back
456  
457 =head1 AUTHORS
458  
459 The original author is Raphael Manfredi.
460  
461 Steven Hilton was long time maintainer of this module.
462  
463 Current maintainer is Alexander Tokarev F<E<lt>tokarev@cpan.orgE<gt>>.
464  
465 =head1 SEE ALSO
466  
467 CGI::Test::Form::Widget(3),
468 CGI::Test::Form::Widget::Menu::List(3),
469 CGI::Test::Form::Widget::Menu::Popup(3).
470  
471 =cut
472  
« Test/Form/Group.pm ↑ Summary Test/Page/HTML.pm »