1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
package Test::TypeTiny;
use strict;
use warnings;
use Test::More qw();
use Scalar::Util qw(blessed);
use Types::TypeTiny qw(to_TypeTiny);
require Exporter::Tiny;
our @ISA = 'Exporter::Tiny';
BEGIN {
*EXTENDED_TESTING = $ENV{EXTENDED_TESTING} ? sub(){!!1} : sub(){!!0};
};
our $AUTHORITY = 'cpan:TOBYINK';
our $VERSION = '1.000004';
our @EXPORT = qw( should_pass should_fail ok_subtype );
our @EXPORT_OK = qw( EXTENDED_TESTING matchfor );
sub matchfor
{
my @matchers = @_;
bless \@matchers, do {
package #
Test::TypeTiny::Internal::MATCHFOR;
use overload
q[==] => 'match',
q[eq] => 'match',
q[""] => 'to_string',
fallback => 1;
sub to_string {
$_[0][0]
}
sub match {
my ($self, $e) = @_;
my $does = Scalar::Util::blessed($e) ? ($e->can('DOES') || $e->can('isa')) : undef;
for my $s (@$self) {
return 1 if ref($s) && $e =~ $s;
return 1 if !ref($s) && $does && $e->$does($s);
}
return;
}
__PACKAGE__;
};
}
sub _mk_message
{
require Type::Tiny;
my ($template, $value) = @_;
sprintf($template, Type::Tiny::_dd($value));
}
sub ok_subtype
{
my ($type, @s) = @_;
@_ = (
not(scalar grep !$_->is_subtype_of($type), @s),
sprintf("%s subtype: %s", $type, join q[, ], @s),
);
goto \&Test::More::ok;
}
eval(EXTENDED_TESTING ? <<'SLOW' : <<'FAST');
sub should_pass
{
my ($value, $type, $message) = @_;
local $Test::Builder::Level = $Test::Builder::Level + 1;
$type = to_TypeTiny($type) unless blessed($type) && $type->can("check");
my $strictures = $type->can("_strict_check");
my $test = "Test::Builder"->new->child(
$message || _mk_message("%s passes type constraint $type", $value),
);
$test->plan(tests => ($strictures ? 2 : 1));
$test->ok(!!$type->check($value), '->check');
$test->ok(!!$type->_strict_check($value), '->_strict_check') if $strictures;
$test->finalize;
return $test->is_passing;
}
sub should_fail
{
my ($value, $type, $message) = @_;
$type = to_TypeTiny($type) unless blessed($type) && $type->can("check");
local $Test::Builder::Level = $Test::Builder::Level + 1;
my $strictures = $type->can("_strict_check");
my $test = "Test::Builder"->new->child(
$message || _mk_message("%s fails type constraint $type", $value),
);
$test->plan(tests => ($strictures ? 2 : 1));
$test->ok(!$type->check($value), '->check');
$test->ok(!$type->_strict_check($value), '->_strict_check') if $strictures;
$test->finalize;
return $test->is_passing;
}
SLOW
sub should_pass
{
my ($value, $type, $message) = @_;
$type = to_TypeTiny($type) unless blessed($type) && $type->can("check");
@_ = (
!!$type->check($value),
$message || _mk_message("%s passes type constraint $type", $value),
);
goto \&Test::More::ok;
}
sub should_fail
{
my ($value, $type, $message) = @_;
$type = to_TypeTiny($type) unless blessed($type) && $type->can("check");
@_ = (
!$type->check($value),
$message || _mk_message("%s fails type constraint $type", $value),
);
goto \&Test::More::ok;
}
FAST
1;
__END__
=pod
=encoding utf-8
=head1 NAME
Test::TypeTiny - useful functions for testing the efficacy of type constraints
=head1 SYNOPSIS
=for test_synopsis
BEGIN { die "SKIP: uses a module that doesn't exist as an example" };
use strict;
use warnings;
use Test::More;
use Test::TypeTiny;
use Types::Mine qw(Integer Number);
should_pass(1, Integer);
should_pass(-1, Integer);
should_pass(0, Integer);
should_fail(2.5, Integer);
ok_subtype(Number, Integer);
done_testing;
=head1 STATUS
This module is covered by the
L<Type-Tiny stability policy|Type::Tiny::Manual::Policies/"STABILITY">.
=head1 DESCRIPTION
L<Test::TypeTiny> provides a few handy functions for testing type constraints.
=head2 Functions
=over
=item C<< should_pass($value, $type, $test_name) >>
=item C<< should_pass($value, $type) >>
Test that passes iff C<< $value >> passes C<< $type->check >>.
=item C<< should_fail($value, $type, $test_name) >>
=item C<< should_fail($value, $type) >>
Test that passes iff C<< $value >> fails C<< $type->check >>.
=item C<< ok_subtype($type, @subtypes) >>
Test that passes iff all C<< @subtypes >> are subtypes of C<< $type >>.
=item C<< EXTENDED_TESTING >>
Exportable boolean constant.
=item C<< matchfor(@things) >>
Assistant for matching exceptions. Not exported by default.
See also L<Test::Fatal::matchfor>.
=back
=head1 ENVIRONMENT
If the C<EXTENDED_TESTING> environment variable is set to true, this
module will promote each C<should_pass> or C<should_fail> test into a
subtest block and test the type constraint in both an inlined and
non-inlined manner.
This variable must be set at compile time (i.e. before this module is
loaded).
=head1 BUGS
Please report any bugs to
L<http://rt.cpan.org/Dist/Display.html?Queue=Type-Tiny>.
=head1 SEE ALSO
L<Type::Tiny>.
For an alternative to C<should_pass>, see L<Test::Deep::Type> which will
happily accept a Type::Tiny type constraint instead of a MooseX::Types one.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2013-2014 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=head1 DISCLAIMER OF WARRANTIES
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|