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
|
#!/usr/bin/perl -w
use strict;
use Test::More;
use lib 't';
use testlib;
# This test file tests the internal routines of Test::HTML.
# The internal routines aren't really intended for public
# consumption, but the tests you'll find in here should
# document the behaviour enough ...
my (%cases_2,%cases_3);
my $count;
BEGIN {
$cases_2{__dwim_compare} = [
"foo" => "bar" => 0,
"foo" => "..." => 0,
"bar" => "foo" => 0,
"bar" => "barra" => 0,
"barra" => "bar" => 0,
"foo" => qr"bar" => 0,
"foo" => qr"..." => 1,
"bar" => qr"foo" => 0,
"bar" => qr"barra" => 0,
"barra" => qr"bar" => 1,
"foo" => qr"^oo" => 0,
"foo" => qr"oo$" => 1,
"FOO" => qr"foo$" => 0,
"FOO" => qr"foo$"i => 1,
];
$cases_2{__match_comment} = [
"hidden message" => qr"hidden\s+message" => 1,
"FOO" => qr"foo$"i => 1,
" FOO" => qr"foo$"i => 1,
"FOO " => qr"foo$"i => 0,
"FOO " => qr"^foo$"i => 0,
" hidden message " => "hidden message" => 1,
" hidden message " => "hidden message" => 0,
];
$cases_2{__match_text} = [
"hidden message" => qr"hidden\s+message" => 1,
"FOO" => qr"foo$"i => 1,
" FOO" => qr"foo$"i => 1,
"FOO " => qr"foo$"i => 0,
"FOO " => qr"^foo$"i => 0,
" hidden message " => "hidden message" => 1,
" hidden message " => "hidden message" => 0,
];
$cases_2{__match_declaration} = [
"hidden message" => qr"hidden\s+message" => 1,
"FOO" => qr"foo$"i => 1,
" FOO" => qr"foo$"i => 1,
"FOO " => qr"foo$"i => 0,
"FOO " => qr"^foo$"i => 0,
" hidden message " => "hidden message" => 1,
" hidden message " => "hidden message" => 0,
];
$cases_3{__match} = [
{href => 'http://www.perl.com', alt =>"foo"},{}, "href" => 0,
{href => 'http://www.perl.com', alt =>"foo"},{}, "alt" => 0,
{href => 'http://www.perl.com', alt =>undef},{alt => "boo"}, "alt" => 0,
{href => undef, alt =>"foo"},{href => 'http://www.perl.com'}, "href" => 0,
{href => 'http://www.perl.com', alt =>"foo"},{href => 'www.perl.com'}, "href" => 0,
{href => 'http://www.perl.com', alt =>"foo"},{href => '.', alt => "foo"}, "href" => 0,
{href => 'http://www.perl.com', alt =>"foo"},{href => 'http://www.perl.com'}, "href" => 1,
{href => qr'www\.perl\.com'},{href => 'http://www.perl.com', alt =>"foo"}, "href" => 1,
{href => qr'.', alt => "foo"},{href => 'http://www.perl.com', alt =>"foo"}, "href" => 1,
];
$count = (18 + 24 + 12);
$count += (@{$cases_2{$_}} / 3) for (keys %cases_2);
$count += (@{$cases_3{$_}} / 4) for (keys %cases_3);
};
sub run_case {
my ($count,$methods) = @_;
my $method;
for $method (sort keys %$methods) {
my @cases = @{$methods->{$method}};
while (@cases) {
my (@params) = splice @cases, 0, $count;
my $outcome = pop @params;
my ($visual);
($visual = $method) =~ tr/_/ /;
$visual =~ s/^\s*(.*?)\s*$/$1/;
no strict 'refs';
cmp_ok("Test::HTML::Content::$method"->(@params), '==',$outcome,"$visual(". join( "=~",@params ).")");
};
};
};
sub run {
run_case( 3, \%cases_2 );
run_case( 4, \%cases_3 );
my ($count,$seen);
($count,$seen) = Test::HTML::Content::__count_tags->("<html><body>foo</body></html>","a",{href => "http://www.perl.com"});
is($count, 0,"Counting tags 1");
is(@$seen, 0,"Checking possible candidates");
($count,$seen) = Test::HTML::Content::__count_tags->("<html><a href='http://www.python.org'>Perl</a></html>","a",{href => "http://www.perl.com"});
is($count, 0,"Counting tags 2");
is(@$seen, 1,"Checking possible candidates");
($count,$seen) = Test::HTML::Content::__count_tags->("<html><b href='http://www.python.org'>Perl</b></html>","a",{href => "http://www.perl.com"});
is($count, 0,"Counting tags 3");
is(@$seen, 0,"Checking possible candidates");
($count,$seen) = Test::HTML::Content::__count_tags->("<html><b href='http://www.perl.com'>Perl</b></html>","a",{href => "http://www.perl.com"});
is($count, 0,"Counting tags 4");
is(@$seen, 0,"Checking possible candidates");
($count,$seen) = Test::HTML::Content::__count_tags->("<html><a href='http://www.perl.com'>Perl</a></html>","a",{href => "http://www.perl.com"});
is($count, 1,"Counting tags 6");
is(@$seen, 1,"Checking possible candidates");
($count,$seen) = Test::HTML::Content::__count_tags->("<html><a href='http://www.perl.com' alt='click here'>Perl</a></html>","a",{href => "http://www.perl.com"});
is($count, 1,"Counting tags 7");
is(@$seen, 1,"Checking possible candidates");
($count,$seen) = Test::HTML::Content::__count_tags->("<html><a href='http://www.perl.com' alt=\"don't click here\">Perl</a><a href='http://www.perl.com'>Perl</a></html>","a",{href => "http://www.perl.com", alt => undef});
is($count, 1,"Counting tags 8");
is(@$seen, 2,"Checking possible candidates");
($count,$seen) = Test::HTML::Content::__count_tags->("<html><a href='http://www.perl.com' alt=\"don't click here\">Perl</a><a href='http://www.perl.com'>Perl</a></html>","a",{href => "http://www.perl.com"});
is($count, 2,"Counting tags 9");
is(@$seen, 2,"Checking possible candidates");
($count,$seen) = Test::HTML::Content::__count_tags->("<html><a href='http://www.perl.com' alt=\"don't click here\">Perl</a><p><b><a href='http://www.perl.com'>Perl</a></b></p></html>","a",{href => "http://www.perl.com"});
is($count, 2,"Counting tags 10");
is(@$seen, 2,"Checking possible candidates");
($count,$seen) = Test::HTML::Content::__count_comments( "<html></html>" => "foo" );
is($count,0,"Counting comments 0");
is(@$seen,0,"Counting possible candidates 0");
($count,$seen) = Test::HTML::Content::__count_comments( "<html>foo</html>" => "foo" );
is($count,0,"Counting comments 1");
is(@$seen,0,"Counting possible candidates 1");
($count,$seen) = Test::HTML::Content::__count_comments( "<html><!-- foo --></html>" => "foo" );
is($count,1,"Counting comments 2");
is(@$seen,1,"Counting possible candidates 2");
($count,$seen) = Test::HTML::Content::__count_comments( "<html><!-- foo bar --></html>" => "foo" );
is($count,0,"Counting comments 3");
is(@$seen,1,"Counting possible candidates 3");
($count,$seen) = Test::HTML::Content::__count_comments( "<html><!-- bar foo --></html>" => "foo" );
is($count,0,"Counting comments 4");
is(@$seen,1,"Counting possible candidates 4");
($count,$seen) = Test::HTML::Content::__count_comments( "<html><!-- bar foo --></html>" => "foo" );
is($count,0,"Counting comments 5");
is(@$seen,1,"Counting possible candidates 5");
($count,$seen) = Test::HTML::Content::__count_comments( "<html><!-- foo --></html>" => "foo " );
is($count,1,"Counting comments 6");
is(@$seen,1,"Counting possible candidates 6");
($count,$seen) = Test::HTML::Content::__count_comments( "<html><!-- bar foo --></html>" => qr"foo" );
is($count,1,"Counting comments 7");
is(@$seen,1,"Counting possible candidates 7");
($count,$seen) = Test::HTML::Content::__count_comments( "<html><!--foo--></html>" => "foo" );
is($count,1,"Counting comments 8");
is(@$seen,1,"Counting possible candidates 8");
($count,$seen) = Test::HTML::Content::__count_comments( "<html><!--foo--><!--foo--></html>" => "foo" );
is($count,2,"Counting comments 9");
is(@$seen,2,"Counting possible candidates 9");
($count,$seen) = Test::HTML::Content::__count_comments( "<html><!--foo--><!--foo--><!--foo--></html>" => "foo" );
is($count,3,"Counting comments 10");
is(@$seen,3,"Counting possible candidates 10");
($count,$seen) = Test::HTML::Content::__count_comments( "<html><!--foo--><!--bar--><!--foo--></html>" => "foo" );
is($count,2,"Counting comments 11");
is(@$seen,3,"Counting possible candidates 11");
($count,$seen) = Test::HTML::Content::__count_text( "<html></html>" => "foo" );
is($count,0,"Counting text occurrences 0");
is(@$seen,0,"Counting possible candidates 0");
($count,$seen) = Test::HTML::Content::__count_text( "<html>foo</html>" => "foo" );
is($count,1,"counting text occurrences 1");
is(@$seen,1,"Counting possible candidates 1");
($count,$seen) = Test::HTML::Content::__count_text( "<html><!-- foo --></html>" => "foo" );
is($count,0,"counting text occurrences 2");
is(@$seen,0,"Counting possible candidates 2");
# This test disabled, as it is not consistent between XPath and NoXPath...
#($count,$seen) = Test::HTML::Content::__count_text( "<html><head></head><body><p> <!-- foo bar --> </p></body></html>" => "foo" );
#is($count,0,"counting text occurrences 3");
#is(@$seen,2,"Counting possible candidates 3");
($count,$seen) = Test::HTML::Content::__count_text( "<html>foo<!-- bar foo --> bar</html>" => "foo" );
is($count,1,"counting text occurrences 4");
is(@$seen,2,"Counting possible candidates 4");
($count,$seen) = Test::HTML::Content::__count_text( "<html>f<!-- bar foo -->o<!-- bar -->o</html>" => "foo" );
is($count,0,"counting text occurrences 5");
is(@$seen,3,"Counting possible candidates 5");
($count,$seen) = Test::HTML::Content::__count_text( "<html>Hello foo World</html>" => qr"foo" );
is($count,1,"Checking RE for text 6");
is(@$seen,1,"Counting possible candidates 6");
};
runtests( $count, \&run );
|