Subject: | stuff_inputs fix |
Date: | Tue, 18 Apr 2006 16:30:01 -0500 |
To: | undisclosed-recipients:; |
From: | moregan [...] flr.follett.com |
This patch makes stuff_inputs() conform to the documentation,
changing the implementation to expect an arrayref for
$options->{ ignore }, as opposed to a hashref.
Also does more checking for invalid args, uses assert_in(),
and ignores zero-length 'fill'.
--- Mechanize.pm.orig 2006-04-18 16:07:44.000000000 -0500
+++ Mechanize.pm 2006-04-18 16:16:28.000000000 -0500
@@ -667,8 +667,6 @@
=head2 $agent->stuff_inputs( [\%options] )
-XXX Delete this when it winds up in Test::WWW::Mechanize
-
Finds all free-text input fields (text, textarea, and password) in the
current form and fills them to their maximum length in hopes of finding
application code that can't handle it. Fields with no maximum length
@@ -685,7 +683,7 @@
=item * ignore
-hash value is hashref of field names to not touch, e.g.:
+hash value is arrayref of field names to not touch, e.g.:
$mech->stuff_inputs( {
ignore => [qw( specialfield1 specialfield2 )],
@@ -725,30 +723,34 @@
sub stuff_inputs {
my $self = shift;
- use Carp;
-
my $options = shift || {};
assert_isa( $options, 'HASH' );
+ assert_in( $_, ['ignore', 'fill', 'specs'] ) foreach ( keys %$options );
+ # set up the fill we'll use unless a field overrides it
my $default_fill = '@';
- if ( exists $options->{fill} && defined $options->{fill} ) {
+ if ( exists $options->{fill} && defined $options->{fill} && length($options->{fill}) > 0 ) {
$default_fill = $options->{fill};
- # TODO: need to verify that length is > 0
}
+ # fields in the form to not stuff
my $ignore = {};
- if ( exists $options->{ignore} && defined $options->{ignore} ) {
- $ignore = $options->{ignore};
- croak if ref $ignore ne 'HASH';
+ if ( exists $options->{ignore} ) {
+ assert_isa( $options->{ignore}, 'ARRAY' );
+ $ignore = { map {($_, 1)} @{$options->{ignore}} };
}
my $specs = {};
- if ( exists $options->{specs} && defined $options->{specs} ) {
+ if ( exists $options->{specs} ) {
+ assert_isa( $options->{specs}, 'HASH' );
$specs = $options->{specs};
- croak if ref $specs ne 'HASH';
- # TODO: verify that only valid options passed
+ foreach my $field_name ( keys %$specs ) {
+ assert_isa( $specs->{$field_name}, 'HASH' );
+ assert_in( $_, ['fill', 'maxlength'] ) foreach ( keys %{$specs->{$field_name}} );
+ }
}
+
my @inputs = $self->grep_inputs( { type => qr/^(text|textarea|password)$/ } );
foreach my $field ( @inputs ) {
@@ -757,17 +759,9 @@
my $name = $field->name();
- # might be one of the fields to ignore
+ # skip if it's one of the fields to ignore
next if exists $ignore->{ $name };
- # TODO: need to check that we're not passed any unsupported specs
- # options
-
- my $fill = $default_fill;
- if ( exists $specs->{$name} && exists $specs->{$name}->{fill} && defined $specs->{$name}->{fill} ) {
- $fill = $specs->{$name}->{fill};
- }
-
# fields with no maxlength will get this many characters
my $maxlength = 66000;
@@ -779,10 +773,20 @@
}
}
- # maxlength override from specs
- if ( exists $specs->{$name} && exists $specs->{$name}->{maxlength} && defined $specs->{$name}->{maxlength} ) {
- $maxlength = $specs->{$name}->{maxlength};
- # TODO: what to do about maxlength==0 ? non-numeric? less than 0?
+ my $fill = $default_fill;
+
+ if ( exists $specs->{$name} ) {
+ # process the per-field info
+
+ if ( exists $specs->{$name}->{fill} && defined $specs->{$name}->{fill} && length($specs->{$name}->{fill}) > 0 ) {
+ $fill = $specs->{$name}->{fill};
+ }
+
+ # maxlength override from specs
+ if ( exists $specs->{$name}->{maxlength} && defined $specs->{$name}->{maxlength} ) {
+ $maxlength = $specs->{$name}->{maxlength};
+ # TODO: what to do about maxlength==0 ? non-numeric? less than 0?
+ }
}
# stuff it