Test/Form/Widget.pm

filter: Statementstmt 71.9% 59 / 82 Branchbran 50.0% 12 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 60.7% 17 / 28 Podpod 90.0% 18 / 20 totaltotal 68.8% 106 / 154 CC 13 SCAR 11.3
CC
13
Cov
67%
is_mutable
10.1
reset_state
6.0
set_value
3.1
CRAP 3.1
« Test/Input.pm ↑ Summary Test/Form/Widget/Box/Radio.pm »
1 package CGI::Test::Form::Widget;
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: Widget.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 CGI form widget (button, text field, etc...).
19 # It belongs to one form, identified by its `form' attribute , a ref
20 # to a CGI::Test::Form object.
21 #
22  
23 ############################################################
24 #
25 # ->new
26 #
27 # Creation routine -- common to ALL widgets but <BUTTON> elements.
28 #
29 ############################################################
30 sub new
31 307 ▶{
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
32 307 my $this = bless {}, shift;
33 307 my ($node, $form) = @_;
34  
35 #
36 # Can't create a CGI::Test::Form::Widget object, only heirs.
37 #
38  
39 307 ▶ confess "%s is a deferred class", __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
if ref $this eq __PACKAGE__ 0 307 307
40 if ref $this eq __PACKAGE__;
41  
42 307 $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 307 $this->_init($node); # Defined in each heir
50  
51 307 return $this;
52 }
53  
54 ############################################################
55 #
56 # ->_common_init
57 #
58 # Common attribute initialization for all widgets
59 #
60 ############################################################
61 sub _common_init
62 371 ▶{
Subroutine _common_init: called CC=1 SCAR=0.0
63 371 my $this = shift;
64 371 my ($form) = @_;
65  
66 371 $this->{form} = $form; # <FORM> containing this widget
67 371 $this->{name} = ""; # Always possible to query, must be defined
68 371 $this->{value} = ""; # Idem
69  
70 371 return;
71 }
72  
73 ############################################################
74 #
75 # ->_init
76 #
77 # Per-widget initialization routine.
78 # Parse HTML node to determine our specific parameters.
79 #
80 ############################################################
81 sub _init
82 0 ▶{
Subroutine _init: not called CC=1 SCAR=6.9
83 0 my $this = shift;
84 0 my ($node) = @_;
85 0 confess "deferred";
86 }
87  
88 ############################################################
89 #
90 # ->_parse_attr
91 #
92 # Each heir locally defines a hash table mapping HTML node attributes to
93 # class attributes. This structure is used to parse the node and setup
94 # the object accordingly.
95 #
96 ############################################################
97 sub _parse_attr
98 371 ▶{
Subroutine _parse_attr: called CC=3 SCAR=11.0
99 371 my $this = shift;
100 371 my ($node, $attr) = @_;
101  
102 371 ▶ while (my ($html_attr, $obj_attr) = each %$attr)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$attr) 1449 371 1820
103 {
104 1449 my $val = $node->attr($html_attr);
105 1449 ▶ $this->{$obj_attr} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 758 691 1449
106 }
107  
108 371 return;
109 }
110  
111 #
112 # Attribute access
113 #
114  
115 sub form
116 19 ▶{
Subroutine form: called CC=1 SCAR=0.0
Pod documented
117 19 my $this = shift;
118 19 return $this->{form};
119 }
120  
121 #
122 # Access to attributes that must be setup by heirs within _init()
123 # Those are common attributes for the whole Widget hierarchy.
124 #
125 # The `value' attribute may not have any meaning (e.g. for an image button)
126 # but it is always possible to query it.
127 #
128  
129 sub name
130 789 ▶{
Subroutine name: called CC=1 SCAR=0.0
Pod documented
131 789 my $this = shift;
132 789 return $this->{name};
133 }
134  
135 sub value
136 382 ▶{
Subroutine value: called CC=1 SCAR=0.0
Pod documented
137 382 my $this = shift;
138 382 return $this->{value};
139 }
140  
141 sub old_value
142 0 ▶{
Subroutine old_value: not called CC=1 SCAR=6.9
Pod undocumented
143 0 my $this = shift;
144 0 return $this->{old_value};
145 }
146  
147 sub is_disabled
148 475 ▶{
Subroutine is_disabled: called CC=1 SCAR=0.0
Pod documented
149 475 my $this = shift;
150 475 return $this->{is_disabled};
151 } # "grayed out"
152  
153 #
154 # Global widget predicates
155 #
156  
157 sub is_read_only
158 0 ▶{
Subroutine is_read_only: not called CC=1 SCAR=6.9
Pod documented
159 0 0
160 } # Can change "value"
161  
162 #
163 # High-level classification predicates
164 #
165  
166 ############################################################
167 sub is_button
168 0 ▶{
Subroutine is_button: not called CC=1 SCAR=6.9
Pod documented
169 0 return 0;
170 }
171 ############################################################
172 sub is_input
173 0 ▶{
Subroutine is_input: not called CC=1 SCAR=6.9
Pod documented
174 0 return 0;
175 }
176 ############################################################
177 sub is_menu
178 21 ▶{
Subroutine is_menu: called CC=1 SCAR=0.0
Pod documented
179 21 return 0;
180 }
181 ############################################################
182 sub is_box
183 0 ▶{
Subroutine is_box: not called CC=1 SCAR=6.9
Pod documented
184 0 return 0;
185 }
186 ############################################################
187 sub is_hidden
188 0 ▶{
Subroutine is_hidden: not called CC=1 SCAR=6.9
Pod documented
189 0 return 0;
190 }
191 ############################################################
192 sub is_file
193 205 ▶{
Subroutine is_file: called CC=1 SCAR=0.0
Pod documented
194 205 return 0;
195 }
196  
197 sub gui_type
198 0 ▶{
Subroutine gui_type: not called CC=1 SCAR=6.9
Pod documented
199 0 confess "deferred";
200 }
201  
202 ############################################################
203 #
204 # ->is_mutable
205 #
206 # Check whether it is possible to change widget's value from a user interface.
207 # Optionally warn if widget's value cannot be changed.
208 #
209 ############################################################
210 sub is_mutable
211 36 ▶{
Subroutine is_mutable: called CC=5 SCAR=23.1
Pod documented
212 36 my $this = shift;
213 36 my ($warn) = @_;
214  
215 36 ▶ if ($this->is_disabled)
Branch: 1 branch
BranchTrueFalseTotal
if ($this->is_disabled) 0 36 36
216 {
217 0 ▶ carp 'cannot change value of disabled %s "%s"', $this->gui_type,
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 0 0
218 $this->name
219 if $warn;
220 0 return 0;
221 }
222  
223 36 ▶ if ($this->is_read_only)
Branch: 1 branch
BranchTrueFalseTotal
if ($this->is_read_only) 0 36 36
224 {
225 0 ▶ carp 'cannot change value of read-only %s "%s"', $this->gui_type,
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 0 0
226 $this->name
227 if $warn;
228 0 return 0;
229 }
230  
231 36 return 1;
232 }
233  
234 ############################################################
235 #
236 # ->set_value
237 #
238 # Change value.
239 # Only allowd to proceed if mutable.
240 #
241 ############################################################
242 sub set_value
243 36 ▶{
Subroutine set_value: called CC=3 SCAR=11.2
Pod documented
244 36 my $this = shift;
245 36 my ($value) = @_;
246  
247 36 ▶ return unless $this->is_mutable(1); # Cannot change value
Branch: 1 branch
BranchTrueFalseTotal
unless $this->is_mutable(1) 0 36 36
248 36 ▶ return if $value eq $this->{value}; # No change
Branch: 1 branch
BranchTrueFalseTotal
if $value eq $this->{"value"} 0 36 36
249  
250 #
251 # To ease redefinition, let this call _frozen_set_value, which is
252 # not redefinable and performs the common operation.
253 #
254  
255 36 $this->_frozen_set_value($value);
256 36 return;
257 }
258  
259 ############################################################
260 #
261 # ->_frozen_set_value -- frozen
262 #
263 # Change value.
264 #
265 ############################################################
266 sub _frozen_set_value
267 36 ▶{
Subroutine _frozen_set_value: called CC=2 SCAR=6.9
268 36 my $this = shift;
269 36 my ($value) = @_;
270  
271 #
272 # The first time we do this, save current value in `old_value'.
273 #
274  
275 36 ▶ $this->{old_value} = $this->{value} unless exists $this->{old_value};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $this->{"old_value"} 18 18 36
276 36 $this->{value} = $value;
277  
278 36 return;
279 }
280  
281 ############################################################
282 #
283 # ->reset_state
284 #
285 # Called when a "Reset" button is pressed to restore the value the widget
286 # had upon form entry.
287 #
288 ############################################################
289 sub reset_state
290 0 ▶{
Subroutine reset_state: not called CC=2 SCAR=17.9
Pod documented
291 0 my $this = shift;
292  
293 #
294 # If there is `old_value' attribute yet, then the value is already OK.
295 #
296  
297 0 ▶ return unless exists $this->{old_value};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $this->{"old_value"} 0 0 0
298  
299 #
300 # Restore value from old_value, and delete this attribute to signal that
301 # the value is now back to its original setting.
302 #
303  
304 0 $this->{value} = delete $this->{old_value};
305 0 return;
306 }
307  
308 ############################################################
309 #
310 # ->is_submitable
311 #
312 # Check whether widget is "successful" (that's such an ugly name), in other
313 # words, whether its name/value pair should be part of submittted form data.
314 #
315 # A "successful" widget must not be disabled.
316 # Heirs should define the _is_successful internal routine.
317 #
318 # Returns true if submitable.
319 #
320 ############################################################
321 sub is_submitable
322 439 ▶{
Subroutine is_submitable: called CC=2 SCAR=7.1
Pod documented
323 439 my $this = shift;
324  
325 439 ▶ return 0 if $this->is_disabled;
Branch: 1 branch
BranchTrueFalseTotal
if $this->is_disabled 0 439 439
326 439 return $this->_is_successful;
327 }
328  
329 ############################################################
330 #
331 # ->_is_successful
332 #
333 # Is the enabled widget "successful", according to W3C's specs?
334 #
335 ############################################################
336 sub _is_successful
337 0 ▶{
Subroutine _is_successful: not called CC=1 SCAR=6.9
338 0 confess "deferred";
339 }
340  
341 ############################################################
342 #
343 # ->submit_tuples
344 #
345 # Returns list of (name => value) tuples that should be part of the
346 # submitted form data. There may be more than one tuple returned for
347 # scrollable lists only: each checkbox is a widget, and therefore can
348 # return only one tuple.
349 #
350 ############################################################
351 sub submit_tuples
352 243 ▶{
Subroutine submit_tuples: called CC=1 SCAR=0.0
Pod documented
353 243 my $this = shift;
354  
355 243 return ($this->name(), $this->value());
356 }
357  
358 ############################################################
359 #
360 # ->delete
361 #
362 # Done with this widget, cleanup by breaking circular refs.
363 #
364 ############################################################
365 sub delete
366 0 ▶{
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
367 0 my $this = shift;
368 0 $this->{form} = undef;
369 0 return;
370 }
371  
372 14 1;
373  
374 =head1 NAME
375  
376 CGI::Test::Form::Widget - Ancestor of all form widget classes
377  
378 =head1 SYNOPSIS
379  
380 # Deferred class, only heirs can be created
381  
382 =head1 DESCRIPTION
383  
384 The C<CGI::Test::Form::Widget> class is deferred.
385 It is an abstract representation of a <FORM> widget, i.e. a graphical control
386 element like a popup menu or a submit button.
387  
388 Here is an outline of the class hierarchy tree, with the leading
389 C<CGI::Test::Form::> string stripped for readability, and a trailing C<*>
390 indicating deferred classes:
391  
392 Widget*
393 . Widget::Box*
394 . . Widget::Box::Check
395 . . Widget::Box::Radio
396 . Widget::Button*
397 . . Widget::Button::Plain
398 . . Widget::Button::Submit
399 . . Widget::Button::Image
400 . . Widget::Button::Reset
401 . Widget::Hidden
402 . Widget::Input*
403 . . Widget::Input::Text_Area
404 . . Widget::Input::Text_Field
405 . . Widget::Input::File
406 . . Widget::Input::Password
407 . Widget::Menu*
408 . . Widget::Menu::List
409 . . Widget::Menu::Popup
410  
411 Only leaf nodes are concrete classes, and there is one such class for each
412 known control type that can appear in the <FORM> element.
413  
414 Those classes are constructed as needed by C<CGI::Test>. They are the
415 programmatic artefacts which can be used to manipulate those graphical
416 elements, on which you would otherwise click and fill within a browser.
417  
418 =head1 INTERFACE
419  
420 This is the interface defined at the C<CGI::Test::Form::Widget> level,
421 and which is therefore common to all classes in the hierarchy.
422 Each subclass may naturally add further specific features.
423  
424 It is very important to stick to using common widget features when
425 writing a matching callback for the C<widgets_matching> routine in
426 C<CGI::Test::Form>, or you run the risk of getting a runtime error
427 since Perl is not statically typed.
428  
429 =head2 Attributes
430  
431 =over 4
432  
433 =item C<form>
434  
435 The C<CGI::Test::Form> to which this widget belongs.
436  
437 =item C<gui_type>
438  
439 A human readable description of the widget, as it would appear on a GUI,
440 like "popup menu" or "radio button". Meant for logging only, not to
441 determine the object type.
442  
443 =item C<name>
444  
445 The CGI parameter name.
446  
447 =item C<value>
448  
449 The current CGI parameter value.
450  
451 =back
452  
453 =head2 Attribute Setting
454  
455 =over 4
456  
457 =item C<set_value> I<new_value>
458  
459 Change the C<value> attribute to I<new_value>.
460 The widget must not be C<is_read_only> nor C<is_disabled>.
461  
462 =back
463  
464 =head2 Widget Modification Predicates
465  
466 Those predicates may be used to determine whether it is possible to
467 change the value of a widget from the user interface.
468  
469 =over 4
470  
471 =item C<is_disabled>
472  
473 When I<true>, the widget is disabled, i.e. not available for editing.
474 It would typically appear as being I<grayed out> within a browser.
475  
476 This predicate is not architecturally defined: a widget may or may not
477 be marked as disabled in HTML via a suitable attribute.
478  
479 =item C<is_mutable> [I<warn_flag>]
480  
481 Test whether widget can change value. Returns I<false> when
482 the widget C<is_read_only> or C<is_disabled>.
483  
484 When the optional I<warn_flag> is true, C<carp> is called
485 to emit a warning from the perspective of the caller.
486  
487 =item C<is_read_only>
488  
489 When I<false>, the C<value> parameter can be changed with C<set_value>.
490 This is an architecturally defined predicate, i.e. its value depends only
491 on the widget type.
492  
493 =back
494  
495 =head2 Widget Classification Predicates
496  
497 Those predicates may be used to determine the overall widget type.
498 The classification is rather high level and only helps determining
499 the kind of calls that may be used on a given widget object.
500  
501 =over 4
502  
503 =item C<is_box>
504  
505 Returns true for radio buttons and checkboxes.
506  
507 =item C<is_button>
508  
509 Returns true for all buttons that are not boxes.
510  
511 =item C<is_file>
512  
513 Returns true for a I<file upload> widget, which allows file selection.
514  
515 =item C<is_hidden>
516  
517 Returns true for hidden fields, which have no graphical representation
518 by definition.
519  
520 =item C<is_input>
521  
522 Returns true for all input fields, where the user can type text.
523  
524 =item C<is_menu>
525  
526 Returns true for popup menus and scrolling lists.
527  
528 =back
529  
530 =head2 Miscellaneous Features
531  
532 Although documented, those features are more targetted for internal use...
533  
534 =over 4
535  
536 =item C<delete>
537  
538 Breaks circular references.
539 This is normally done by the C<delete> routine on the enclosing form.
540  
541 =item C<is_submitable>
542  
543 Returns I<true> when the name/value tupple of this widget need to be
544 part of the submitted parameters. The rules for determining the submitable
545 nature of a widget vary depending on the widget type.
546  
547 =item C<reset_state>
548  
549 Reset the widget's C<value> to the one it had initially. Invoked internally
550 when a reset button is pressed.
551  
552 =item C<submit_tuples>
553  
554 For submitable widgets, return the list of (name => value) tupples that
555 should be part of the submitted data. Widgets like scrolling list may return
556 more than one tuple.
557  
558 This routine is invoked to compute the parameter list that must be sent back
559 when pressing a submit button.
560  
561 =back
562  
563 =head1 AUTHORS
564  
565 The original author is Raphael Manfredi.
566  
567 Steven Hilton was long time maintainer of this module.
568  
569 Current maintainer is Alexander Tokarev F<E<lt>tokarev@cpan.orgE<gt>>.
570  
571 =head1 SEE ALSO
572  
573 CGI::Test::Form(3),
574 CGI::Test::Form::Widget::Box(3),
575 CGI::Test::Form::Widget::Button(3),
576 CGI::Test::Form::Widget::Input(3),
577 CGI::Test::Form::Widget::Hidden(3),
578 CGI::Test::Form::Widget::Menu(3).
579  
580 =cut
581  
« Test/Input.pm ↑ Summary Test/Form/Widget/Box/Radio.pm »