File Coverage

blib/lib/DateTime/Format/Natural/Test.pm
Criterion Covered Total %
statement 60 65 92.3
branch 9 14 64.2
condition n/a
subroutine 18 19 94.7
pod n/a
total 87 98 88.7


line stmt bran cond sub pod time code
1             package DateTime::Format::Natural::Test;
2              
3 21     21   10706 use strict;
  21         65  
  21         611  
4 21     21   121 use warnings;
  21         48  
  21         588  
5 21     21   113 use base qw(Exporter);
  21         52  
  21         2199  
6 21     21   1493 use boolean qw(true false);
  21         10384  
  21         154  
7 21     21   1443 use constant truncated => true;
  21         71  
  21         81  
8 21     21   1579 use constant unaltered => false;
  21         65  
  21         82  
9              
10 21     21   1145 use File::Find;
  21         55  
  21         1765  
11 21     21   10327 use File::Spec::Functions qw(abs2rel);
  21         19143  
  21         1413  
12 21     21   2133 use List::MoreUtils qw(any);
  21         44869  
  21         158  
13 21     21   25444 use Module::Util qw(fs_path_to_module);
  21         35514  
  21         1428  
14 21     21   10543 use Test::More;
  21         1483983  
  21         248  
15              
16             our ($VERSION, @EXPORT_OK, %EXPORT_TAGS, %time, $case_strings, $time_entries);
17             my @set;
18              
19             $VERSION = '0.13';
20              
21             @set = qw(truncated unaltered %time $case_strings
22             $time_entries _run_tests _result_string
23             _result_string_hires _message);
24              
25             @EXPORT_OK = (qw(_find_modules _find_files), @set);
26             %EXPORT_TAGS = ('set' => [ @set ]);
27              
28             %time = map { split /:/ }
29             split /\n/,
30             do { local $/ = '__END__';
31             local $_ = <DATA>;
32             chomp;
33             $_ };
34              
35             $case_strings = sub { ($_[0], lc $_[0], uc $_[0]) };
36             $time_entries = sub
37             {
38             my ($string, $result) = @_;
39              
40             my $subst = sub
41             {
42             my ($str, $res, $entries) = @_;
43              
44             if ($str =~ /\{(?: |at)\}/) {
45             my @strings;
46             if ($str =~ /\{ \}/) {
47             foreach my $space ('', ' ') {
48             (my $str_new = $str) =~ s/\{ \}/$space/;
49             push @strings, $str_new;
50             }
51             }
52             if ($str =~ /\{at\}/) {
53             @strings = ($str) unless @strings;
54             my @strings_new;
55             foreach my $string (@strings) {
56             foreach my $at ('', ' at') {
57             (my $str_new = $string) =~ s/ \{at\}/$at/;
58             push @strings_new, $str_new;
59             }
60             }
61             @strings = @strings_new;
62             }
63             push @$entries, [ $_, $res ] foreach @strings;
64             }
65             else {
66             push @$entries, [ $str, $res ];
67             }
68             };
69              
70             my @entries;
71             if ($string =~ /\{(?:min_)?sec\}/) {
72             my ($desc, @values);
73             my $sec = sprintf '%02d', int rand(60);
74             local $1;
75             if ($string =~ /\{(min_sec)\}/) {
76             @values = (
77             [ '', '00:00' ], # hour
78             [ ':00', '00:00' ], # minute
79             [ ":00:$sec", "00:$sec" ], # second
80             );
81             $desc = $1;
82             }
83             elsif ($string =~ /\{(sec)\}/) {
84             @values = (
85             [ '', '00' ], # minute
86             [ ":$sec", $sec ], # second
87             );
88             $desc = $1;
89             }
90             my $is_aref = ref $result eq 'ARRAY';
91             foreach my $value (@values) {
92             (my $str = $string) =~ s/\{$desc\}/$value->[0]/;
93             (my $res = $is_aref ? $result->[0] : $result) =~ s/\{$desc\}/$value->[1]/;
94             $subst->($str, $is_aref ? [ $res, $result->[1] ] : $res, \@entries);
95             }
96             }
97             else {
98             $subst->($string, $result, \@entries);
99             }
100              
101             return @entries;
102             };
103              
104             sub _run_tests
105             {
106 16     16   53327 my ($tests, $sets, $check) = @_;
107              
108 16         54 $tests *= 3; # case tests
109              
110 16         41 local $@;
111              
112 16 50       1010 if (eval "require Date::Calc") {
113 16         126 plan tests => $tests * 2;
114 16         18653 foreach my $set (@$sets) {
115 27         242 $check->(@$set);
116             }
117             }
118             else {
119 0         0 plan tests => $tests;
120             }
121              
122 16         284 $DateTime::Format::Natural::Compat::Pure = true;
123              
124 16         111 foreach my $set (@$sets) {
125 27         237 $check->(@$set);
126             }
127             }
128              
129             my $result_string = sub
130             {
131             my ($dt, $fmt, $units) = @_;
132             return sprintf($fmt, map $dt->$_, @$units);
133             };
134              
135             sub _result_string
136             {
137 10767     10767   159368 return $result_string->(shift,
138             '%02d.%02d.%4d %02d:%02d:%02d',
139             [qw(day month year hour min sec)]);
140             }
141              
142             sub _result_string_hires
143             {
144 198     198   4749 return $result_string->(shift,
145             '%02d.%02d.%4d %02d:%02d:%02d.%03d',
146             [qw(day month year hour min sec millisecond)]);
147             }
148              
149             sub _message
150             {
151 9966     9966   276981 my ($msg) = @_;
152              
153 9966 100       24510 my $how = $DateTime::Format::Natural::Compat::Pure
154             ? '(using DateTime)'
155             : '(using Date::Calc)';
156              
157 9966         101178 return "$msg $how";
158             }
159              
160             sub _find_modules
161             {
162 1     1   2352 my ($lib, $modules, $exclude) = @_;
163 1         4 _gather_data($lib, undef, $modules, $exclude);
164             }
165              
166             sub _find_files
167             {
168 0     0   0 my ($lib, $files, $exclude) = @_;
169 0         0 _gather_data($lib, $files, undef, $exclude);
170             }
171              
172             sub _gather_data
173             {
174 1     1   2 my ($lib, $files, $modules, $exclude) = @_;
175              
176 1         5 my ($save_files, $save_modules) = map defined, ($files, $modules);
177 1         8 my $ext = qr/\.pm$/;
178              
179             find(sub {
180 21 100   21   700 return unless $_ =~ $ext;
181 15         52 my $rel_path = abs2rel($File::Find::name, $lib);
182 15 50       843 my $module = fs_path_to_module($rel_path) or return;
183 15 50       798 return if any { $module =~ /${_}$/ } @$exclude;
  0         0  
184 15 50       62 if ($save_files) {
    50          
185 0         0 push @$files, $File::Find::name;
186             }
187             elsif ($save_modules) {
188 15         356 push @$modules, $module;
189             }
190 1         139 }, $lib);
191             }
192              
193             1;
194             __DATA__
195             year:2006
196             month:11
197             day:24
198             hour:1
199             minute:13
200             second:8
201             nanosecond:0
202              
203             __END__
204              
205             =head1 NAME
206              
207             DateTime::Format::Natural::Test - Common test routines/data
208              
209             =head1 SYNOPSIS
210              
211             Please see the DateTime::Format::Natural documentation.
212              
213             =head1 DESCRIPTION
214              
215             The C<DateTime::Format::Natural::Test> class exports common test routines.
216              
217             =head1 SEE ALSO
218              
219             L<DateTime::Format::Natural>
220              
221             =head1 AUTHOR
222              
223             Steven Schubiger <schubiger@cpan.org>
224              
225             =head1 LICENSE
226              
227             This program is free software; you may redistribute it and/or
228             modify it under the same terms as Perl itself.
229              
230             See L<http://dev.perl.org/licenses/>
231              
232             =cut