BookBot/Test.pm

filter: Statementstmt 83.8% 161 / 192 Branchbran 40.6% 13 / 32 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 76.9% 30 / 39 Podpod 0.0% 0 / 30 totaltotal 67.9% 206 / 303 CC 22 SCAR 12.7
CC
22
Cov
77%
match_print
20.0
string_limitlen
6.0
read_file
4.2
CRAP 3.6
« BookBot/FakeCookies.pm ↑ Summary BookBot/Chinese/Agriculture/Cast.pm »
1 package WWW::BookBot::Test;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶no warnings qw(uninitialized utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use vars qw($VERSION @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 $VERSION = "0.12";
8 3 @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 3 @EXPORT_OK = @EXPORT;
20  
21 3 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 #------ contants and vars
27 3 our $CLEARRESULT=1; #clear reseult dir before quit
28 3 our $TESTONLINE=0; #test online or not
29 3 our $classname='WWW::BookBot'; #class to be tested
30 3 our $testdir; #the dir to put logs in
31 3 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 21 ▶sub new_bot(@) {
Subroutine new_bot: called CC=1 SCAR=0.0
Pod undocumented
48 21 $bot=$classname->new({work_dir=>$testdir, @_});
49 }
50 24 ▶sub read_file($) {
Subroutine read_file: called CC=4 SCAR=14.2
Pod undocumented
51 24 my $filename = shift;
52 24 my $str="";
53 24 local(*WORK);
54 24 ▶ open(WORK, $filename) or return "";
Branch: 1 branch
BranchTrueFalseTotal
open WORK, $filename or return "" 24 0 24
55 24 ▶ sysread(WORK, $str, 6000000) or return "";
Branch: 1 branch
BranchTrueFalseTotal
sysread WORK, $str, 6000000 or return "" 24 0 24
56 24 ▶ close(WORK) or return "";
Branch: 1 branch
BranchTrueFalseTotal
close WORK or return "" 24 0 24
57 24 $str=~s/\r\n|\r/\n/; #to ensure tests work on both unix and win32
58 24 return $str;
59 }
60 0 ▶sub dump_class() {
Subroutine dump_class: not called CC=1 SCAR=6.9
Pod undocumented
61 0 $bot->dump_class;
62 }
63 0 ▶sub dump_var($) {
Subroutine dump_var: not called CC=1 SCAR=6.9
Pod undocumented
64 0 $bot->dump_var($_[0]);
65 }
66 0 ▶sub en_code($) {
Subroutine en_code: not called CC=1 SCAR=6.9
Pod undocumented
67 0 $bot->en_code($_[0]);
68 }
69 0 ▶sub de_code($) {
Subroutine de_code: not called CC=1 SCAR=6.9
Pod undocumented
70 0 $bot->de_code($_[0]);
71 }
72 0 ▶sub parse_patterns($) {
Subroutine parse_patterns: not called CC=1 SCAR=6.9
Pod undocumented
73 0 $bot->parse_patterns($_[0]);
74 }
75 0 ▶sub get_pattern($) {
Subroutine get_pattern: not called CC=1 SCAR=6.9
Pod undocumented
76 0 $bot->{patterns}->{$_[0]};
77 }
78 0 ▶sub test_pattern($$) {
Subroutine test_pattern: not called CC=1 SCAR=6.9
Pod undocumented
79 0 $bot->test_pattern(@_);
80 }
81 0 ▶sub string_limitlen($$) {
Subroutine string_limitlen: not called CC=2 SCAR=17.9
Pod undocumented
82 0 ▶ length($_[1])<=($_[0]+8) ?
Branch: 1 branch
BranchTrueFalseTotal
length $_[1] <= $_[0] + 8 ? : 0 0 0
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 0 ▶sub match_print($) {
Subroutine match_print: not called CC=4 SCAR=30.0
Pod undocumented
87 0 my ($result)=@_;
88 0 ▶ if( $result ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 0 0 0
89 0 print "Match ";
90 0 my $i=0;
91 0 foreach ($1, $2, $3, $4, $5, $6, $7, $8, $9) {
92 0 $i++;
93 0 ▶ next if $_ eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "" 0 0 0
94 0 printf " [%d]=\'%s\'\n", $i, string_limitlen(300,$_);
95 }
96 0 print "\n";
97 } else {
98 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 3 ▶sub test_init($) {
Subroutine test_init: called CC=1 SCAR=0.0
Pod undocumented
109 3 $classname=$_[0];
110 }
111 3 ▶sub test_begin() {
Subroutine test_begin: called CC=3 SCAR=11.2
Pod undocumented
112 3 ▶ chdir 't' if -d 't';
Branch: 1 branch
BranchTrueFalseTotal
if -d "t" 3 0 3
113 3 ▶ $testdir=($classname=~/::([^:]*)$/) ? $1 : $classname;
Branch: 1 branch
BranchTrueFalseTotal
$WWW::BookBot::Test::classname =~ /::([^:]*)$/ ? : 3 0 3
114 3 rmtree($testdir);
115 3 new_bot();
116 3 ok(defined($bot), "new $classname");
117 3 ok(-d $testdir, "create directory $testdir");
118 }
119 3 ▶sub test_end() {
Subroutine test_end: called CC=2 SCAR=7.1
Pod undocumented
120 3 ▶ rmtree($testdir) if $CLEARRESULT;
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::BookBot::Test::CLEARRESULT 3 0 3
121 3 chdir 't';
122 3 chdir '..';
123 }
124  
125 #-------------------------------------------------------------
126 # Tests
127 #-------------------------------------------------------------
128 6 ▶sub test_encoding {
Subroutine test_encoding: called CC=1 SCAR=0.0
Pod undocumented
129 6 my ($in, $out, $disp)=@_;
130 6 print "Content='$in'\n";
131 6 my $decode=$bot->de_code($in);
132 6 print " [decode as $bot->{LANGUAGE_ENCODE}] $decode\n";
133 6 my $encode=$bot->en_code($decode);
134 6 print " [encode as $bot->{LANGUAGE_ENCODE}] $encode\n";
135 6 is($encode, $out, "function en_code and de_code $disp");
136 }
137  
138 15 ▶sub test_parse_patterns {
Subroutine test_parse_patterns: called CC=1 SCAR=0.0
Pod undocumented
139 15 my ($in, $out, $disp)=@_;
140 15 is($bot->en_code($bot->parse_patterns($in)), $out, "function parse_patterns $disp");
141 }
142  
143 3 ▶sub test_msg_format {
Subroutine test_msg_format: called CC=1 SCAR=0.0
Pod undocumented
144 3 my ($pargs, $out, $disp)=@_;
145 3 is($bot->msg_format("TestMsg",$pargs), $out, "function msg_format $disp");
146 }
147  
148 3 ▶sub test_file {
Subroutine test_file: called CC=3 SCAR=11.0
Pod undocumented
149 3 my $filename=catfile($testdir, "test_file.txt");
150 3 ▶ unlink $filename if -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $filename 0 3 3
151 3 $bot->file_init("DB", $filename);
152 3 ok(-f $filename, "function file_init");
153 3 my $result="";
154 3 foreach (@_) {
155 6 $bot->file_add("DB", $filename, $_);
156 6 $result.=$_;
157 }
158 3 is(read_file($filename), $result, "function file_add file=$filename");
159 }
160  
161 3 ▶sub test_log {
Subroutine test_log: called CC=4 SCAR=14.0
Pod undocumented
162 3 my $filename=$bot->{file_log};
163 3 ▶ unlink $filename if -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $filename 0 3 3
164 3 $bot->log_msg($_[0]);
165 3 $bot->log_msg("");
166 3 $bot->log_msg($_[0], $_[1], "\n");
167 3 is(read_file($filename), "$_[0]$_[0]$_[1]\n", "function log_msg file=$filename");
168 3 ▶ unlink $filename if -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $filename 3 0 3
169 3 my $str=$_[2];
170 3 $bot->log_msgen($bot->de_code($str));
171 3 print "\n";
172 3 is(read_file($filename), $str, "function log_msgen file=$filename");
173 3 ▶ unlink $filename if -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $filename 3 0 3
174 3 $bot->log_add("TestMsg", $_[3]);
175 3 print "\n";
176 3 is(read_file($filename), $_[4], "function logadd file=$filename");
177 }
178  
179 3 ▶sub test_result {
Subroutine test_result: called CC=3 SCAR=11.0
Pod undocumented
180 3 my $timenow=time;
181 3 sleep 1;
182 3 my $filename=$bot->result_filename({title=>'Result*Test'}); #bad name
183 3 ▶ unlink $filename if -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $filename 0 3 3
184 3 $bot->result_init({title=>'ResultTest'});
185 3 ok(-f $filename, "function result_init file=$filename");
186 3 my $result="";
187 3 foreach (@_) {
188 6 $bot->result_add($filename, $_);
189 6 $result.=$_;
190 }
191 3 is(read_file($filename), $result, "function result_add file=$filename");
192 3 $bot->result_settime($filename, $timenow);
193 3 ok((stat($filename))[9]==$timenow, "fucntion result_settime file=$filename");
194 }
195  
196 3 ▶sub test_DB {
Subroutine test_DB: called CC=2 SCAR=6.9
Pod undocumented
197 3 my $filename=$bot->{file_DB};
198 3 ▶ unlink $filename if -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $filename 3 0 3
199 3 $bot->db_init;
200 3 ok(read_file($filename)=~/use $classname/, "function db_init create file=$filename");
201 3 $bot->db_clear;
202 3 ok(not(-f $filename), "function db_clear file=$filename");
203 3 $bot->db_init;
204 3 $bot->db_add("Book", "OK", {other=>'hoho', url=>'http://test.com/test.html'});
205 3 ok(read_file($filename)=~/test\.com/, "function db_add file=$filename");
206 3 $bot->db_load;
207 3 is($bot->{DB_visited_book}->{"http://test.com/test.html"}, "OK", "function db_load file=$filename");
208 }
209  
210 3 ▶sub test_agent {
Subroutine test_agent: called CC=3 SCAR=12.3
Pod undocumented
211 3 $bot->{get_agent_proxy}="No;192.168.1.8:8888";
212 3 $bot->agent_init;
213 3 ▶ ok((defined($bot->{get_agent_array}->[0]) and not(defined($bot->{get_agent_array}->[0]->proxy('http')))),
Condition33%1/3
defined $WWW::BookBot::Test::bot->{'get_agent_array'}[0] && !defined($WWW::BookBot::Test::bot->{'get_agent_array'}[0]->proxy('http'))
!ll&&!rl&&r
003
MC/DC0%0/2
defined $WWW::BookBot::Test::bot->{'get_agent_array'}[0] && !defined($WWW::BookBot::Test::bot->{'get_agent_array'}[0]->proxy('http'))
defined $WWW::BookBot::Test::bot->{'get_agent_array'}[0] !defined($WWW::BookBot::Test::bot->{'get_agent_array'}[0]->proxy('http'))
Truth table33%1/3
A && not(B)
A= defined $WWW::BookBot::Test::bot->{'get_agent_array'}[0]B= !defined($WWW::BookBot::Test::bot->{'get_agent_array'}[0]->proxy('http'))
ABresult
0X0
100
111
214 "function agent_init/agent_setproxy: get_agent_proxy=No");
215 ok((defined($bot->{get_agent_array}->[1])
216 3 ▶ and ($bot->{get_agent_array}->[1]->proxy('http') eq 'http://192.168.1.8:8888/')),
Condition33%1/3
defined $WWW::BookBot::Test::bot->{'get_agent_array'}[1] && $WWW::BookBot::Test::bot->{'get_agent_array'}[1]->proxy('http') eq 'http://192.168.1.8:8888/'
!ll&&!rl&&r
003
MC/DC0%0/2
defined $WWW::BookBot::Test::bot->{'get_agent_array'}[1] && $WWW::BookBot::Test::bot->{'get_agent_array'}[1]->proxy('http') eq 'http://192.168.1.8:8888/'
defined $WWW::BookBot::Test::bot->{'get_agent_array'}[1] $WWW::BookBot::Test::bot->{'get_agent_array'}[1]->proxy('http') eq 'http://192.168.1.8:8888/'
Truth table33%1/3
A && B
A= defined $WWW::BookBot::Test::bot->{'get_agent_array'}[1]B= $WWW::BookBot::Test::bot->{'get_agent_array'}[1]->proxy('http') eq 'http://192.168.1.8:8888/'
ABresult
0X0
100
111
217 "function agent_init/agent_setproxy: get_agent_proxy=192.168.1.8:8888");
218 3 new_bot();
219 }
220  
221 3 ▶sub test_url {
Subroutine test_url: called CC=1 SCAR=0.0
Pod undocumented
222 3 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 3 ▶sub test_fetch {
Subroutine test_fetch: called CC=2 SCAR=13.6
Pod undocumented
226 SKIP: {
227 3 my ($url, $res);
228 3 ▶ skip "cannot visit online sites", 3 if not $TESTONLINE;
Branch: 1 branch
BranchTrueFalseTotal
unless $WWW::BookBot::Test::TESTONLINE 3 0 3
229 0 $url="http://www.cpan.org/";
230 0 $res=$bot->get_url($url);
231 0 print "\n";
232 0 ok($res->content=~/<html/, "function get_url text $url");
233 0 $url="http://www.cpan.org/misc/gif/funet.gif";
234 0 $res=$bot->get_url($url);
235 0 print "\n";
236 0 ok($res->content=~/^GIF89/, "function get_url gif $url");
237 0 $url="http://www.cpan.org/unavailable-test.txt";
238 0 $res=$bot->get_url($url);
239 0 print "\n";
240 0 ok(not($res->is_success), "function get_url WRONG $url");
241 0 undef $res;
242 }
243 }
244  
245 21 ▶sub test_parser {
Subroutine test_parser: called CC=1 SCAR=0.0
Pod undocumented
246 21 my ($func, $in, $out, $disp)=@_;
247 21 my $str=$bot->de_code($in);
248 21 $bot->$func($str);
249 21 my $result=$bot->en_code($str);
250 21 is($result, $out, "function $func $disp");
251 }
252  
253 9 ▶sub test_parser_enin_deout {
Subroutine test_parser_enin_deout: called CC=1 SCAR=0.0
Pod undocumented
254 9 my ($func, $pargs, $in, $out, $disp)=@_;
255 9 my $str=$in;
256 9 $bot->$func($pargs, $str);
257 9 my $result=$bot->en_code($str);
258 9 is($result, $out, "function $func $disp");
259 }
260 3 ▶sub test_catalog_get_book {
Subroutine test_catalog_get_book: called CC=1 SCAR=0.0
Pod undocumented
261 3 my ($str_nothing, $str_good, $url, $title)=@_;
262 3 my $pargs={url_base=>'http://www.sina.com.cn/index.htm'};
263 3 my @a1=$bot->catalog_get_book($pargs, $bot->de_code($str_nothing));
264 3 is(scalar(@a1), 0, "function catalog_get_book empty catalog");
265 3 my @a2=$bot->catalog_get_book($pargs, $bot->de_code($str_good));
266 3 is(scalar(@a2), 1, "function catalog_get_book good catalog");
267 3 is($a2[0]->{url}, $url, 'function catalog_get_book get url');
268 3 is($a2[0]->{title}, $title, 'function catalog_get_book get title');
269 }
270 3 ▶sub test_book_chapters {
Subroutine test_book_chapters: called CC=1 SCAR=0.0
Pod undocumented
271 3 my ($str_nothing, $str_good, $url, $title)=@_;
272 3 my $pargs={url_base=>'http://www.sina.com.cn/index.htm'};
273 3 my @a1=$bot->book_chapters($pargs, $bot->de_code($str_nothing));
274 3 is(scalar(@a1), 0, "function book_chapters empty catalog");
275 3 my @a2=$bot->book_chapters($pargs, $bot->de_code($str_good));
276 3 is(scalar(@a2), 1, "function book_chapters good catalog");
277 3 is($a2[0]->{url}, $url, 'function book_chapters get url');
278 3 is($a2[0]->{title}, $title, 'function book_chapters get title');
279 }
280 3 ▶sub test_writebin {
Subroutine test_writebin: called CC=1 SCAR=0.0
Pod undocumented
281 3 my ($str)=@_;
282 3 new_bot();
283 3 my $pargs={title=>"WBIN", ext_save=>"cov"};
284 3 my $filename_full=$bot->result_filename($pargs);
285 3 my $filename=$bot->book_writebin($pargs, $str);
286 3 is($filename, "0000WBIN.cov", "function book_writebin return correct name");
287 3 is(read_file($filename_full), $str, "function book_writebin write file");
288 }
289  
290 3 ▶sub test_parse_bintext {
Subroutine test_parse_bintext: called CC=1 SCAR=0.0
Pod undocumented
291 3 my ($str)=@_;
292 3 new_bot();
293 3 test_parser_enin_deout('book_bin',
294 {title=>"WBIN", ext_save=>"cov"},
295 $str, "[0000WBIN.cov]");
296 3 test_parser_enin_deout('book_text',
297 {title=>"WTXT", ext_save=>"txt", level=>0},
298 $str, "[0001WTXT.txt]", "level=0");
299 3 test_parser_enin_deout('book_text',
300 {title=>"WTXT", ext_save=>"txt", level=>1},
301 $str, $str, "level=1");
302 }
303  
304 3 1;
305 __END__
« BookBot/FakeCookies.pm ↑ Summary BookBot/Chinese/Agriculture/Cast.pm »