File Coverage

xt/Util.pm
Criterion Covered Total %
statement 12 86 13.9
branch 0 12 0.0
condition 0 6 0.0
subroutine 4 25 16.0
pod 0 11 0.0
total 16 140 11.4


line stmt bran cond sub pod time code
1             package xt::Util;
2 7     7   9079 use strict;
  7         14  
  7         316  
3 7     7   39 use warnings;
  7         15  
  7         229  
4 7     7   37 use Exporter qw/import/;
  7         14  
  7         225  
5 7     7   10849 use Time::Piece qw/gmtime/;
  7         244237  
  7         53  
6              
7             our %EXPORT_TAGS = (
8             cmp_deeply => [qw(
9             cmp_deeply
10             hash
11             array_of_hashes
12             uri
13             integer
14             real
15             datetime
16             game_result
17             user_name
18             user_rank
19             )],
20             );
21              
22             our @EXPORT_OK = ( 'build_gokgs', map { @$_ } values %EXPORT_TAGS );
23              
24             sub build_gokgs {
25 0     0 0   my $gokgs = do {
26 0 0         if ( $ENV{WWW_GOKGS_LIBXML} ) {
27 0           require WWW::GoKGS::LibXML;
28 0           WWW::GoKGS::LibXML->new( from => 'anazawa@cpan.org' );
29             }
30             else {
31 0           require WWW::GoKGS;
32 0           WWW::GoKGS->new( from => 'anazawa@cpan.org' );
33             }
34             };
35              
36 0           $gokgs->user_agent->delay( 1/60 );
37              
38 0           $gokgs;
39             }
40              
41             sub cmp_deeply {
42 0     0 0   my ( $got, $expected, $name ) = @_;
43              
44             Test::More::subtest(
45             $name || 'unknown',
46 0     0     sub { $expected->( $got ) }
47 0   0       );
48              
49 0           return;
50             }
51              
52             sub hash {
53 0     0 0   my %expected = @_;
54              
55             sub {
56 0     0     my $got = shift;
57 0   0       my $name = shift || '$hash';
58              
59 0           Test::More::isa_ok( $got, 'HASH', $name );
60              
61 0           for my $key ( keys %$got ) {
62 0           my $value = $got->{$key};
63 0           my $n = "$name\->{$key}";
64              
65 0 0         if ( ref $expected{$key} eq 'CODE' ) {
    0          
66 0           local $_ = $value;
67 0           my $bool = $expected{$key}->( $value, $n );
68 0 0         Test::More::ok( $bool, $n ) if defined $bool;
69             }
70             elsif ( ref $expected{$key} eq 'ARRAY' ) {
71 0           for my $e ( @{$expected{$key}} ) {
  0            
72 0           local $_ = $value;
73 0           my $bool = $e->( $value, $n );
74 0 0         Test::More::ok( $bool, $n ) if defined $bool;
75             }
76             }
77             }
78              
79 0           return;
80 0           };
81             }
82              
83             sub array_of_hashes {
84 0     0 0   my $expected = hash( @_ );
85              
86             sub {
87 0     0     my $got = shift;
88 0   0       my $name = shift || '$array';
89              
90 0           Test::More::isa_ok( $got, 'ARRAY', $name );
91              
92 0           my $i = 0;
93 0           for my $g ( @$got ) {
94 0           local $_ = $g;
95 0           my $n = "$name\->[$i]";
96 0           my $bool = $expected->( $g, $n );
97 0 0         Test::More::ok( $bool, $n ) if defined $bool;
98 0           $i++;
99             }
100              
101 0           return;
102 0           };
103             }
104              
105             sub uri {
106             sub {
107 0     0     my ( $got, $name ) = @_;
108 0           Test::More::isa_ok( $got, 'URI', $name );
109 0           return;
110 0     0 0   };
111             }
112              
113             sub integer {
114             sub {
115 0     0     my ( $got, $name ) = @_;
116              
117 0           Test::More::like(
118             $got,
119             qr{^(?:0|\-?[1-9][0-9]*)$},
120             "'$name' should be integer"
121             );
122              
123 0           return;
124 0     0 0   };
125             }
126              
127             sub real {
128             sub {
129 0     0     my ( $got, $name ) = @_;
130              
131 0           Test::More::like(
132             $got,
133             qr{^(?:0|\-?[1-9][0-9]*(?:\.[0-9]*[1-9])?)$},
134             "'$name' should be real"
135             );
136              
137 0           return;
138 0     0 0   };
139             }
140              
141             sub datetime {
142 0     0 0   my $format = shift;
143              
144             sub {
145 0     0     my ( $got, $name ) = @_;
146 0           eval { gmtime->strptime( $got, $format ) };
  0            
147 0           Test::More::ok( !$@, "'$name' ($got) should be '$format': $@" );
148 0           return;
149 0           };
150             }
151              
152             sub game_result {
153             sub {
154 0     0     my ( $got, $name ) = @_;
155              
156 0           Test::More::like(
157             $got,
158             qr{^(?:
159             Unfinished
160             | Draw
161             | (?:B|W)\+(?:
162             Resign
163             | Forfeit
164             | Time
165             | (?:0|[1-9][0-9]*)(?:\.5)? )
166             )$}x,
167             $name
168             );
169              
170 0           return;
171 0     0 0   };
172             }
173              
174             sub user_name {
175             sub {
176 0     0     my ( $got, $name ) = @_;
177 0           Test::More::like( $got, qr/^[a-zA-Z][a-zA-Z0-9]{0,9}$/, $name );
178 0           return;
179 0     0 0   };
180             }
181              
182             sub user_rank {
183             sub {
184 0     0     my ( $got, $name ) = @_;
185              
186 0           Test::More::like(
187             $got,
188             qr{^(?:
189             \-
190             | \?
191             | [1-9](?:p|d\??|k\??)
192             | [12][0-9]k\??
193             | 30k\??
194             )$}x,
195             $name
196             );
197              
198 0           return;
199 0     0 0   };
200             }
201              
202             1;