-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.pl
353 lines (304 loc) · 13 KB
/
test.pl
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'
use ExtUtils::testlib;
use lib 't/lib','./blib/lib';
use Test::More;
$VERSION = sprintf("%d.%02d", q$Revision: 1.07 $ =~ /(\d+)\.(\d+)/);
my @TestTheseOnly;# = qw(Sherlock); # this is active only when WWW::Scraper::isGlennWood;
######################### We start with some black magic to print on failure.
# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t subdirectory.)
use WWW::Scraper(qw(3.01));
BEGIN { select STDERR; $| = 1; select STDOUT; $| = 1; }
END {
}
my $countErrorMessages = 0;
my $countWarningMessages = 0;
use FileHandle;
my $traceFile = new FileHandle('>test.trace') or die "Can't open test.trace file: $!";
select ($traceFile); $| = 1; select STDOUT;
# Report current versions of modules we depend on.
traceBreak(); ##_##_##_##_##_##_##_##_##_##_##_##_##_##_##_##
print $traceFile "Operating system: $^O\n";
print $traceFile "Perl version: $]\n";
my $msg = <<EOT;
VERSIONS OF MODULES ON WHICH SCRAPER DEPENDS
EOT
print $traceFile $msg;
diag $msg;
open TMP, "<Makefile.PL";
my @makefile = <TMP>;
close TMP;
my $makefile = join '',@makefile;
use vars qw($prereq_pm);
$makefile =~ s/^.*'PREREQ_PM'\s*=>([^}]*}).*$/\$prereq_pm = $1/s;
eval $makefile;
for ( sort keys %$prereq_pm ) {
my $mod_version = '';
eval "use $_($$prereq_pm{$_}); \$mod_version = \$$_\:\:VERSION;";
$mod_version = '' unless $mod_version;
print $traceFile " using $_($mod_version);\n";
diag " using $_($mod_version);\n";
}
traceBreak(); ##_##_##_##_##_##_##_##_##_##_##_##_##_##_##_##
print $traceFile <<EOT;
LIST SCRAPER SUB-CLASSES, FROM THE MANIFEST
EOT
open TMP, "<MANIFEST";
my (@modules, @skipModules, @todoModules);
while (<TMP>) {
if ( my ($scraperEngine) = m-^lib/WWW/Scraper/(\w+)\.pm$- ) {
# If parameters supplied to "make test", then test just those engines.
if ( WWW::Scraper::isGlennWood and @TestTheseOnly ) {
my $testThis;
map { $testThis = ( $scraperEngine eq $_ ) } @TestTheseOnly;
unless ( $testThis ) {
TRACE(0, " - $scraperEngine will not be tested: it is not in the \@TestTheseOnly list.\n");
next;
}
}
my $testParameters;
eval "use WWW::Scraper::$1; \$testParameters = &WWW::Scraper::$1\::testParameters()";
if ( $@ ) { # $@ just means the module is not a Scraper sub-class.
TRACE(0, " - $scraperEngine will not be tested: it is not a Scraper sub-class.\n");
}
elsif ( $testParameters ) {
if ( $testParameters->{'SKIP'} ) {
push @skipModules, $scraperEngine;
}
elsif ( $testParameters->{'TODO'} ) {
push @todoModules, $scraperEngine;
}
else {
push @modules, $scraperEngine;
}
my $mod_version;
eval "\$mod_version = \$WWW::Scraper::$scraperEngine\:\:VERSION;";
$mod_version = '' unless $mod_version;
TRACE(0, " + $1($mod_version)\n");
}
}
}
close TMP;
traceBreak(); ##_##_##_##_##_##_##_##_##_##_##_##_##_##_##_##
my $testCount = scalar(@modules) + scalar(@skipModules) + scalar(@todoModules);
plan(tests => $testCount + 2);
ok(1, "$testCount Scraper modules listed in MANIFEST (".scalar(@modules).','.scalar(@todoModules).','.scalar(@skipModules).')');
use strict;
use WWW::Scraper(qw(2.13));
use WWW::Scraper::Request;
ok(1, "WWW::Scraper loaded");
push @modules, @todoModules, @skipModules;
for my $sEngine ( sort @modules ) {
SKIP: {
TODO: {
traceBreak(); ##_##_##_##_##_##_##_##_##_##_##_##_##_##_##_##
my $testParameters;
eval "\$testParameters = &WWW::Scraper::$sEngine\::testParameters()";
skip $testParameters->{'SKIP'}, 1 if $testParameters->{'SKIP'};
local $TODO = $testParameters->{'TODO'} if $testParameters->{'TODO'};
my $result;
eval { $result = TestThisEngine($sEngine) };
if ( (not $@) and $result ) {
ok (1, "$sEngine");
} else {
TRACE(0, "Scraper engine $sEngine failed once: $@\n");
# Some of these search engines don't always work the first time.
# Give them a second shot before declaring that Scraper is the one that failed!
eval { $result = TestThisEngine($sEngine) };
if ( (not $@) and $result ) {
ok(1, "$sEngine");
} else {
ok(0, "$sEngine");
diag $@ if $@;
TRACE(2, "Scraper engine $sEngine failed twice: $@\n");
}
}
}
}
}
close $traceFile;
if ( $countWarningMessages and WWW::Scraper::isGlennWood() ) {
diag "$countWarningMessages warning".(($countWarningMessages>1)?'s':'').". See file 'test.trace' for details.\n";
}
if ( $countErrorMessages ) {
diag "$countErrorMessages test".(($countErrorMessages>1)?'s':'')." had problems. See file 'test.trace' for details.\n";
}
if ( $countErrorMessages ) {
open TMP, "<test.trace";
print join '', <TMP>;
close TMP;
}
#######################################################################################
#######################################################################################
#######################################################################################
#######################################################################################
#######################################################################################
#######################################################################################
sub TestThisEngine {
my ($sEngine) = @_;
my $success = 1;
my $jTest = 0;
TRACE(0, "Test #$jTest: $sEngine\n");
my $oSearch = new WWW::Scraper($sEngine);
if ( not ref($oSearch)) {
TRACE(1, "Can't load scraper module for $sEngine: $!\n");
return 0;
}
#######################################################################################
#
# BOGUS QUERY
#
# This test returns no results (but we should not get an HTTP error):
#
#######################################################################################
$jTest++;
TRACE(0, "Test #$jTest: $sEngine 'bogus' search\n");
my $iResults = 0;
my ($sQuery, $options, $onePageCount, $multiPageCount, $bogusPageCount) = $oSearch->setupStandardAndExceptionalOptions($sEngine);
$sQuery = "Bogus" . $$ . "NoSuchWord" . time;
my $request = new WWW::Scraper::Request($oSearch, $sQuery, $options);
$oSearch->setScraperTrace($ENV{'SCRAPER_DEBUG_MODE'});
$oSearch->SetRequest($request);
my @aoResults = $oSearch->results();
$iResults = scalar(@aoResults);
if ( $bogusPageCount < $iResults ) {
TRACE (2, " --- got $iResults 'bogus' results, expected $bogusPageCount\n");
#$success = 0; # A fail of the bogus query is not a fail of the installation, right? reallY?
}
#######################################################################################
#
# ONE-PAGE QUERY
#
# This query returns 1 page of results
#
#######################################################################################
$jTest++;
TRACE(0, "Test #$jTest: $sEngine one-page search\n");
# Set up standard, and exceptional, options.
($sQuery, $options, $onePageCount, $multiPageCount, $bogusPageCount) = $oSearch->setupStandardAndExceptionalOptions($sEngine);
# Skip this test if no results are expected anyway.
if ( $onePageCount ) {
my $request = new WWW::Scraper::Request($oSearch, $sQuery, $options);
$oSearch->native_query($sQuery); # This let's us test pre-v2.00 modules from here, too.
$oSearch->SetRequest($request);
my $maximum_to_retrieve = $onePageCount;
$oSearch->maximum_to_retrieve($maximum_to_retrieve); # 1 page
my $iResults = 0;
eval {
my @aoResults = $oSearch->results();
$iResults = scalar(@aoResults);
};
TRACE(0, " + got $iResults results for '$sQuery'\n");
if ( $maximum_to_retrieve > $iResults )
{
my ($message, $bytes);
if (my $response = $oSearch->response) {
$message = $response->status_line();
$bytes = length $response->content();
} else {
($message,$bytes) = ('(no response object)', '(no response object)');
}
TRACE(1, <<EOT);
--- got $iResults results for $sEngine '$sQuery', but expected $maximum_to_retrieve
--- base URL: $oSearch->{'_base_url'}
--- first URL: $oSearch->{'_first_url'}
--- last URL: $oSearch->{'_last_url'}
--- next URL: $oSearch->{'_next_url'}
--- response message: $message
--- content size (bytes): $bytes
--- ERRNO: $!
--- Extended OS error: $^E
EOT
return 0;
}
}
#######################################################################################
#
# MULTI-PAGE QUERY
#
# This query returns MANY pages of results
#
#######################################################################################
$jTest++;
TRACE(0, "Test #$jTest: $sEngine multi-page search\n");
($sQuery, $options, $onePageCount, $multiPageCount, $bogusPageCount) = $oSearch->setupStandardAndExceptionalOptions($sEngine);
# Don't bother with this test if $multiPageCount <= $onePageCount - we've already done it.
if ( $multiPageCount > $onePageCount ) {
my $maximum_to_retrieve = $multiPageCount; # 2 or 3 pages
$oSearch->maximum_to_retrieve($maximum_to_retrieve); # 2 or 3 pages
my $request = new WWW::Scraper::Request($oSearch, $sQuery, $options);
$oSearch->native_query($sQuery); # This let's us test pre-v2.00 modules from here, too.
$oSearch->SetRequest($request);
$iResults = 0;
eval {
while ( $iResults < $maximum_to_retrieve ) {
last unless $oSearch->next_response();
$iResults += 1;
}
};
TRACE(0, " + got $iResults multi-page results for '$sQuery'\n");
if ( $maximum_to_retrieve > $iResults )
{
my $message = $oSearch->response()->status_line();
my $bytes = length $oSearch->response()->content();
my $contentAnalysis = ' --- Content Analysis: '.${$oSearch->ContentAnalysis()}."'\n";
$contentAnalysis =~ s/\n/\n --- /gs;
TRACE(1, <<EOT);
--- got $iResults results for multi-page $sEngine '$sQuery', but expected $maximum_to_retrieve.
--- base URL: $oSearch->{'_base_url'}
--- first URL: $oSearch->{'_first_url'}
--- last URL: $oSearch->{'_last_url'}
--- next URL: $oSearch->{'_next_url'}
--- response message: $message
--- content size (bytes): $bytes
--- ERRNO: $!
--- Extended OS error: $^E
$contentAnalysis
EOT
#$success = 0; # A fail of the multi-page query is not a fail of the installation, right? reallY?
}
}
return $success;
}
sub TRACE {
$countWarningMessages += 1 if $_[0] == 1;
$countErrorMessages += 1 if $_[0] == 2;
$traceFile->print($_[1]);
# print $_[1] if WWW::Scraper::isGlennWood();
}
{ package WWW::Scraper;
# Set up standard, and exceptional, options.
sub setupStandardAndExceptionalOptions {
my ($oSearch, $sEngine) = @_;
my $sQuery = 'Perl';
my $options;
my $onePageCount = 9;
my $multiPageCount = 41;
my $bogusPageCount = 0;
my %specialOptions;
# Most Scraper sub-classes will define their own testParameters . . .
# See Dogpile.pm for the most mature example of how to set your testParameters.
if ( my $testParameters = $oSearch->testParameters() ) {
$sQuery = $testParameters->{'testNativeQuery'} || $sQuery;
$options = $testParameters->{'testNativeOptions'};
$options = {} unless $options;
($onePageCount,$multiPageCount,$bogusPageCount) = (9,41,0);
$onePageCount = $testParameters->{'expectedOnePage'} || $onePageCount;
$multiPageCount = $testParameters->{'expectedMultiPage'} || $multiPageCount;
$bogusPageCount = $testParameters->{'expectedBogusPage'} || $bogusPageCount;
return ($sQuery, $options, $onePageCount, $multiPageCount, $bogusPageCount);
}
# . . . others aren't ready for prime-time, so we hard wire their testParameters here.
my %specialQuery = (
);
$sQuery = $specialQuery{$sEngine} if defined $specialQuery{$sEngine};
return ($sQuery,$options,$onePageCount,$multiPageCount,$bogusPageCount);
}
}
sub traceBreak {
print $traceFile <<EOT;
##_##_##_##_##_##_##_##_##_##_##_##_##_##_##
EOT
}
__END__