1=pod 2 3=encoding utf-8 4 5=head1 PURPOSE 6 7Tests constraints for L<Types::Common::Numeric>'s 8C<IntRange> and C<NumRange>. 9 10=head1 AUTHOR 11 12Toby Inkster. 13 14=head1 COPYRIGHT AND LICENCE 15 16This software is copyright (c) 2018-2021 by Toby Inkster. 17 18This is free software; you can redistribute it and/or modify it under 19the same terms as the Perl 5 programming language system itself. 20 21=cut 22 23use strict; 24use warnings FATAL => 'all'; 25use Test::More; 26use Test::TypeTiny qw( -all ); 27use Test::Fatal; 28 29BEGIN { 30 plan skip_all => "https://github.com/perl11/cperl/issues/409" if "$^V" =~ /c$/; 31}; 32 33use Types::Common::Numeric -all; 34 35should_fail($_, IntRange[10,15]) for -19 .. +9; 36should_pass($_, IntRange[10,15]) for 10 .. 15; 37should_fail($_, IntRange[10,15]) for 16 .. 20; 38 39should_fail($_ + 0.5, IntRange[10,15]) for -9 .. 20; 40 41should_fail($_, IntRange[10,15]) for ([], {}, sub { 3 }, "hello world"); 42 43should_fail($_, IntRange[10]) for -19 .. 9; 44should_pass($_, IntRange[10]) for 10 .. 24, 1000, 1_000_000; 45 46########### 47 48should_fail($_, NumRange[10,15]) for -19 .. +9; 49should_pass($_, NumRange[10,15]) for 10 .. 15; 50should_fail($_, NumRange[10,15]) for 16 .. 20; 51 52should_fail($_ + 0.5, NumRange[10,15]) for -9 .. 9; 53should_pass($_ + 0.5, NumRange[10,15]) for 10 .. 14; 54should_fail($_ + 0.5, NumRange[10,15]) for 15 .. 20; 55 56should_fail($_, NumRange[10,15]) for ([], {}, sub { 3 }, "hello world"); 57 58should_fail($_, NumRange[10]) for -19 .. 9; 59should_pass($_, NumRange[10]) for 10 .. 24, 1000, 1_000_000; 60 61########### 62 63should_fail( '9.99', NumRange[10,15,0,0] ); 64should_pass( '10.00', NumRange[10,15,0,0] ); 65should_pass( '10.01', NumRange[10,15,0,0] ); 66should_pass( '12.50', NumRange[10,15,0,0] ); 67should_pass( '14.99', NumRange[10,15,0,0] ); 68should_pass( '15.00', NumRange[10,15,0,0] ); 69should_fail( '15.01', NumRange[10,15,0,0] ); 70 71should_fail( '9.99', NumRange[10,15,1,0] ); 72should_fail( '10.00', NumRange[10,15,1,0] ); 73should_pass( '10.01', NumRange[10,15,1,0] ); 74should_pass( '12.50', NumRange[10,15,1,0] ); 75should_pass( '14.99', NumRange[10,15,1,0] ); 76should_pass( '15.00', NumRange[10,15,1,0] ); 77should_fail( '15.01', NumRange[10,15,1,0] ); 78 79should_fail( '9.99', NumRange[10,15,0,1] ); 80should_pass( '10.00', NumRange[10,15,0,1] ); 81should_pass( '10.01', NumRange[10,15,0,1] ); 82should_pass( '12.50', NumRange[10,15,0,1] ); 83should_pass( '14.99', NumRange[10,15,0,1] ); 84should_fail( '15.00', NumRange[10,15,0,1] ); 85should_fail( '15.01', NumRange[10,15,0,1] ); 86 87should_fail( '9.99', NumRange[10,15,1,1] ); 88should_fail( '10.00', NumRange[10,15,1,1] ); 89should_pass( '10.01', NumRange[10,15,1,1] ); 90should_pass( '12.50', NumRange[10,15,1,1] ); 91should_pass( '14.99', NumRange[10,15,1,1] ); 92should_fail( '15.00', NumRange[10,15,1,1] ); 93should_fail( '15.01', NumRange[10,15,1,1] ); 94 95########### 96 97should_pass(1, IntRange); 98should_fail($_, IntRange) for ([], {}, sub { 3 }, "hello world", '1.2345'); 99 100should_pass(1, NumRange); 101should_fail($_, NumRange) for ([], {}, sub { 3 }, "hello world"); 102should_pass('1.2345', NumRange); 103 104########### 105 106foreach my $test ( 107 [NumRange, [{}, 5], qr/NumRange min must be a num/, "NumRange non-numeric min"], 108 [NumRange, [5, {}], qr/NumRange max must be a num/, "NumRange non-numeric max"], 109 [NumRange, [5, 10, {}], qr/NumRange minexcl must be a boolean/, "NumRange non-boolean minexcl"], 110 [NumRange, [5, 10, 0, {}], qr/NumRange maxexcl must be a boolean/, "NumRange non-boolean maxexcl"], 111 [NumRange, [{}, {}], qr/NumRange min must be a num/, "NumRange non-numeric min and max"], 112 [IntRange, [{}, 5], qr/IntRange min must be a int/, "IntRange non-numeric min"], 113 [IntRange, [5, {}], qr/IntRange max must be a int/, "IntRange non-numeric max"], 114 [IntRange, [5, 10, {}], qr/IntRange minexcl must be a boolean/, "IntRange non-boolean minexcl"], 115 [IntRange, [5, 10, 0, {}], qr/IntRange maxexcl must be a boolean/, "IntRange non-boolean maxexcl"], 116 [IntRange, [{}, {}], qr/IntRange min must be a int/, "IntRange non-numeric min and max"], 117 [IntRange, [1.1, 5], qr/IntRange min must be a int/, "IntRange non-integer min"], 118 [IntRange, [5, 9.9], qr/IntRange max must be a int/, "IntRange non-integer max"], 119) { 120 my ($base, $params, $qr, $desc) = @$test; 121 my $e = exception { $base->of(@$params) }; 122 like($e, $qr, "Exception thrown for $desc"); 123} 124 125done_testing; 126