Test/Form/Group.pm

filter: Statementstmt 84.4% 38 / 45 Branchbran 80.0% 8 / 10 Conditioncond 33.3% 2 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 8 / 8 Podpod 80.0% 4 / 5 totaltotal 79.2% 61 / 77 CC 12 SCAR 20.5
CC
12
Cov
79%
_validate_radios
17.1
new
4.0
widgets_in
2.0
CRAP 7.8
« Test.pm ↑ Summary Test/Form/Widget/Menu.pm »
1 package CGI::Test::Form::Group;
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: Group.pm 411 2011-09-26 11:19:30Z nohuhu@nohuhu.org $
6 # $Name: cgi-test_0-104_t1 $
7 ################################################################
8 # Copyright (c) 2001, Raphael Manfredi
9 #
10 # You may redistribute only under the terms of the Artistic License,
11 # as specified in the README file that comes with the distribution.
12  
13 #
14 # This class records names of grouped objects (radio buttons, checkboxes),
15 # and which buttons belong to some named group.
16 #
17  
18 #
19 # ->new
20 #
21 # Creation routine
22 #
23 # From a listref of box widgets, build a hash table indexed by group name
24 # and listing all the buttons belonging to the named group. Each box is
25 # also made aware of this object.
26 #
27 sub new
28 32 ▶{
Subroutine new: called CC=4 SCAR=13.9
Pod undocumented
29 32 my $this = bless {}, shift; # The object is the hash table we use
30 32 my ($rlist) = @_;
31  
32 #
33 # Create map: "group name" => [list of buttons in group]
34 #
35  
36 32 foreach my $b (@$rlist)
37 {
38 128 my $gname = $b->name;
39 128 ▶ $this->{$gname} = [] unless exists $this->{$gname};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $this->{$gname} 48 80 128
40 128 push @{$this->{$gname}}, $b;
41 128 $b->set_group($this);
42 }
43  
44 32 ▶ $this->_validate_radios() if $rlist->[ 0 ]->is_radio();
Branch: 1 branch
BranchTrueFalseTotal
if $rlist->[0]->is_radio 16 16 32
45  
46 32 return $this;
47 }
48  
49 #
50 # Attribute access
51 #
52  
53 sub names
54 18 ▶{
Subroutine names: called CC=1 SCAR=0.0
Pod documented
55 18 my $this = shift;
56 18 return keys %{$this};
57 }
58  
59 #
60 # ->widgets_in
61 #
62 # Returns list of widgets held within named group, empty if none.
63 #
64 sub widgets_in
65 31 ▶{
Subroutine widgets_in: called CC=2 SCAR=6.9
Pod documented
66 31 my $this = shift;
67 31 my ($gname) = @_;
68  
69 31 ▶ my $list = $this->{$gname} || [];
Condition100%2/2
$this->{$gname} || []
l!l
301
MC/DC100%1/1
$this->{$gname} || []
$this->{$gname}
Truth table100%2/2
A
A= $this->{$gname}
Aresult
11
00
70 31 return @$list;
71 }
72  
73 #
74 # ->widget_count
75 #
76 # Returns amount of widgets held within named group, 0 if none.
77 #
78 sub widget_count
79 4 ▶{
Subroutine widget_count: called CC=2 SCAR=6.9
Pod documented
80 4 my $this = shift;
81 4 my ($gname) = @_;
82  
83 4 my $list = $this->{$gname};
84 4 ▶ return ref $list ? scalar(@$list) : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $list ? : 3 1 4
85 }
86  
87 #
88 # ->is_groupname
89 #
90 # Check whether name is that of a known widget group.
91 #
92 sub is_groupname
93 2 ▶{
Subroutine is_groupname: called CC=1 SCAR=0.0
Pod documented
94 2 my $this = shift;
95 2 my ($gname) = @_;
96  
97 2 return exists $this->{$gname};
98 }
99  
100 #
101 # ->_validate_radios
102 #
103 # When groupping radio buttons, make sure there is at least one such
104 # button selected, otherwise mark the first as selected. Also ensure
105 # exactly one radio is selected, or unselect all extra.
106 #
107 sub _validate_radios
108 16 ▶{
Subroutine _validate_radios: called CC=7 SCAR=28.4
109 16 my $this = shift;
110  
111 16 foreach my $gname ($this->names)
112 {
113 16 my @checked = grep {$_->is_checked} $this->widgets_in($gname);
114 16 my $checked = @checked;
115  
116 16 ▶ if ($checked > 1)
Branch: 2 branches
BranchTrueFalseTotal
if ($checked > 1) { } 0 16 16
elsif ($checked == 0) { } 0 16 16
117 {
118 0 my $first = shift @checked;
119  
120 #
121 # NB: we're not calling uncheck() nor set_is_checked() to fix
122 # incorrectly configured radio buttons, since it is normally an
123 # invalid operation. We're resettting the attribute directly.
124 #
125  
126 0 ▶ warn
Condition0%0/2
$first->value || ''
l!l
00
MC/DC0%0/1
$first->value || ''
$first->value
Truth table0%0/2
A
A= $first->value
Aresult
11
00
127 "found %d checked %ss for '%s', keeping first (tag \"%s\")",
128 $checked, $first->gui_type, $gname, ($first->value || "");
129  
130 0 foreach my $b (@checked)
131 {
132 0 $b->{is_checked} = 0; # Direct access
133 }
134 }
135 elsif ($checked == 0)
136 {
137 0 my $first = $this->{$gname}->[ 0 ];
138 0 ▶ warn "no checked %ss for '%s', checking first (tag \"%s\")",
Condition0%0/2
$first->value || ''
l!l
00
MC/DC0%0/1
$first->value || ''
$first->value
Truth table0%0/2
A
A= $first->value
Aresult
11
00
139 $first->gui_type, $gname, ($first->value || "");
140 0 $first->{is_checked} = 1; # Direct access
141 }
142  
143 }
144  
145 16 return;
146 }
147  
148 14 1;
149  
150 =head1 NAME
151  
152 CGI::Test::Form::Group - Records groups of box-type widgets
153  
154 =head1 SYNOPSIS
155  
156 # $form is a CGI::Test::Form object
157  
158 use CGI::Test;
159  
160 my $rgroup = $form->radio_groups;
161 ok 1, defined $rgroup;
162  
163 my @title = $rgroup->widgets_in("title");
164 my ($mister) = grep { $_->value eq "Mr" } @title;
165 ok 2, $mister->is_checked;
166  
167 =head1 DESCRIPTION
168  
169 This class is a container for box-type widgets, i.e. radio buttons and
170 checkboxes, which may be groupped by name.
171  
172 It can be queried to easily retrieve widgets belonging to a group, or to
173 get all the group names.
174  
175 It is also used internally by C<CGI::Test> to keep track of associated
176 radio buttons, so that checking one automatically unchecks the others in the
177 same group.
178  
179 =head1 INTERFACE
180  
181 The following features are available:
182  
183 =over 4
184  
185 =item C<is_groupname> I<name>
186  
187 Checks whether I<name> is the name of a group.
188  
189 =item C<names>
190  
191 Returns a list of group names, in random order.
192  
193 =item C<widget_count> I<groupname>
194  
195 Returns amount of widgets held in I<groupname>, 0 if none.
196  
197 =item C<widgets_in> I<groupname>
198  
199 Returns a list of all the widgets in the given I<groupname>. If the
200 name is not a valid group name, the list will be empty.
201  
202 =back
203  
204 =head1 AUTHORS
205  
206 The original author is Raphael Manfredi.
207  
208 Steven Hilton was long time maintainer of this module.
209  
210 Current maintainer is Alexander Tokarev F<E<lt>tokarev@cpan.orgE<gt>>.
211  
212 =head1 SEE ALSO
213  
214 CGI::Test::Form(3), CGI::Test::Form::Widget::Box(3).
215  
216 =cut
217  
« Test.pm ↑ Summary Test/Form/Widget/Menu.pm »