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[5], 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';
61 my $object3 = __PACKAGE__->new(
62 varchar_five => [qw/aa bb/],
63 varchar_ten => '123456789',
66 Test::More::is $object3->varchar_five, 'aabb',
67 'coercion as expected';
75 use MooseX::Types -declare=>[qw(Set UniqueInt PositiveSet PositiveUniqueInt )];
78 as class_type("Set::Scalar");
81 as Parameterizable[Int, Set],
91 ! grep { $_ < 0 } $set->members;
94 subtype PositiveUniqueInt,
95 as UniqueInt[PositiveSet];
97 my $set = Set::Scalar->new(-1,-2,1,2,3);
98 my $positive_set = Set::Scalar->new(1,2,3);
99 my $negative_set = Set::Scalar->new(-1,-2,-3);
104 ok Set->check($positive_set),
107 ok Set->check($negative_set),
110 ok !PositiveSet->check($set),
111 'Is Not a Positive Set';
113 ok PositiveSet->check($positive_set),
116 ok !PositiveSet->check($negative_set),
117 'Is Not a Positive Set';
119 ok UniqueInt([$set])->check(100),
122 ok UniqueInt([$positive_set])->check(100),
125 ok UniqueInt([$negative_set])->check(100),
128 ok UniqueInt([$set])->check(-99),
131 ok UniqueInt([$positive_set])->check(-99),
134 ok UniqueInt([$negative_set])->check(-99),
137 ok !UniqueInt([$set])->check(2),
140 ok !UniqueInt([$positive_set])->check(2),
143 ok UniqueInt([$negative_set])->check(2),
149 ok UniqueInt([$set])->check(100); ## Okay, 100 isn't in (1,2,3)
150 ok UniqueInt([$set])->check(-99); ## Okay, -99 isn't in (1,2,3)
151 ok !UniqueInt([$set])->check(2); ## Not OK, 2 is in (1,2,3)
153 ok PositiveUniqueInt([$set])->check(100); ## Okay, 100 isn't in (1,2,3)
154 ok !PositiveUniqueInt([$set])->check(-99); ## Not OK, -99 not Positive Int
155 ok !PositiveUniqueInt([$set])->check(2); ## Not OK, 2 is in (1,2,3)
157 my $negative_set = Set::Scalar->new(-1,-2,-3);
159 ok UniqueInt([$negative_set])->check(100); ## Throws exception