Dry/Types.pm

filter: Statementstmt 100% 51 / 51 Branchbran 88.8% 16 / 18 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 13 / 13 Podpod 100% 5 / 5 totaltotal 94.1% 96 / 102 CC 13 SCAR 10.4
CC
13
Cov
95%
Object
4.3
String
4.0
Ref
4.0
CRAP 2.8
« Dry/Types/Number.pm ↑ Summary Dry/Types/DateTime.pm »
1 #!/usr/bin/perl
2 #*
3 #* Name: Params::Dry::Types
4 #* Info: Types definitions module
5 #* Author: Pawel Guspiel (neo77) <neo@cpan.org>
6 #*
7 #* This module keeps validation functions. You can of course add your modules which uses this and will add additional checks
8 #* Build in types for Params::Dry
9 #*
10 package Params::Dry::Types;
11 {
12 4 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # --- version ---
17 4 our $VERSION = 1.20_03;
18  
19 #=------------------------------------------------------------------------ { use, constants }
20  
21 4 ▶ use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 4 ▶ use constant PASS => 1; # pass test
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶ use constant FAIL => 0; # test fail
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 #=------------------------------------------------------------------------ { export }
27  
28 4 ▶ use Exporter; # to export _ rq and opt
Subroutine BEGIN: called CC=1 SCAR=0.0
29 4 our @ISA = qw(Exporter);
30  
31 4 our @EXPORT_OK = qw(PASS FAIL);
32  
33 4 our %EXPORT_TAGS = ( const => [qw(PASS FAIL)], );
34  
35 #=------------------------------------------------------------------------ { module public functions }
36  
37 #=---------
38 # String
39 #=---------
40 #* string type check (parameter sets max length)
41 #* RETURN: PASS if test pass otherwise FAIL
42 17 ▶ sub String {
Subroutine String: called CC=4 SCAR=13.9
Pod documented
43 17 ▶ ref( $_[0] ) and return FAIL;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] and return 0 1 16 17
44 16 ▶ $_[1] and length $_[0] > $_[1] and return FAIL;
Branch: 1 branch
BranchTrueFalseTotal
$_[1] and length $_[0] > $_[1] and return 0 4 12 16
Condition100%3/3
$_[1] and length $_[0] > $_[1]
!ll&&!rl&&r
1114
MC/DC100%2/2
$_[1] and length $_[0] > $_[1]
$_[1] length $_[0] > $_[1]
Truth table100%3/3
A and B
A= $_[1]B= length $_[0] > $_[1]
ABresult
0X0
100
111
45 12 PASS;
46 } #+ end of: sub String
47  
48 #=---------
49 # Object
50 #=---------
51 #* Object type check, Object - just object, or Object(Params::Dry::Types) check if is Params::Dry::Types type
52 #* RETURN: PASS if test pass otherwise FAIL
53 4 ▶ sub Object {
Subroutine Object: called CC=4 SCAR=14.6
Pod documented
54 4 my $class = blessed( $_[0] );
55 4 ▶ return FAIL if !$class; # not an object
Branch: 1 branch
BranchTrueFalseTotal
unless $class 3 1 4
56 1 ▶ return FAIL if $_[1] and ( $_[1] ne $class );
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] and $_[1] ne $class 0 1 1
Condition33%1/3
$_[1] and $_[1] ne $class
!ll&&!rl&&r
010
MC/DC0%0/2
$_[1] and $_[1] ne $class
$_[1] $_[1] ne $class
Truth table33%1/3
A and B
A= $_[1]B= $_[1] ne $class
ABresult
0X0
100
111
57 1 PASS;
58 } #+ end of: sub Object
59  
60 #=------
61 # Ref
62 #=------
63 #* ref type check
64 #* RETURN: PASS if test pass otherwise FAIL
65 24 ▶ sub Ref {
Subroutine Ref: called CC=4 SCAR=13.9
Pod documented
66 24 ▶ my $ref = ref( $_[0] ) or return FAIL;
Branch: 1 branch
BranchTrueFalseTotal
my $ref = ref $_[0] or return 0 20 4 24
67  
68 20 ▶ return FAIL if $_[1] and $ref ne $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] and $ref ne $_[1] 2 18 20
Condition100%3/3
$_[1] and $ref ne $_[1]
!ll&&!rl&&r
1172
MC/DC100%2/2
$_[1] and $ref ne $_[1]
$_[1] $ref ne $_[1]
Truth table100%3/3
A and B
A= $_[1]B= $ref ne $_[1]
ABresult
0X0
100
111
69 18 PASS;
70 } #+ end of: sub Ref
71  
72 #=----------
73 # Defined
74 #=----------
75 #* Allows anything what is defined
76 #* RETURN: PASS if defined
77 3 ▶ sub Defined {
Subroutine Defined: called CC=2 SCAR=6.9
Pod documented
78 3 ▶ defined $_[0] ? PASS : FAIL;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 2 1 3
79 } #+ end of: sub Defined
80  
81 #=--------
82 # Value
83 #=--------
84 #* Allows anything what is not a reference
85 #* RETURN: PASS if defined
86 2 ▶ sub Value {
Subroutine Value: called CC=3 SCAR=11.2
Pod documented
87 2 ▶ $_[0] and ref $_[0] ? FAIL : PASS;
Branch: 2 branches
BranchTrueFalseTotal
$_[0] and ref $_[0] ? 0 : 1 2 0 2
ref $_[0] ? : 1 1 2
88 } #+ end of: sub Value
89  
90 {
91 4 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 #+ Number - mapped types
94 4 *Params::Dry::Types::Int = *Params::Dry::Types::Number::Int;
95 4 *Params::Dry::Types::Float = *Params::Dry::Types::Number::Float;
96 4 *Params::Dry::Types::Bool = *Params::Dry::Types::Number::Bool;
97  
98 #+ Ref - mapped types
99 4 *Params::Dry::Types::Scalar = *Params::Dry::Types::Ref::Scalar;
100 4 *Params::Dry::Types::Array = *Params::Dry::Types::Ref::Array;
101 4 *Params::Dry::Types::Hash = *Params::Dry::Types::Ref::Hash;
102 4 *Params::Dry::Types::Code = *Params::Dry::Types::Ref::Code;
103 4 *Params::Dry::Types::Regexp = *Params::Dry::Types::Ref::Regexp;
104 };
105  
106 };
107 4 0115 && 0x4d;
108  
109 #+ End of Params::Dry::Types
110 __END__
« Dry/Types/Number.pm ↑ Summary Dry/Types/DateTime.pm »