- bump version to 0.001_003
[WWW-Mechanize-Script.git] / lib / WWW / Mechanize / Script / Plugin / TextMatchTest.pm
blob6025abe6344db5befee08f133dcb6c7353e8dacf
1 package WWW::Mechanize::Script::Plugin::TextMatchTest;
3 use strict;
4 use warnings;
6 use parent qw(WWW::Mechanize::Script::Plugin);
8 # ABSTRACT: check for required/forbidden text in response
10 use Params::Util qw(_ARRAY0);
12 our $VERSION = '0.001_003';
14 use 5.014;
16 sub check_value_names
18 return qw(text_forbid text_require);
21 sub check_response
23 my ( $self, $check, $mech ) = @_;
25 my $text_require = $self->get_check_value( $check, "text_require" );
26 my $text_forbid = $self->get_check_value( $check, "text_forbid" );
27 my $ignore_case = $self->get_check_value_as_bool( $check, "ignore_case" );
28 my $content = $mech->is_html() ? $mech->text() : $mech->content();
30 defined($text_require) and ref($text_require) ne "ARRAY" and $text_require = [$text_require];
31 defined($text_forbid) and ref($text_forbid) ne "ARRAY" and $text_forbid = [$text_forbid];
33 my @match_fails;
34 my $code = 0;
35 my $case_ign = $ignore_case ? "(?i)" : "";
36 my @msg;
37 foreach my $text_line ( @{$text_require} )
39 if ( $content !~ m/$case_ign\Q$text_line\E/ )
41 my $err_code = $self->get_check_value( $check, "text_require_code" ) // 1;
42 $code = &{ $check->{compute_code} }( $code, $err_code );
43 push( @match_fails, $text_line );
46 @match_fails
47 and push( @msg,
48 "required text "
49 . join( ", ", map { "'" . $_ . "'" } @match_fails )
50 . " not found in received content" );
52 @match_fails = ();
53 foreach my $text_line ( @{$text_forbid} )
55 if ( $content =~ m/$case_ign\Q$text_line\E/ )
57 my $err_code = $self->get_check_value( $check, "text_forbid_code" ) // 1;
58 $code = &{ $check->{compute_code} }( $code, $err_code );
59 push( @match_fails, $text_line );
62 @match_fails
63 and push( @msg,
64 "forbidden text "
65 . join( ", ", map { "'" . $_ . "'" } @match_fails )
66 . " found in received content" );
68 if ( $code or @msg )
70 return ( $code, @msg );
73 return (0);