blib/lib/CLDR/Number/Role/Base.pm

filter: Statementstmt 99.1% 119 / 120 Branchbran 84.7% 39 / 46 Conditioncond 85.7% 18 / 21 MC/DCmcdc 76.9% 10 / 13 Subroutinesub 100% 23 / 23 Podpod n/a totaltotal 93.7% 209 / 223 CC 34 SCAR 12.9
CC
34
Cov
94%
_build_inheritance
7.0
_trigger_locale
6.0
_get_data
5.0
CRAP 3.6
« Build ↑ Summary blib/lib/CLDR/Number/Format/Percent.pm »
1 package CLDR::Number::Role::Base;
2  
3 18 ▶use v5.8.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use Scalar::Util qw( looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use CLDR::Number::Data::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use CLDR::Number::Data::System;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 18 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # This role does not have a publicly supported interface and may change in
13 # backward incompatible ways in the future. Please use one of the documented
14 # classes instead.
15  
16 18 our $VERSION = '0.17';
17  
18 18 requires qw( BUILD );
19  
20 18 has version => (
21 is => 'ro',
22 default => $VERSION,
23 );
24  
25 18 has cldr_version => (
26 is => 'ro',
27 default => $CLDR::Number::Data::Base::CLDR_VERSION,
28 );
29  
30 18 has locale => (
31 is => 'rw',
32 trigger => 1,
33 );
34  
35 has default_locale => (
36 is => 'ro',
37 coerce => sub {
38 2 ▶ my ($locale) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.2
39  
40 2 ▶ if (!defined $locale) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $locale) { } 0 2 2
elsif (not exists $CLDR::Number::Data::Base::DATA->{$locale}) { } 1 1 2
41 0 carp 'default_locale is not defined';
42 }
43 elsif (!exists $CLDR::Number::Data::Base::DATA->{$locale}) {
44 1 carp "default_locale '$locale' is unknown";
45 }
46 else {
47 1 return $locale;
48 }
49  
50 1 return;
51 },
52 18 );
53  
54 has numbering_system => (
55 is => 'rw',
56 isa => sub {
57 9 ▶ carp 'numbering_system is not defined'
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 9 9
58 unless defined $_[0];
59 carp "numbering_system '$_[0]' is unknown"
60 9 ▶ unless exists $CLDR::Number::Data::System::DATA->{$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$CLDR::Number::Data::System::DATA{$_[0]} 1 8 9
61 },
62 9 ▶ coerce => sub { defined $_[0] ? lc $_[0] : $_[0] },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 9 0 9
63 18 trigger => 1,
64 );
65  
66 has minimum_grouping_digits => (
67 is => 'rw',
68 isa => sub {
69 199 ▶ croak "minimum_grouping_digits '$_[0]' is invalid"
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and not &looks_like_number($_[0]) 0 199 199
Condition33%1/3
defined $_[0] and not &looks_like_number($_[0])
!ll&&!rl&&r
01990
MC/DC0%0/2
defined $_[0] and not &looks_like_number($_[0])
defined $_[0] not &looks_like_number($_[0])
Truth table33%1/3
A and not(B)
A= defined $_[0]B= not &looks_like_number($_[0])
ABresult
0X0
100
111
70 if defined $_[0] && !looks_like_number $_[0];
71 },
72 18 );
73  
74 # TODO: length NYI
75 18 has length => (
76 is => 'rw',
77 );
78  
79 18 has decimal_sign => (
80 is => 'rw',
81 );
82  
83 18 has group_sign => (
84 is => 'rw',
85 );
86  
87 18 has plus_sign => (
88 is => 'rw',
89 );
90  
91 18 has minus_sign => (
92 is => 'rw',
93 );
94  
95 18 has infinity => (
96 is => 'rw',
97 );
98  
99 18 has nan => (
100 is => 'rw',
101 );
102  
103 has _locale_inheritance => (
104 is => 'rw',
105 145 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
106 18 );
107  
108 18 has _init_args => (
109 is => 'rw',
110 );
111  
112 around BUILDARGS => sub {
113 145 ▶ my ($orig, $class, @args) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.1
114  
115 145 ▶ return $class->$orig(@args) if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 0 145 145
116 145 return $class->$orig(@args, _init_args => {@args});
117 18 };
118  
119 before BUILD => sub {
120 145 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
121  
122 145 ▶ return if $self->_has_init_arg('locale');
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_init_arg("locale") 129 16 145
123  
124 16 $self->_trigger_locale;
125 18 };
126  
127 after BUILD => sub {
128 145 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
129  
130 145 $self->_init_args({});
131 18 };
132  
133 sub _has_init_arg {
134 3419 ▶ my ($self, $arg) = @_;
Subroutine _has_init_arg: called CC=2 SCAR=7.1
135  
136 3419 ▶ return unless $self->_init_args;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_init_args 0 3419 3419
137 3419 return exists $self->_init_args->{$arg};
138 }
139  
140 sub _set_unless_init_arg {
141 1582 ▶ my ($self, $attribute, $value) = @_;
Subroutine _set_unless_init_arg: called CC=2 SCAR=6.9
142  
143 1582 ▶ return if $self->_has_init_arg($attribute);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_init_arg($attribute) 16 1566 1582
144  
145 1566 $self->$attribute($value);
146 }
147  
148 sub _build_signs {
149 208 ▶ my ($self, @signs) = @_;
Subroutine _build_signs: called CC=3 SCAR=11.0
150  
151 208 for my $sign (@signs) {
152 1200 my $attribute = $sign;
153  
154 1200 ▶ next if $self->_has_init_arg($attribute);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_init_arg($attribute) 0 1200 1200
155  
156 1200 $sign =~ s{ _sign $ }{}x;
157  
158 1200 $self->$attribute($self->_get_data(symbol => $sign));
159 }
160 }
161  
162 sub _trigger_locale {
163 196 ▶ my ($self, $locale) = @_;
Subroutine _trigger_locale: called CC=6 SCAR=17.9
164 196 my ($lang, $script, $region, $ext) = _split_locale($locale);
165  
166 196 ▶ if ($lang && exists $CLDR::Number::Data::Base::DATA->{$lang}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lang and exists $CLDR::Number::Data::Base::DATA->{$lang}) { } 164 32 196
elsif ($self->default_locale) { } 2 30 32
Condition100%3/3
$lang and exists $CLDR::Number::Data::Base::DATA->{$lang}
!ll&&!rl&&r
293164
MC/DC100%2/2
$lang and exists $CLDR::Number::Data::Base::DATA->{$lang}
$lang exists $CLDR::Number::Data::Base::DATA->{$lang}
Truth table100%3/3
A and B
A= $langB= exists $CLDR::Number::Data::Base::DATA->{$lang}
ABresult
0X0
100
111
167 164 $self->_locale_inheritance(
168 _build_inheritance($lang, $script, $region, $ext)
169 );
170 164 $locale = $self->_locale_inheritance->[0];
171 }
172 elsif ($self->default_locale) {
173 2 $locale = $self->default_locale;
174 2 ($lang, $script, $region, $ext) = _split_locale($locale);
175 2 $self->_locale_inheritance(
176 _build_inheritance($lang, $script, $region, $ext)
177 );
178 }
179 else {
180 30 $locale = 'root';
181 30 $self->_locale_inheritance( [$locale] );
182 }
183  
184 196 ▶ if ($ext && $ext =~ m{ -nu- ( [^-]+ ) }x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext and $ext =~ / -nu- ( [^-]+ ) /x) { } 4 192 196
Condition100%3/3
$ext and $ext =~ / -nu- ( [^-]+ ) /x
!ll&&!rl&&r
19114
MC/DC100%2/2
$ext and $ext =~ / -nu- ( [^-]+ ) /x
$ext $ext =~ / -nu- ( [^-]+ ) /x
Truth table100%3/3
A and B
A= $extB= $ext =~ / -nu- ( [^-]+ ) /x
ABresult
0X0
100
111
185 4 $self->numbering_system($1);
186 }
187 else {
188 192 $self->_trigger_numbering_system;
189 }
190  
191 196 $self->{locale} = $locale;
192  
193 196 $self->_build_signs(qw{
194 decimal_sign group_sign plus_sign minus_sign infinity nan
195 });
196  
197 196 $self->_set_unless_init_arg(
198 minimum_grouping_digits => $self->_get_data(attr => 'min_group')
199 );
200 }
201  
202 sub _trigger_numbering_system {
203 201 ▶ my ($self, $system) = @_;
Subroutine _trigger_numbering_system: called CC=3 SCAR=11.0
204  
205 return if defined $system
206 201 ▶ && exists $CLDR::Number::Data::System::DATA->{$system};
Branch: 1 branch
BranchTrueFalseTotal
if defined $system and exists $CLDR::Number::Data::System::DATA->{$system} 8 193 201
Condition100%3/3
defined $system and exists $CLDR::Number::Data::System::DATA->{$system}
!ll&&!rl&&r
19218
MC/DC100%2/2
defined $system and exists $CLDR::Number::Data::System::DATA->{$system}
defined $system exists $CLDR::Number::Data::System::DATA->{$system}
Truth table100%3/3
A and B
A= defined $systemB= exists $CLDR::Number::Data::System::DATA->{$system}
ABresult
0X0
100
111
207  
208 193 $self->{numbering_system} = $self->_get_data(attr => 'system');
209 }
210  
211 sub _split_locale {
212 239 ▶ my ($locale) = @_;
Subroutine _split_locale: called CC=4 SCAR=13.9
213  
214 239 ▶ return unless defined $locale;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $locale 16 223 239
215  
216 223 $locale = lc $locale;
217 223 $locale =~ tr{_}{-};
218  
219 223 my ($lang, $script, $region, $ext) = $locale =~ m{ ^
220 ( [a-z]{2,3} ) # language
221 (?: - ( [a-z]{4} ) )? # script
222 (?: - ( [a-z]{2} | [0-9]{3} ) )? # country or region
223 (?: - ( u- .+ ) )? # extension
224 -? # trailing separator
225 $ }xi;
226  
227 223 ▶ $script = ucfirst $script if $script;
Branch: 1 branch
BranchTrueFalseTotal
if $script 13 210 223
228 223 ▶ $region = uc $region if $region;
Branch: 1 branch
BranchTrueFalseTotal
if $region 119 104 223
229  
230 223 return $lang, $script, $region, $ext;
231 }
232  
233 sub _build_inheritance {
234 207 ▶ my ($lang, $script, $region, $ext) = @_;
Subroutine _build_inheritance: called CC=7 SCAR=19.5
235 207 my @tree;
236  
237 207 for my $subtags (
238 [$lang, $region, $ext],
239 [$lang, $script, $region],
240 [$lang, $script],
241 [$lang, $region],
242 [$lang],
243 ) {
244 984 ▶ next if grep { !$_ } @$subtags;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $_;} @$subtags 692 292 984
245 292 my $locale = join '-', @$subtags;
246 292 ▶ next if !exists $CLDR::Number::Data::Base::DATA->{$locale};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $CLDR::Number::Data::Base::DATA->{$locale} 44 248 292
247 248 push @tree, $locale;
248  
249 248 ▶ if (my $parent = $CLDR::Number::Data::Base::PARENT->{$locale}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $CLDR::Number::Data::Base::PARENT->{$locale}) 41 207 248
250 41 push @tree, @{_build_inheritance(_split_locale($parent))};
251 41 last;
252 }
253 }
254  
255 207 ▶ if (!@tree || $tree[-1] ne 'root') {
Branch: 1 branch
BranchTrueFalseTotal
if (not @tree or $tree[-1] ne "root") 166 41 207
Condition100%3/3
not @tree or $tree[-1] ne "root"
l!l&&r!l&&!r
815841
MC/DC100%2/2
not @tree or $tree[-1] ne "root"
not @tree $tree[-1] ne "root"
Truth table100%3/3
not(A) or B
A= not @treeB= $tree[-1] ne "root"
ABresult
1X1
011
000
256 166 push @tree, 'root';
257 }
258  
259 207 return \@tree;
260 }
261  
262 sub _get_data {
263 1835 ▶ my ($self, $type, $key) = @_;
Subroutine _get_data: called CC=5 SCAR=16.1
264 1835 my $data = $CLDR::Number::Data::Base::DATA;
265  
266 1835 for my $locale (@{$self->_locale_inheritance}) {
267 return $data->{$locale}{$type}{$key}
268 if exists $data->{$locale}
269 && exists $data->{$locale}{$type}
270 3725 ▶ && exists $data->{$locale}{$type}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{$locale} and exists $data->{$locale}{$type} and exists $data->{$locale}{$type}{$key} 1750 1975 3725
Condition83%5/6
exists $data->{$locale} and exists $data->{$locale}{$type} and exists $data->{$locale}{$type}{$key}
!ll&&!rl&&r
13406351750
exists $data->{$locale} and exists $data->{$locale}{$type}
!ll&&!rl&&r
013402385
MC/DC66%2/3
exists $data->{$locale} and exists $data->{$locale}{$type} and exists $data->{$locale}{$type}{$key}
exists $data->{$locale} exists $data->{$locale}{$type} exists $data->{$locale}{$type}{$key}
Truth table75%3/4
A and B and C
A= exists $data->{$locale}B= exists $data->{$locale}{$type}C= exists $data->{$locale}{$type}{$key}
ABCresult
0XX0
10X0
1100
1111
271 }
272  
273 85 return undef;
274 }
275  
276 18 1;
« Build ↑ Summary blib/lib/CLDR/Number/Format/Percent.pm »