Test/Form/Widget/Button.pm

filter: Statementstmt 66.6% 36 / 54 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 55.5% 10 / 18 Podpod 81.8% 9 / 11 totaltotal 65.8% 56 / 85 CC 2 SCAR 4.2
CC
2
Cov
66%
new
2.0
_init
2.0
is_enhanced
2.0
CRAP 1.5
« Test/Page.pm ↑ Summary Test/Form/Widget/Button/Reset.pm »
1 package CGI::Test::Form::Widget::Button;
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: Button.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 button.
19 #
20  
21 14 ▶use base qw(CGI::Test::Form::Widget);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 ############################################################
24 #
25 # ->new
26 #
27 # Creation routine for <BUTTON> elements.
28 #
29 ############################################################
30 sub new
31 64 ▶{
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
32 64 my $this = bless {}, shift;
33 64 my ($node, $form) = @_;
34  
35 #
36 # Can't create a CGI::Test::Form::Widget::Button object, only heirs.
37 #
38  
39 64 ▶ confess "%s is a deferred class", __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
if ref $this eq __PACKAGE__ 0 64 64
40 if ref $this eq __PACKAGE__;
41  
42 64 $this->_common_init($form);
43  
44 #
45 # We don't keep any reference on the node.
46 # Analyze the HTML tree to determine some parameters.
47 #
48  
49 64 $this->_init_button($node);
50  
51 64 return $this;
52 }
53  
54 ############################################################
55 #
56 # %attr
57 # %attr_button
58 #
59 # Defines which HTML attributes we should look at within the node, and how
60 # to translate that into class attributes. The %attr_button is specific
61 # to the <BUTTON> tags.
62 #
63 ############################################################
64  
65 14 my %attr = ('name' => 'name',
66 'value' => 'value',
67 'disabled' => 'is_disabled',
68 );
69  
70 14 my %attr_button = (%attr,);
71  
72 ############################################################
73 #
74 # ->_init
75 #
76 # Per-widget initialization routine, for <INPUT>.
77 # Parse HTML node to determine our specific parameters.
78 #
79 ############################################################
80 sub _init
81 0 ▶{
Subroutine _init: not called CC=1 SCAR=6.9
82 0 my $this = shift;
83 0 my ($node) = shift;
84 0 $this->_parse_attr($node, \%attr);
85 0 $this->{is_enhanced} = 0;
86 0 $this->{is_pressed} = 0;
87 0 return;
88 }
89  
90 ############################################################
91 #
92 # ->_init_button
93 #
94 # Per-widget initialization routine, for <BUTTON>.
95 # Parse HTML node to determine our specific parameters.
96 #
97 ############################################################
98 sub _init_button
99 64 ▶{
Subroutine _init_button: called CC=1 SCAR=0.0
100 64 my $this = shift;
101 64 my ($node) = shift;
102 64 $this->_parse_attr($node, \%attr_button);
103 64 $this->{is_enhanced} = 1;
104 64 $this->{is_pressed} = 0;
105 64 return;
106 }
107  
108 ############################################################
109 #
110 # ->_is_successful -- defined
111 #
112 # Is the enabled widget "successful", according to W3C's specs?
113 # Any pressed button is.
114 #
115 ############################################################
116 sub _is_successful
117 76 ▶{
Subroutine _is_successful: called CC=1 SCAR=0.0
118 76 my $this = shift;
119 76 return $this->is_pressed();
120 }
121  
122 #
123 # Attribute access
124 #
125  
126 ############################################################
127 sub is_enhanced
128 0 ▶{
Subroutine is_enhanced: not called CC=1 SCAR=6.9
Pod undocumented
129 0 my $this = shift;
130 0 return $this->{is_enhanced};
131 } # True for <BUTTON> elements
132 ############################################################
133 sub is_pressed
134 76 ▶{
Subroutine is_pressed: called CC=1 SCAR=0.0
Pod documented
135 76 my $this = shift;
136 76 return $this->{is_pressed};
137 }
138  
139 ############################################################
140 #
141 # ->press
142 #
143 # Press button.
144 #
145 # Has immediate effect:
146 # * If it's a reset button, all widgets are reset to their initial state.
147 # * If it's a submit button, a GET/POST request is issued.
148 # * By default, a warning is issued that the action is ignored.
149 #
150 # Returns undef if no submit is done, a new CGI::Test::Page otherwise.
151 #
152 ############################################################
153 sub press
154 0 ▶{
Subroutine press: not called CC=1 SCAR=6.9
Pod documented
155 0 my $this = shift;
156  
157 #
158 # Default action: do nothing
159 # Routine is redefined in heirs when processing required.
160 #
161  
162 0 warn 'ignoring button press: name="%s", value="%s"', $this->name(),
163 $this->value();
164  
165 0 return undef;
166 }
167  
168 ############################################################
169 #
170 # ->set_is_pressed
171 #
172 # Press or unpress button.
173 #
174 ############################################################
175 sub set_is_pressed
176 19 ▶{
Subroutine set_is_pressed: called CC=1 SCAR=0.0
Pod documented
177 19 my $this = shift;
178 19 my ($pressed) = @_;
179 19 $this->{is_pressed} = $pressed;
180 19 return;
181 }
182  
183 ############################################################
184 #
185 # ->reset_state -- redefined
186 #
187 # Called when a "Reset" button is pressed to restore the value the widget
188 # had upon form entry.
189 #
190 ############################################################
191 sub reset_state
192 0 ▶{
Subroutine reset_state: not called CC=1 SCAR=6.9
Pod documented
193 0 my $this = shift;
194 0 $this->{is_pressed} = 0;
195 0 return;
196 }
197  
198 ############################################################
199 #
200 #
201 # Global widget predicates
202 #
203 ############################################################
204 sub is_read_only
205 0 ▶{
Subroutine is_read_only: not called CC=1 SCAR=6.9
Pod documented
206 0 return 1;
207 }
208  
209 #
210 # Button predicates
211 #
212  
213 ############################################################
214 sub is_reset
215 0 ▶{
Subroutine is_reset: not called CC=1 SCAR=6.9
Pod documented
216 0 return 0;
217 }
218 ############################################################
219 sub is_submit
220 16 ▶{
Subroutine is_submit: called CC=1 SCAR=0.0
Pod documented
221 16 return 0;
222 }
223 ############################################################
224 sub is_plain
225 0 ▶{
Subroutine is_plain: not called CC=1 SCAR=6.9
Pod documented
226 0 return 0;
227 }
228  
229 #
230 # High-level classification predicates
231 #
232  
233 ############################################################
234 sub is_button
235 0 ▶{
Subroutine is_button: not called CC=1 SCAR=6.9
Pod documented
236 0 return 1;
237 }
238  
239 14 1;
240  
241 =head1 NAME
242  
243 CGI::Test::Form::Widget::Button - Abstract representation of a button
244  
245 =head1 SYNOPSIS
246  
247 # Inherits from CGI::Test::Form::Widget
248  
249 =head1 DESCRIPTION
250  
251 This class is the abstract representation of a button, i.e. a submit
252 button, an image button, a reset button or a plain button.
253  
254 Pressing a button is achieved by calling C<press()> on it, which returns a
255 new page, as a C<CGI::Test::Page> object, or C<undef> if pressing had
256 no round-trip effect.
257  
258 =head1 INTERFACE
259  
260 The interface is the same as the one described in L<CGI::Test::Form::Widget>,
261 with the following additions:
262  
263 =head2 Attributes
264  
265 =over 4
266  
267 =item C<is_pressed>
268  
269 True when the button is pressed.
270  
271 =back
272  
273 =head2 Attribute Setting
274  
275 =over 4
276  
277 =item C<press>
278  
279 Press the button, setting C<is_pressed> to true.
280  
281 If the button is a reset button (C<is_reset> is true), all widgets
282 are reset to their initial state, and C<undef> is returned.
283  
284 If the button is a submit button (C<is_submit> is true), then a GET/POST
285 request is issued as appropriate and the reply is made available through
286 a C<CGI::Test::Page> object.
287  
288 Otherwise, the button pressing is ignored, a warning is issued from the
289 perspective of the caller, via C<carp>, and C<undef> is returned.
290  
291 =back
292  
293 =head2 Widget Classification Predicates
294  
295 There is an additional set of predicates to distinguish between the various
296 buttons:
297  
298 =over 4
299  
300 =item C<is_plain>
301  
302 Returns I<true> for a plain button, i.e. a button that has no submit/reset
303 effects. Usually, those buttons are linked to a script, but C<CGI::Test>
304 does not support scripting yet.
305  
306 =item C<is_reset>
307  
308 Returns I<true> for reset buttons.
309  
310 =item C<is_submit>
311  
312 Returns I<true> for submit buttons, whether they are really shown as
313 buttons or as images. A submit button will cause an HTTP request to be
314 issued in response to its being pressed.
315  
316 =back
317  
318 =head2 Miscellaneous Features
319  
320 Although documented, those features are more targetted for
321 internal use...
322  
323 =over 4
324  
325 =item C<set_is_pressed> I<flag>
326  
327 Change the pressed status of the button, to the value of I<flag>.
328 It does not raise any other side effect, like submitting an HTTP request
329 if the button is a submit button.
330  
331 You should probably use the C<press> convenience routine instead of calling
332 this feature directly.
333  
334 =back
335  
336 =head1 AUTHORS
337  
338 The original author is Raphael Manfredi.
339  
340 Steven Hilton was long time maintainer of this module.
341  
342 Current maintainer is Alexander Tokarev F<E<lt>tokarev@cpan.orgE<gt>>.
343  
344 =head1 SEE ALSO
345  
346 CGI::Test::Form::Widget(3),
347 CGI::Test::Form::Widget::Button::Image(3),
348 CGI::Test::Form::Widget::Button::Plain(3),
349 CGI::Test::Form::Widget::Button::Reset(3),
350 CGI::Test::Form::Widget::Button::Submit(3).
351  
352 =cut
353  
« Test/Page.pm ↑ Summary Test/Form/Widget/Button/Reset.pm »