| 1 | package DateTime::Format::Natural::Test; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 22 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 22 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 22 | ▶ | use base qw(Exporter); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 22 | ▶ | use boolean qw(true false); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 22 | ▶ | use constant truncated => true; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 22 | ▶ | use constant unaltered => false; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 22 | ▶ | use File::Find; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 22 | ▶ | use File::Spec::Functions qw(abs2rel); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 22 | ▶ | use List::Util 1.33 qw(any); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 22 | ▶ | use Module::Util qw(fs_path_to_module); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 22 | ▶ | use Test::More; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | 22 | our ($VERSION, @EXPORT_OK, %EXPORT_TAGS, %time, $case_strings, $time_entries); | |||||||||||||
| 17 | 22 | my @set; | |||||||||||||
| 18 | |||||||||||||||
| 19 | 22 | $VERSION = '0.15'; | |||||||||||||
| 20 | |||||||||||||||
| 21 | 22 | @set = qw(truncated unaltered %time $case_strings | |||||||||||||
| 22 | $time_entries _run_tests _result_string | ||||||||||||||
| 23 | _result_string_hires _message); | ||||||||||||||
| 24 | |||||||||||||||
| 25 | 22 | @EXPORT_OK = (qw(_find_modules _find_files), @set); | |||||||||||||
| 26 | 22 | %EXPORT_TAGS = ('set' => [ @set ]); | |||||||||||||
| 27 | |||||||||||||||
| 28 | 154 | %time = map { split /:/ } | |||||||||||||
| 29 | split /\n/, | ||||||||||||||
| 30 | 22 | do { local $/ = '__END__'; | |||||||||||||
| 31 | 22 | local $_ = <DATA>; | |||||||||||||
| 32 | 22 | chomp; | |||||||||||||
| 33 | 22 | $_ }; | |||||||||||||
| 34 | |||||||||||||||
| 35 | 3438 | ▶ | $case_strings = sub { ($_[0], lc $_[0], uc $_[0]) }; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 36 | $time_entries = sub | ||||||||||||||
| 37 | 1228 | ▶ | { | ||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||
| 38 | 1228 | my ($string, $result) = @_; | |||||||||||||
| 39 | |||||||||||||||
| 40 | my $subst = sub | ||||||||||||||
| 41 | 1710 | ▶ | { | ||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
| |||||||||||||||
| 42 | 1710 | my ($str, $res, $entries) = @_; | |||||||||||||
| 43 | |||||||||||||||
| 44 | 1710 | ▶ | if ($str =~ /\{(?: |at)\}/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | 794 | my @strings; | |||||||||||||
| 46 | 794 | ▶ | if ($str =~ /\{ \}/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 47 | 616 | foreach my $space ('', ' ') { | |||||||||||||
| 48 | 1232 | (my $str_new = $str) =~ s/\{ \}/$space/; | |||||||||||||
| 49 | 1232 | push @strings, $str_new; | |||||||||||||
| 50 | } | ||||||||||||||
| 51 | } | ||||||||||||||
| 52 | 794 | ▶ | if ($str =~ /\{at\}/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 53 | 408 | ▶ | @strings = ($str) unless @strings; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 54 | 408 | my @strings_new; | |||||||||||||
| 55 | 408 | foreach my $string (@strings) { | |||||||||||||
| 56 | 638 | foreach my $at ('', ' at') { | |||||||||||||
| 57 | 1276 | (my $str_new = $string) =~ s/ \{at\}/$at/; | |||||||||||||
| 58 | 1276 | push @strings_new, $str_new; | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | } | ||||||||||||||
| 61 | 408 | @strings = @strings_new; | |||||||||||||
| 62 | } | ||||||||||||||
| 63 | 794 | push @$entries, [ $_, $res ] foreach @strings; | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | else { | ||||||||||||||
| 66 | 916 | push @$entries, [ $str, $res ]; | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | 1228 | }; | |||||||||||||
| 69 | |||||||||||||||
| 70 | 1228 | my @entries; | |||||||||||||
| 71 | 1228 | ▶ | if ($string =~ /\{(?:min_)?sec\}/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 72 | 330 | my ($desc, @values); | |||||||||||||
| 73 | 330 | my $sec = sprintf '%02d', int rand(60); | |||||||||||||
| 74 | 330 | ▶ | if ($string =~ /\{(min_sec)\}/) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 75 | 152 | @values = ( | |||||||||||||
| 76 | [ '', '00:00' ], # hour | ||||||||||||||
| 77 | [ ':00', '00:00' ], # minute | ||||||||||||||
| 78 | [ ":00:$sec", "00:$sec" ], # second | ||||||||||||||
| 79 | ); | ||||||||||||||
| 80 | 152 | $desc = $1; | |||||||||||||
| 81 | } | ||||||||||||||
| 82 | elsif ($string =~ /\{(sec)\}/) { | ||||||||||||||
| 83 | 178 | @values = ( | |||||||||||||
| 84 | [ '', '00' ], # minute | ||||||||||||||
| 85 | [ ":$sec", $sec ], # second | ||||||||||||||
| 86 | ); | ||||||||||||||
| 87 | 178 | $desc = $1; | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | 330 | my $is_aref = ref $result eq 'ARRAY'; | |||||||||||||
| 90 | 330 | foreach my $value (@values) { | |||||||||||||
| 91 | 812 | (my $str = $string) =~ s/\{$desc\}/$value->[0]/; | |||||||||||||
| 92 | 812 | ▶ | (my $res = $is_aref ? $result->[0] : $result) =~ s/\{$desc\}/$value->[1]/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | 812 | ▶ | $subst->($str, $is_aref ? [ $res, $result->[1] ] : $res, \@entries); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 94 | } | ||||||||||||||
| 95 | } | ||||||||||||||
| 96 | else { | ||||||||||||||
| 97 | 898 | $subst->($string, $result, \@entries); | |||||||||||||
| 98 | } | ||||||||||||||
| 99 | |||||||||||||||
| 100 | 1228 | return @entries; | |||||||||||||
| 101 | 22 | }; | |||||||||||||
| 102 | |||||||||||||||
| 103 | sub _run_tests | ||||||||||||||
| 104 | 17 | ▶ | { | ||||||||||||
|
Subroutine
_run_tests: called CC=4 SCAR=14.0
| |||||||||||||||
| 105 | 17 | my ($tests, $sets, $check) = @_; | |||||||||||||
| 106 | |||||||||||||||
| 107 | 17 | $tests *= 3; # case tests | |||||||||||||
| 108 | |||||||||||||||
| 109 | 17 | ▶ | if (!$DateTime::Format::Natural::Compat::Pure) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 110 | 17 | plan tests => $tests * 2; | |||||||||||||
| 111 | 17 | foreach my $set (@$sets) { | |||||||||||||
| 112 | 29 | $check->(@$set); | |||||||||||||
| 113 | } | ||||||||||||||
| 114 | } | ||||||||||||||
| 115 | else { | ||||||||||||||
| 116 | 0 | plan tests => $tests; | |||||||||||||
| 117 | } | ||||||||||||||
| 118 | |||||||||||||||
| 119 | 17 | $DateTime::Format::Natural::Compat::Pure = true; | |||||||||||||
| 120 | |||||||||||||||
| 121 | 17 | foreach my $set (@$sets) { | |||||||||||||
| 122 | 29 | $check->(@$set); | |||||||||||||
| 123 | } | ||||||||||||||
| 124 | } | ||||||||||||||
| 125 | |||||||||||||||
| 126 | my $result_string = sub | ||||||||||||||
| 127 | 11267 | ▶ | { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 128 | 11267 | my ($dt, $fmt, $units) = @_; | |||||||||||||
| 129 | 11267 | return sprintf($fmt, map $dt->$_, @$units); | |||||||||||||
| 130 | 22 | }; | |||||||||||||
| 131 | |||||||||||||||
| 132 | sub _result_string | ||||||||||||||
| 133 | 11069 | ▶ | { | ||||||||||||
|
Subroutine
_result_string: called CC=1 SCAR=0.0
| |||||||||||||||
| 134 | 11069 | return $result_string->(shift, | |||||||||||||
| 135 | '%02d.%02d.%4d %02d:%02d:%02d', | ||||||||||||||
| 136 | [qw(day month year hour min sec)]); | ||||||||||||||
| 137 | } | ||||||||||||||
| 138 | |||||||||||||||
| 139 | sub _result_string_hires | ||||||||||||||
| 140 | 198 | ▶ | { | ||||||||||||
|
Subroutine
_result_string_hires: called CC=1 SCAR=0.0
| |||||||||||||||
| 141 | 198 | return $result_string->(shift, | |||||||||||||
| 142 | '%02d.%02d.%4d %02d:%02d:%02d.%03d', | ||||||||||||||
| 143 | [qw(day month year hour min sec millisecond)]); | ||||||||||||||
| 144 | } | ||||||||||||||
| 145 | |||||||||||||||
| 146 | sub _message | ||||||||||||||
| 147 | 10248 | ▶ | { | ||||||||||||
|
Subroutine
_message: called CC=2 SCAR=6.9
| |||||||||||||||
| 148 | 10248 | my ($msg) = @_; | |||||||||||||
| 149 | |||||||||||||||
| 150 | 10248 | ▶ | my $how = $DateTime::Format::Natural::Compat::Pure | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 151 | ? '(using DateTime)' | ||||||||||||||
| 152 | : '(using Date::Calc)'; | ||||||||||||||
| 153 | |||||||||||||||
| 154 | 10248 | return "$msg $how"; | |||||||||||||
| 155 | } | ||||||||||||||
| 156 | |||||||||||||||
| 157 | sub _find_modules | ||||||||||||||
| 158 | 1 | ▶ | { | ||||||||||||
|
Subroutine
_find_modules: called CC=1 SCAR=0.0
| |||||||||||||||
| 159 | 1 | my ($lib, $modules, $exclude) = @_; | |||||||||||||
| 160 | 1 | _gather_data($lib, undef, $modules, $exclude); | |||||||||||||
| 161 | } | ||||||||||||||
| 162 | |||||||||||||||
| 163 | sub _find_files | ||||||||||||||
| 164 | 0 | ▶ | { | ||||||||||||
|
Subroutine
_find_files: not called CC=1 SCAR=6.9
| |||||||||||||||
| 165 | 0 | my ($lib, $files, $exclude) = @_; | |||||||||||||
| 166 | 0 | _gather_data($lib, $files, undef, $exclude); | |||||||||||||
| 167 | } | ||||||||||||||
| 168 | |||||||||||||||
| 169 | sub _gather_data | ||||||||||||||
| 170 | 1 | ▶ | { | ||||||||||||
|
Subroutine
_gather_data: called CC=1 SCAR=0.2
| |||||||||||||||
| 171 | 1 | my ($lib, $files, $modules, $exclude) = @_; | |||||||||||||
| 172 | |||||||||||||||
| 173 | 1 | my ($save_files, $save_modules) = map defined, ($files, $modules); | |||||||||||||
| 174 | 1 | my $ext = qr/\.pm$/; | |||||||||||||
| 175 | |||||||||||||||
| 176 | 22 | ▶ | find(sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=19.9
| |||||||||||||||
| 177 | 22 | ▶ | return unless $_ =~ $ext; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 178 | 16 | my $rel_path = abs2rel($File::Find::name, $lib); | |||||||||||||
| 179 | 16 | ▶ | my $module = fs_path_to_module($rel_path) or return; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 180 | 0 | ▶ | return if any { $module =~ /${_}$/ } @$exclude; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
| |||||||||||||||
| 181 | 16 | ▶ | if ($save_files) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 182 | 0 | push @$files, $File::Find::name; | |||||||||||||
| 183 | } | ||||||||||||||
| 184 | elsif ($save_modules) { | ||||||||||||||
| 185 | 16 | push @$modules, $module; | |||||||||||||
| 186 | } | ||||||||||||||
| 187 | 1 | }, $lib); | |||||||||||||
| 188 | } | ||||||||||||||
| 189 | |||||||||||||||
| 190 | 22 | 1; | |||||||||||||
| 191 | __DATA__ | ||||||||||||||