Pad/Type.pm

filter: Statementstmt 100% 172 / 172 Branchbran 91.6% 22 / 24 Conditioncond 73.3% 11 / 15 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 34 / 34 Podpod 64.2% 9 / 14 totaltotal 93.6% 251 / 268 CC 18 SCAR 7.4
CC
18
Cov
97%
check
4.0
takeFromSpec
4.0
glSpec
4.0
CRAP 2.1
« Pad/Type/Duration.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.06';
13  
14 12 my @builtins = map { "Getopt::Pad::Type::$_" } qw(Flag Bool Counter String Int Float File Dir Url Date Duration);
15 12 my $registry;
16  
17 1329 ▶sub registry() {
Subroutine registry: called CC=2 SCAR=7.2
Pod undocumented
18 1329 ▶ 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
1319100
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 4 ▶sub registerType($class) {
Subroutine registerType: called CC=1 SCAR=0.0
Pod documented
22 4 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 1324 ▶sub takeFromSpec($spec, $defaultName, $owner) {
Subroutine takeFromSpec: called CC=4 SCAR=13.9
Pod undocumented
32 1324 ▶ my $typeName = delete $spec->{type} // $defaultName;
Condition66%2/3
delete $spec->{"type"} // $defaultName
l!l&&r!l&&!r
8364880
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 1324 my $typeClass = registry()->resolve($typeName, $owner);
34 1321 ▶ my @typeKeys = $typeClass->can('SPEC_KEYS') ? $typeClass->SPEC_KEYS->@* : ();
Branch: 1 branch
BranchTrueFalseTotal
$typeClass->can('SPEC_KEYS') ? : 199 1122 1321
35 1321 my %typeArgs = map { $_ => delete $spec->{$_} } grep { exists $spec->{$_} } @typeKeys;
36  
37 1321 my $problem = $typeClass->checkSpecKeys(%typeArgs);
38 1321 ▶ specError("%s: %s", $owner, $problem) if defined $problem;
Branch: 1 branch
BranchTrueFalseTotal
if defined $problem 3 1318 1321
39 1318 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 1122 ▶ method checkSpecKeys :common (%args) {
Subroutine checkSpecKeys: called CC=1 SCAR=0.0
Pod documented
48 1122 return undef;
49 12 }
50  
51 376 ▶ method takesValue() {
Subroutine takesValue: called CC=2 SCAR=6.9
Pod undocumented
52 376 ▶ return $self->glSuffix =~ /=/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->glSuffix =~ /=/u ? : 343 33 376
53 12 }
54  
55 235 ▶ method negatable() {
Subroutine negatable: called CC=2 SCAR=6.9
Pod undocumented
56 235 ▶ return $self->glSuffix eq '!' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->glSuffix eq '!' ? : 19 216 235
57 12 }
58  
59 1115 ▶ method glSpec($names, %flags) {
Subroutine glSpec: called CC=4 SCAR=13.9
Pod undocumented
60 1115 my $suffix = $self->glSuffix;
61 1115 ▶ $suffix =~ s/^=/:/ if $flags{optionalValue};
Branch: 1 branch
BranchTrueFalseTotal
if $flags{"optionalValue"} 109 1006 1115
62 1115 my $glSpec = $names . $suffix;
63 1115 ▶ $glSpec .= '@' if $flags{multiple};
Branch: 1 branch
BranchTrueFalseTotal
if $flags{"multiple"} 59 1056 1115
64 1115 ▶ $glSpec .= '%' if $flags{hash};
Branch: 1 branch
BranchTrueFalseTotal
if $flags{"hash"} 56 1059 1115
65 1115 return $glSpec;
66 12 }
67  
68 222 ▶ method coerce($value) {
Subroutine coerce: called CC=1 SCAR=0.0
Pod documented
69 222 return $value;
70 12 }
71  
72 221 ▶ method check($value) {
Subroutine check: called CC=1 SCAR=0.0
Pod documented
73 221 return undef;
74 12 }
75  
76 # Runs once per parse on every scalar of the value an option or arg
77 # settles on, for checks that depend on the machine rather than on the
78 # value (a path exists). Never runs on a spec default when the spec is
79 # built. Return a problem description without the option name, or
80 # undef.
81 129 ▶ method verify($value) {
Subroutine verify: called CC=1 SCAR=0.0
Pod documented
82 129 return undef;
83 12 }
84  
85 # Like verify, for types whose values need the world arranged (a path
86 # created on demand). Runs only after every value of the parse passed
87 # verify, so a parse that fails arranges nothing.
88 129 ▶ method prepare($value) {
Subroutine prepare: called CC=1 SCAR=0.0
Pod documented
89 129 return undef;
90 12 }
91  
92 83 ▶ method label() {
Subroutine label: called CC=1 SCAR=0.0
Pod documented
93 83 return undef;
94 12 }
95  
96 94 ▶ method constraintNotes() {
Subroutine constraintNotes: called CC=1 SCAR=0.0
Pod documented
97 94 return ();
98 12 }
99  
100 # Which of the shell's own completions a value of this type gets:
101 # 'files', 'dirs', or undef for none.
102 16 ▶ method completes() {
Subroutine completes: called CC=1 SCAR=0.0
Pod documented
103 16 return undef;
104 12 }
105 }
106  
107 12 class Getopt::Pad::Type::Flag :isa(Getopt::Pad::Type) :strict(params) {
108 12 ▶ use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
109  
110 12 ▶ use constant NAMES => ['flag'];
Subroutine BEGIN: called CC=1 SCAR=0.0
111  
112 361 ▶ method glSuffix() { return '' }
Subroutine glSuffix: called CC=1 SCAR=0.0
113  
114 # Config files give JSON or YAML booleans, 1/0 or ''; an undefined spec
115 # default stays undefined.
116 18 ▶ method check($value) {
Subroutine check: called CC=4 SCAR=13.9
117 18 ▶ 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 17 1 18
Condition83%5/6
not defined $value or JSON::PP::is_bool($value) or $value =~ /\A[01]?\z/u
l!l&&r!l&&!r
1161
not defined $value or JSON::PP::is_bool($value)
l!l&&r!l&&!r
0117
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
118 1 return sprintf("'%s' is not a boolean (use true or false)", $value);
119 12 }
120  
121 14 ▶ method coerce($value) {
Subroutine coerce: called CC=3 SCAR=11.0
122 14 ▶ return $value if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 14 14
123 14 ▶ return $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 12 2 14
124 12 }
125 }
126  
127 12 class Getopt::Pad::Type::Bool :isa(Getopt::Pad::Type::Flag) :strict(params) {
128 12 ▶ use constant NAMES => ['!', 'bool', 'boolean'];
Subroutine BEGIN: called CC=1 SCAR=0.0
129  
130 49 ▶ method glSuffix() { return '!' }
Subroutine glSuffix: called CC=1 SCAR=0.0
131 }
132  
133 12 class Getopt::Pad::Type::Counter :isa(Getopt::Pad::Type) :strict(params) {
134 12 ▶ use constant NAMES => ['+', 'counter', 'count'];
Subroutine BEGIN: called CC=1 SCAR=0.0
135  
136 23 ▶ method glSuffix() { return '+' }
Subroutine glSuffix: called CC=1 SCAR=0.0
137  
138 4 ▶ method check($value) {
Subroutine check: called CC=3 SCAR=11.0
139 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
140 1 return sprintf("'%s' is not a count", $value);
141 12 }
142  
143 2 ▶ method coerce($value) {
Subroutine coerce: called CC=2 SCAR=7.0
144 2 ▶ return $value if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 2 2
145 2 return $value + 0;
146 12 }
147 }
148  
149 12 class Getopt::Pad::Type::String :isa(Getopt::Pad::Type) :strict(params) {
150 12 ▶ use constant NAMES => ['s', 'string', 'str'];
Subroutine BEGIN: called CC=1 SCAR=0.0
151  
152 1057 ▶ method glSuffix() { return '=s' }
Subroutine glSuffix: called CC=1 SCAR=0.0
153 }
154  
155 12 1;
156  
157 __END__
« Pad/Type/Duration.pm ↑ Summary Pad/Spec/Level.pm »