blib/lib/Zuzu/Module/Base64.pm

filter: Statementstmt 74.6% 50 / 67 Branchbran 56.2% 9 / 16 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 77.7% 14 / 18 Podpod 0.0% 0 / 1 totaltotal 70.5% 79 / 112 CC 11 SCAR 11.7
CC
11
Cov
71%
_decode_urlsafe
12.0
_type_name
4.1
_assert_binary
3.0
CRAP 3.2
« blib/lib/Zuzu/Error.pm ↑ Summary blib/lib/Zuzu/Value/BinaryString.pm »
1 package Zuzu::Module::Base64;
2  
3 12 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 12 our $VERSION = '0.007002';
6  
7 12 ▶use MIME::Base64 qw( decode_base64 encode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 12 ▶use Zuzu::Util::NativeHelpers qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 native_function
12 12 );
13 12 ▶use Zuzu::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use Zuzu::Value::BinaryString;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶sub _type_name {
Subroutine _type_name: called CC=4 SCAR=14.1
17 2 my ( $value ) = @_;
18  
19 2 ▶ return 'Null' if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 2 2
20 2 ▶ return 'BinaryString'
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString") 1 1 2
Condition66%2/3
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
!ll&&!rl&&r
101
MC/DC50%1/2
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
builtin::blessed($value) $value->isa("Zuzu::Value::BinaryString")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::BinaryString")
ABresult
0X0
100
111
21 if blessed($value) and $value->isa('Zuzu::Value::BinaryString');
22 1 return 'String';
23 }
24  
25 11 ▶sub _assert_binary {
Subroutine _assert_binary: called CC=3 SCAR=11.0
26 11 my ( $value, $label ) = @_;
27  
28 11 ▶ return if blessed($value) and $value->isa('Zuzu::Value::BinaryString');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString") 10 1 11
Condition66%2/3
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
!ll&&!rl&&r
1010
MC/DC50%1/2
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
builtin::blessed($value) $value->isa("Zuzu::Value::BinaryString")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::BinaryString")
ABresult
0X0
100
111
29  
30 1 my $type = _type_name( $value );
31 1 die Zuzu::Error->new_runtime(
32 message => "TypeException: $label expects BinaryString, got $type",
33 file => '<std/string/base64>',
34 line => 0,
35 );
36 }
37  
38 6 ▶sub _assert_string {
Subroutine _assert_string: called CC=3 SCAR=11.0
39 6 my ( $value, $label ) = @_;
40  
41 6 ▶ return if not blessed($value);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($value) 5 1 6
42 1 ▶ return if not $value->isa('Zuzu::Value::BinaryString');
Branch: 1 branch
BranchTrueFalseTotal
unless $value->isa("Zuzu::Value::BinaryString") 0 1 1
43  
44 1 my $type = _type_name( $value );
45 1 die Zuzu::Error->new_runtime(
46 message => "TypeException: $label expects String, got $type",
47 file => '<std/string/base64>',
48 line => 0,
49 );
50 }
51  
52 11 ▶sub _encode {
Subroutine _encode: called CC=1 SCAR=0.0
53 11 my ( $value ) = @_;
54  
55 11 _assert_binary( $value, 'encode' );
56  
57 10 return encode_base64( $value->bytes, '' );
58 }
59  
60 6 ▶sub _decode {
Subroutine _decode: called CC=2 SCAR=7.0
61 6 my ( $value ) = @_;
62  
63 6 _assert_string( $value, 'decode' );
64  
65 5 ▶ my $decoded = decode_base64( defined $value ? "$value" : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 5 0 5
66 5 return Zuzu::Value::BinaryString->new( bytes => $decoded );
67 }
68  
69 0 ▶sub _encode_urlsafe {
Subroutine _encode_urlsafe: not called CC=1 SCAR=6.9
70 0 my ( $value ) = @_;
71  
72 0 my $encoded = _encode( $value );
73 0 $encoded =~ tr/\+\//\-_/;
74 0 $encoded =~ s/=+\z//;
75 0 return $encoded;
76 }
77  
78 0 ▶sub _decode_urlsafe {
Subroutine _decode_urlsafe: not called CC=3 SCAR=24.8
79 0 my ( $value ) = @_;
80  
81 0 _assert_string( $value, 'decode_urlsafe' );
82  
83 0 ▶ my $text = defined $value ? "$value" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 0 0
84 0 $text =~ tr/\-_/\+\//;
85 0 my $mod = length($text) % 4;
86 0 ▶ if ( $mod != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod != 0) 0 0 0
87 0 $text .= '=' x ( 4 - $mod );
88 }
89 0 return _decode($text);
90 }
91  
92 87 ▶sub IMPORT {
Subroutine IMPORT: called CC=1 SCAR=0.2
Pod undocumented
93 87 my ( $class, $runtime ) = @_;
94  
95 my $encode_fn = native_function(
96 name => 'encode',
97 11 ▶ native => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
98 11 my ( $value ) = @_;
99 11 return _encode($value);
100 },
101 87 );
102  
103 my $decode_fn = native_function(
104 name => 'decode',
105 6 ▶ native => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
106 6 my ( $value ) = @_;
107 6 return _decode($value);
108 },
109 87 );
110  
111 my $encode_urlsafe_fn = native_function(
112 name => 'encode_urlsafe',
113 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
114 0 my ( $value ) = @_;
115 0 return _encode_urlsafe($value);
116 },
117 87 );
118  
119 my $decode_urlsafe_fn = native_function(
120 name => 'decode_urlsafe',
121 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 0 my ( $value ) = @_;
123 0 return _decode_urlsafe($value);
124 },
125 87 );
126  
127 return {
128 87 encode => $encode_fn,
129 decode => $decode_fn,
130 encode_urlsafe => $encode_urlsafe_fn,
131 decode_urlsafe => $decode_urlsafe_fn,
132 };
133 }
134  
135 12 1;
136  
137 =pod
138  
139 =head1 NAME
140  
141 Zuzu::Module::Base64 - std/string/base64 bindings.
142  
143 =head1 DESCRIPTION
144  
145 Implements C<std/string/base64> and exports C<encode>,
146 C<decode>, C<encode_urlsafe>, and C<decode_urlsafe>.
147  
148 =head1 COPYRIGHT AND LICENCE
149  
150 B<< Zuzu::Module::Base64 >> is copyright Toby Inkster.
151  
152 It is free software; you may redistribute it and/or modify it under
153 the terms of either the Artistic License 1.0 or the GNU General Public
154 License version 2.
155  
156 =cut
« blib/lib/Zuzu/Error.pm ↑ Summary blib/lib/Zuzu/Value/BinaryString.pm »