9 use Moose::Util::TypeConstraints;
10 use Scalar::Util qw( blessed openhandle );
19 my $EMPTY_STRING = q{};
21 my $NUM_IN_STRING = 'has 42 in it';
22 my $INT_WITH_NL1 = "1\n";
23 my $INT_WITH_NL2 = "\n1";
25 my $SCALAR_REF = \( my $var );
26 my $SCALAR_REF_REF = \$SCALAR_REF;
29 my $CODE_REF = sub { };
31 my $GLOB = do { no warnings 'once'; *GLOB_REF };
32 my $GLOB_REF = \$GLOB;
34 open my $FH, '<', $0 or die "Could not open $0 for the test";
36 my $FH_OBJECT = IO::File->new( $0, 'r' )
37 or die "Could not open $0 for the test";
40 my $REGEX_OBJ = bless qr/../, 'BlessedQR';
42 my $OBJECT = bless {}, 'Foo';
52 my $CLASS_NAME = 'Thing';
61 my $ROLE_NAME = 'Role';
694 for my $name ( sort keys %tests ) {
695 test_constraint( $name, $tests{$name} );
698 # We need to test that the Str constraint accepts the return val of substr() -
699 # which means passing that return val directly to the checking code
701 my $str = 'some string';
703 my $type = Moose::Util::TypeConstraints::find_type_constraint('Str');
707 ? $type->_compile_subtype( $type->constraint )
708 : $type->_compile_type( $type->constraint );
713 $inlined = eval 'sub { ( ' . $type->_inline_check('$_[0]') . ' ) }';
718 $type->check( substr( $str, 1, 3 ) ),
719 'Str accepts return val from substr using ->check'
722 $unoptimized->( substr( $str, 1, 3 ) ),
723 'Str accepts return val from substr using unoptimized constraint'
726 $inlined->( substr( $str, 1, 3 ) ),
727 'Str accepts return val from substr using inlined constraint'
731 $type->check( substr( $str, 0, 0 ) ),
732 'Str accepts empty return val from substr using ->check'
735 $unoptimized->( substr( $str, 0, 0 ) ),
736 'Str accepts empty return val from substr using unoptimized constraint'
739 $inlined->( substr( $str, 0, 0 ) ),
740 'Str accepts empty return val from substr using inlined constraint'
745 my $class_tc = class_type('Thing');
750 ( bless {}, 'Thing' ),
804 my @methods = qw( quack flap );
805 duck_type 'Duck' => @methods;
810 ( bless {}, 'Duck' ),
811 ( bless {}, 'DuckLike' ),
837 ( bless {}, 'Bird' ),
845 my @allowed = qw( bar baz quux );
846 enum 'Enumerated' => @allowed;
882 my $union = Moose::Meta::TypeConstraint::Union->new(
883 type_constraints => [
884 find_type_constraint('Int'),
885 find_type_constraint('Object'),
931 # Test how $_ is used in XS implementation
935 Moose::Util::TypeConstraints::Builtins::_RegexpRef(),
939 !Moose::Util::TypeConstraints::Builtins::_RegexpRef(1),
940 '$_ is not read when param provided'
943 $_ = bless qr/./, 'Blessed';
946 Moose::Util::TypeConstraints::Builtins::_RegexpRef(),
952 !Moose::Util::TypeConstraints::Builtins::_RegexpRef(),
953 '$_ is not RegexpRef'
956 Moose::Util::TypeConstraints::Builtins::_RegexpRef(qr/./),
957 '$_ is not read when param provided'
962 or warn "Could not close the filehandle $0 for test";
964 or warn "Could not close the filehandle $0 for test";
968 sub test_constraint {
972 local $Test::Builder::Level = $Test::Builder::Level + 1;
974 unless ( blessed $type ) {
975 $type = Moose::Util::TypeConstraints::find_type_constraint($type)
976 or BAIL_OUT("No such type $type!");
979 my $name = $type->name;
983 ? $type->_compile_subtype( $type->constraint )
984 : $type->_compile_type( $type->constraint );
987 if ( $type->has_inlined_type_constraint ) {
989 $inlined = eval 'sub { ( ' . $type->_inline_check('$_[0]') . ' ) }';
993 for my $accept ( @{ $tests->{accept} || [] } ) {
994 my $described = describe($accept);
996 $type->check($accept),
997 "$name accepts $described using ->check"
1000 $unoptimized->($accept),
1001 "$name accepts $described using unoptimized constraint"
1005 $inlined->($accept),
1006 "$name accepts $described using inlined constraint"
1011 for my $reject ( @{ $tests->{reject} || [] } ) {
1012 my $described = describe($reject);
1014 !$type->check($reject),
1015 "$name rejects $described using ->check"
1018 !$unoptimized->($reject),
1019 "$name rejects $described using unoptimized constraint"
1023 !$inlined->($reject),
1024 "$name rejects $described using inlined constraint"
1033 return 'undef' unless defined $val;
1036 return q{''} if $val eq q{};
1043 return 'open filehandle'
1044 if openhandle $val && !blessed $val;
1047 ? ( ref $val ) . ' object'
1048 : ( ref $val ) . ' reference';