| 1 |
# $Id: use.t,v 1.27 2008-07-14 03:11:10 Martin Exp $ |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use ExtUtils::testlib; |
| 7 |
use Test::More 'no_plan'; |
| 8 |
|
| 9 |
use IO::Capture::Stderr; |
| 10 |
ok(my $oICE = IO::Capture::Stderr->new); |
| 11 |
|
| 12 |
BEGIN |
| 13 |
{ |
| 14 |
use_ok('WWW::Search'); |
| 15 |
use_ok('WWW::SearchResult'); |
| 16 |
use_ok('WWW::Search::Result'); |
| 17 |
use_ok('WWW::Search::Test', |
| 18 |
qw(new_engine run_gui_test run_test skip_test count_results)); |
| 19 |
} # end of BEGIN block |
| 20 |
|
| 21 |
my @as; |
| 22 |
eval { @as = &WWW::Search::installed_engines }; |
| 23 |
ok(0 < scalar(@as), 'any installed engines'); |
| 24 |
diag('FYI the following backends are already installed (including ones in this distribution): '. join(', ', sort @as)); |
| 25 |
my %hs = map { $_ => 1 } @as; |
| 26 |
my $sBackend = 'AltaVista'; |
| 27 |
$ENV{HARNESS_PERL_SWITCHES} ||= ''; |
| 28 |
if (exists $hs{$sBackend} && ($ENV{HARNESS_PERL_SWITCHES} =~ m!Devel::Cover!)) |
| 29 |
{ |
| 30 |
diag(qq{You are running 'make cover' and I found WWW::Search::$sBackend installed, therefore I will do some live searches to enhance the coverage testing...}); |
| 31 |
my $o = new WWW::Search($sBackend); |
| 32 |
$o->native_query('Vader'); |
| 33 |
$o->maximum_to_retrieve(111); |
| 34 |
my @ao = $o->results(); |
| 35 |
} # if |
| 36 |
|
| 37 |
# Make sure an undef query does not die; |
| 38 |
my $o1 = new WWW::Search; # NO BACKEND SPECIFIED |
| 39 |
ok(ref $o1); |
| 40 |
my @ao = $o1->results(); |
| 41 |
ok(ref $o1->response); |
| 42 |
ok($o1->response->is_error); |
| 43 |
ok(scalar(@ao) == 0); |
| 44 |
# Make sure an empty query does not die; |
| 45 |
my $o2 = new WWW::Search; # NO BACKEND SPECIFIED |
| 46 |
ok(ref $o2); |
| 47 |
$o2->native_query(''); # EMPTY STRING |
| 48 |
my @ao2 = $o2->results(); |
| 49 |
ok(ref $o2->response); |
| 50 |
ok($o2->response->is_error); |
| 51 |
ok(scalar(@ao2) == 0); |
| 52 |
# Tests for approx_result_count: |
| 53 |
is($o2->approximate_result_count(3), 3); |
| 54 |
is($o2->approximate_result_count(undef), 3); |
| 55 |
is($o2->approximate_result_count(''), 3); |
| 56 |
is($o2->approximate_result_count(0), 0); |
| 57 |
is($o2->approximate_result_count(2), 2); |
| 58 |
is($o2->approximate_hit_count(undef), 2); |
| 59 |
is($o2->approximate_hit_count(-1), 2); |
| 60 |
# Test for what happens when a backend is not installed: |
| 61 |
my $o3; |
| 62 |
eval { $o3 = new WWW::Search('No_Such_Backend') }; |
| 63 |
like($@, qr{(?i:can not load backend)}); |
| 64 |
my $iCount = 44; |
| 65 |
my $o4 = new WWW::Search('Null::Count', |
| 66 |
'_null_count' => $iCount, |
| 67 |
); |
| 68 |
# Get result_count before submitting query: |
| 69 |
is($o4->approximate_result_count, $iCount); |
| 70 |
# Get some results: |
| 71 |
ok($o4->login, 'login'); |
| 72 |
$o4->maximum_to_retrieve($iCount/2); |
| 73 |
my $iCounter = 0; |
| 74 |
while ($o4->next_result) |
| 75 |
{ |
| 76 |
$iCounter++; |
| 77 |
} # while |
| 78 |
is($iCounter, $iCount/2, 'next_result stops at maximum_to_retrieve'); |
| 79 |
$o4->maximum_to_return($iCount * 2); |
| 80 |
while ($o4->next_result) |
| 81 |
{ |
| 82 |
$iCounter++; |
| 83 |
} # while |
| 84 |
is($iCounter, $iCount, 'next_result goes to end of cache'); |
| 85 |
ok($o4->logout, 'logout'); |
| 86 |
ok($o4->response, 'response'); |
| 87 |
ok($o4->submit, 'submit'); |
| 88 |
is($o4->opaque, undef, 'opaque is undef'); |
| 89 |
$o4->opaque('hello'); |
| 90 |
is($o4->opaque, 'hello', 'opaque is hello'); |
| 91 |
$o4->seek_result(undef); |
| 92 |
$o4->seek_result(-1); |
| 93 |
$o4->seek_result(3); |
| 94 |
is(WWW::Search::escape_query('+hi +mom'), '%2Bhi+%2Bmom', 'escape'); |
| 95 |
is(WWW::Search::unescape_query('%2Bhi+%2Bmom'), '+hi +mom', 'unescape'); |
| 96 |
# Use a backend for a second time (just to exercise the code in |
| 97 |
# Search.pm): |
| 98 |
my $o5 = new WWW::Search('Null::Count'); |
| 99 |
# Test the version() function: |
| 100 |
ok($o4->version, 'version defined in backend'); |
| 101 |
$o5 = new WWW::Search('Null::NoVersion'); |
| 102 |
is($o5->version, $WWW::Search::VERSION, 'default version'); |
| 103 |
# Test the maintainer() function: |
| 104 |
ok($o4->maintainer, 'maintainer defined in backend'); |
| 105 |
is($o5->maintainer, $WWW::Search::MAINTAINER, 'default maintainer'); |
| 106 |
# Exercise / test the cookie_jar() function: |
| 107 |
$o4->cookie_jar('t/cookies.txt'); |
| 108 |
my $oCookies = new HTTP::Cookies; |
| 109 |
$o5->cookie_jar($oCookies); |
| 110 |
$oICE->start; |
| 111 |
eval { $o2->cookie_jar($o4) }; |
| 112 |
$oICE->stop; |
| 113 |
$oCookies = $o4->cookie_jar; |
| 114 |
# Exercise / test the native_query() function: |
| 115 |
$o4->{_debug} = 1; |
| 116 |
$oICE->start; |
| 117 |
$o4->gui_query('query', {option1 => 1, |
| 118 |
search_option2 => 2, |
| 119 |
}); |
| 120 |
$oICE->stop; |
| 121 |
$o4->{_debug} = 0; |
| 122 |
$o4->gui_query('query', {option1 => 1, |
| 123 |
search_option2 => 2, |
| 124 |
}); |
| 125 |
# Exercise other set/get functions: |
| 126 |
is($o4->date_from, ''); |
| 127 |
is($o4->date_to, ''); |
| 128 |
is($o4->env_proxy, 0); |
| 129 |
is($o4->http_proxy, undef); |
| 130 |
is($o4->http_proxy_user, undef); |
| 131 |
is($o4->http_proxy_pwd, undef); |
| 132 |
$o4->date_from('dummydate'); |
| 133 |
$o4->date_to ('dummydate'); |
| 134 |
$o4->env_proxy('dummydate'); |
| 135 |
$o4->http_proxy('dummydate'); |
| 136 |
$o4->http_proxy_user('dummydate'); |
| 137 |
$o4->http_proxy_pwd('dummydate'); |
| 138 |
is($o4->date_from, 'dummydate'); |
| 139 |
is($o4->date_to, 'dummydate'); |
| 140 |
is($o4->env_proxy, 'dummydate'); |
| 141 |
is_deeply($o4->http_proxy, ['dummydate']); |
| 142 |
is($o4->http_proxy_user, 'dummydate'); |
| 143 |
is($o4->http_proxy_pwd, 'dummydate'); |
| 144 |
# Sanity-tests for proxy stuff: |
| 145 |
my $o6 = new WWW::Search; |
| 146 |
ok(! $o6->is_http_proxy); |
| 147 |
ok(! $o6->is_http_proxy_auth_data); |
| 148 |
$o6->http_proxy(''); |
| 149 |
ok(! $o6->is_http_proxy); |
| 150 |
ok(! $o6->is_http_proxy_auth_data); |
| 151 |
$o6->http_proxy('some', 'things'); |
| 152 |
ok($o6->is_http_proxy); |
| 153 |
ok(! $o6->is_http_proxy_auth_data); |
| 154 |
$o6->http_proxy_user('something'); |
| 155 |
ok(! $o6->is_http_proxy_auth_data); |
| 156 |
$o6->http_proxy_pwd('something'); |
| 157 |
# use Data::Dumper; |
| 158 |
# print STDERR Dumper(\$o6); |
| 159 |
ok($o6->is_http_proxy_auth_data); |
| 160 |
# Sanity-tests for the timeout() method: |
| 161 |
is($o6->timeout, 60); |
| 162 |
$o6->timeout(120); |
| 163 |
is($o6->timeout, 120); |
| 164 |
is(WWW::Search::strip_tags('hello<TAG> <TAG2>world'), 'hello world'); |
| 165 |
ok($o6->user_agent('non-robot')); |
| 166 |
ok($o6->agent_name('junk')); |
| 167 |
is($o6->agent_name, 'junk'); |
| 168 |
is($o6->agent_name, 'junk'); |
| 169 |
ok(! $o6->agent_email('junk')); |
| 170 |
is($o6->agent_email, 'junk'); |
| 171 |
is($o6->agent_email, 'junk'); |
| 172 |
ok($o6->user_agent); |
| 173 |
ok(! $o6->http_referer('junk')); |
| 174 |
is($o6->http_referer, 'junk'); |
| 175 |
is($o6->http_referer, 'junk'); |
| 176 |
ok($o6->http_method('junk')); |
| 177 |
is($o6->http_method, 'junk'); |
| 178 |
is($o6->http_method, 'junk'); |
| 179 |
|
| 180 |
# Tests for WWW::SearchResult: |
| 181 |
my $oWSR = new WWW::SearchResult; |
| 182 |
$oWSR->add_url('url1'); |
| 183 |
$oWSR->thumb_url('url1.thumb'); |
| 184 |
$oWSR->image_url('url1.png'); |
| 185 |
$oWSR->title('title1'); |
| 186 |
$oWSR->description('description1'); |
| 187 |
$oWSR->change_date("yesterday"); |
| 188 |
$oWSR->start_date("last Tuesday"); |
| 189 |
$oWSR->index_date("today"); |
| 190 |
$oWSR->end_date("tomorrow"); |
| 191 |
$oWSR->raw(qq{<A HREF="url1">title1</a>}); |
| 192 |
$oWSR->score(99); |
| 193 |
$oWSR->normalized_score(990); |
| 194 |
$oWSR->size(4096); |
| 195 |
$oWSR->source('WWW::Search'); |
| 196 |
$oWSR->company('Dub Dub Dub Search, Inc.'); |
| 197 |
$oWSR->location('Ashburn, VA'); |
| 198 |
$oWSR->bid_amount(9.99); |
| 199 |
$oWSR->shipping(4.85); |
| 200 |
$oWSR->bid_count(9); |
| 201 |
$oWSR->question_count(3); |
| 202 |
$oWSR->watcher_count(65); |
| 203 |
$oWSR->item_number(987654321); |
| 204 |
$oWSR->category(7654); |
| 205 |
$oWSR->bidder('Joe'); |
| 206 |
$oWSR->seller('Jane'); |
| 207 |
ok($o4->result_as_HTML($oWSR)); |
| 208 |
is($o4->result_as_HTML(), ''); |
| 209 |
is($o4->result_as_HTML(undef), ''); |
| 210 |
is($o4->result_as_HTML(0), ''); |
| 211 |
is($o4->result_as_HTML(1), ''); |
| 212 |
is($o4->result_as_HTML([1,2]), ''); |
| 213 |
my $s = $oWSR->as_HTML; |
| 214 |
# Other miscellaneous sanity checks and coverage tests: |
| 215 |
is(&WWW::Search::escape_query, ''); |
| 216 |
my @a = &WWW::Search::unescape_query(qw(a b c)); |
| 217 |
$o2->strip_tags('a', undef, 'b'); |
| 218 |
delete $ENV{WWW_SEARCH_USERAGENT}; |
| 219 |
$o2->user_agent(1); |
| 220 |
$o2->user_agent; |
| 221 |
$ENV{WWW_SEARCH_USERAGENT} = 'No::Such::Module'; |
| 222 |
$oICE->start; |
| 223 |
$o2->user_agent(1); |
| 224 |
$o2->user_agent; |
| 225 |
$oICE->stop; |
| 226 |
my $sICE = join("\n", $oICE->read); |
| 227 |
like($sICE, qr'can not load'); |
| 228 |
$ENV{WWW_SEARCH_USERAGENT} = 'Carp'; # a module which does not have a new() |
| 229 |
$oICE->start; |
| 230 |
$o2->user_agent(1); |
| 231 |
$o2->user_agent; |
| 232 |
$oICE->stop; |
| 233 |
$sICE = join("\n", $oICE->read); |
| 234 |
like($sICE, qr'can not create'); |
| 235 |
$s = qq{foo\nbar\nbaz}; |
| 236 |
$o2->split_lines($s); |
| 237 |
$o2->split_lines(['a'], $s); |
| 238 |
$o2->generic_option; |
| 239 |
$o2->_native_setup_search; |
| 240 |
$o2->user_agent_delay; |
| 241 |
$o2->user_agent_delay(1); |
| 242 |
$o2->absurl; |
| 243 |
$o2->absurl('foo'); |
| 244 |
$o2->absurl('foo', 'bar'); |
| 245 |
$o2->need_to_delay; |
| 246 |
$o2->_parse_tree; |
| 247 |
$o2->_native_retrieve_some; |
| 248 |
$o2->preprocess_results_page; |
| 249 |
$o2->preprocess_results_page('foo'); |
| 250 |
$o2->test_cases; |
| 251 |
$o2->hash_to_cgi_string; |
| 252 |
$o2->hash_to_cgi_string({ |
| 253 |
foo => 'foo', |
| 254 |
bar => undef, |
| 255 |
undef => 'baz', |
| 256 |
empty => '', |
| 257 |
}); |
| 258 |
exit 0; |
| 259 |
|
| 260 |
foreach my $sEngine (@as) |
| 261 |
{ |
| 262 |
my $o; |
| 263 |
# diag(qq{trying $sEngine}); |
| 264 |
eval { $o = new WWW::Search($sEngine) }; |
| 265 |
ok(ref($o), qq{loaded WWW::Search::$sEngine}); # } ) # Emacs bug |
| 266 |
} # foreach |
| 267 |
|
| 268 |
exit 0; |
| 269 |
|
| 270 |
__END__ |
| 271 |
|