This file is indexed.

/usr/share/perl5/Test/WWW/Declare.pm is in libtest-www-declare-perl 0.02-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
package Test::WWW::Declare;
use warnings;
use strict;
use base 'Test::More';
use Test::WWW::Mechanize;
use Test::Builder;

our $VERSION  = '0.02';

our @EXPORT = qw(flow run get session check mech match follow_link content
                 should shouldnt click href button fill form SKIP _twd_dummy
                 title equal caselessly contain matches equals contains
                 never always lack lacks url uri);
our $BUILDER = Test::Builder->new();
our $WWW_MECHANIZE;
our $IN_FLOW;
our %mechs;

=begin private

=head2 import_extra

Called by L<Test::More>'s C<import> code when L<Test::WWW::Declare> is first
C<use>'d, it asks Test::More to export its symbols to the namespace that
C<use>'d this one.

=end private

=cut

sub import_extra {
    Test::More->export_to_level(2);
}

=head1 NAME

Test::WWW::Declare - declarative testing for your web app

=head1 SYNOPSIS

    use Test::WWW::Declare tests => 3;
    use Your::Web::App::Test;

    Your::Web::App::Test->start_server;

    session 'testuser' => run {
        flow 'log in and out' => check {
            flow 'log in' => check {
                get 'http://localhost/';
                fill form 'login' => {
                    username => 'testuser',
                    password => 'drowssap',
                };
                content should contain 'log out';
            };

            flow 'log out' => check {
                get 'http://localhost/';
                click href 'log out';
            };
        };
    };

=head1 DESCRIPTION

Often in web apps, tests are very dependent on the state set up by previous
tests. If one test fails (e.g. "follow the link to the admin page") then it's
likely there will be many more failures. This module aims to alleviate this
problem, as well as provide a nicer interface to L<Test::WWW::Mechanize>.

