Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a checker we can use in other test functions
- Loading branch information
Showing
6 changed files
with
292 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ t/sizeof.t | |
t/bool.t | ||
lib/Test2/Tools/Type.pm | ||
t/test2-tools-type.t | ||
lib/Test2/Compare/Type.pm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package Test2::Compare::Type; | ||
|
||
# almost entirely cargo-culted from Test2::Compare::Pattern | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use base 'Test2::Compare::Base'; | ||
|
||
our $VERSION = '1'; | ||
|
||
use Test2::Util::HashBase qw(type); | ||
use Test2::Compare::Negatable; | ||
use Test2::Tools::Type (); | ||
use Scalar::Type qw(bool_supported); | ||
|
||
use Carp qw(croak); | ||
|
||
sub init { | ||
my $self = shift; | ||
|
||
croak "'type' is a required attribute" unless($self->{+TYPE}); | ||
croak "'".$self->{+TYPE}."' is not a valid type" | ||
unless(Test2::Tools::Type->can('is_'.$self->{+TYPE})); | ||
|
||
$self->SUPER::init(); | ||
} | ||
|
||
sub name { shift->{+TYPE} . "" } | ||
sub operator { join(' ', 'is', (shift->{+NEGATE} ? 'not' : ()), 'of type') } | ||
|
||
sub verify { | ||
my $self = shift; | ||
my %params = @_; | ||
my ($got, $exists) = @params{qw/got exists/}; | ||
|
||
return 0 unless $exists; | ||
|
||
my $is_func = 'Test2::Tools::Type::is_'.$self->{+TYPE}; | ||
my $result; | ||
{ | ||
no strict 'refs'; | ||
local $Test2::Compare::Type::verifying = 1; | ||
$result = $is_func->($got); | ||
} | ||
$result = !$result if($self->{+NEGATE}); | ||
return $result; | ||
} | ||
|
||
1; | ||
|
||
=head1 NAME | ||
Test2::Compare::Type - Use a type to validate values in a deep comparison. | ||
=head1 DESCRIPTION | ||
This allows you to validate a value's type in a deep comparison. | ||
Sometimes a value just needs to look right, it may not need to be exact. An | ||
example is that you care that your code always returns an integer, but you | ||
don't care whether it is 192 or 3. | ||
=head1 CAVEATS | ||
The definitions of Boolean, integer and number are exactly the same as those in | ||
L<Scalar::Type>, which this is a thin wrapper around. | ||
=head1 SEE ALSO | ||
L<Scalar::Type> | ||
L<Test2::Tools::Type> | ||
L<Test2> | ||
=head1 BUGS | ||
If you find any bugs please report them on Github, preferably with a test case. | ||
=head1 FEEDBACK | ||
I welcome feedback about my code, especially constructive criticism. | ||
=head1 AUTHOR, COPYRIGHT and LICENCE | ||
Mostly cargo-culted from L<Test2::Compare::Pattern>. Differences from that are | ||
Copyright 2024 David Cantrell E<lt>F<david@cantrell.org.uk>E<gt> | ||
This software is free-as-in-speech software, and may be used, | ||
distributed, and modified under the terms of either the GNU | ||
General Public Licence version 2 or the Artistic Licence. It's | ||
up to you which one you use. The full text of the licences can | ||
be found in the files GPL2.txt and ARTISTIC.txt, respectively. | ||
=head1 CONSPIRACY | ||
This module is also free-as-in-mason software. | ||
=cut |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.