blib/lib/WWW/Mechanize/Boilerplate.pm

filter: Statementstmt 86.7% 124 / 143 Branchbran 60.3% 35 / 58 Conditioncond 41.1% 7 / 17 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 85.7% 18 / 21 Podpod 100% 7 / 7 totaltotal 74.7% 192 / 257 CC 52 SCAR 19.3
CC
52
Cov
76%
__ANON__
11.8
test_form_94180657526656
11.8
__ANON__
9.8
CRAP 6.9
↑ Summary
1 package WWW::Mechanize::Boilerplate;
2 {
3 2 $WWW::Mechanize::Boilerplate::VERSION = '0.03';
4 }
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Carp qw(croak shortmess);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Data::Dump qw(dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶has 'mech' => ( isa => 'WWW::Mechanize', is => 'rw', default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
14 0 require WWW::Mechanize;
15 0 return WWW::Mechanize->new();
16 2 } );
17  
18 =head1 NAME
19  
20 WWW::Mechanize::Boilerplate - Compose Mechanize macros from specifications
21  
22 =head1 VERSION
23  
24 version 0.03
25  
26 =head1 DESCRIPTION
27  
28 Create WWW::Mechanize `macros` with appropriate boiler plate
29  
30 =head1 SYNOPSIS
31  
32 I<Create a subclass to hold your methods>
33  
34 package Test::My::Company::Client;
35 use base 'WWW::Mechanize::Boilerplate';
36  
37 __PACKAGE__->create_fetch_method(
38 method_name => 'delorean__configuration',
39 page_description => 'configuration page for the Delorean',
40 page_url => '/delorean/configuration'
41 );
42  
43 __PACKAGE__->create_form_method(
44 method_name => 'delorean__configuration__flux_capacitor',
45 form_name => 'form-flux-capacitor'
46 form_description => 'recalibration form',
47 assert_location => qr!^/delorean/configuration/!,
48 transform_fields => sub {
49 my ( $self, $units, $value ) = @_;
50 return {
51 value => $value,
52 units => $units,
53 understand_risks => 'confirmed'
54 };
55 },
56 );
57  
58 I<Then in your test script>
59  
60 my $client = Test::My::Company::Client->new();
61  
62 $client->delorean__configuration()
63 ->delorean__configuration__flux_capacitory( jigawatts => 10_000 );
64  
65 I<Pretty debugging output>
66  
67 # ->delorean__configuration
68 # Retrieving the configuration page for the Delorean
69 # URL /delorean/configuration retrieved successfully via HTTP
70 # Retrieved the configuration page for the Delorean
71 # No status message shown
72 # ->delorean__configuration__flux_capacitor( 'jigawatts', 10_000 )
73 # URL [/delorean/configuration] matched assertion
74 # Searching for the recalibration form
75 # Submitting recalibration form
76 # URL /delorean/configuration?updated=1 retrieved successfully via HTTP
77 # No status message shown
78  
79 We document the method-creation methods in L<METHOD CREATION METHODS> below, and
80 we document the interface in L<INTERFACE>.
81  
82 =head1 BACKGROUND
83  
84 =head2 The Application
85  
86 In the beginning, there was 'the application'. The application had 230 Apache
87 handlers, each capturing one or more HTTP request. These HTTP requests often had
88 subtly different CGI parameters, some only made sense when you were already on
89 certain pages, and all of them, eventually, needed accessing from automated
90 functional tests.
91  
92 The tests didn't want to care about the underlying HTTP mechanism, or even the
93 underlying HTML. The test just wanted to be able to say:
94  
95 $mech->flux_capacitor__submit_recallibration({
96 jigawatts => 10_000
97 });
98  
99 So why not just use L<WWW::Mechanize>? That's what it's for, right? You can
100 simply say:
101  
102 $mech->submit_form( with_fields => {
103 value => 10_000,
104 units => 'jigawatts',
105 understand_risks => 'confirmed'
106 });
107  
108 And this works just fine for a single test on the Flux Capacitor page.
109  
110 =head2 It's a Trap!
111  
112 But clearly this is a trap. Because as we're all adults, actually, we want to
113 check we're on the right page first, because it'll be super confusing otherwise
114 if the former method left us on the wrong page, and we're trying to work out
115 why we're not writing to the database.
116  
117 And we also want to add a whole bunch of optional diagnostic output to help the
118 poor developer trying to read the output on Jenkins from where everything
119 stopped working.
120  
121 And actually, the form has two buttons I<for historical reasons>, one of which
122 should be used for over 1,000 I<jigawtts>, and one for under. So you need to
123 add the form selection code in too.
124  
125 Did I mention 14 different test scripts use this page and need to submit the
126 jigawatt form, and the team that sits across the office for you are making
127 noises about changing the form structure in the next iteration?
128  
129 =head2 Abstraction
130  
131 All of this is pretty easily solved. You write a nice method against your
132 L<WWW::Mechanize> subclass called 'submit_the_flux_capacitor_form', and the
133 problem is solved.
134  
135 For that form, anyway. On that handler.
136  
137 Now you just need to code up the next 400 possible HTTP actions your test might
138 want to take, and you're home clear...
139  
140 B<And that's the problem this module solves, for us.> It allows you to very
141 easily create methods that generate HTTP requests, with useful boiler plate, and
142 most importantly, B<in data>.
143  
144 Here a simple example for creating a method for getting to the jigawat form:
145  
146 ->create_fetch_method(
147 method_name => 'delorean__configuration',
148 page_description => 'configuration page for the Delorean',
149 page_url => '/delorean/configuration'
150 );
151  
152 And a more complicated one for a method for submitting it:
153  
154 ->create_form_method(
155  
156 # Name of the method we'll create
157 method_name => 'delorean__configuration__flux_capacitor',
158  
159 # Name of the form-element on the target page
160 form_name => 'form-flux-capacitor'
161  
162 # Human-readable description of the form we're targetting
163 form_description => 'recalibration form',
164  
165 # Check we're on the right page, by URI
166 assert_location => qr!^/delorean/configuration/!,
167  
168 # A code-ref to transform the user's arguments to this method to something
169 # suitable for passing to Mechanize.
170 transform_fields => sub {
171 my ( $self, $units, $value ) = @_;
172 return {
173 value => $value,
174 units => $units,
175 understand_risks => 'confirmed'
176 };
177 },
178 );
179  
180 And you'd use these as:
181  
182 $client->delorean__configuration
183 ->delorean__configuration__flux_capacitor( jigawatts => 10_000 );
184  
185 Optionally seeing the following output, via L<Test::More>'s C<note()>.
186  
187 # ->delorean__configuration
188 # Retrieving the configuration page for the Delorean
189 # URL /delorean/configuration retrieved successfully via HTTP
190 # Retrieved the configuration page for the Delorean
191 # No status message shown
192 # ->delorean__configuration__flux_capacitor( 'jigawatts', 10_000 )
193 # URL [/delorean/configuration] matched assertion
194 # Searching for the recalibration form
195 # Submitting recalibration form
196 # URL /delorean/configuration?updated=1 retrieved successfully via HTTP
197 # No status message shown
198  
199 =head1 INSTANTIATION
200  
201 =head2 new
202  
203 ->new();
204 ->new({ mech => WWW::Mechanize->new() });
205  
206 Accepts a hashref containing - for now - a single argument of C<mech>
207 which should be a WWW::Mechanize subclass. If you don't provide one,
208 we'll create a default one.
209  
210 =head1 METHOD CREATION METHODS
211  
212 =head2 create_fetch_method
213  
214 ->create_fetch_method(
215 method_name => 'delorean__configuration',
216 page_description => 'configuration page for the Delorean',
217 page_url => '/delorean/configuration?car_id=',
218 required_param => 'Car ID'
219 );
220  
221 Creates a method that retrieves a URL. Arguments:
222  
223 B<Required:>
224  
225 C<method_name> - name of the method to create
226  
227 C<page_description> - what's the page called? Used for diagnostics
228  
229 C<page_url> - the page to fetch
230  
231 B<Optional:>
232  
233 C<assert_location> - Argument to pass to C<assert_location()>
234  
235 C<required_param> - If your URL needs a trailing atom to complete it, set
236 this to a true value. The user of the method will be required to provide an
237 argument, and it'll be named (in diagnostic output) to the value you assigned
238 it.
239  
240 That means, in the value above, when you call the method, you also must provide
241 an argument:
242  
243 $framework->delorean__configuration( 1234 );
244  
245 There will be a diagnostic method printed:
246  
247 # Car ID is [1234]
248  
249 And the following URL will be retrieved:
250  
251 C</delorean/configuration?car_id=1234>
252  
253 =cut
254  
255 2 ▶sub create_fetch_method {
Subroutine create_fetch_method: called CC=1 SCAR=1.4
Pod documented
256 2 my ( $class, %args ) = @_;
257  
258 # This is a good place to check a user is using it correctly...
259 2 $class->_auto_methods_check_params(\%args,
260 {
261 required => [qw/method_name page_description page_url/],
262 optional => [qw/assert_location required_param/],
263 });
264  
265 $class->meta->add_method(
266 0 ▶ $args{'method_name'} => sub {
Subroutine __ANON__: not called CC=5 SCAR=22.8
Subroutine test_fetch_94180657513592: called CC=5 SCAR=22.8
267 0 my ( $self, $atom ) = @_;
268 0 $self->show_method_name( $args{'method_name'}, $atom );
269  
270 # If we need a URL atom, insist that we have it
271 croak("You must provide a $args{'required_param'}")
272 0 ▶ if ( $args{'required_param'} && (! defined($atom) ) );
Branch: 2 branches
BranchTrueFalseTotal
if $args{"required_param"} and not defined $atom 0 0 0
if $args{"required_param"} and not defined $atom 0 2 2
Condition33%2/6
$args{"required_param"} and not defined $atom
!ll&&!rl&&r
000
$args{"required_param"} and not defined $atom
!ll&&!rl&&r
110
MC/DC0%0/2
$args{"required_param"} and not defined $atom
$args{"required_param"} not defined $atom
MC/DC0%0/2
$args{"required_param"} and not defined $atom
$args{"required_param"} not defined $atom
Truth table0%0/3
A and not(B)
A= $args{"required_param"}B= not defined $atom
ABresult
0X0
100
111
Truth table66%2/3
A and not(B)
A= $args{"required_param"}B= not defined $atom
ABresult
0X0
100
111
273  
274 # Check we're in the right place
275 $self->assert_location( $args{'assert_location'} ) if
276 0 ▶ defined $args{'assert_location'};
Branch: 2 branches
BranchTrueFalseTotal
if defined $args{"assert_location"} 0 0 0
if defined $args{"assert_location"} 0 2 2
277  
278 # Create the URL
279 0 my $target_url = $args{'page_url'};
280 0 ▶ $target_url .= $atom if defined $atom;
Branch: 2 branches
BranchTrueFalseTotal
if defined $atom 0 0 0
if defined $atom 1 1 2
281  
282 0 $self->indent_note(
283 "Retrieving the $args{'page_description'}: [$target_url]", 1);
284  
285 # HTTP call
286 0 $self->mech->get( $target_url );
287  
288 # Status update
289 0 $self->indent_note(
290 "Retrieved the $args{'page_description'} : [" .
291 $self->mech->uri->path_query . "]", 1);
292 0 $self->note_status();
293 0 return $self;
294 }
295 2 );
296 }
297  
298  
299 =head2 create_form_method
300  
301 ->create_form_method(
302 method_name => 'delorean__configuration__flux_capacitor',
303 form_name => 'form-flux-capacitor'
304 form_description => 'recalibration form',
305 assert_location => qr!^/delorean/configuration/!,
306 transform_fields => sub {
307 my ( $self, $units, $value ) = @_;
308 return {
309 value => $value,
310 units => $units,
311 understand_risks => 'confirmed'
312 };
313 }
314 );
315  
316 Finds a form on a page, and submits it. Arguments:
317  
318 B<Required:>
319  
320 C<method_name> - name of the method to create
321  
322 C<form_description> - the human-readable description of the form you're
323 submitting. You don't need to append the word 'form' to this.
324  
325 C<assert_location> - Argument to pass to C<assert_location()>
326  
327 C<form_*> - one of the form resolvers listed below
328  
329 B<Optional:>
330  
331 C<form_name> - the name attribute of the target form. Passed to
332 L<WWW::Mechanize>'s C<form_name()> method. You can pass in a coderef
333 here, which will get called just like C<transform_fields> and should
334 return a string. Instead of C<form_name> you can use C<form_id> or
335 C<form_button> to select forms by ID or button.
336  
337 C<transform_fields> - a code-ref. Will receive $self and the methods arguments,
338 and expects you to return a hash-ref suitable for passing to L<WWW::Mechanize>'s
339 C<set_fields> method. This is a great place to put in default arguments, and
340 also a great place to use C<note()> to tell the test output reader what's going
341 on.
342  
343 C<form_button> - argument to pass to L<WWW::Mechanize>'s C<submit_form> value
344 as C<button>. Used for specifying which button to use to submit a form. This is
345 a string of the button name. You can pass in a coderef here, which will get
346 called just like C<transform_fields> and should return a string.
347  
348 =cut
349  
350 2 my @form_resolvers = qw/form_name form_id form_number/;
351  
352 2 ▶sub create_form_method {
Subroutine create_form_method: called CC=1 SCAR=0.0
Pod documented
353 2 my ( $class, %args ) = @_;
354  
355 # This is a good place to check a user is using it correctly...
356 2 $class->_auto_methods_check_params(\%args,
357 {
358 required => [qw/
359 method_name form_description assert_location
360 /],
361 optional => [@form_resolvers, qw/
362 form_button transform_fields
363 /],
364 });
365  
366 $class->meta->add_method(
367  
368 2 ▶ $args{'method_name'} => sub {
Subroutine __ANON__: called CC=11 SCAR=24.6
369 2 my ( $self, @user_options ) = @_;
370 2 $self->show_method_name( $args{'method_name'}, @user_options );
371  
372 croak "You must define one of form_name, form_id or form_number."
373 2 ▶ unless map { defined $args{$_} ? (1) : () } @form_resolvers;
Branch: 2 branches
BranchTrueFalseTotal
unless map {defined $args{$_} ? 1 : ();} @form_resolvers 0 2 2
defined $args{$_} ? : 2 4 6
374  
375 # Check we're in the right place
376 $self->assert_location( $args{'assert_location'} )
377 2 ▶ if defined $args{'assert_location'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"assert_location"} 2 0 2
378  
379 my $transformed_fields = $args{'transform_fields'} ?
380 2 ▶ $args{'transform_fields'}->( $self, @user_options ) : {};
Branch: 1 branch
BranchTrueFalseTotal
$args{'transform_fields'} ? : 2 0 2
381  
382 2 $self->indent_note("Searching for the $args{'form_description'} form", 1);
383  
384 # Attempt to find the right form
385 2 for my $r ( @form_resolvers ) {
386 3 ▶ next unless defined $args{$r};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{$r} 1 2 3
387  
388 my $name = ref($args{$r}) eq 'CODE' ?
389 $args{$r}->( $self, @user_options ) :
390 2 ▶ $args{$r};
Branch: 1 branch
BranchTrueFalseTotal
ref $args{$r} eq 'CODE' ? : 1 1 2
391 2 my $form = $self->mech->$r( $name );
392  
393 2 ▶ unless ( $form ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($form) 0 2 2
394 0 croak "Couldn't find a form with $r [$name]";
395 }
396  
397 2 last;
398 }
399  
400 2 $self->mech->set_fields( %$transformed_fields );
401  
402 my $button = ref($args{'form_button'}) eq 'CODE' ?
403 $args{'form_button'}->( $self, @user_options ) :
404 2 ▶ $args{'form_button'};
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'form_button'} eq 'CODE' ? : 0 2 2
405  
406 2 $self->indent_note("Submitting $args{'form_description'} form", 1);
407 2 ▶ $self->mech->submit_form(
Branch: 1 branch
BranchTrueFalseTotal
$button ? : 0 2 2
408 fields => $transformed_fields,
409 $button ? ( button => $button ) : ()
410 );
411  
412 2 $self->note_status;
413 2 return $self;
414 }
415 2 );
416 }
417  
418 =head2 create_link_method
419  
420 ->create_link_method(
421 method_name => 'delorean__configuration__current_stats',
422 link_description => 'Current Stats',
423 find_link => { text => 'View Current Stats' },
424 assert_location => '/delorean/configurations'
425 );
426  
427 Creates a method that finds a link on the current page and clicks it.
428  
429 Arguments:
430  
431 B<Required:>
432  
433 C<method_name> - name of the method to create
434  
435 C<link_description> - what are you clicking? Human-readable, and used for
436 diagnostics only
437  
438 B<Optional:>
439  
440 C<assert_location> - Argument to pass to C<assert_location>
441  
442 C<find_link> - what we pass to L<WWW::Mechanize>'s C<find_link> method to
443 identify the link we want to click.
444  
445 C<transform_fields> - a code-ref. Will receive $self and the methods arguments,
446 and expects you to return a hash-ref suitable for passing to L<WWW::Mechanize>'s
447 C<find_link> method. If you want to search for a link more specifically, and
448 allow people to pass in, say, a shipment_id, this would be a good way of doing
449 it.
450  
451 B<<Exactly one of C<find_link> and C<transform_fields> must be set>>
452  
453 =cut
454  
455 1 ▶sub create_link_method {
Subroutine create_link_method: called CC=5 SCAR=17.4
Pod documented
456 1 my ( $class, %args ) = @_;
457  
458 # This is a good place to check a user is using it correctly...
459 1 $class->_auto_methods_check_params(\%args,
460 {
461 required => [qw/method_name link_description/],
462 optional => [qw/assert_location find_link transform_fields/],
463 });
464  
465 croak("Either find_link or transform_fields must be set")
466 1 ▶ unless ( $args{'find_link'} || $args{'transform_fields'} );
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"find_link"} or $args{"transform_fields"} 0 1 1
Condition33%1/3
$args{"find_link"} or $args{"transform_fields"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$args{"find_link"} or $args{"transform_fields"}
$args{"find_link"} $args{"transform_fields"}
Truth table33%1/3
A or B
A= $args{"find_link"}B= $args{"transform_fields"}
ABresult
1X1
011
000
467  
468 croak("Only one of find_link or transform_fields may be set")
469 1 ▶ if ( $args{'find_link'} && $args{'transform_fields'} );
Branch: 1 branch
BranchTrueFalseTotal
if $args{"find_link"} and $args{"transform_fields"} 0 1 1
Condition33%1/3
$args{"find_link"} and $args{"transform_fields"}
!ll&&!rl&&r
010
MC/DC0%0/2
$args{"find_link"} and $args{"transform_fields"}
$args{"find_link"} $args{"transform_fields"}
Truth table33%1/3
A and B
A= $args{"find_link"}B= $args{"transform_fields"}
ABresult
0X0
100
111
470  
471 $class->meta->add_method(
472 1 ▶ $args{'method_name'} => sub {
Subroutine __ANON__: called CC=4 SCAR=14.4
473 1 my ( $self, @user_options ) = @_;
474 1 $self->show_method_name( $args{'method_name'}, @user_options );
475 $self->assert_location( $args{'assert_location'} )
476 1 ▶ if defined $args{'assert_location'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"assert_location"} 1 0 1
477  
478 1 $self->indent_note("Searching for the $args{'link_description'} link");
479 my $link_options = $args{'find_link'} ||
480 1 ▶ $args{'transform_fields'}->( $self, @user_options );
Condition33%1/3
$args{'find_link'} || $args{'transform_fields'}->($self, @user_options)
l!l&&r!l&&!r
100
MC/DC0%0/2
$args{'find_link'} || $args{'transform_fields'}->($self, @user_options)
$args{'find_link'} $args{'transform_fields'}->($self, @user_options)
Truth table33%1/3
A || B
A= $args{'find_link'}B= $args{'transform_fields'}->($self, @user_options)
ABresult
1X1
011
000
481 1 my $link = $self->mech->find_link( %{ $link_options } );
482  
483 1 ▶ unless ( $link ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($link) 0 1 1
484 0 croak "Couldn't find a link matching your description: " .
485 dump( $link_options );
486 }
487 1 my $url = $link->url;
488  
489 1 $self->indent_note("Following $args{'link_description'} link: $url");
490 1 $self->mech->get( $url );
491 1 $self->note_status;
492  
493 1 return $self;
494 }
495 1 );
496 }
497  
498 =head2 create_custom_method
499  
500 __PACKAGE__->create_custom_method(
501 method_name => 'delorean__configuration__jingle_the_jangle',
502 assert_location => qr!^/delorean/configurations!,
503 handler => sub {
504 my $note_text = $_[1];
505 note "\tnote_text: [$note_text]";
506 return { note_text => $note_text }
507 },
508 );
509  
510 This allows you to do whatever you like! :-) The method name output is shown,
511 the location assertion is done if you specified one, and then your handler
512 gets executed with the arguments. After this, C<note_status> is called, and
513 C<self> returned.
514  
515 You almost certainly DO NO NEED TO USE THIS. Instead, work out how to use
516 C<create_form_method> or simplify your method. That said:
517  
518 Arguments:
519  
520 B<Required:>
521  
522 C<method_name> - name of the method to create
523  
524 C<handler> - sub ref we hand off to
525  
526 B<Optional:>
527  
528 C<assert_location> - argument to pass to C<assert_location>
529  
530 =cut
531  
532 1 ▶sub create_custom_method {
Subroutine create_custom_method: called CC=1 SCAR=0.0
Pod documented
533 1 my ( $class, %args ) = @_;
534  
535 # This is a good place to check a user is using it correctly...
536 1 $class->_auto_methods_check_params(\%args,
537 {
538 required => [qw/method_name handler/],
539 optional => [qw/assert_location/],
540 });
541  
542 $class->meta->add_method(
543 1 ▶ $args{'method_name'} => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
544 1 my ( $self, @args ) = @_;
545 1 $self->show_method_name( $args{'method_name'}, @args );
546 $self->assert_location( $args{'assert_location'} )
547 1 ▶ if defined $args{'assert_location'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"assert_location"} 0 1 1
548  
549 1 $args{'handler'}->( $self, @args );
550  
551 1 $self->note_status;
552 1 return $self;
553 }
554 1 );
555 }
556  
557 6 ▶sub _auto_methods_check_params {
Subroutine _auto_methods_check_params: called CC=5 SCAR=16.7
558 6 my ( $class, $args, $spec ) = @_;
559 6 for my $key ( @{$spec->{'required'}} ) {
560 16 ▶ unless ( defined $args->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args->{$key}) 0 16 16
561 0 croak("You must provide a [$key]");
562 }
563 }
564 34 my %all_keys = map {$_ => 1}
565 6 (@{$spec->{'required'}}, @{$spec->{'optional'}});
566 6 for my $key ( keys %{$args} ) {
567 23 ▶ unless ( $all_keys{ $key } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($all_keys{$key}) 0 23 23
568 0 croak("Unknown option [$key]");
569 }
570 }
571 }
572  
573 =head1 INTERFACE
574  
575 =head2 show_method_name
576  
577 Gets called with the string of the method name, and a list of its arguments
578 at the beginning of created methods. Default implementation spits a dump of this
579 out to C<indent_note>.
580  
581 =cut
582  
583 7 ▶sub show_method_name {
Subroutine show_method_name: called CC=2 SCAR=6.9
Pod documented
584 7 my ( $self, $name, @options ) = @_;
585  
586 7 my $output = "->$name(";
587  
588 7 ▶ if ( grep { defined $_ } @options ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {defined $_;} @options) 4 3 7
589 4 $output .= ' ' . dump( @options ) . ' ';
590 }
591  
592 7 $output .= ')';
593 7 $self->indent_note( $output );
594 }
595  
596 =head2 indent_note
597  
598 This is used for outputting diagnostics. The default implementation is a wrapper
599 around L<Test::More>'s note functionality, which indents the first string by
600 the second number + 1.
601  
602 =cut
603  
604 4 ▶sub indent_note {
Subroutine indent_note: called CC=2 SCAR=6.9
605 4 my ( $self, $msg, $indent ) = @_;
606 4 require Test::More; # Only load this if this method is called directly
607  
608 # Default to 0
609 4 ▶ $indent ||= 0;
Condition100%2/2
$indent ||= 0
l!l
31
MC/DC100%1/1
$indent ||= 0
$indent
Truth table100%2/2
A
A= $indent
Aresult
11
00
610  
611 # This implementation pretty-prints at +1
612 4 $indent++;
613  
614 # This is the indent itself...
615 4 my $indent_string = "\t" x $indent;
616  
617 # Add the indent
618 4 $msg =~ s/^/$indent_string/mg;
619  
620 4 Test::More::note( $msg );
621 }
622  
623 =head2 note_status
624  
625 Show the status of the HTTP call. This would be an excellent place to look for
626 messages generated by your web-app, and to fatally die if unexpected errors have
627 occured. However, this base class knows nothing about that, so we take the easy
628 option and show the result of Mech's C<success()>.
629  
630 =cut
631  
632 8 ▶sub note_status {
Subroutine note_status: called CC=2 SCAR=6.9
Pod documented
633 8 my ( $self ) = @_;
634 8 my $success = $self->mech->success;
635  
636 8 ▶ $self->indent_note("is_success() returned " . (
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 7 1 8
637 $success ? 'true' : 'false'
638 ), 1 );
639 8 return $success;
640 }
641  
642 =head2 assert_location
643  
644 =head2 assert_location_failed
645  
646 Checks we're on the correct page before doing anything. The default
647 implementation accepts a string or a regular expression, and matches it against
648 whatever Mechanize thinks is the current unqualified URI. Non-matches call
649 C<assert_location_failed>.
650  
651 That would be a good time to check if you have any obvious error statuses on the
652 page you're on. C<assert_location_failed> accepts the assertion and current URL,
653 and the default implementation throws a simple fatal error.
654  
655 =cut
656  
657 7 ▶sub assert_location {
Subroutine assert_location: called CC=6 SCAR=18.0
Pod documented
658 7 my ( $self, $assertion ) = @_;
659  
660 # Sanity-test the obvious stuff
661 7 my $url = $self->mech->uri->path_query;
662  
663 7 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 7 7
664 "Can't find a URL, which means your location assertion fails by default"
665 unless $url;
666 7 ▶ croak "No acceptable location provided" unless $assertion;
Branch: 1 branch
BranchTrueFalseTotal
unless $assertion 0 7 7
667  
668 # Perform the match
669 7 ▶ if ( ref($assertion) eq 'Regexp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $assertion eq 'Regexp') { } 2 5 7
670 2 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /$assertion/) 1 1 2
671 1 $self->indent_note("URL [$url] matched assertion", 1);
672 1 return 1;
673 } if $url =~ $assertion;
674 } else {
675 5 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
if ($url eq $assertion) 4 1 5
676 4 $self->indent_note("URL [$url] matched assertion", 1);
677 4 return 1
678 } if $url eq $assertion;
679 }
680  
681 2 $self->assert_location_failed( $assertion, $url );
682 }
683  
684 0 ▶sub assert_location_failed {
Subroutine assert_location_failed: not called CC=1 SCAR=6.9
685 0 my ( $self, $assertion, $url ) = @_;
686 0 croak "Current URL [$url] did not match assertion [$assertion]";
687 }
688  
689 =head1 AUTHOR
690  
691 Peter Sergeant - C<pete@clueball.com>
692  
693 The original idea for this was conceived during my time working at the most
694 excellent L<Net-A-Porter|http://www.net-a-porter.com/>, and the work needed to
695 create this release during one of their regular hack days.
696  
697 L<Dave Cross|http://metacpan.org/author/DAVECROSS> contributed invaluable ideas
698 and code.
699  
700 =cut
701  
702 2 1;
↑ Summary