7 package Test::MooseX::Types::Parameterizable::Synopsis;
10 use MooseX::Types::Parameterizable qw(Parameterizable);
11 use MooseX::Types::Moose qw(Str Int ArrayRef);
12 use MooseX::Types -declare=>[qw(Varchar)];
14 ## Create a type constraint that is a string but parameterizes an integer
15 ## that is used as a maximum length constraint on that string, similar to
16 ## an SQL Varchar type.
19 as Parameterizable[Str,Int],
21 my($string, $int) = @_;
22 $int >= length($string) ? 1:0;
24 message { "'$_' is too long" };
29 my ($arrayref, $int) = @_;
33 my $varchar_five = Varchar[5];
35 Test::More::ok $varchar_five->check('four');
36 Test::More::ok ! $varchar_five->check('verylongstrong');
38 my $varchar_ten = Varchar[10];
40 Test::More::ok $varchar_ten->check( 'X' x 9 );
41 Test::More::ok ! $varchar_ten->check( 'X' x 12 );
43 has varchar_five => (isa=>$varchar_five, is=>'ro', coerce=>1);
44 has varchar_ten => (isa=>Varchar[10], is=>'ro');
46 my $object1 = __PACKAGE__->new(
47 varchar_five => '1234',
48 varchar_ten => '123456789',
52 my $object2 = __PACKAGE__->new(
53 varchar_five => '12345678',
54 varchar_ten => '123456789',
58 Test::More::ok $@, 'There was an error';
59 Test::More::like $@, qr('12345678' is too long), 'Correct custom error';
67 use MooseX::Types -declare=>[qw(Set UniqueInt PositiveSet PositiveUniqueInt )];
70 as class_type("Set::Scalar");
73 as Parameterizable[Int, Set],
83 ! grep { $_ < 0 } $set->members;
86 subtype PositiveUniqueInt,
87 as UniqueInt[PositiveSet];
89 my $set = Set::Scalar->new(-1,-2,1,2,3);
90 my $positive_set = Set::Scalar->new(1,2,3);
91 my $negative_set = Set::Scalar->new(-1,-2,-3);
96 ok Set->check($positive_set),
99 ok Set->check($negative_set),
102 ok !PositiveSet->check($set),
103 'Is Not a Positive Set';
105 ok PositiveSet->check($positive_set),
108 ok !PositiveSet->check($negative_set),
109 'Is Not a Positive Set';
111 ok UniqueInt([$set])->check(100),
114 ok UniqueInt([$positive_set])->check(100),
117 ok UniqueInt([$negative_set])->check(100),
120 ok UniqueInt([$set])->check(-99),
123 ok UniqueInt([$positive_set])->check(-99),
126 ok UniqueInt([$negative_set])->check(-99),
129 ok !UniqueInt([$set])->check(2),
132 ok !UniqueInt([$positive_set])->check(2),
135 ok UniqueInt([$negative_set])->check(2),
141 ok UniqueInt([$set])->check(100); ## Okay, 100 isn't in (1,2,3)
142 ok UniqueInt([$set])->check(-99); ## Okay, -99 isn't in (1,2,3)
143 ok !UniqueInt([$set])->check(2); ## Not OK, 2 is in (1,2,3)
145 ok PositiveUniqueInt([$set])->check(100); ## Okay, 100 isn't in (1,2,3)
146 ok !PositiveUniqueInt([$set])->check(-99); ## Not OK, -99 not Positive Int
147 ok !PositiveUniqueInt([$set])->check(2); ## Not OK, 2 is in (1,2,3)
149 my $negative_set = Set::Scalar->new(-1,-2,-3);
151 ok UniqueInt([$negative_set])->check(100); ## Throws exception