blib/lib/Text/KDL/XS/Value.pm

filter: Statementstmt 97.4% 116 / 119 Branchbran 83.3% 75 / 90 Conditioncond 77.7% 21 / 27 MC/DCmcdc 66.6% 12 / 18 Subroutinesub 96.4% 27 / 28 Podpod 100% 13 / 13 totaltotal 89.4% 264 / 295 CC 55 SCAR 17.3
CC
55
Cov
90%
new
10.0
_text_field
9.7
_number_fields
9.0
CRAP 5.6
« blib/lib/Text/KDL/XS/Emitter.pm ↑ Summary blib/lib/Text/KDL/XS/Document.pm »
1 package Text::KDL::XS::Value;
2  
3 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 24 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 24 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 24 ▶use Text::KDL::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 24 our @CARP_NOT = qw(
12 Text::KDL::XS Text::KDL::XS::Document Text::KDL::XS::Emitter
13 Text::KDL::XS::Node Text::KDL::XS::Parser
14 );
15  
16 24 my %IS_FIELD = map { $_ => 1 } qw(type kind value type_annotation);
17 24 my %IS_TYPE = map { $_ => 1 } qw(null bool number string);
18 24 my %IS_KIND = map { $_ => 1 } qw(integer float string);
19  
20 24 my $INFINITY = 9**9**9;
21 24 my %KEYWORD_NUMBER = ('#inf' => $INFINITY, '#-inf' => -$INFINITY, '#nan' => $INFINITY - $INFINITY);
22  
23 126 ▶sub new {
Subroutine new: called CC=10 SCAR=23.1
Pod documented
24 126 my ($class, @fields) = @_;
25 126 my $fields = Text::KDL::XS::_parse_named_arguments(__PACKAGE__ . '->new', 'field', \%IS_FIELD, @fields);
26 126 my $type = $fields->{type};
27  
28 126 ▶ Carp::croak(__PACKAGE__ . "->new: 'type' is required") unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 126 126
29 Carp::croak(__PACKAGE__ . "->new: unknown type '$type' (expected null, bool, number or string)")
30 126 ▶ unless $IS_TYPE{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless $IS_TYPE{$type} 2 124 126
31 Carp::croak(__PACKAGE__ . "->new: 'kind' is only allowed for numbers")
32 124 ▶ if defined $fields->{kind} && $type ne 'number';
Branch: 1 branch
BranchTrueFalseTotal
if defined $fields->{"kind"} and $type ne "number" 1 123 124
Condition100%3/3
defined $fields->{"kind"} and $type ne "number"
!ll&&!rl&&r
88351
MC/DC100%2/2
defined $fields->{"kind"} and $type ne "number"
defined $fields->{"kind"} $type ne "number"
Truth table100%3/3
A and B
A= defined $fields->{"kind"}B= $type ne "number"
ABresult
0X0
100
111
33 Carp::croak(__PACKAGE__ . "->new: 'type_annotation' must be a string")
34 123 ▶ if ref $fields->{type_annotation};
Branch: 1 branch
BranchTrueFalseTotal
if ref $fields->{"type_annotation"} 0 123 123
35  
36 123 my %value = (type => $type, kind => undef, value => undef, type_annotation => $fields->{type_annotation});
37 123 ▶ if ($type eq 'bool') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'bool') { } 5 118 123
elsif ($type eq 'string') { } 72 46 118
elsif ($type eq 'number') { } 42 4 46
38 5 ▶ $value{value} = $fields->{value} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$fields->{'value'} ? : 3 2 5
39 }
40 elsif ($type eq 'string') {
41 72 $value{value} = _text_field('a string', $fields->{value});
42 }
43 elsif ($type eq 'number') {
44 42 @value{qw(kind value)} = _number_fields($fields->{kind}, $fields->{value});
45 }
46 104 return bless \%value, $class;
47 }
48  
49 0 ▶sub type { $_[0]->{type} }
Subroutine type: not called CC=1 SCAR=6.9
Pod documented
50 14 ▶sub kind { $_[0]->{kind} }
Subroutine kind: called CC=1 SCAR=0.0
Pod documented
51 35 ▶sub type_annotation { $_[0]->{type_annotation} }
Subroutine type_annotation: called CC=1 SCAR=0.0
Pod documented
52 69 ▶sub value { $_[0]->{value} }
Subroutine value: called CC=1 SCAR=0.0
Pod documented
53  
54 3 ▶sub is_null { $_[0]->{type} eq 'null' }
Subroutine is_null: called CC=1 SCAR=0.0
Pod documented
55 4 ▶sub is_bool { $_[0]->{type} eq 'bool' }
Subroutine is_bool: called CC=1 SCAR=0.0
Pod documented
56 4 ▶sub is_number { $_[0]->{type} eq 'number' }
Subroutine is_number: called CC=1 SCAR=0.0
Pod documented
57 33 ▶sub is_string { $_[0]->{type} eq 'string' }
Subroutine is_string: called CC=1 SCAR=0.0
Pod documented
58  
59 47 ▶sub as_perl {
Subroutine as_perl: called CC=4 SCAR=13.9
Pod documented
60 47 my ($self) = @_;
61 47 ▶ return undef if $self->{type} eq 'null';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"type"} eq "null" 0 47 47
62 47 ▶ return $self->{value} ? 1 : 0 if $self->{type} eq 'bool';
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"type"} eq "bool" 5 42 47
$self->{'value'} ? : 3 2 5
63 42 return $self->{value};
64 }
65  
66 34 ▶sub as_string {
Subroutine as_string: called CC=6 SCAR=18.7
Pod documented
67 34 my ($self) = @_;
68 34 my $type = $self->{type};
69 34 ▶ return undef if $type eq 'null';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "null" 0 34 34
70 34 ▶ return $self->{value} ? 'true' : 'false' if $type eq 'bool';
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "bool" 0 34 34
$self->{'value'} ? : 0 0 0
71 34 ▶ return Text::KDL::XS::_float_text($self->{value}) if $type eq 'number' && $self->_kind_is('float');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "number" and $self->_kind_is("float") 8 26 34
Condition100%3/3
$type eq "number" and $self->_kind_is("float")
!ll&&!rl&&r
14128
MC/DC100%2/2
$type eq "number" and $self->_kind_is("float")
$type eq "number" $self->_kind_is("float")
Truth table100%3/3
A and B
A= $type eq "number"B= $self->_kind_is("float")
ABresult
0X0
100
111
72 26 return "$self->{value}";
73 }
74  
75 10017 ▶sub as_number {
Subroutine as_number: called CC=6 SCAR=18.0
Pod documented
76 10017 my ($self) = @_;
77 10017 my $type = $self->{type};
78 10017 ▶ return undef if $type eq 'null';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "null" 1 10016 10017
79 10016 ▶ return $self->{value} ? 1 : 0 if $type eq 'bool';
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "bool" 1 10015 10016
$self->{'value'} ? : 1 0 1
80 10015 ▶ return _number_from_text($self->{value}) if $type eq 'number' && $self->_kind_is('string');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "number" and $self->_kind_is("string") 9457 558 10015
Condition66%2/3
$type eq "number" and $self->_kind_is("string")
!ll&&!rl&&r
05589457
MC/DC50%1/2
$type eq "number" and $self->_kind_is("string")
$type eq "number" $self->_kind_is("string")
Truth table66%2/3
A and B
A= $type eq "number"B= $self->_kind_is("string")
ABresult
0X0
100
111
81 558 return _numified($self->{value});
82 }
83  
84 9 ▶sub as_bignum {
Subroutine as_bignum: called CC=5 SCAR=16.1
Pod documented
85 9 my ($self) = @_;
86 9 my $type = $self->{type};
87 9 ▶ return undef if $type eq 'null';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "null" 1 8 9
88 8 ▶ return $self->{value} ? 1 : 0 if $type eq 'bool';
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "bool" 1 7 8
$self->{'value'} ? : 0 1 1
89 7 ▶ Carp::croak(__PACKAGE__ . "::as_bignum: the value is a $type, not a number") unless $type eq 'number';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "number" 1 6 7
90 6 return _bignum_from_text($self->as_string);
91 }
92  
93 10035 ▶sub _kind_is {
Subroutine _kind_is: called CC=2 SCAR=7.1
94 10035 my ($self, $kind) = @_;
95 10035 ▶ return defined $self->{kind} && $self->{kind} eq $kind;
Condition66%2/3
defined $self->{'kind'} && $self->{'kind'} eq $kind
!ll&&!rl&&r
05709465
MC/DC50%1/2
defined $self->{'kind'} && $self->{'kind'} eq $kind
defined $self->{'kind'} $self->{'kind'} eq $kind
Truth table66%2/3
A && B
A= defined $self->{'kind'}B= $self->{'kind'} eq $kind
ABresult
0X0
100
111
96 }
97  
98 # The value of a string: a plain scalar, or an object with a string
99 # conversion, which is stringified once here.
100 114 ▶sub _text_field {
Subroutine _text_field: called CC=5 SCAR=22.7
101 114 my ($what, $text) = @_;
102 114 ▶ Carp::croak(__PACKAGE__ . "->new: $what needs a defined value") unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 2 112 114
103 112 ▶ return $text unless ref $text;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $text 112 0 112
104 0 ▶ Carp::croak(__PACKAGE__ . "->new: cannot use a " . ref($text) . " reference as $what")
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($text) and overload::Method($text, "\"\"") 0 0 0
Condition0%0/3
builtin::blessed($text) and overload::Method($text, "\"\"")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($text) and overload::Method($text, "\"\"")
builtin::blessed($text) overload::Method($text, "\"\"")
Truth table0%0/3
A and B
A= builtin::blessed($text)B= overload::Method($text, "\"\"")
ABresult
0X0
100
111
105 unless Scalar::Util::blessed($text) && overload::Method($text, '""');
106 0 return "$text";
107 }
108  
109 # The kind and value of a number, the kind inferred when it is not given.
110 42 ▶sub _number_fields {
Subroutine _number_fields: called CC=9 SCAR=22.0
111 42 my ($kind, $number) = @_;
112 42 $number = _text_field('a number', $number);
113 41 ▶ $kind //= _inferred_kind($number);
Condition66%2/3
$kind //= _inferred_kind($number)
l!l&&r!l&&!r
3470
MC/DC0%0/2
$kind //= _inferred_kind($number)
$kind _inferred_kind($number)
Truth table66%2/3
A //= B
A= $kindB= _inferred_kind($number)
ABresult
1X1
011
000
114  
115 Carp::croak(__PACKAGE__ . "->new: unknown kind '$kind' (expected integer, float or string)")
116 40 ▶ unless $IS_KIND{$kind};
Branch: 1 branch
BranchTrueFalseTotal
unless $IS_KIND{$kind} 1 39 40
117 39 ▶ Carp::croak(__PACKAGE__ . "->new: '$number' is not an integer from -2**63 to 2**64-1")
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "integer" and not Text::KDL::XS::_is_native_integer($number) 6 33 39
Condition100%3/3
$kind eq "integer" and not Text::KDL::XS::_is_native_integer($number)
!ll&&!rl&&r
3126
MC/DC100%2/2
$kind eq "integer" and not Text::KDL::XS::_is_native_integer($number)
$kind eq "integer" not Text::KDL::XS::_is_native_integer($number)
Truth table100%3/3
A and not(B)
A= $kind eq "integer"B= not Text::KDL::XS::_is_native_integer($number)
ABresult
0X0
100
111
118 if $kind eq 'integer' && !Text::KDL::XS::_is_native_integer($number);
119 33 ▶ Carp::croak(__PACKAGE__ . "->new: '$number' is not a number")
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "float" and not &Scalar::Util::looks_like_number($number) 1 32 33
Condition100%3/3
$kind eq "float" and not &Scalar::Util::looks_like_number($number)
!ll&&!rl&&r
2751
MC/DC100%2/2
$kind eq "float" and not &Scalar::Util::looks_like_number($number)
$kind eq "float" not &Scalar::Util::looks_like_number($number)
Truth table100%3/3
A and not(B)
A= $kind eq "float"B= not &Scalar::Util::looks_like_number($number)
ABresult
0X0
100
111
120 if $kind eq 'float' && !Scalar::Util::looks_like_number($number);
121 32 ▶ Carp::croak(__PACKAGE__ . "->new: '$number' is not a KDL number")
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "string" and not Text::KDL::XS::_is_number_literal($number) 8 24 32
Condition100%3/3
$kind eq "string" and not Text::KDL::XS::_is_number_literal($number)
!ll&&!rl&&r
7178
MC/DC100%2/2
$kind eq "string" and not Text::KDL::XS::_is_number_literal($number)
$kind eq "string" not Text::KDL::XS::_is_number_literal($number)
Truth table100%3/3
A and not(B)
A= $kind eq "string"B= not Text::KDL::XS::_is_number_literal($number)
ABresult
0X0
100
111
122 if $kind eq 'string' && !Text::KDL::XS::_is_number_literal($number);
123 24 return ($kind, $number);
124 }
125  
126 # A Perl number is an integer or a float; any other text must be a KDL number
127 # literal, which is kept verbatim as kind 'string'.
128 7 ▶sub _inferred_kind {
Subroutine _inferred_kind: called CC=3 SCAR=11.0
129 7 my ($number) = @_;
130 7 my $kind = Text::KDL::XS::_number_kind_of($number);
131 7 ▶ return $kind if defined $kind;
Branch: 1 branch
BranchTrueFalseTotal
if defined $kind 5 2 7
132 2 ▶ return 'string' if Text::KDL::XS::_is_number_literal($number);
Branch: 1 branch
BranchTrueFalseTotal
if Text::KDL::XS::_is_number_literal($number) 1 1 2
133 1 Carp::croak(__PACKAGE__ . "->new: '$number' is not a number");
134 }
135  
136 # A native number from a Perl number or numeric text. A number is returned as
137 # it is: arithmetic would turn -0.0 into 0.
138 10011 ▶sub _numified {
Subroutine _numified: called CC=2 SCAR=6.9
139 10011 my ($number) = @_;
140 10011 ▶ return $number if defined Text::KDL::XS::_number_kind_of($number);
Branch: 1 branch
BranchTrueFalseTotal
if defined Text::KDL::XS::_number_kind_of($number) 558 9453 10011
141 9453 return 0 + $number;
142 }
143  
144 # A native number from KDL number text; lossy beyond the range and precision
145 # of Perl's numbers.
146 9457 ▶sub _number_from_text {
Subroutine _number_from_text: called CC=5 SCAR=16.1
147 9457 my ($text) = @_;
148 9457 ▶ return $KEYWORD_NUMBER{$text} if exists $KEYWORD_NUMBER{$text};
Branch: 1 branch
BranchTrueFalseTotal
if exists $KEYWORD_NUMBER{$text} 2 9455 9457
149 9455 (my $digits = $text) =~ tr/_//d;
150 9455 ▶ my ($sign, $radix, $magnitude) = $digits =~ /\A([+-]?)0([xob])(.+)\z/
Branch: 1 branch
BranchTrueFalseTotal
() = $digits =~ /\A([+-]?)0([xob])(.+)\z/ or return _numified($digits) 2 9453 9455
151 or return _numified($digits);
152 24 ▶ no warnings qw(overflow portable);
Subroutine BEGIN: called CC=1 SCAR=0.0
153 2 ▶ my $number = oct($radix eq 'o' ? "0$magnitude" : "0$radix$magnitude");
Branch: 1 branch
BranchTrueFalseTotal
$radix eq 'o' ? : 1 1 2
154 2 ▶ return $sign eq '-' ? -$number : $number;
Branch: 1 branch
BranchTrueFalseTotal
$sign eq '-' ? : 1 1 2
155 }
156  
157 # A Math::BigInt for integral KDL number text, a Math::BigFloat otherwise.
158 6 ▶sub _bignum_from_text {
Subroutine _bignum_from_text: called CC=7 SCAR=19.5
159 6 my ($text) = @_;
160 6 require Math::BigInt;
161 6 require Math::BigFloat;
162  
163 6 ▶ return Math::BigFloat->binf('+') if $text eq '#inf';
Branch: 1 branch
BranchTrueFalseTotal
if $text eq "#inf" 1 5 6
164 5 ▶ return Math::BigFloat->binf('-') if $text eq '#-inf';
Branch: 1 branch
BranchTrueFalseTotal
if $text eq "#-inf" 0 5 5
165 5 ▶ return Math::BigFloat->bnan if $text eq '#nan';
Branch: 1 branch
BranchTrueFalseTotal
if $text eq "#nan" 0 5 5
166  
167 5 (my $digits = $text) =~ tr/_//d;
168 5 ▶ if (my ($sign, $radix, $magnitude) = $digits =~ /\A([+-]?)0([xob])(.+)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $digits =~ /\A([+-]?)0([xob])(.+)\z/) 2 3 5
169 2 my $number = _bigint_from_radix($radix, $magnitude);
170 2 ▶ return $sign eq '-' ? $number->bneg : $number;
Branch: 1 branch
BranchTrueFalseTotal
$sign eq '-' ? : 1 1 2
171 }
172 3 ▶ return $digits =~ /[.eE]/ ? Math::BigFloat->new($digits) : Math::BigInt->new($digits);
Branch: 1 branch
BranchTrueFalseTotal
$digits =~ /[.eE]/ ? : 2 1 3
173 }
174  
175 2 ▶sub _bigint_from_radix {
Subroutine _bigint_from_radix: called CC=3 SCAR=11.0
176 2 my ($radix, $magnitude) = @_;
177 2 ▶ return Math::BigInt->new("0x$magnitude") if $radix eq 'x';
Branch: 1 branch
BranchTrueFalseTotal
if $radix eq "x" 1 1 2
178 1 ▶ return Math::BigInt->new("0b$magnitude") if $radix eq 'b';
Branch: 1 branch
BranchTrueFalseTotal
if $radix eq "b" 0 1 1
179 # Octal goes through the binary form (three bits per digit), which every
180 # Math::BigInt version reads.
181 1 return Math::BigInt->new('0b' . join '', map { sprintf '%03b', $_ } split //, $magnitude);
182 }
183  
184 24 1;
185  
186 __END__
« blib/lib/Text/KDL/XS/Emitter.pm ↑ Summary blib/lib/Text/KDL/XS/Document.pm »