| 1 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | package HTML::Form::ForceValue; | ||||||||||||||||||||||||||||
| 4 | # ABSTRACT: who cares what values are legal, anyway? | ||||||||||||||||||||||||||||
| 5 | 2 | $HTML::Form::ForceValue::VERSION = '0.009'; | |||||||||||||||||||||||||||
| 6 | # =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # use Test::WWW::Mechanize tests => 5; | ||||||||||||||||||||||||||||
| 9 | # use HTML::Form::ForceValue; | ||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||
| 11 | # my $mech = WWW::Mechanize->new; | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | # # We're going to test our form. | ||||||||||||||||||||||||||||
| 14 | # $mech->get_ok("http://cgi.example.com/form"); | ||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||
| 16 | # $mech->set_fields( | ||||||||||||||||||||||||||||
| 17 | # name => 'Crazy Ivan', | ||||||||||||||||||||||||||||
| 18 | # city => 'Vladivostok', | ||||||||||||||||||||||||||||
| 19 | # ); | ||||||||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||||||||
| 21 | # # What if insane bot tries to claim it's from USSR? | ||||||||||||||||||||||||||||
| 22 | # $mech->form_name("user_info")->find_input("country")->force_value("su"); | ||||||||||||||||||||||||||||
| 23 | # | ||||||||||||||||||||||||||||
| 24 | # $mech->submit; | ||||||||||||||||||||||||||||
| 25 | # | ||||||||||||||||||||||||||||
| 26 | # =head1 DEPRECATION NOTICE | ||||||||||||||||||||||||||||
| 27 | # | ||||||||||||||||||||||||||||
| 28 | # As of C<libwww-perl> 5.817, HTML::Form has a strict mode, which restricts form | ||||||||||||||||||||||||||||
| 29 | # values to the options given. Without strict mode, values may be set to | ||||||||||||||||||||||||||||
| 30 | # anything you like, making this module unnecessary. It remains on the CPAN for | ||||||||||||||||||||||||||||
| 31 | # use by those who choose not to upgrade their LWP, but in general this code is | ||||||||||||||||||||||||||||
| 32 | # now obsolete. | ||||||||||||||||||||||||||||
| 33 | # | ||||||||||||||||||||||||||||
| 34 | # =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 35 | # | ||||||||||||||||||||||||||||
| 36 | # L<HTML::Form|HTML::Form> is a very useful module that provides objects to | ||||||||||||||||||||||||||||
| 37 | # represent HTML forms. They can be filled in, and the filled-in values can be | ||||||||||||||||||||||||||||
| 38 | # converted into an HTTP::Request for submission to a server. | ||||||||||||||||||||||||||||
| 39 | # | ||||||||||||||||||||||||||||
| 40 | # L<WWW::Mechanize|WWW::Mechanize> makes this even easier by providing a very | ||||||||||||||||||||||||||||
| 41 | # easy to automate user agent that provides HTML::Form objects to represent | ||||||||||||||||||||||||||||
| 42 | # forms. L<Test::WWW::Mechanize|Test::WWW::Mechanize> hangs some testing | ||||||||||||||||||||||||||||
| 43 | # features on Mech, making it easy to automatically test how web applications | ||||||||||||||||||||||||||||
| 44 | # behave. | ||||||||||||||||||||||||||||
| 45 | # | ||||||||||||||||||||||||||||
| 46 | # One really important thing to test is how a web application responds to invalid | ||||||||||||||||||||||||||||
| 47 | # input. Unfortunately, HTML::Form protects you from doing this by throwing an | ||||||||||||||||||||||||||||
| 48 | # exception when an invalid datum is assigned to an enumerated field. | ||||||||||||||||||||||||||||
| 49 | # HTML::Form::ForceValue mixes in to HTML::Form classes to provide C<force_value> | ||||||||||||||||||||||||||||
| 50 | # methods which behave like C<value>, but will automatically add any invalid | ||||||||||||||||||||||||||||
| 51 | # datum to the list of valid data. | ||||||||||||||||||||||||||||
| 52 | # | ||||||||||||||||||||||||||||
| 53 | # =cut | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 2 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 57 | 2 | HTML::Form::ForceValue::Form->import(@_); | |||||||||||||||||||||||||||
| 58 | 2 | HTML::Form::ForceValue::Form::Input->import(@_); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | package HTML::Form::ForceValue::Form; | ||||||||||||||||||||||||||||
| 62 | 2 | $HTML::Form::ForceValue::Form::VERSION = '0.009'; | |||||||||||||||||||||||||||
| 63 | 2 | ▶ | use Sub::Exporter 0.960 -setup => { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | into => 'HTML::Form', | ||||||||||||||||||||||||||||
| 65 | exports => [ qw(force_value) ], | ||||||||||||||||||||||||||||
| 66 | groups => [ default => [ '-all' ] ], | ||||||||||||||||||||||||||||
| 67 | 2 | }; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 1 | ▶ | sub force_value { | ||||||||||||||||||||||||||
|
Subroutine
force_value: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 70 | 1 | my ($self, $name, $value) = @_; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 1 | my $input = $self->find_input($name); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1 | ▶ | unless ($input) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 1 | $input = HTML::Form::ListInput->new( | |||||||||||||||||||||||||||
| 76 | type => 'option', | ||||||||||||||||||||||||||||
| 77 | name => $name, | ||||||||||||||||||||||||||||
| 78 | menu => [ { value => $value, name => $value } ], | ||||||||||||||||||||||||||||
| 79 | current => 0, | ||||||||||||||||||||||||||||
| 80 | multiple => 1, | ||||||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 1 | $input->add_to_form($self); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | $input->force_value($value); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | package HTML::Form::ForceValue::Form::Input; | ||||||||||||||||||||||||||||
| 90 | 2 | $HTML::Form::ForceValue::Form::Input::VERSION = '0.009'; | |||||||||||||||||||||||||||
| 91 | 2 | ▶ | use Sub::Exporter -setup => { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 92 | into => 'HTML::Form::Input', | ||||||||||||||||||||||||||||
| 93 | exports => [ qw(force_value) ], | ||||||||||||||||||||||||||||
| 94 | groups => [ default => [ '-all' ] ], | ||||||||||||||||||||||||||||
| 95 | 2 | }; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 2 | ▶ | sub force_value { | ||||||||||||||||||||||||||
|
Subroutine
force_value: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 98 | 2 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 99 | 2 | my $old = $self->value; | |||||||||||||||||||||||||||
| 100 | 2 | eval { $self->value($value); }; | |||||||||||||||||||||||||||
| 101 | 2 | ▶ | if ($@ and $@ =~ /Illegal value/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$@ and $@ =~ /Illegal value/
MC/DC50%1/2
$@ and $@ =~ /Illegal value/
$@
$@ =~ /Illegal value/
Truth table66%2/3
A and B
A= $@B= $@ =~ /Illegal value/
| |||||||||||||||||||||||||||||
| 102 | 1 | push @{$self->{menu}}, { name => $value, value => $value }; | |||||||||||||||||||||||||||
| 103 | 1 | return $self->value($value); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 1 | return $old; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # =head1 WARNING | ||||||||||||||||||||||||||||
| 109 | # | ||||||||||||||||||||||||||||
| 110 | # This implementation is extremely crude. This feature should really be in | ||||||||||||||||||||||||||||
| 111 | # HTML::Form (in my humble opinion), and this module should cease to exist once | ||||||||||||||||||||||||||||
| 112 | # it is. In the meantime, just keep in mind that I spent a lot more time | ||||||||||||||||||||||||||||
| 113 | # packaging this than I did writing it. I<Caveat importor!> | ||||||||||||||||||||||||||||
| 114 | # | ||||||||||||||||||||||||||||
| 115 | # =cut | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 2 | 1; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | __END__ | ||||||||||||||||||||||||||||