a66d919a292ed42b88e90ae58e5945be96d1aadb
[WWW-Mechanize-Script.git] / lib / WWW / Mechanize / Script / Plugin / TextMatchTest.pm
bloba66d919a292ed42b88e90ae58e5945be96d1aadb
1 package WWW::Mechanize::Script::Plugin::TextMatchTest;
3 use strict;
4 use warnings;
6 use parent qw(WWW::Mechanize::Script::Plugin);
8 use Params::Util qw(_ARRAY0);
10 our $VERSION = '0.001_002';
12 use 5.014;
14 sub check_value_names
16 return qw(text_forbid text_require);
19 sub check_response
21 my ( $self, $check, $mech ) = @_;
23 my $text_require = $self->get_check_value( $check, "text_require" );
24 my $text_forbid = $self->get_check_value( $check, "text_forbid" );
25 my $ignore_case = $self->get_check_value_as_bool( $check, "ignore_case" );
26 my $content = $mech->is_html() ? $mech->text() : $mech->content();
28 _ARRAY0($text_require) or $text_require = [$text_require];
29 _ARRAY0($text_forbid) or $text_forbid = [$text_forbid];
31 my @match_fails;
32 my $code = 0;
33 my $case_ign = $ignore_case ? "(?i)" : "";
34 my @msg;
35 foreach my $text_line ( @{$text_require} )
37 if ( $content !~ m/$case_ign\Q$text_line\E/ )
39 my $err_code = $self->get_check_value( $check, "text_require_code" ) // 1;
40 $code = &{ $check->{compute_code} }( $code, $err_code );
41 push( @match_fails, $text_line );
44 @match_fails and push(@msg, "required text " . join( ", ", map { "'" . $_ . "'" } @match_fails ) . " not found in received content");
46 @match_fails = ();
47 foreach my $text_line ( @{$text_forbid} )
49 if ( $content =~ m/$case_ign\Q$text_line\E/ )
51 my $err_code = $self->get_check_value( $check, "text_forbid_code" ) // 1;
52 $code = &{ $check->{compute_code} }( $code, $err_code );
53 push( @match_fails, $text_line );
56 @match_fails and push(@msg, "forbidden text " . join( ", ", map { "'" . $_ . "'" } @match_fails ) . " found in received content");
58 if( $code or @msg )
60 return ($code, @msg);
63 return (0);