Messages/Message.pm

filter: Statementstmt 26.7% 15 / 56 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 40.0% 4 / 10 Podpod 100% 2 / 2 totaltotal 20.1% 21 / 104 CC 15 SCAR 39.2
CC
15
Cov
17%
_to_hash
90.0
_from_hash
56.0
_camelize
2.0
CRAP 50.6
↑ Summary Messages.pm »
1  
2 package Cucumber::Messages::Message;
3 3 $Cucumber::Messages::Message::VERSION = '35.0.0';
4 =head1 NAME
5  
6 Cucumber::Messages::Message - Base class for cucumber messages
7  
8 =head1 SYNOPSIS
9  
10  
11 # Create a new message class:
12 use Moo;
13 extends 'Cucumber::Messages::Message';
14  
15 has 'prop1';
16 has 'prop2';
17  
18  
19 =head1 DESCRIPTION
20  
21  
22  
23 =cut
24  
25 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 3 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 # 'use Moo' implies 'use strict; use warnings;'
32  
33 # The message classes have been inspired by the Ruby implementation, which
34 # existed before this Perl implementation was created.
35  
36 # Perl has multiple object systems: Moo, Moose, Mouse, ...
37 # Moo is Perl's minimalistic object system: geared towards speed, not aimed
38 # at completeness. Moose (not used in this code) is the one aiming at
39 # completeness. Moose has type checking of attributes, Moo lacks that.
40 # In that respect, Ruby and Perl are very much alike. Looking at the Ruby
41 # code (which doesn't have type checking), I decided not to go for
42 # type-checking. Given the expected short-livedness of the objects
43 # and as a way to reduce the dependency tree for the Cucumber::Messages
44 # library, I decided to go for Moo instead of Moose.
45  
46  
47 3 my $json = JSON::MaybeXS->new(
48 utf8 => 0, pretty => 0, indent => 0, canonical => 1
49 );
50  
51 0 ▶sub _camelize {
Subroutine _camelize: not called CC=1 SCAR=6.9
52 0 my ($self, $str) = @_;
53  
54 # "abc_def_ghi -> abcDefGhi
55 0 return ($str =~ s/(?:_(\w))/uc($1)/egr);
56 }
57  
58 0 ▶sub _snaked {
Subroutine _snaked: not called CC=1 SCAR=6.9
59 0 my ($self, $str) = @_;
60  
61 # abcDefGhi -> abc_def_ghi
62 0 return ($str =~ s/([A-Z])/lc("_$1")/egr);
63 }
64  
65 0 ▶sub _to_hash {
Subroutine _to_hash: not called CC=9 SCAR=45.0
66 0 my ($value, %args) = @_;
67  
68 0 ▶ if (my $ref = ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $value) { } 0 0 0
69 0 ▶ if ($ref eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "ARRAY") 0 0 0
70 0 ▶ $args{type} //= '';
Condition0%0/2
$args{"type"} //= ""
l!l
00
MC/DC0%0/1
$args{"type"} //= ""
$args{"type"}
Truth table0%0/2
A
A= $args{"type"}
Aresult
11
00
71 0 my $type = $args{type} =~ s/^\[\]//r;
72 0 return [ map { _to_hash( $_, %args, type => $type ) } @$value ];
73 }
74  
75 0 ▶ croak 'Cucumber::Messages::Message expected in message serialization; found: ' . $ref
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($value) and $value->isa("Cucumber::Messages::Message") 0 0 0
Condition0%0/3
builtin::blessed($value) and $value->isa("Cucumber::Messages::Message")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Cucumber::Messages::Message")
builtin::blessed($value) $value->isa("Cucumber::Messages::Message")
Truth table0%0/3
A and B
A= builtin::blessed($value)B= $value->isa("Cucumber::Messages::Message")
ABresult
0X0
100
111
76 unless blessed( $value ) and $value->isa( 'Cucumber::Messages::Message' );
77  
78 0 my $types = $value->_types;
79 return {
80 map {
81 __PACKAGE__->_camelize($_)
82 0 => _to_hash( $value->{$_}, %args, type => $types->{$_} )
83 0 } grep { defined $value->{$_} } keys %$value
84 };
85 }
86 else {
87 0 ▶ if (not $args{type} or $args{type} ne 'boolean') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args{'type'} or $args{'type'} ne 'boolean') { } 0 0 0
Condition0%0/3
not $args{"type"} or $args{"type"} ne "boolean"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $args{"type"} or $args{"type"} ne "boolean"
not $args{"type"} $args{"type"} ne "boolean"
Truth table0%0/3
not(A) or B
A= not $args{"type"}B= $args{"type"} ne "boolean"
ABresult
1X1
011
000
88 0 return $value;
89 }
90 else {
91 0 ▶ return $value ? JSON::MaybeXS->true : JSON::MaybeXS->false;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
92 }
93 }
94 }
95  
96  
97 0 ▶sub _from_hash {
Subroutine _from_hash: not called CC=7 SCAR=40.3
98 0 my ($value, %args) = @_;
99  
100 0 ▶ if (my $ref = ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $value) { } 0 0 0
101 0 ▶ return $value ? 1 : ''
Branch: 2 branches
BranchTrueFalseTotal
if $json->is_bool($value) 0 0 0
$value ? : 0 0 0
102 if $json->is_bool( $value );
103  
104 0 ▶ if ($ref eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "ARRAY") 0 0 0
105 0 ▶ $args{type} //= '';
Condition0%0/2
$args{"type"} //= ""
l!l
00
MC/DC0%0/1
$args{"type"} //= ""
$args{"type"}
Truth table0%0/2
A
A= $args{"type"}
Aresult
11
00
106 0 my $type = $args{type} =~ s/^\[\]//r;
107 0 return [ map { _from_hash( $_, %args, type => $type ) } @$value ];
108 }
109 croak 'No type supplied to deserialize hash'
110 0 ▶ unless $args{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"type"} 0 0 0
111  
112 0 my $types = $args{type}->_types;
113 return $args{type}->new(
114 map {
115 0 my $propname = __PACKAGE__->_snaked( $_ );
116 $propname
117 => _from_hash( $value->{$_}, %args,
118 0 type => $types->{$propname} )
119 } keys %$value
120 );
121 }
122 else {
123 0 return $value;
124 }
125 }
126  
127 =head1 METHODS
128  
129 =head2 $self->to_json
130  
131 Instance method.
132  
133 Returns the data encapsulated by C<$self> as a serialized byte string
134 represented as a single NDJSON line. Note that line-terminating newline
135 character (C<\n>) is not included in the return value.
136  
137 =cut
138  
139 0 ▶sub to_json {
Subroutine to_json: not called CC=1 SCAR=6.9
Pod documented
140 0 my ($self, %args) = @_;
141  
142 0 return $json->encode( _to_hash( $self, %args ) );
143 }
144  
145 =head2 $class->from_json( $str )
146  
147 Returns an instance of class C<$class> which encapsulates the data
148 from the bytestring C<$str>, assuming that it is a single valid NDJSON
149 line.
150  
151 =cut
152  
153 0 ▶sub from_json {
Subroutine from_json: not called CC=1 SCAR=6.9
Pod documented
154 0 my ($class, $msgstr ) = @_;
155  
156 0 my $args = $json->decode( $msgstr );
157 0 my $rv = _from_hash( $args, type => $class );
158 0 $rv;
159 }
160  
161 3 1;
162  
163 __END__
↑ Summary Messages.pm »