YAML/KelpExtensions.pm

filter: Statementstmt 97.4% 75 / 77 Branchbran 75.0% 12 / 16 Conditioncond 60.0% 6 / 10 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 95.6% 22 / 23 Podpod 0.0% 0 / 6 totaltotal 84.0% 116 / 138 CC 13 SCAR 8.2
CC
13
Cov
90%
yaml_param
5.0
yaml_cmp
4.2
yaml
2.1
CRAP 2.3
↑ Summary YAML.pm »
1 package Kelp::Module::YAML::KelpExtensions;
2 1 $Kelp::Module::YAML::KelpExtensions::VERSION = '2.00';
3 1 ▶use Kelp::Base -strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Kelp::Module::YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Kelp::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Kelp::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Kelp::Test;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Test::Deep;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub _replace
15 1 ▶{
Subroutine _replace: called CC=1 SCAR=0.0
16 1 my ($name, $new) = @_;
17  
18 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 my $old = \&{$name};
22 1 ▶ *{$name} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
23 2 unshift @_, $old;
24 2 goto $new;
25 1 };
26 }
27  
28 sub Kelp::Request::is_yaml
29 13 ▶{
Subroutine is_yaml: called CC=2 SCAR=7.1
Pod undocumented
30 13 my $self = shift;
31 13 ▶ return 0 unless $self->content_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->content_type 0 13 13
32 13 return $self->content_type =~ m{$Kelp::Module::YAML::content_type_re};
33 }
34  
35 sub Kelp::Request::yaml_param
36 6 ▶{
Subroutine yaml_param: called CC=5 SCAR=16.1
Pod undocumented
37 6 my $self = shift;
38  
39 6 ▶ my $hash = $self->{_param_yaml_content} //= do {
Condition66%2/3
$self->{"_param_yaml_content"} //= do { my $hash = $self->yaml_content // {}; ref $hash eq "HASH" ? $hash : {ref $hash, $hash} }
l!l&&r!l&&!r
330
MC/DC0%0/2
$self->{"_param_yaml_content"} //= do { my $hash = $self->yaml_content // {}; ref $hash eq "HASH" ? $hash : {ref $hash, $hash} }
$self->{"_param_yaml_content"} do { my $hash = $self->yaml_content // {}; ref $hash eq "HASH" ? $hash : {ref $hash, $hash} }
Truth table66%2/3
A //= B
A= $self->{"_param_yaml_content"}B= do { my $hash = $self->yaml_content // {}; ref $hash eq "HASH" ? $hash : {ref $hash, $hash} }
ABresult
1X1
011
000
40 3 ▶ my $hash = $self->yaml_content // {};
Condition100%2/2
$self->yaml_content // {}
l!l
21
MC/DC100%1/1
$self->yaml_content // {}
$self->yaml_content
Truth table100%2/2
A
A= $self->yaml_content
Aresult
11
00
41 3 ▶ ref $hash eq 'HASH' ? $hash : {ref $hash, $hash};
Branch: 1 branch
BranchTrueFalseTotal
ref $hash eq 'HASH' ? : 2 1 3
42 };
43  
44 6 ▶ return $hash->{$_[0]} if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 3 6
45 3 return keys %$hash;
46 }
47  
48 sub Kelp::Request::yaml_content
49 8 ▶{
Subroutine yaml_content: called CC=2 SCAR=6.9
Pod undocumented
50 8 my $self = shift;
51 8 ▶ return undef unless $self->is_yaml;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_yaml 2 6 8
52  
53 6 ▶ return try {
Subroutine __ANON__: called CC=2 SCAR=6.9
54 6 my @documents = $self->app->get_encoder(yaml => 'internal')->decode($self->content);
55 5 ▶ die if @documents > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @documents > 1 1 4 5
56 4 return $documents[0];
57 }
58 2 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
59 2 undef;
60 6 };
61 }
62  
63 # Response
64  
65 sub Kelp::Response::yaml
66 2 ▶{
Subroutine yaml: called CC=2 SCAR=7.6
Pod undocumented
67 2 my $self = shift;
68 2 ▶ $self->set_content_type(
Condition33%1/3
$self->charset || $self->app->charset
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->charset || $self->app->charset
$self->charset $self->app->charset
Truth table33%1/3
A || B
A= $self->charsetB= $self->app->charset
ABresult
1X1
011
000
69 $Kelp::Module::YAML::content_type,
70 $self->charset || $self->app->charset
71 );
72  
73 2 return $self;
74 }
75  
76 _replace(
77 'Kelp::Response::_render_ref',
78 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
79 2 my ($super, $self, $body) = @_;
80  
81 2 ▶ if ($self->content_type =~ m{$Kelp::Module::YAML::content_type_re}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->content_type =~ /$Kelp::Module::YAML::content_type_re/i) { } 2 0 2
82 2 return $self->app->get_encoder(yaml => 'internal')->encode($body);
83 }
84 else {
85 0 return $super->($self, $body);
86 }
87 }
88 1 );
89  
90 # Test
91  
92 sub Kelp::Test::yaml_content
93 2 ▶{
Subroutine yaml_content: called CC=1 SCAR=0.0
Pod undocumented
94 2 my $self = shift;
95 2 my $result;
96 2 my $decoder = $self->app->get_encoder(yaml => 'internal');
97 2 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
98 2 $result = $decoder->decode(
99 $self->_decode($self->res->content)
100 );
101 }
102 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
103 0 fail("Poorly formatted YAML");
104 2 };
105 2 return $result;
106 }
107  
108 sub Kelp::Test::yaml_cmp
109 2 ▶{
Subroutine yaml_cmp: called CC=4 SCAR=14.3
Pod undocumented
110 2 my ($self, $expected, $test_name) = @_;
111 2 local $Test::Builder::Level = $Test::Builder::Level + 1;
112  
113 2 ▶ $test_name ||= "YAML structure matches";
Condition50%1/2
$test_name ||= "YAML structure matches"
l!l
02
MC/DC0%0/1
$test_name ||= "YAML structure matches"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
114 2 ▶ like $self->res->header('content-type'), qr/yaml/, 'Content-Type is YAML'
Branch: 1 branch
BranchTrueFalseTotal
&like(scalar $self->res->header("content-type"), qr/yaml/, "Content-Type is YAML") or return $self 2 0 2
115 or return $self;
116 2 my $json = $self->yaml_content;
117 2 ▶ cmp_deeply($json, $expected, $test_name) or diag explain $json;
Branch: 1 branch
BranchTrueFalseTotal
cmp_deeply($json, $expected, $test_name) or diag(explain($json)) 2 0 2
118 2 return $self;
119 }
120  
121 1 1;
122  
↑ Summary YAML.pm »