File Coverage

blib/lib/WWW/BookBot/Test.pm
Criterion Covered Total %
statement 152 183 83.0
branch 13 32 40.6
condition 2 6 33.3
subroutine 30 39 76.9
pod 0 30 0.0
total 197 290 67.9


line stmt bran cond sub pod time code
1             package WWW::BookBot::Test;
2 3     3   71175 use strict;
  3         7  
  3         106  
3 3     3   14 use warnings;
  3         6  
  3         89  
4 3     3   12 no warnings qw(uninitialized utf8);
  3         9  
  3         96  
5 3     3   15 use base qw(Exporter);
  3         4  
  3         385  
6 3     3   15 use vars qw($VERSION @EXPORT @EXPORT_OK);
  3         5  
  3         379  
7             $VERSION = "0.12";
8             @EXPORT = qw(
9             new_bot read_file
10             dump_class dump_var en_code de_code parse_patterns test_pattern
11             match_print string_limitlen get_pattern
12             test_init test_begin test_end
13             test_encoding test_parse_patterns test_msg_format
14             test_file test_log test_result test_DB
15             test_agent test_url test_fetch test_parser
16             test_catalog_get_book test_book_chapters
17             test_writebin test_parse_bintext
18             );
19             @EXPORT_OK = @EXPORT;
20              
21 3     3   13 use Test::More;
  3         4  
  3         16  
22 3     3   16489 use Data::Dumper;
  3         35609  
  3         209  
23 3     3   3171 use File::Spec::Functions;
  3         2608  
  3         273  
24 3     3   18 use File::Path;
  3         5  
  3         8966  
