Pad/Type.pm

filter: Statementstmt 100% 166 / 166 Branchbran 91.6% 22 / 24 Conditioncond 73.3% 11 / 15 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 33 / 33 Podpod 61.5% 8 / 13 totaltotal 93.4% 243 / 260 CC 18 SCAR 7.5
CC
18
Cov
97%
check
4.0
takeFromSpec
4.0
glSpec
4.0
CRAP 2.1
« Pad/Result.pm ↑ Summary Pad/Spec/Level.pm »
1 package Getopt::Pad::Type;
2  
3 12 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 12 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Getopt::Pad::Registry;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use Getopt::Pad::Util qw(specError);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 12 our $VERSION = '0.05';
13  
14 12 my @builtins = map { "Getopt::Pad::Type::$_" } qw(Flag Bool Counter String Int Float File Dir Url);
15 12 my $registry;
16  
17 1176 ▶sub registry() {
Subroutine registry: called CC=2 SCAR=7.2
Pod undocumented
18 1176 ▶ return $registry //= Getopt::Pad::Registry->new(kind => 'option type')->register(@builtins);
Condition66%2/3
$registry //= "Getopt::Pad::Registry"->new("kind", "option type")->register(@builtins)
l!l&&r!l&&!r
1166100
MC/DC0%0/2
$registry //= "Getopt::Pad::Registry"->new("kind", "option type")->register(@builtins)
$registry "Getopt::Pad::Registry"->new("kind", "option type")->register(@builtins)
Truth table66%2/3
A //= B
A= $registryB= "Getopt::Pad::Registry"->new("kind", "option type")->register(@builtins)
ABresult
1X1
011
000
19 }
20  
21 3 ▶sub registerType($class) {
Subroutine registerType: called CC=1 SCAR=0.0
Pod documented
22 3 return registry()->register($class);
23 }
24  
25 # Build the Type an option or arg spec names. Takes the type name (or
26 # $defaultName when the spec has none) and the keys the Type declares in
27 # SPEC_KEYS out of %$spec, so the caller can treat whatever is left as
28 # unknown. The Type checks those keys first; a problem is reported for
29 # $owner (e.g. "option 'retries'"). Returns the Type and the name it was
30 # resolved under.
31 1172 ▶sub takeFromSpec($spec, $defaultName, $owner) {
Subroutine takeFromSpec: called CC=4 SCAR=13.9
Pod undocumented
32 1172 ▶ my $typeName = delete $spec->{type} // $defaultName;
Condition66%2/3
delete $spec->{"type"} // $defaultName
l!l&&r!l&&!r
7334390
MC/DC0%0/2
delete $spec->{"type"} // $defaultName
delete $spec->{"type"} $defaultName
Truth table66%2/3
A // B
A= delete $spec->{"type"}B= $defaultName
ABresult
1X1
011
000
33 1172 my $typeClass = registry()->resolve($typeName);
34 1169 ▶ my @typeKeys = $typeClass->can('SPEC_KEYS') ? $typeClass->SPEC_KEYS->@* : ();
Branch: 1 branch
BranchTrueFalseTotal
$typeClass->can('SPEC_KEYS') ? : 173 996 1169
35 1169 my %typeArgs = map { $_ => delete $spec->{$_} } grep { exists $spec->{$_} } @typeKeys;
36  
37 1169 my $problem = $typeClass->checkSpecKeys(%typeArgs);
38 1169 ▶ specError("%s: %s", $owner, $problem) if defined $problem;
Branch: 1 branch
BranchTrueFalseTotal
if defined $problem 3 1166 1169
39 1166 return ($typeClass->new(%typeArgs), $typeName);
40 }
41  
42 class Getopt::Pad::Type :abstract {
43 12 method glSuffix;
44  
45 # Types with SPEC_KEYS check them here, before construction: return a
46 # problem description without the option name, or undef.
47 996 ▶ method checkSpecKeys :common (%args) {
Subroutine checkSpecKeys: called CC=1 SCAR=0.0
Pod documented
48 996 return undef;
49 12 }
50  
51 336 ▶ method takesValue() {
Subroutine takesValue: called CC=2 SCAR=6.9
Pod undocumented
52 336 ▶ return $self->glSuffix =~ /=/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->glSuffix =~ /=/u ? : 309 27 336
53 12 }
54  
55 217 ▶ method negatable() {
Subroutine negatable: called CC=2 SCAR=6.9
Pod undocumented
56 217 ▶ return $self->glSuffix eq '!' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->glSuffix eq '!' ? : 15 202 217
57 12 }
58  
59 983 ▶ method glSpec($names, %flags) {
Subroutine glSpec: called CC=4 SCAR=13.9
Pod undocumented
60 983 my $suffix = $self->glSuffix;
61 983 ▶ $suffix =~ s/^=/:/ if $flags{optionalValue};
Branch: 1 branch
BranchTrueFalseTotal
if $flags{"optionalValue"} 97 886 983
62 983 my $glSpec = $names . $suffix;
63 983 ▶ $glSpec .= '@' if $flags{multiple};
Branch: 1 branch
BranchTrueFalseTotal
if $flags{"multiple"} 51 932 983
64 983 ▶ $glSpec .= '%' if $flags{hash};
Branch: 1 branch
BranchTrueFalseTotal
if $flags{"hash"} 47 936 983
65 983 return $glSpec;
66 12 }
67  
68 194 ▶ method coerce($value) {
Subroutine coerce: called CC=1 SCAR=0.0
Pod documented
69 194 return $value;
70 12 }
71  
72 186 ▶ method check($value) {
Subroutine check: called CC=1 SCAR=0.0
Pod documented
73 186 return undef;
74 12 }
75  
76 # Runs once per parse on every scalar of an option's or arg's effective
77 # value, after the value pipeline, for types whose values need the
78 # world arranged (a path created on demand). Return a problem
79 # description without the option name, or undef.
80 147 ▶ method prepare($value) {
Subroutine prepare: called CC=1 SCAR=0.0
Pod documented
81 147 return undef;
82 12 }
83  
84 75 ▶ method label() {
Subroutine label: called CC=1 SCAR=0.0
Pod documented
85 75 return undef;
86 12 }
87  
88 77 ▶ method constraintNotes() {
Subroutine constraintNotes: called CC=1 SCAR=0.0
Pod documented
89 77 return ();
90 12 }
91  
92 # Which of the shell's own completions a value of this type gets:
93 # 'files', 'dirs', or undef for none.
94 16 ▶ method completes() {
Subroutine completes: called CC=1 SCAR=0.0
Pod documented
95 16 return undef;
96 12 }
97 }
98  
99 12 class Getopt::Pad::Type::Flag :isa(Getopt::Pad::Type) :strict(params) {
100 12 ▶ use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102 12 ▶ use constant NAMES => ['flag'];
Subroutine BEGIN: called CC=1 SCAR=0.0
103  
104 322 ▶ method glSuffix() { return '' }
Subroutine glSuffix: called CC=1 SCAR=0.0
105  
106 # Config files give JSON or YAML booleans, 1/0 or ''; an undefined spec
107 # default stays undefined.
108 17 ▶ method check($value) {
Subroutine check: called CC=4 SCAR=13.9
109 17 ▶ return undef if !defined $value || JSON::PP::is_bool($value) || $value =~ /\A[01]?\z/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or JSON::PP::is_bool($value) or $value =~ /\A[01]?\z/u 16 1 17
Condition83%5/6
not defined $value or JSON::PP::is_bool($value) or $value =~ /\A[01]?\z/u
l!l&&r!l&&!r
1151
not defined $value or JSON::PP::is_bool($value)
l!l&&r!l&&!r
0116
MC/DC66%2/3
not defined $value or JSON::PP::is_bool($value) or $value =~ /\A[01]?\z/u
not defined $value JSON::PP::is_bool($value) $value =~ /\A[01]?\z/u
Truth table75%3/4
not(A) or B or C
A= not defined $valueB= JSON::PP::is_bool($value)C= $value =~ /\A[01]?\z/u
ABCresult
1XX1
01X1
0011
0000
110 1 return sprintf("'%s' is not a boolean (use true or false)", $value);
111 12 }
112  
113 13 ▶ method coerce($value) {
Subroutine coerce: called CC=3 SCAR=11.0
114 13 ▶ return $value if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 13 13
115 13 ▶ return $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 11 2 13
116 12 }
117 }
118  
119 12 class Getopt::Pad::Type::Bool :isa(Getopt::Pad::Type::Flag) :strict(params) {
120 12 ▶ use constant NAMES => ['!', 'bool', 'boolean'];
Subroutine BEGIN: called CC=1 SCAR=0.0
121  
122 41 ▶ method glSuffix() { return '!' }
Subroutine glSuffix: called CC=1 SCAR=0.0
123 }
124  
125 12 class Getopt::Pad::Type::Counter :isa(Getopt::Pad::Type) :strict(params) {
126 12 ▶ use constant NAMES => ['+', 'counter', 'count'];
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 19 ▶ method glSuffix() { return '+' }
Subroutine glSuffix: called CC=1 SCAR=0.0
129  
130 4 ▶ method check($value) {
Subroutine check: called CC=3 SCAR=11.0
131 4 ▶ return undef if !defined $value || $value =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value =~ /\A[0-9]+\z/u 3 1 4
Condition66%2/3
not defined $value or $value =~ /\A[0-9]+\z/u
l!l&&r!l&&!r
031
MC/DC50%1/2
not defined $value or $value =~ /\A[0-9]+\z/u
not defined $value $value =~ /\A[0-9]+\z/u
Truth table66%2/3
not(A) or B
A= not defined $valueB= $value =~ /\A[0-9]+\z/u
ABresult
1X1
011
000
132 1 return sprintf("'%s' is not a count", $value);
133 12 }
134  
135 2 ▶ method coerce($value) {
Subroutine coerce: called CC=2 SCAR=7.0
136 2 ▶ return $value if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 2 2
137 2 return $value + 0;
138 12 }
139 }
140  
141 12 class Getopt::Pad::Type::String :isa(Getopt::Pad::Type) :strict(params) {
142 12 ▶ use constant NAMES => ['s', 'string', 'str'];
Subroutine BEGIN: called CC=1 SCAR=0.0
143  
144 951 ▶ method glSuffix() { return '=s' }
Subroutine glSuffix: called CC=1 SCAR=0.0
145 }
146  
147 12 1;
148  
149 __END__
« Pad/Result.pm ↑ Summary Pad/Spec/Level.pm »