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
|
=pod
=encoding utf-8
=head1 PURPOSE
Tests constraints for L<Types::Common::Numeric>'s
C<IntRange> and C<NumRange>.
=head1 AUTHOR
Toby Inkster.
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2018-2023 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.
=cut
use strict;
use warnings FATAL => 'all';
use Test::More;
use Test::TypeTiny qw( -all );
use Test::Fatal;
BEGIN {
plan skip_all => "https://github.com/perl11/cperl/issues/409" if "$^V" =~ /c$/;
};
use Types::Common::Numeric -all;
should_fail($_, IntRange[10,15]) for -19 .. +9;
should_pass($_, IntRange[10,15]) for 10 .. 15;
should_fail($_, IntRange[10,15]) for 16 .. 20;
should_fail($_ + 0.5, IntRange[10,15]) for -9 .. 20;
should_fail($_, IntRange[10,15]) for ([], {}, sub { 3 }, "hello world");
should_fail($_, IntRange[10]) for -19 .. 9;
should_pass($_, IntRange[10]) for 10 .. 24, 1000, 1_000_000;
###########
should_fail($_, NumRange[10,15]) for -19 .. +9;
should_pass($_, NumRange[10,15]) for 10 .. 15;
should_fail($_, NumRange[10,15]) for 16 .. 20;
should_fail($_ + 0.5, NumRange[10,15]) for -9 .. 9;
should_pass($_ + 0.5, NumRange[10,15]) for 10 .. 14;
should_fail($_ + 0.5, NumRange[10,15]) for 15 .. 20;
should_fail($_, NumRange[10,15]) for ([], {}, sub { 3 }, "hello world");
should_fail($_, NumRange[10]) for -19 .. 9;
should_pass($_, NumRange[10]) for 10 .. 24, 1000, 1_000_000;
###########
should_fail( '9.99', NumRange[10,15,0,0] );
should_pass( '10.00', NumRange[10,15,0,0] );
should_pass( '10.01', NumRange[10,15,0,0] );
should_pass( '12.50', NumRange[10,15,0,0] );
should_pass( '14.99', NumRange[10,15,0,0] );
should_pass( '15.00', NumRange[10,15,0,0] );
should_fail( '15.01', NumRange[10,15,0,0] );
should_fail( '9.99', NumRange[10,15,1,0] );
should_fail( '10.00', NumRange[10,15,1,0] );
should_pass( '10.01', NumRange[10,15,1,0] );
should_pass( '12.50', NumRange[10,15,1,0] );
should_pass( '14.99', NumRange[10,15,1,0] );
should_pass( '15.00', NumRange[10,15,1,0] );
should_fail( '15.01', NumRange[10,15,1,0] );
should_fail( '9.99', NumRange[10,15,0,1] );
should_pass( '10.00', NumRange[10,15,0,1] );
should_pass( '10.01', NumRange[10,15,0,1] );
should_pass( '12.50', NumRange[10,15,0,1] );
should_pass( '14.99', NumRange[10,15,0,1] );
should_fail( '15.00', NumRange[10,15,0,1] );
should_fail( '15.01', NumRange[10,15,0,1] );
should_fail( '9.99', NumRange[10,15,1,1] );
should_fail( '10.00', NumRange[10,15,1,1] );
should_pass( '10.01', NumRange[10,15,1,1] );
should_pass( '12.50', NumRange[10,15,1,1] );
should_pass( '14.99', NumRange[10,15,1,1] );
should_fail( '15.00', NumRange[10,15,1,1] );
should_fail( '15.01', NumRange[10,15,1,1] );
###########
should_pass(1, IntRange);
should_fail($_, IntRange) for ([], {}, sub { 3 }, "hello world", '1.2345');
should_pass(1, NumRange);
should_fail($_, NumRange) for ([], {}, sub { 3 }, "hello world");
should_pass('1.2345', NumRange);
###########
foreach my $test (
[NumRange, [{}, 5], qr/NumRange min must be a num/, "NumRange non-numeric min"],
[NumRange, [5, {}], qr/NumRange max must be a num/, "NumRange non-numeric max"],
[NumRange, [5, 10, {}], qr/NumRange minexcl must be a boolean/, "NumRange non-boolean minexcl"],
[NumRange, [5, 10, 0, {}], qr/NumRange maxexcl must be a boolean/, "NumRange non-boolean maxexcl"],
[NumRange, [{}, {}], qr/NumRange min must be a num/, "NumRange non-numeric min and max"],
[IntRange, [{}, 5], qr/IntRange min must be a int/, "IntRange non-numeric min"],
[IntRange, [5, {}], qr/IntRange max must be a int/, "IntRange non-numeric max"],
[IntRange, [5, 10, {}], qr/IntRange minexcl must be a boolean/, "IntRange non-boolean minexcl"],
[IntRange, [5, 10, 0, {}], qr/IntRange maxexcl must be a boolean/, "IntRange non-boolean maxexcl"],
[IntRange, [{}, {}], qr/IntRange min must be a int/, "IntRange non-numeric min and max"],
[IntRange, [1.1, 5], qr/IntRange min must be a int/, "IntRange non-integer min"],
[IntRange, [5, 9.9], qr/IntRange max must be a int/, "IntRange non-integer max"],
) {
my ($base, $params, $qr, $desc) = @$test;
my $e = exception { $base->of(@$params) };
like($e, $qr, "Exception thrown for $desc");
}
done_testing;
|