Struct/Value/Quantity.pm

filter: Statementstmt 98.3% 59 / 60 Branchbran 87.5% 21 / 24 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 92.1% 94 / 102 CC 14 SCAR 14.4
CC
14
Cov
93%
obj2struct
7.0
struct2obj
6.2
_add_plus
2.0
CRAP 4.2
« Struct/Sense.pm ↑ Summary Struct/Statement.pm »
1 package Wikibase::Datatype::Struct::Value::Quantity;
2  
3 54 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 54 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 54 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 54 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 54 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 54 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 54 ▶use Wikibase::Datatype::Value::Quantity;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 54 Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
13  
14 54 our $VERSION = 0.15;
15  
16 7 ▶sub obj2struct {
Subroutine obj2struct: called CC=7 SCAR=19.5
Pod documented
17 7 my ($obj, $base_uri) = @_;
18  
19 7 ▶ if (! defined $obj) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $obj) 1 6 7
20 1 err "Object doesn't exist.";
21 }
22 6 ▶ if (! $obj->isa('Wikibase::Datatype::Value::Quantity')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->isa("Wikibase::Datatype::Value::Quantity")) 1 5 6
23 1 err "Object isn't 'Wikibase::Datatype::Value::Quantity'.";
24 }
25 5 ▶ if (! defined $base_uri) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $base_uri) 1 4 5
26 1 err 'Base URI is required.';
27 }
28  
29 4 my $amount = $obj->value;
30 4 $amount = _add_plus($amount);
31 4 my $unit;
32 4 ▶ if (defined $obj->unit) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj->unit) { } 3 1 4
33 3 $unit = $base_uri.$obj->unit;
34 } else {
35 1 $unit = '1';
36 }
37 4 ▶ my $struct_hr = {
Branch: 2 branches
BranchTrueFalseTotal
defined $obj->lower_bound ? : 1 3 4
defined $obj->upper_bound ? : 1 3 4
38 'value' => {
39 'amount' => $amount,
40 defined $obj->lower_bound ? (
41 'lowerBound' => _add_plus($obj->lower_bound),
42 ) : (),
43 'unit' => $unit,
44 defined $obj->upper_bound ? (
45 'upperBound' => _add_plus($obj->upper_bound),
46 ) : (),
47 },
48 'type' => 'quantity',
49 };
50  
51 4 return $struct_hr;
52 }
53  
54 4 ▶sub struct2obj {
Subroutine struct2obj: called CC=6 SCAR=18.2
Pod documented
55 4 my $struct_hr = shift;
56  
57 4 ▶ if (! exists $struct_hr->{'type'}
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $struct_hr->{"type"} or $struct_hr->{"type"} ne "quantity") 0 4 4
Condition33%1/3
not exists $struct_hr->{"type"} or $struct_hr->{"type"} ne "quantity"
l!l&&r!l&&!r
004
MC/DC0%0/2
not exists $struct_hr->{"type"} or $struct_hr->{"type"} ne "quantity"
not exists $struct_hr->{"type"} $struct_hr->{"type"} ne "quantity"
Truth table33%1/3
not(A) or B
A= not exists $struct_hr->{"type"}B= $struct_hr->{"type"} ne "quantity"
ABresult
1X1
011
000
58 || $struct_hr->{'type'} ne 'quantity') {
59  
60 0 err "Structure isn't for 'quantity' datatype.";
61 }
62  
63 4 my $amount = $struct_hr->{'value'}->{'amount'};
64 4 $amount = _remove_plus($amount);
65 4 my $unit = $struct_hr->{'value'}->{'unit'};
66 4 ▶ if ($unit eq 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit eq 1) { } 1 3 4
67 1 $unit = undef;
68 } else {
69 3 my $u = URI->new($unit);
70 3 my @path_segments = $u->path_segments;
71 3 $unit = $path_segments[-1];
72 }
73 my $obj = Wikibase::Datatype::Value::Quantity->new(
74 $struct_hr->{'value'}->{'lowerBound'} ? (
75 'lower_bound' => _remove_plus($struct_hr->{'value'}->{'lowerBound'}),
76 ) : (),
77 'unit' => $unit,
78 $struct_hr->{'value'}->{'upperBound'} ? (
79 4 ▶ 'upper_bound' => _remove_plus($struct_hr->{'value'}->{'upperBound'}),
Branch: 2 branches
BranchTrueFalseTotal
$struct_hr->{'value'}{'lowerBound'} ? : 1 3 4
$struct_hr->{'value'}{'upperBound'} ? : 1 3 4
80 ) : (),
81 'value' => $amount,
82 );
83  
84 4 return $obj;
85 }
86  
87 6 ▶sub _add_plus {
Subroutine _add_plus: called CC=2 SCAR=7.0
88 6 my $value = shift;
89  
90 6 ▶ if ($value =~ m/^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\d+$/) 6 0 6
91 6 $value = '+'.$value;
92 }
93  
94 6 return $value;
95 }
96  
97 6 ▶sub _remove_plus {
Subroutine _remove_plus: called CC=2 SCAR=7.0
98 6 my $value = shift;
99  
100 6 ▶ if ($value =~ m/^\+(\d+)$/ms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\+(\d+)$/ms) 6 0 6
101 6 $value = $1;
102 }
103  
104 6 return $value;
105 }
106  
107 54 1;
108  
109 __END__
« Struct/Sense.pm ↑ Summary Struct/Statement.pm »