25              
26             #------ contants and vars
27             our $CLEARRESULT=1; #clear reseult dir before quit
28             our $TESTONLINE=0; #test online or not
29             our $classname='WWW::BookBot'; #class to be tested
30             our $testdir; #the dir to put logs in
31             our $bot; #the bot created as $classname
32              
33             #-------------------------------------------------------------
34             # Support functions
35             # new_bot(@args) => N/A
36             # read_file($filename) => $content
37             # dump_class() => N/A
38             # dump_var($var_name) => N/A
39             # en_code($content_de) => $content_en
40             # de_code($content_en) => $content_de
41             # parse_patterns($str) => $pattern
42             # get_pattern($pattern_name) => $pattern
43             # test_pattern($pattern_name, $content) => 1-match 0-no match
44             # string_limitlen($len, $content_de) => $content_en
45             # match_print($match_bool_result) => N/A
46             #-------------------------------------------------------------
47             sub new_bot(@) {
48 21     21 0 4426 $bot=$classname->new({work_dir=>$testdir, @_});
49             }
50             sub read_file($) {
51 24     24 0 49 my $filename = shift;
52 24         45 my $str="";
53 24         59 local(*WORK);
54 24 50       877 open(WORK, $filename) or return "";
55 24 50       323 sysread(WORK, $str, 6000000) or return "";
56 24 50       261 close(WORK) or return "";
57 24         62 $str=~s/\r\n|\r/\n/; #to ensure tests work on both unix and win32
58 24         258 return $str;
59             }
60             sub dump_class() {
61 0     0 0 0 $bot->dump_class;
62             }
63             sub dump_var($) {
64 0     0 0 0 $bot->dump_var($_[0]);
65             }
66             sub en_code($) {
67 0     0 0 0 $bot->en_code($_[0]);
68             }
69             sub de_code($) {
70 0     0 0 0 $bot->de_code($_[0]);
71             }
72             sub parse_patterns($) {
73 0     0 0 0 $bot->parse_patterns($_[0]);
74             }
75             sub get_pattern($) {
76 0     0 0 0 $bot->{patterns}->{$_[0]};
77             }
78             sub test_pattern($$) {
79 0     0 0 0 $bot->test_pattern(@_);
80             }
81             sub string_limitlen($$) {
82 0 0   0 0 0 length($_[1])<=($_[0]+8) ?
83             $bot->en_code($_[1]) :
84             $bot->en_code(substr($_[1],0,$_[0]/2))."......".$bot->en_code(substr($_[1],length($_[1])-$_[0]/2));
85             }
86             sub match_print($) {
87 0     0 0 0 my ($result)=@_;
88 0 0       0 if( $result ) {
89 0         0 print "Match ";
90 0         0 my $i=0;
91 0         0 foreach ($1, $2, $3, $4, $5, $6, $7, $8, $9) {
92 0         0 $i++;
93 0 0       0 next if $_ eq '';
94 0         0 printf " [%d]=\'%s\'\n", $i, string_limitlen(300,$_);
95             }
96 0         0 print "\n";
97             } else {
98 0         0 print "No Match\n";
99             }
100             }
101              
102             #-------------------------------------------------------------
103             # Routines
104             # test_init($classname) => N/A
105             # test_begin() => N/A
106             # test_end() => N/A
107             #-------------------------------------------------------------
108             sub test_init($) {
109 3     3 0 2371 $classname=$_[0];
110             }
111             sub test_begin() {
112 3 50   3 0 123 chdir 't' if -d 't';
113 3 50       40 $testdir=($classname=~/::([^:]*)$/) ? $1 : $classname;
114 3         502 rmtree($testdir);
115 3         17 new_bot();
116 3         38 ok(defined($bot), "new $classname");
117 3         1734 ok(-d $testdir, "create directory $testdir");
118             }
119             sub test_end() {
120 3 50   3 0 3878 rmtree($testdir) if $CLEARRESULT;
121 3         26 chdir 't';
122 3         402 chdir '..';
123             }
124              
125             #-------------------------------------------------------------
126             # Tests
127             #-------------------------------------------------------------
128             sub test_encoding {
129 6     6 0 663 my ($in, $out, $disp)=@_;
130 6         994 print "Content='$in'\n";
131 6         33 my $decode=$bot->de_code($in);
132 6         516 print " [decode as $bot->{LANGUAGE_ENCODE}] $decode\n";
133 6         42 my $encode=$bot->en_code($decode);
134 6         447 print " [encode as $bot->{LANGUAGE_ENCODE}] $encode\n";
135 6         45 is($encode, $out, "function en_code and de_code $disp");
136             }
137              
138             sub test_parse_patterns {
139 15     15 0 5339 my ($in, $out, $disp)=@_;
140 15         58 is($bot->en_code($bot->parse_patterns($in)), $out, "function parse_patterns $disp");
141             }
142              
143             sub test_msg_format {
144 3     3 0 1117 my ($pargs, $out, $disp)=@_;
145 3         23 is($bot->msg_format("TestMsg",$pargs), $out, "function msg_format $disp");
146             }
147              
148             sub test_file {
149 3     3 0 1081 my $filename=catfile($testdir, "test_file.txt");
150 3 50       107 unlink $filename if -f $filename;
151 3         18 $bot->file_init("DB", $filename);
152 3         47 ok(-f $filename, "function file_init");
153 3         1209 my $result="";
154 3         11 foreach (@_) {
155 6         42 $bot->file_add("DB", $filename, $_);
156 6         17 $result.=$_;
157             }
158 3         17 is(read_file($filename), $result, "function file_add file=$filename");
159             }
160              
161             sub test_log {
162 3     3 0 2253 my $filename=$bot->{file_log};
163 3 50       99 unlink $filename if -f $filename;
164 3         154 $bot->log_msg($_[0]);
165 3         17 $bot->log_msg("");
166 3         20 $bot->log_msg($_[0], $_[1], "\n");
167 3         15 is(read_file($filename), "$_[0]$_[0]$_[1]\n", "function log_msg file=$filename");
168 3 50       2158 unlink $filename if -f $filename;
169 3         11 my $str=$_[2];
170 3         22 $bot->log_msgen($bot->de_code($str));
171 3         1269 print "\n";
172 3         18 is(read_file($filename), $str, "function log_msgen file=$filename");
173 3 50       3131 unlink $filename if -f $filename;
174 3         51 $bot->log_add("TestMsg", $_[3]);
175 3         262 print "\n";
176 3         14 is(read_file($filename), $_[4], "function logadd file=$filename");
177             }
178              
179             sub test_result {
180 3     3 0 1328 my $timenow=time;
181 3         3000457 sleep 1;
182 3         102 my $filename=$bot->result_filename({title=>'Result*Test'}); #bad name
183 3 50       145 unlink $filename if -f $filename;
184 3         32 $bot->result_init({title=>'ResultTest'});
185 3         81 ok(-f $filename, "function result_init file=$filename");
186 3         1882 my $result="";
187 3         13 foreach (@_) {
188 6         42 $bot->result_add($filename, $_);
189 6         20 $result.=$_;
190             }
191 3         19 is(read_file($filename), $result, "function result_add file=$filename");
192 3         1211 $bot->result_settime($filename, $timenow);
193 3         62 ok((stat($filename))[9]==$timenow, "fucntion result_settime file=$filename");
194             }
195              
196             sub test_DB {
197 3     3 0 1065 my $filename=$bot->{file_DB};
198 3 50       631 unlink $filename if -f $filename;
199 3         25 $bot->db_init;
200 3         27 ok(read_file($filename)=~/use $classname/, "function db_init create file=$filename");
201 3         1153 $bot->db_clear;
202 3         55 ok(not(-f $filename), "function db_clear file=$filename");
203 3         1171 $bot->db_init;
204 3         47 $bot->db_add("Book", "OK", {other=>'hoho', url=>'http://test.com/test.html'});
205 3         23 ok(read_file($filename)=~/test\.com/, "function db_add file=$filename");
206 3         1093 $bot->db_load;
207 3         21 is($bot->{DB_visited_book}->{"http://test.com/test.html"}, "OK", "function db_load file=$filename");
208             }
209              
210             sub test_agent {
211 3     3 0 1029 $bot->{get_agent_proxy}="No;192.168.1.8:8888";
212 3         18 $bot->agent_init;
213 3   33     28 ok((defined($bot->{get_agent_array}->[0]) and not(defined($bot->{get_agent_array}->[0]->proxy('http')))),
214             "function agent_init/agent_setproxy: get_agent_proxy=No");
215 3   33     1110 ok((defined($bot->{get_agent_array}->[1])
216             and ($bot->{get_agent_array}->[1]->proxy('http') eq 'http://192.168.1.8:8888/')),
217             "function agent_init/agent_setproxy: get_agent_proxy=192.168.1.8:8888");
218 3         972 new_bot();
219             }
220              
221             sub test_url {
222 3     3 0 525 is($bot->url_rel2abs("index.htm", "http://w.c.c/s/m.txt"), "http://w.c.c/s/index.htm", "function url_rel2abs");
223             }
224              
225             sub test_fetch {
226 3         8 SKIP: {
227 3     3 0 2052 my ($url, $res);
228 3 50       35 skip "cannot visit online sites", 3 if not $TESTONLINE;
229 0         0 $url="http://www.cpan.org/";
230 0         0 $res=$bot->get_url($url);
231 0         0 print "\n";
232 0         0 ok($res->content=~/
233 0         0 $url="http://www.cpan.org/misc/gif/funet.gif";
234 0         0 $res=$bot->get_url($url);
235 0         0 print "\n";
236 0         0 ok($res->content=~/^GIF89/, "function get_url gif $url");
237 0         0 $url="http://www.cpan.org/unavailable-test.txt";
238 0         0 $res=$bot->get_url($url);
239 0         0 print "\n";
240 0         0 ok(not($res->is_success), "function get_url WRONG $url");
241 0         0 undef $res;
242             }
243             }
244              
245             sub test_parser {
246 21     21 0 8172 my ($func, $in, $out, $disp)=@_;
247 21         182 my $str=$bot->de_code($in);
248 21         211 $bot->$func($str);
249 21         79 my $result=$bot->en_code($str);
250 21         159 is($result, $out, "function $func $disp");
251             }
252              
253             sub test_parser_enin_deout {
254 9     9 0 145 my ($func, $pargs, $in, $out, $disp)=@_;
255 9         15 my $str=$in;
256 9         64 $bot->$func($pargs, $str);
257 9         41 my $result=$bot->en_code($str);
258 9         54 is($result, $out, "function $func $disp");
259             }
260             sub test_catalog_get_book {
261 3     3 0 1021 my ($str_nothing, $str_good, $url, $title)=@_;
262 3         13 my $pargs={url_base=>'http://www.sina.com.cn/index.htm'};
263 3         39 my @a1=$bot->catalog_get_book($pargs, $bot->de_code($str_nothing));
264 3         14 is(scalar(@a1), 0, "function catalog_get_book empty catalog");
265 3         1241 my @a2=$bot->catalog_get_book($pargs, $bot->de_code($str_good));
266 3         14 is(scalar(@a2), 1, "function catalog_get_book good catalog");
267 3         1105 is($a2[0]->{url}, $url, 'function catalog_get_book get url');
268 3         1083 is($a2[0]->{title}, $title, 'function catalog_get_book get title');
269             }
270             sub test_book_chapters {
271 3     3 0 1040 my ($str_nothing, $str_good, $url, $title)=@_;
272 3         15 my $pargs={url_base=>'http://www.sina.com.cn/index.htm'};
273 3         18 my @a1=$bot->book_chapters($pargs, $bot->de_code($str_nothing));
274 3         16 is(scalar(@a1), 0, "function book_chapters empty catalog");
275 3         984 my @a2=$bot->book_chapters($pargs, $bot->de_code($str_good));
276 3         15 is(scalar(@a2), 1, "function book_chapters good catalog");
277 3         1082 is($a2[0]->{url}, $url, 'function book_chapters get url');
278 3         1051 is($a2[0]->{title}, $title, 'function book_chapters get title');
279             }
280             sub test_writebin {
281 3     3 0 995 my ($str)=@_;
282 3         19 new_bot();
283 3         233 my $pargs={title=>"WBIN", ext_save=>"cov"};
284 3         155 my $filename_full=$bot->result_filename($pargs);
285 3         30 my $filename=$bot->book_writebin($pargs, $str);
286 3         38 is($filename, "0000WBIN.cov", "function book_writebin return correct name");
287 3         1656 is(read_file($filename_full), $str, "function book_writebin write file");
288             }
289              
290             sub test_parse_bintext {
291 3     3 0 1143 my ($str)=@_;
292 3         14 new_bot();
293 3         183 test_parser_enin_deout('book_bin',
294             {title=>"WBIN", ext_save=>"cov"},
295             $str, "[0000WBIN.cov]");
296 3         1713 test_parser_enin_deout('book_text',
297             {title=>"WTXT", ext_save=>"txt", level=>0},
298             $str, "[0001WTXT.txt]", "level=0");
299 3         1350 test_parser_enin_deout('book_text',
300             {title=>"WTXT", ext_save=>"txt", level=>1},
301             $str, $str, "level=1");
302             }
303              
304             1;
305             __END__