| 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
| |||||||||||||||||||||
| 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}
}
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}
}
| |||||||||||||||||||||
| 40 | 3 | ▶ | my $hash = $self->yaml_content // {}; | ||||||||||||||||||
|
Condition100%2/2
$self->yaml_content // {}
MC/DC100%1/1
$self->yaml_content // {}
$self->yaml_content
Truth table100%2/2
A
A= $self->yaml_content
| |||||||||||||||||||||
| 41 | 3 | ▶ | ref $hash eq 'HASH' ? $hash : {ref $hash, $hash}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | }; | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 6 | ▶ | return $hash->{$_[0]} if @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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"
MC/DC0%0/1
$test_name ||= "YAML structure matches"
$test_name
Truth table50%1/2
A
A= $test_name
| |||||||||||||||||||||
| 114 | 2 | ▶ | like $self->res->header('content-type'), qr/yaml/, 'Content-Type is YAML' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 118 | 2 | return $self; | |||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 1 | 1; | |||||||||||||||||||
| 122 | |||||||||||||||||||||