fix issue with non-existing opts
[WWW-Mechanize-Script.git] / lib / WWW / Mechanize / Script / Plugin / TextMatchTest.pm
blob33e1b9fd2067a259e382ce8f2a8f08db78b6aab5
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 defined($text_require) and ref($text_require) ne "ARRAY" and $text_require = [$text_require];
29 defined($text_forbid) and ref($text_forbid) ne "ARRAY" and $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
45 and push( @msg,
46 "required text "
47 . join( ", ", map { "'" . $_ . "'" } @match_fails )
48 . " not found in received content" );
50 @match_fails = ();
51 foreach my $text_line ( @{$text_forbid} )
53 if ( $content =~ m/$case_ign\Q$text_line\E/ )
55 my $err_code = $self->get_check_value( $check, "text_forbid_code" ) // 1;
56 $code = &{ $check->{compute_code} }( $code, $err_code );
57 push( @match_fails, $text_line );
60 @match_fails
61 and push( @msg,
62 "forbidden text "
63 . join( ", ", map { "'" . $_ . "'" } @match_fails )
64 . " found in received content" );
66 if ( $code or @msg )
68 return ( $code, @msg );
71 return (0);