The central idea is that of "flow". Each flow is a sequence of commands ("fill
in this form") and assertions ("content should contain 'testuser'"). If any of
these commands or assertions fail then the flow is aborted. Only that one
failure is reported to the test harness and user. Flows may also contain other
flows. If an inner flow fails, then the outer flow fails as well.

=head1 FLOWS AND SESSIONS

=head2 session NAME => run { CODE }

Sessions are a way of associating a set of flows with a L<WWW::Mechanize>
instance. A session is mostly equivalent with a user interacting with your web
app.

Within a session, every command (C<get>, C<click link>, etc) is operating on
that session's L<WWW::Mechanize> instance. You may have multiple sessions in
one test file. Two sessions with the same name are in fact the same session.
This lets you write code like the following, simplified slightly:

    session 'first user' => run {
        get "$URL/give?task=1&victim=other";
        session 'other user' => run {
            get "$URL/tasks";
            content should match qr/task 1/;

            # this is the same session/mech as the outermost 'first user'
            session 'first user' => run {
                get "$URL/tasks";
                content shouldnt match qr/task 1/;
            };
        };
    };

=head2 flow NAME => check { CODE }

A flow encompasses a single test. As described above, each flow is a sequence
of commands, assertions, and other flows. If any of the components of a flow
fail, the rest of the flow is aborted and one or more test failures are
reported to the test harness.

=head1 COMMANDS

=head2 get URL

=head2 click button

=head2 click href

=head2 follow_link

=head2 fill form NAME => {FIELD1 => VALUE1, FIELD2 => VALUE2}

=head1 ASSERTIONS

Every assertion has two parts: a subject and a verb.

=head2 SUBJECTS

=head3 content

=head3 title

=head3 url

=head2 VERBS

=head3 should(nt) (caselessly) match REGEX

=head3 should(nt) (caselessly) contain STRING

=head3 should(nt) (caselessly) lack STRING

=head3 should(nt) (caselessly) equal STRING

=cut

# DSLey functions
sub to($) { return $_[0] }

sub _args {
    my $args = shift;
    return $args if ref($args) eq 'HASH';
    return {expected => $args};
}

sub should ($) {
    return _args(shift);
}

sub shouldnt ($) {
    my $args = _args(shift);
    $args->{negative} = 1;
    return $args;
}

sub match ($) {
    my $args = _args(shift);
    $args->{match} = 'regex';
    return $args;
}

sub equal ($) {
    my $args = _args(shift);
    $args->{match} = 'equality';
    return $args;
}

sub contain ($) {
    my $args = _args(shift);
    $args->{match} = 'index';
    return $args;
}

sub lack ($) {
    my $args = _args(shift);
    $args->{match} = 'index';
    $args->{negative} = 1;
    return $args;
}

sub caselessly ($) {
    my $args = _args(shift);
    $args->{case_insensitive} = 1;
    return $args;
}

sub check (&) {
    my $coderef = shift;

    return $coderef;
}

sub run (&) {
    my $coderef = shift;

    return $coderef;
}

# alternates (e.g. "foo matches bar" instead of "foo should match bar")
sub contains ($) { contain  $_[0] }
sub equals   ($) { equal    $_[0] }
sub matches  ($) { match    $_[0] }
sub lacks    ($) { lack     $_[0] }

sub always   ($) { should   $_[0] }
sub never    ($) { shouldnt $_[0] }

# Mech interactions
sub mech(;$) {
    my $name = shift;
    return defined $name ? $mechs{$name} : $WWW_MECHANIZE;
}

sub get {
    my $url = shift;

    mech()->get($url);
    if (!$IN_FLOW)
    {
        $BUILDER->ok(mech->success, "navigated to $url");
    }

    return if mech->success;

    Carp::croak mech->status
             . (mech->response ? ' - ' . mech->response->message : '')
}

sub href ($) {
    return (shift, 'href');
}

sub button ($) {
    return (shift, 'button');
}

sub click {
    my $link = shift;
    my $type = shift;

    if ($type eq 'button') {
        my $ok = mech()->click_button(value => $link);
        $ok = $ok->is_success if $ok;
        my $verb = ref($link) eq 'Regexp' ? "matching " : "";
        $BUILDER->ok($ok, "Clicked button $verb$link") if !$IN_FLOW;
        return $ok;
    }
    else {
        if (ref $link ne 'Regexp') {
            Carp::croak "click doesn't know what to do with a link type of "
              . ref($link);
        }
        my $ok;
        my $response = mech()->follow_link(text_regex => $link);
        $ok = 1 if $response && $response->is_success;
        $BUILDER->ok($ok, "Clicked link matching $link") if !$IN_FLOW;
        Carp::croak($response ? $response->as_string : "No link matching $link found") if !$ok;
        return $ok;
    }
}

sub follow_link {
    my $ret = mech()->follow_link(@_);

    if (!$ret) {
        Carp::croak "follow_link couldn't find a link matching "
          . "(" . join(', ', @_) . ")";
    }
}

sub content ($) {
    _magic_match({got => mech()->content, name => "Content", %{shift @_}});
}

sub title ($) {
    my $title = mech()->title;
    _magic_match({got => $title, name => "Title '$title'", %{shift @_}});
}

sub url ($) {
    my $url = mech()->uri;
    _magic_match({got => $url, name => "URL '$url'", %{shift @_}});
}
*uri = \&url;

# yes, there's a little too much logic in here. that's why it's magic
sub _magic_match {
    my $orig = shift @_;
    my %args = %$orig;
    my $match;
    my @output;

    $args{negative} ||= 0;

    push @output, $args{name};
    push @output, $args{negative} ? ()
                                  : "does not";

    if ($args{match} eq 'equality') {
        if ($args{case_insensitive}) {
            push @output, "caselessly";
            $args{got} = lc $args{got};
            $args{expected} = lc $args{expected};
        }

        push @output, $args{negative} ? "equals"
                                      : "equal";
        push @output, $orig->{expected};

        $match = $args{got} eq $args{expected};
    }
    elsif ($args{match} eq 'index') {
        if ($args{case_insensitive}) {
            push @output, "caselessly";
            $args{got} = lc $args{got};
            $args{expected} = lc $args{expected};
        }

        push @output, $args{negative} ? "contains"
                                      : "contain";
        push @output, $orig->{expected};

        $match = index($args{got}, $args{expected}) >= 0;
    }
    elsif ($args{match} eq 'regex') {
        if ($args{case_insensitive}) {
            push @output, "caselessly";
            push @output, $args{expected};
            $args{expected} = "(?i:$args{expected})";
        }

        push @output, $args{negative} ? "matches"
                                      : "match";
        push @output, $orig->{expected};

        $match = $args{got} =~ $args{expected};
    }
    else {
        Carp::croak "No \$args{match} (yes this error needs to be fixed)";
    }

    my $ok = ($match ? 1 : 0) ^ $args{negative};
    if (!$IN_FLOW) {
        $BUILDER->ok($ok, join(' ', @output));
        return $ok;
    }

    return 1 if $ok;
    Carp::croak join(' ', @output);
}

sub form ($$) {
    my $form_name = shift;
    my $data = shift;

    my $form = mech()->form_name($form_name);

    if (!defined($form)) {
        Carp::croak "There is no form named '$form_name'";
    }

    return $data;
}

sub fill {
    my $data = shift;

    Carp::croak "fill expects a hashref" if ref($data) ne 'HASH';

    mech()->set_fields(%{$data});
}

# the meat of the module
sub SKIP ($) {
    my $reason = shift;

    Carp::croak "SKIP: $reason";
}

sub flow ($$) {
    my $name = shift;
    my $coderef = shift;

    eval { local $IN_FLOW = 1; $coderef->() };

    if ($@ =~ /^SKIP: (.*)$/) {
        my $reason = $1;
        $BUILDER->skip($reason);
    }
    elsif ($@) {
        if ($IN_FLOW) {
            if ($@ =~ /^Flow '/)
            {
                die $@;
            }
            die "Flow '$name' failed: $@";
        }

        $BUILDER->ok(0, $name);

        if ($@ =~ /^Flow '/) {
            $BUILDER->diag($@);
        }
        else {
            $BUILDER->diag("Flow '$name' failed: $@");
        }
    }
    else {
        $BUILDER->ok(1, $name);
    }
}

sub session ($$) {
    my $title = shift;
    my $coderef = shift;

    $mechs{$title} ||= Test::WWW::Mechanize->new(quiet => 1);
    local $WWW_MECHANIZE = $mechs{$title};

    $coderef->();

    if ($@ =~ /^SKIP: (.*)$/) {
        my $reason = $1;
        $BUILDER->skip($reason);
    }
    elsif ($@ =~ /^Flow '/) {
        # flow already displayed the error
    }
    elsif ($@) {
        $BUILDER->diag($@);
    }
}

sub dump($) {
    my $file = shift;
    mech->save_content($file);
}

# used only for testing that we got T:W:D's goods
sub _twd_dummy { "XYZZY" }

=head1 SUBCLASSING

One of the goals of this module is to let you subclass it to provide extra
features, such as automatically logging in a user each time a session is
created.

=head1 CAVEATS

If you fail any tests, then the actual number of tests run may be fewer than
you have in your file. This is because when a flow fails, it immediately aborts
the rest of its body (which may include other flows). So if you're setting the
number of tests based on how many ran, make sure that all tests passed.

=head1 BUGS

Hopefully few. We'd like to know about any of them. Please report them to
C<bug-test-www-declare@rt.cpan.org>.

=head1 SEE ALSO

L<Test::WWW::Mechanize>, L<Jifty>.

=head1 MAINTAINER

Shawn M Moore C<< <sartak@bestpractical.com> >>

=head1 ORIGINAL AUTHOR

Jesse Vincent C<< <jesse@bestpractical.com> >>

=head1 COPYRIGHT

Copyright 2007-2008 Best Practical Solutions, LLC

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

=cut

1;