App-Test-Generator
view release on metacpan or search on metacpan
lib/App/Test/Generator/Template.pm view on Meta::CPAN
if(!defined $spec->{max}) {
push @cases, { %{$mandatory_args}, ( $arg_name => int($min + abs(rand_int())) ), _LINE => __LINE__ };
}
if($min <= 0) {
push @cases, { %{$mandatory_args}, ( $arg_name => -0.0, _STATUS => 'OK' ) }; # Negative 0
}
} else {
push @cases,
{ %{$mandatory_args}, ( $arg_name => 1e5, _STATUS => 'OK' ) }, # Scientific notation
{ %{$mandatory_args}, ( $arg_name => 1_000, _STATUS => 'OK' ) }, # Underscored
{ %{$mandatory_args}, ( $arg_name => ' 42 ', _STATUS => 'OK' ) }; # Padded integer
}
if (defined $spec->{max}) {
my $max = $spec->{max};
push @cases,
{ %{$mandatory_args}, ( $arg_name => $max - 1 ) },
{ %{$mandatory_args}, ( $arg_name => $max ) },
{ %{$mandatory_args}, ( $arg_name => $max + 1, _STATUS => 'DIES' ) };
if(defined $spec->{min}) {
# Test 0 if it's in range
push @cases, { %{$mandatory_args}, ( $arg_name => 0 ) } if($spec->{'min'} >= 0);
} else {
push @cases, { %{$mandatory_args}, ( $arg_name => $max - rand_int() ), _DESCRIPTION => 'max is defined but min is not' };
if($max >= 0) {
push @cases, { %{$mandatory_args}, ( $arg_name => -0.0, _STATUS => 'OK' ) }; # Negative 0
if($max == 0) {
push @cases, { %{$mandatory_args}, ( $arg_name => abs(rand_int()) * -1 ), _LINE => __LINE__ }; # Any negative integer
}
}
}
} elsif(!defined $spec->{min}) {
# Can take any number, so give it one
push @cases,
{ %{$mandatory_args}, ( $arg_name => rand_int() ), _LINE => __LINE__ },
{ %{$mandatory_args}, ( $arg_name => 0) }; # 0 is in range
}
return \@cases;
}
sub _generate_float_cases {
my ($arg_name, $spec, $mandatory_args) = @_;
my @cases;
if((!defined $spec->{min}) || ($spec->{min} <= -0.1)) {
push @cases, { %{$mandatory_args}, ( $arg_name => -0.1, _LINE => __LINE__ ) };
}
if((!defined $spec->{min}) || ($spec->{min} <= 43.56)) {
push @cases, { %{$mandatory_args}, ( $arg_name => 43.56 ) };
}
[% IF module %]
# Send wrong data type - builtins aren't good at checking this
push @cases,
{ %{$mandatory_args}, ( $arg_name => "test string in float field $arg_name", _STATUS => 'DIES', _LINE => __LINE__ ) },
{ %{$mandatory_args}, ( $arg_name => {}, _STATUS => 'DIES', _LINE => __LINE__ ) },
{ %{$mandatory_args}, ( $arg_name => \42.1, _STATUS => 'DIES' ) }, # Scalar ref
# NaN and Inf are valid according to looks_like_number() so we
# cannot assume they die
# { %{$mandatory_args}, ( $arg_name => "NaN", _STATUS => 'DIES' ) },
{ %{$mandatory_args}, ( $arg_name => [], _STATUS => 'DIES', _LINE => __LINE__ ) };
[% END %]
# min/max numeric boundaries
if (defined $spec->{min}) {
my $min = $spec->{min};
push @cases,
{ %{$mandatory_args}, ( $arg_name => $min - 0.001, _STATUS => 'DIES' ) },
{ %{$mandatory_args}, ( $arg_name => $min, _LINE => __LINE__ ) }, # border
{ %{$mandatory_args}, ( $arg_name => $min + 0.001 ) }; # just inside
[% IF module %]
push @cases, { %{$mandatory_args}, ( $arg_name => "-inf", _STATUS => 'DIES' ) };
[% END %]
if(!defined $spec->{max}) {
push @cases, { %{$mandatory_args}, ( $arg_name => $min + rand_num() ), _DESCRIPTION => 'min is defined but max is not' };
if($min == 0) {
push @cases, { %{$mandatory_args}, ( $arg_name => abs(rand_num()) ), _LINE => __LINE__ }; # Any positive number
}
}
}
if (defined $spec->{max}) {
my $max = $spec->{max};
push @cases,
{ %{$mandatory_args}, ( $arg_name => $max - 0.000001 ) },
{ %{$mandatory_args}, ( $arg_name => $max ) },
{ %{$mandatory_args}, ( $arg_name => $max + 0.000001, _STATUS => 'DIES' ) };
[% IF module %]
push @cases,
{ %{$mandatory_args}, ( $arg_name => "inf", _STATUS => 'DIES' ) },
{ %{$mandatory_args}, ( $arg_name => 9**9**9, _STATUS => 'DIES' ) };
[% END %]
if(defined $spec->{min}) {
# Test 0 if it's in range
push @cases, { %{$mandatory_args}, ( $arg_name => 0 ) } if($spec->{'min'} >= 0);
} else {
push @cases, { %{$mandatory_args}, ( $arg_name => $max - rand_num() ), _LINE => __LINE__ };
if($max == 0) {
push @cases, { %{$mandatory_args}, ( $arg_name => abs(rand_num()) * -0.00000001 ) }; # Any negative number
}
}
} elsif(!defined $spec->{min}) {
# Can take any number, so give it some
push @cases,
{ %{$mandatory_args}, ( $arg_name => rand_num(), _LINE => __LINE__ ) },
{ %{$mandatory_args}, ( $arg_name => 1.23 ) },
{ %{$mandatory_args}, ( $arg_name => -42.1 ) },
{ %{$mandatory_args}, ( $arg_name => -0.0 ) }, # -0 is in range
{ %{$mandatory_args}, ( $arg_name => 0 ) }; # 0 is in range
}
return \@cases;
}
# basic boolean edge cases
lib/App/Test/Generator/Template.pm view on Meta::CPAN
}
sub _fill_foundation
{
my $foundation;
foreach my $field (keys %input) {
my $spec = $input{$field} || {};
my $type = $spec->{type} || 'string';
if(($type eq 'number') || ($type eq 'float')) {
if(defined $spec->{min}) {
if(defined $spec->{max}) {
$foundation->{$field} = $spec->{max}; # border
} else {
$foundation->{$field} = rand_num() + $spec->{'min'};
}
} else {
if(defined $spec->{max}) {
$foundation->{$field} = $spec->{max}; # border
} else {
$foundation->{$field} = -0.01; # No min, so -0.01 should be allowable
}
}
} elsif($type eq 'string') {
if(defined $spec->{min} && $spec->{min} > 0) {
$foundation->{$field} = rand_str($spec->{min});
} elsif(defined $spec->{max} && $spec->{max} > 0) {
$foundation->{$field} = rand_str($spec->{max});
} else {
$foundation->{$field} = 'test_value';
}
} elsif ($type eq 'integer') {
if (defined $spec->{min}) {
$foundation->{$field} = $spec->{min};
} elsif (defined $spec->{max}) {
$foundation->{$field} = rand_int() + $spec->{max};
} else {
$foundation->{$field} = rand_int();
}
} elsif ($type eq 'boolean') {
$foundation->{$field} = 1;
} elsif ($type eq 'arrayref') {
$foundation->{$field} = rand_arrayref(defined($spec->{'min'}) ? $spec->{'min'} : ($spec->{'max'} // 5));
} elsif ($type eq 'hashref') {
$foundation->{$field} = { key => 'value' };
} else {
die("TODO: transform type $type for foundation");
}
}
return $foundation;
}
[% IF use_properties %]
# ============================================================
# Property-Based Transform Tests (Test::LectroTest)
# ============================================================
use Test::LectroTest::Compat;
use Test::LectroTest::Generator qw(:common);
use Scalar::Util qw(looks_like_number);
diag('Run property-based transform tests') if($ENV{'TEST_VERBOSE'});
[% transform_properties_code %]
[% END %]
[% corpus_code %]
done_testing();
1;
package MyTestPackage;
sub new { return bless {}, 'MyTestPackage' }
1;
__END__
( run in 0.637 second using v1.01-cache-2.11-cpan-39bf76dae61 )