| 1 | # ABSTRACT: The Type::Tiny constraints that the evidence of an analysis amounts to | ||||||||||||||||||||||||||||
| 2 | # PODNAME: Pod::Coverage::More::Types | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package Pod::Coverage::More::Types 0.001; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 21 | ▶ | use 5.036; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 21 | ▶ | use warnings FATAL => q{all}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 21 | ▶ | use re q{/xsaa}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 21 | ▶ | use Exporter qw{import}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 21 | ▶ | use Type::Registry; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 21 | ▶ | use Type::Tiny::Intersection; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 21 | ▶ | use Type::Tiny::Union; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 21 | ▶ | use Types::Standard qw{Any ArrayRef Bool CodeRef GlobRef HasMethods HashRef InstanceOf Int Maybe Num Object Ref ScalarRef Str Undef}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 21 | our @EXPORT_OK = qw{type_of_value type_of_usage type_named}; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 322 | ▶ | sub type_of_value { | ||||||||||||||||||||||||||
|
Subroutine
type_of_value: called CC=22 SCAR=30.9
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 322 | my ($evidence) = @_; | |||||||||||||||||||||||||||
| 21 | 322 | my $type = $evidence->{type}; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 322 | ▶ | if ( $type eq 'number' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 87 | my $value = $evidence->{value}; | |||||||||||||||||||||||||||
| 25 | 87 | ▶ | return Maybe [Int] if $evidence->{integer} && $evidence->{maybe_undef}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$evidence->{"integer"} and $evidence->{"maybe_undef"}
MC/DC100%2/2
$evidence->{"integer"} and $evidence->{"maybe_undef"}
$evidence->{"integer"}
$evidence->{"maybe_undef"}
Truth table100%3/3
A and B
A= $evidence->{"integer"}B= $evidence->{"maybe_undef"}
| |||||||||||||||||||||||||||||
| 26 | 85 | ▶ | return Int if $evidence->{integer}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 78 | ▶ | return ( defined $value && $value =~ m/\A-?\d+\z/ ) ? Int : Num; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value and $value =~ /\A-?\d+\z/aasx
MC/DC100%2/2
defined $value and $value =~ /\A-?\d+\z/aasx
defined $value
$value =~ /\A-?\d+\z/aasx
Truth table100%3/3
A and B
A= defined $valueB= $value =~ /\A-?\d+\z/aasx
| |||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | 235 | ▶ | return Str if $type eq 'string'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 213 | ▶ | return Bool if $type eq 'boolean' || $type eq 'context'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$type eq "boolean" or $type eq "context"
MC/DC50%1/2
$type eq "boolean" or $type eq "context"
$type eq "boolean"
$type eq "context"
Truth table66%2/3
A or B
A= $type eq "boolean"B= $type eq "context"
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # An empty return gives undef to a caller that wants one value. | ||||||||||||||||||||||||||||
| 33 | 212 | ▶ | return Undef if $type eq 'undef' || $type eq 'empty'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$type eq "undef" or $type eq "empty"
MC/DC100%2/2
$type eq "undef" or $type eq "empty"
$type eq "undef"
$type eq "empty"
Truth table100%3/3
A or B
A= $type eq "undef"B= $type eq "empty"
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 130 | ▶ | return HashRef if $type eq 'hashref'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 120 | ▶ | return ArrayRef if $type eq 'arrayref'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 99 | ▶ | return CodeRef if $type eq 'coderef'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 99 | ▶ | return Ref if $type eq 'ref'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 99 | ▶ | if ( $type eq 'object' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 27 | ▶ | my @classes = @{ $evidence->{classes} || [] }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$evidence->{'classes'} || []
MC/DC100%1/1
$evidence->{'classes'} || []
$evidence->{'classes'}
Truth table100%2/2
A
A= $evidence->{'classes'}
| |||||||||||||||||||||||||||||
| 41 | 27 | ▶ | return InstanceOf [ $classes[0] ] if @classes == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 26 | ▶ | if ( @classes > 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 1 | my @instances; | |||||||||||||||||||||||||||
| 44 | 1 | foreach my $class (@classes) { | |||||||||||||||||||||||||||
| 45 | 2 | push @instances, InstanceOf [$class]; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 1 | return Type::Tiny::Union->new( type_constraints => \@instances ); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 25 | ▶ | return defined $evidence->{class} ? InstanceOf [ $evidence->{class} ] : Object; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 72 | return; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 276 | ▶ | sub type_of_usage { | ||||||||||||||||||||||||||
|
Subroutine
type_of_usage: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 276 | my ($usage) = @_; | |||||||||||||||||||||||||||
| 57 | 276 | my @alternatives; | |||||||||||||||||||||||||||
| 58 | 276 | ▶ | push @alternatives, HashRef if $usage->{hashref}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 276 | ▶ | push @alternatives, ArrayRef if $usage->{arrayref}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 276 | ▶ | push @alternatives, CodeRef if $usage->{coderef}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Perl makes a string of a number, so a variable used as both is a string. | ||||||||||||||||||||||||||||
| 63 | 276 | ▶ | if ( $usage->{string} ) { push @alternatives, Str } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 64 | 38 | elsif ( $usage->{numeric} ) { push @alternatives, Num } | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 276 | my $type; | |||||||||||||||||||||||||||
| 67 | 276 | ▶ | $type = $alternatives[0] if @alternatives == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 276 | ▶ | $type = Type::Tiny::Union->new( type_constraints => \@alternatives ) if @alternatives > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 276 | ▶ | my @methods = @{ $usage->{methods} || [] }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$usage->{'methods'} || []
MC/DC100%1/1
$usage->{'methods'} || []
$usage->{'methods'}
Truth table100%2/2
A
A= $usage->{'methods'}
| |||||||||||||||||||||||||||||
| 71 | 276 | ▶ | return $type if !@methods; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 7 | my $duck = HasMethods [@methods]; | |||||||||||||||||||||||||||
| 73 | 7 | ▶ | return $duck if !$type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 1 | return Type::Tiny::Intersection->new( type_constraints => [ $type, $duck ] ); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # The type words of the POD that Pod::Coverage::More has always read, in lower case. | ||||||||||||||||||||||||||||
| 78 | 21 | our %POD_WORDS = ( | |||||||||||||||||||||||||||
| 79 | scalar => Any, | ||||||||||||||||||||||||||||
| 80 | mixed => Any, | ||||||||||||||||||||||||||||
| 81 | string => Str, | ||||||||||||||||||||||||||||
| 82 | integer => Int, | ||||||||||||||||||||||||||||
| 83 | float => Num, | ||||||||||||||||||||||||||||
| 84 | boolean => Bool, | ||||||||||||||||||||||||||||
| 85 | object => Object, | ||||||||||||||||||||||||||||
| 86 | scalarref => ScalarRef, | ||||||||||||||||||||||||||||
| 87 | arrayref => ArrayRef, | ||||||||||||||||||||||||||||
| 88 | hashref => HashRef, | ||||||||||||||||||||||||||||
| 89 | coderef => CodeRef, | ||||||||||||||||||||||||||||
| 90 | typeglobref => GlobRef, | ||||||||||||||||||||||||||||
| 91 | array => ArrayRef, | ||||||||||||||||||||||||||||
| 92 | hash => HashRef, | ||||||||||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 235 | ▶ | sub type_named { | ||||||||||||||||||||||||||
|
Subroutine
type_named: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 235 | my ($name) = @_; | |||||||||||||||||||||||||||
| 98 | 235 | ▶ | return if !defined $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 234 | my $word = $POD_WORDS{ lc $name }; | |||||||||||||||||||||||||||
| 100 | 234 | ▶ | return $word if $word; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 148 | my $registry = Type::Registry->for_class(__PACKAGE__); | |||||||||||||||||||||||||||
| 103 | 148 | ▶ | $registry->add_types('Types::Standard') if !$registry->simple_lookup('Str'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 148 | my $type = eval { $registry->lookup($name) }; | |||||||||||||||||||||||||||
| 105 | 148 | return $type; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 21 | 1; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | __END__ | ||||||||||||||||||||||||||||