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
|
=head1 PURPOSE
Check that the required (C<< ! >>) postfix sigil works, and that the
scalar ((C<< $ >>), array (C<< @ >>) and hash (C<< % >>) prefix sigils
work.
Check that the C<< + >> postfix sigil works, that numbers can default to
values other than zero, and that an explicit C<isa> works.
Make sure that sigils are just hints, and can be overridden by an explicit
attribute spec.
Checks that attribute specs can be hashrefs or arrayrefs.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2012 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 Test::More;
use Scalar::Util qw( looks_like_number );
use MooX::Struct
Structure => [
qw( $value %dict @list ),
'@value2' => { isa => sub { die if ref $_[0] } },
'%list2' => [ isa => sub { die unless ref $_[0] eq 'ARRAY' } ],
'$dict2' => [ isa => sub { die unless ref $_[0] eq 'HASH' } ],
],
OtherStructure => [qw( id! ego )],
Point => ['+x', '+y' => [default => sub { 101 }]],
Point3D => [-extends => ['Point'], '+z' => [isa => sub { die unless looks_like_number($_[0]) || !defined $_[0] }]],
PointReq => ['+x!', '+y!'],
;
ok eval {
Structure->new( value => Structure->new )
};
ok eval {
Structure->new( value => 42 )
};
ok eval {
Structure->new( list => [] )
};
ok eval {
Structure->new( dict => +{} )
};
ok eval {
Structure->new( value2 => "Hello World" );
};
ok eval {
Structure->new( list2 => [] );
};
ok eval {
Structure->new( dict2 => {foo => 42} );
};
ok !eval {
Structure->new( value => [] )
};
ok !eval {
Structure->new( value => +{} )
};
ok !eval {
Structure->new( list => 42 )
};
ok !eval {
Structure->new( dict => 42 )
};
ok !eval {
Structure->new( value2 => [] );
};
ok !eval {
Structure->new( list2 => +{} );
};
ok !eval {
Structure->new( dict2 => 42 );
};
ok eval {
OtherStructure->new(id => undef);
};
ok !eval {
OtherStructure->new(ego => undef);
};
my $point = Point->new;
ok defined $point->x;
ok defined $point->y;
is($point->x, 0);
is($point->y, 101);
ok eval {
Point[ 42, 42 ];
Point[ 42.1, 42.2 ];
Point[ "99", "999" ];
Point[ "+Inf", "-Inf" ];
};
ok not eval {
Point[ "Hello", "World" ];
};
ok not eval {
Point[ "", "" ];
};
ok not eval {
Point[ "Hello", "99" ];
};
ok eval {
Point3D[ 1, 2 ];
Point3D[ 1, 2, 3 ];
Point3D[ 1, 2, undef ];
};
is_deeply(
Point3D->new->TO_ARRAY,
[ 0, 101, 0 ],
);
ok not eval {
Point3D[ 1, 2, "Hello" ];
};
ok eval {
PointReq[ 1, 2 ];
PointReq[ 0, '-Inf' ];
PointReq[ 0, 0 ];
};
ok not eval {
PointReq[ ];
};
ok not eval {
PointReq[ "abc", 0 ];
};
done_testing();
|