| 1 | package Test2::Compare::Type; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use base 'Test2::Compare::Base'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | our $VERSION = '1.0.1'; | |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Test2::Compare qw(compare strict_convert); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Test2::Compare::Negatable; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Test2::Tools::Type (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Test2::Util::HashBase qw(type); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Scalar::Type qw(bool_supported); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use Scalar::Util qw(reftype blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 28 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 28 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 28 | ▶ | croak "'type' requires at least one argument" unless(@{$self->{+TYPE}}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 27 | foreach my $type (@{$self->{+TYPE}}) { | |||||||||||||||||||||||||||||||||||||||||
| 25 | 51 | ▶ | croak "'$type' is not a valid argument, must either be Test2::Tools::Type checkers or Test2::Compare::* object" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
"Test2::Tools::Type"->can("is_$type") or builtin::blessed($type) and $type->isa("Test2::Compare::Base")
builtin::blessed($type) and $type->isa("Test2::Compare::Base")
MC/DC66%2/3
"Test2::Tools::Type"->can("is_$type") or builtin::blessed($type) and $type->isa("Test2::Compare::Base")
"Test2::Tools::Type"->can("is_$type")
builtin::blessed($type)
$type->isa("Test2::Compare::Base")
Truth table75%3/4
A or B and C
A= "Test2::Tools::Type"->can("is_$type")B= builtin::blessed($type)C= $type->isa("Test2::Compare::Base")
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | unless( | ||||||||||||||||||||||||||||||||||||||||||
| 27 | Test2::Tools::Type->can("is_$type") || | ||||||||||||||||||||||||||||||||||||||||||
| 28 | (blessed($type) && $type->isa('Test2::Compare::Base')) | ||||||||||||||||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 25 | $self->SUPER::init(); | |||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 12 | ▶ | sub name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
name: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | join( | ||||||||||||||||||||||||||||||||||||||||||
| 37 | " and ", | ||||||||||||||||||||||||||||||||||||||||||
| 38 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 26 | ▶ | blessed($_) ? blessed($_) : $_ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 12 | } @{shift->{+TYPE}} | |||||||||||||||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 12 | ▶ | sub operator { join(' ', 'is', (shift->{+NEGATE} ? 'not' : ()), 'of type') } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
operator: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 25 | ▶ | sub verify { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
verify: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 25 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 25 | my %params = @_; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 25 | my ($got, $exists) = @params{qw/got exists/}; | |||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 25 | ▶ | return 0 unless $exists; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 25 | my $result = 1; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 25 | foreach my $type (@{$self->{+TYPE}}) { | |||||||||||||||||||||||||||||||||||||||||
| 55 | 48 | ▶ | if(Test2::Tools::Type->can("is_$type")) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 40 | local $Test2::Compare::Type::verifying = 1; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 40 | ▶ | $result &&= "Test2::Tools::Type::is_$type"->($got); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$result &&= ("Test2::Tools::Type::is_$type")->($got)
MC/DC100%2/2
$result &&= ("Test2::Tools::Type::is_$type")->($got)
$result
("Test2::Tools::Type::is_$type")->($got)
Truth table100%3/3
A &&= B
A= $resultB= ("Test2::Tools::Type::is_$type")->($got)
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 8 | ▶ | $result &&= !compare($got, $type, \&strict_convert); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$result &&= !compare($got, $type, \&Test2::Compare::Type::strict_convert)
MC/DC100%2/2
$result &&= !compare($got, $type, \&Test2::Compare::Type::strict_convert)
$result
!compare($got, $type, \&Test2::Compare::Type::strict_convert)
Truth table100%3/3
A &&= not(B)
A= $resultB= !compare($got, $type, \&Test2::Compare::Type::strict_convert)
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 25 | ▶ | $result = !$result if($self->{+NEGATE}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 25 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | Test2::Compare::Type - Use a type to validate values in a deep comparison. | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | This allows you to validate a value's type in a deep comparison. | ||||||||||||||||||||||||||||||||||||||||||
| 76 | Sometimes a value just needs to look right, it may not need to be exact. An | ||||||||||||||||||||||||||||||||||||||||||
| 77 | example is that you care that your code always returns an integer, but you | ||||||||||||||||||||||||||||||||||||||||||
| 78 | don't care whether it is 192 or 3. | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | =head1 CAVEATS | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | The definitions of Boolean, integer and number are exactly the same as those in | ||||||||||||||||||||||||||||||||||||||||||
| 83 | L<Scalar::Type>, which this is a thin wrapper around. | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | L<Scalar::Type> | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | L<Test2::Tools::Type> | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | L<Test2> | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | If you find any bugs please report them on Github, preferably with a test case. | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | =head1 FEEDBACK | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | I welcome feedback about my code, especially constructive criticism. | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | =head1 AUTHOR, COPYRIGHT and LICENCE | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | Mostly cargo-culted from L<Test2::Compare::Pattern>. Differences from that are | ||||||||||||||||||||||||||||||||||||||||||
| 104 | Copyright 2024 David Cantrell E<lt>F<david@cantrell.org.uk>E<gt> | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | This software is free-as-in-speech software, and may be used, | ||||||||||||||||||||||||||||||||||||||||||
| 107 | distributed, and modified under the terms of either the GNU | ||||||||||||||||||||||||||||||||||||||||||
| 108 | General Public Licence version 2 or the Artistic Licence. It's | ||||||||||||||||||||||||||||||||||||||||||
| 109 | up to you which one you use. The full text of the licences can | ||||||||||||||||||||||||||||||||||||||||||
| 110 | be found in the files GPL2.txt and ARTISTIC.txt, respectively. | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | =head1 CONSPIRACY | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | This module is also free-as-in-mason software. | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | =cut | ||||||||||||||||||||||||||||||||||||||||||