6 eval "use Set::Scalar"; if($@) {
7 plan skip_all => 'Set::Scalar not installed';
12 package Test::MooseX::Types::Parameterizable::Synopsis;
15 use MooseX::Types::Parameterizable qw(Parameterizable);
16 use MooseX::Types::Moose qw(Str Int);
17 use MooseX::Types -declare=>[qw(Varchar)];
19 ## Create a type constraint that is a string but parameterizes an integer
20 ## that is used as a maximum length constraint on that string, similar to
21 ## an SQL Varchar type.
24 as Parameterizable[Str,Int],
26 my($string, $int) = @_;
27 $int >= length($string) ? 1:0;
29 message { "'$_' is too long" };
31 my $varchar_five = Varchar[5];
33 Test::More::ok $varchar_five->check('four');
34 Test::More::ok ! $varchar_five->check('verylongstrong');
36 my $varchar_ten = Varchar[10];
38 Test::More::ok $varchar_ten->check( 'X' x 9 );
39 Test::More::ok ! $varchar_ten->check( 'X' x 12 );
41 has varchar_five => (isa=>Varchar[5], is=>'ro');
42 has varchar_ten => (isa=>Varchar[10], is=>'ro');
44 my $object1 = __PACKAGE__->new(
45 varchar_five => '1234',
46 varchar_ten => '123456789',
50 my $object2 = __PACKAGE__->new(
51 varchar_five => '12345678',
52 varchar_ten => '123456789',
56 Test::More::ok $@, 'There was an error';
57 Test::More::like $@, qr('12345678' is too long), 'Correct custom error';
65 use MooseX::Types -declare=>[qw(Set UniqueInt PositiveSet PositiveUniqueInt )];
68 as class_type("Set::Scalar");
71 as Parameterizable[Int, Set],
81 ! grep { $_ < 0 } $set->members;
84 subtype PositiveUniqueInt,
85 as UniqueInt[PositiveSet];
87 my $set = Set::Scalar->new(-1,-2,1,2,3);
88 my $positive_set = Set::Scalar->new(1,2,3);
89 my $negative_set = Set::Scalar->new(-1,-2,-3);
94 ok Set->check($positive_set),
97 ok Set->check($negative_set),
100 ok !PositiveSet->check($set),
101 'Is Not a Positive Set';
103 ok PositiveSet->check($positive_set),
106 ok !PositiveSet->check($negative_set),
107 'Is Not a Positive Set';
109 ok UniqueInt([$set])->check(100),
112 ok UniqueInt([$positive_set])->check(100),
115 ok UniqueInt([$negative_set])->check(100),
118 ok UniqueInt([$set])->check(-99),
121 ok UniqueInt([$positive_set])->check(-99),
124 ok UniqueInt([$negative_set])->check(-99),
127 ok !UniqueInt([$set])->check(2),
130 ok !UniqueInt([$positive_set])->check(2),
133 ok UniqueInt([$negative_set])->check(2),
139 ok UniqueInt([$set])->check(100); ## Okay, 100 isn't in (1,2,3)
140 ok UniqueInt([$set])->check(-99); ## Okay, -99 isn't in (1,2,3)
141 ok !UniqueInt([$set])->check(2); ## Not OK, 2 is in (1,2,3)
143 ok PositiveUniqueInt([$set])->check(100); ## Okay, 100 isn't in (1,2,3)
144 ok !PositiveUniqueInt([$set])->check(-99); ## Not OK, -99 not Positive Int
145 ok !PositiveUniqueInt([$set])->check(2); ## Not OK, 2 is in (1,2,3)
147 my $negative_set = Set::Scalar->new(-1,-2,-3);
149 ok UniqueInt([$negative_set])->check(100); ## Throws exception