Dios

 view release on metacpan or  search on metacpan

t/types_basic.t  view on Meta::CPAN

{
        package Local::Class1;
        use strict;
}

{
        no warnings 'once';
        $Local::Class2::VERSION = 0.001;
        @Local::Class3::ISA     = qw(UNIVERSAL);
        @Local::Dummy1::FOO     = qw(UNIVERSAL);
}

{
        package Local::Class4;
        sub XYZ () { 1 }
}

type_fail( undef            , 'Class'             );
type_fail( []               , 'Class'             );
type_okay( "Local::Class$_" , 'Class'             ) for 2..4;
type_fail( "Local::Dummy1"  , 'Class'             );
type_okay( []               , 'Array[Int]'        );
type_okay( [1,2,3]          , 'Array[Int]'        );
type_fail( [1.1, 2,3]       , 'Array[Int]'        );
type_fail( [1,2,3.1]        , 'Array[Int]'        );
type_fail( [[]]             , 'Array[Int]'        );
type_okay( [[3]]            , 'Array[Array[Int]]' );
type_fail( [["A"]]          , 'Array[Array[Int]]' );

my $deep = 'Array[Hash[Array[Hash[Int]]]]';

type_okay( [{foo1=>[{bar=>1}]},{foo2=>[{baz=>2}]}], $deep );
type_okay( [{foo1=>[{bar=>1}]},{foo2=>[]}],         $deep );
type_fail( [{foo1=>[{bar=>1}]},{foo2=>[2]}],        $deep );

type_okay(  undef, 'Int|Undef');
type_okay(  123,   'Int|Undef');
type_fail(  1.3,   'Int|Undef');

my $i = 1;
my $f = 1.1;
my $s = "Hello";
type_okay( \$s, 'Ref[Value]' );
type_okay( \$f, 'Ref[Value]' );
type_okay( \$i, 'Ref[Value]' );
type_okay( \$s, 'Ref[Str]' );
type_okay( \$f, 'Ref[Str]' );
type_okay( \$i, 'Ref[Str]' );
type_fail( \$s, 'Ref[Num]' );
type_okay( \$f, 'Ref[Num]' );
type_okay( \$i, 'Ref[Num]' );
type_fail( \$s, 'Ref[Int]' );
type_fail( \$f, 'Ref[Int]' );
type_okay( \$i, 'Ref[Int]' );

SKIP: {
        skip "requires Perl 5.8", 3 if $] < 5.008;

        type_okay( "Inf",  'Num');
        type_okay( "-Inf", 'Num');
        type_fail( "NaN",  'Num');

        type_okay( "Inf",  'Int');
        type_okay( "-Inf", 'Int');
        type_fail( "NaN",  'Int');
}

my @VALID_NUMS = qw(
      0      0e123        0e+123        0e-123
     +0     +0e123       +0e+123       +0e-123
     -0     -0e123       -0e+123       -0e-123

      0.12   0.12e123     0.12e+123     0.12e-123
     +0.12  +0.12e123    +0.12e+123    +0.12e-123
     -0.12  -0.12e123    -0.12e+123    -0.12e-123

      0.     0.e123       0.e+123       0.e-123
     +0.    +0.e123      +0.e+123      +0.e-123
     -0.    -0.e123      -0.e+123      -0.e-123

     .0     .0e123       .0e+123       .0e-123
    +.0    +.0e123      +.0e+123      +.0e-123
    -.0    -.0e123      -.0e+123      -.0e-123

     1234567890123456789012345678901234567890
    +1234567890123456789012345678901234567890
    -1234567890123456789012345678901234567890
);

for my $num (@VALID_NUMS) {
    type_okay( $num,      'Num' );
    type_fail( "a$num",   'Num' );
    type_fail( "${num}a", 'Num' );
}

type_okay( { a => 1, e => 2, u => 3 },  'Hash[Match[^[aeiou]$] => Int]');
type_okay( { a => 1, e => 2, u => [] }, 'Hash[Match[^[aeiou]$] => Int|Array]');
type_fail( { a => 1, e => 2, v => 3 },  'Hash[Match[^[aeiou]$] => Int]');
type_fail( { a => 1, e => 2, u => [] }, 'Hash[Match[^[aeiou]$] => Int]');

type_okay( { a => 1, bb => 2, ccc => 3 }, 'Hash[Not[Empty]=>Int]');
type_fail( { a => 1, bb => 2, q{} => 3 }, 'Hash[Not[Empty]=>Int]');

type_okay( { 'Dios::Types' => undef },     'Hash[Class=>Undef]');
type_fail( { 'Bios::Hypes' => undef },     'Hash[Class=>Undef]');


type_okay( 'Dios::Types', 'Can[validate]');
type_okay( 'Dios::Types', 'Class & Can[validate]');
type_okay( 'Dios::Types', 'Can[ validate , import ]');
type_fail( 'Dios::Types', 'Can[validate, export]');


{ package Overloaded; use overload '+' => sub{}, q{""} => sub{}, '%{}' => sub{}; }

type_okay( 'Overloaded', 'Overloads[+]' );
type_fail( 'Overloaded', 'Overloads[-]' );

type_okay( 'Overloaded', 'Overloads[ +, "", %{} ]' );
type_fail( 'Overloaded', 'Overloads[ +, "", %{}, -- ]' );

type_okay( 'Overloaded', 'Overloads[ +, "", %{} ] & Class' );

done_testing;



( run in 0.755 second using v1.01-cache-2.11-cpan-39bf76dae61 )