Skip to content

Commit 895a635

Browse files
simbabqueoalders
authored andcommitted
die with our own die method whenever possible #80
1 parent 42b0ba8 commit 895a635

File tree

10 files changed

+65
-10
lines changed

10 files changed

+65
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ tidyall.ERR
1919
/WWW-Mechanize-*/
2020
/WWW-Mechanize-*.tar.gz
2121
/.vscode
22+
/cover_db

Changes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Revision history for WWW::Mechanize
44
[ENHANCEMENTS]
55
- mech_dump now treats all local files like HTML regardless of what it
66
thinks their content types are (GH#63) (Julien Fiegehenn)
7+
- We now consistently use our own die() method and therefore the onerror
8+
handler wherever possible (GH#80) (Julien Fiegehenn)
79

810
2.12 2022-07-20 06:45:40Z
911

lib/WWW/Mechanize.pm

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,7 +1711,7 @@ Selects a form by passing in a list of field names it must contain. All matchin
17111711

17121712
sub all_forms_with_fields {
17131713
my ($self, @fields) = @_;
1714-
die 'no fields provided' unless scalar @fields;
1714+
$self->die('no fields provided') unless scalar @fields;
17151715

17161716
my @matches;
17171717
FORMS: for my $form (@{ $self->forms }) {
@@ -1754,7 +1754,7 @@ Note that this functionality requires libwww-perl 5.69 or higher.
17541754

17551755
sub form_with_fields {
17561756
my ($self, @fields) = @_;
1757-
die 'no fields provided' unless scalar @fields;
1757+
$self->die('no fields provided') unless scalar @fields;
17581758

17591759
my $nth;
17601760
if ( @fields > 1 && ref $fields[-1] eq 'HASH' ) {
@@ -2506,7 +2506,6 @@ sub submit_form {
25062506

25072507
my @filtered_sets;
25082508
if ( $args{with_fields} ) {
2509-
$fields || die q{must submit some 'fields' with with_fields};
25102509
my @got = $self->all_forms_with_fields(keys %{$fields});
25112510
$self->die("There is no form with the requested fields") if not @got;
25122511
push @filtered_sets, \@got;
@@ -2536,7 +2535,7 @@ sub submit_form {
25362535
# Assume that each filtered set only has a given form object once.
25372536
# So we can count occurrences.
25382537
#
2539-
tie my %c, 'Tie::RefHash' or die;
2538+
tie my %c, 'Tie::RefHash' or $self->die('Cannot determine a form to use');
25402539
foreach (@filtered_sets) {
25412540
foreach (@$_) {
25422541
++$c{$_};
@@ -2938,7 +2937,7 @@ sub request {
29382937
my $self = shift;
29392938
my $request = shift;
29402939

2941-
_die( '->request was called without a request parameter' )
2940+
$self->die( '->request was called without a request parameter' )
29422941
unless $request;
29432942

29442943
$request = $self->_modify_request( $request );
@@ -3171,7 +3170,7 @@ sub _taintedness {
31713170
}
31723171

31733172
# Sanity check
3174-
die "Our taintbrush should have zero length!" if length $_taintbrush;
3173+
die("Our taintbrush should have zero length!") if length $_taintbrush;
31753174

31763175
return $_taintbrush;
31773176
}

t/content.t

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use warnings;
22
use strict;
3-
use Test::More tests => 9;
3+
use Test::More;
4+
use Test::Exception;
45

56
=head1 NAME
67
@@ -51,6 +52,8 @@ SKIP: {
5152
unlike( $text, qr/html/i, 'Could not find "html"' );
5253
}
5354

55+
dies_ok { $mech->content(format => 'no_such_format' ) } 'Unkown format';
56+
5457
=head2 $mech->content(base_href => undef)
5558
5659
=head2 $mech->content(base_href => $basehref)
@@ -79,6 +82,10 @@ is($content, 'this is a slightly decoded content', 'decoded_by_headers => 1');
7982
$content = $mech->content(charset => 'whatever');
8083
is($content, 'this is charset whatever', 'charset => ...');
8184

85+
dies_ok { $mech->content(unhandled => 'param') } 'unhandled param';
86+
87+
done_testing;
88+
8289
package Test::MockResponse;
8390

8491
sub new {

t/credentials.t

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use warnings;
44
use strict;
55

66
use WWW::Mechanize;
7-
use Test::More tests => 14;
7+
use Test::More;
8+
use Test::Fatal;
89

910
my $mech = WWW::Mechanize->new;
1011
isa_ok( $mech, 'WWW::Mechanize' );
@@ -17,6 +18,13 @@ my $uri = URI->new( 'http://localhost' );
1718
is $user, undef, 'default username is undefined at first';
1819
is $pass, undef, 'default password is undefined at first';
1920

21+
like(
22+
exception {
23+
$mech->credentials("one", "two", "three");
24+
},
25+
qr/Invalid # of args for overridden credentials/,
26+
'credentials dies with wrong number of args'
27+
);
2028

2129
$mech->credentials("username", "password");
2230

@@ -52,3 +60,5 @@ is $user, 'username',
5260
'cloned object still has username for get_basic_credentials';
5361
is $pass, 'password',
5462
'cloned object still has password for get_basic_credentials';
63+
64+
done_testing;

t/form_with_fields.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<input type="hidden" name="5e" value="value" />
4949
</form>
5050

51-
<form action="http://localhost/" method="post" name="6th_form">
51+
<form action="http://localhost/" method="post" name="6th_form" id="6th_form">
5252
<select name="select">
5353
<option value="one">Option 1</option>
5454
<option value="two">Option 2</option>

t/local/click_button.t

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ isa_ok( $mech, 'WWW::Mechanize', 'Created the object' );
1818
my $server = LocalServer->spawn();
1919
isa_ok( $server, 'LocalServer' );
2020

21+
dies_ok { $mech->click_button( id => 0 ) } 'Dies without a form';
22+
2123
my $response = $mech->get( $server->url );
2224
isa_ok( $response, 'HTTP::Response', 'Got back a response' );
2325
ok( $response->is_success, 'Got URL' ) or die q{Can't even fetch local url};

t/local/follow.t

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use warnings;
22
use strict;
3-
use Test::More tests => 28;
3+
use Test::More;
4+
use Test::Exception;
45
use lib 't/local';
56
use LocalServer;
67

@@ -60,3 +61,6 @@ is( $agent->uri, $server->url, 'Needs to be on the same page' );
6061
eval {$agent->follow_link( '/foo' )};
6162
like($@, qr/Needs to get key-value pairs of parameters.*follow\.t/, "Invalid parameter passing gets better error message");
6263

64+
dies_ok { WWW::Mechanize->new->follow_link( url => '/404' ) } "dies when link does not exist with autocheck";
65+
66+
done_testing;

t/local/select_multiple.t

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use warnings;
22
use strict;
33
use Test::More;
4+
use Test::Exception;
45

56
use lib 't/local';
67
use LocalServer;
@@ -34,6 +35,9 @@ HTML
3435
my $server = LocalServer->spawn( html => $html );
3536
isa_ok( $server, "LocalServer" );
3637

38+
dies_ok { $mech->submit_form( form_number => 1, fields => { none => 0 } ) }
39+
'Dies without a form';
40+
3741
$mech->get($server->url);
3842
ok( $mech->success, 'Fetched OK' );
3943

t/submit_form.t

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ $mech->get($uri);
2727
);
2828
}
2929

30+
{
31+
$mech->get($uri);
32+
is(
33+
exception {
34+
$mech->submit_form(
35+
form_id => '6th_form',
36+
);
37+
},
38+
undef,
39+
'submit_form with valid form_id',
40+
);
41+
}
42+
3043
{
3144
$mech->get($uri);
3245
like(
@@ -91,4 +104,17 @@ $mech->get($uri);
91104
);
92105
}
93106

107+
{
108+
$mech->get($uri);
109+
like(
110+
exception {
111+
$mech->submit_form(
112+
with_fields => {}, # left empty on purpose
113+
)
114+
},
115+
qr/no fields provided/,
116+
'submit_form with no fields',
117+
);
118+
}
119+
94120
done_testing();

0 commit comments

Comments
 (0)