maybe a real fix for coercions?
[gitmo/MooseX-Dependent.git] / t / 05-pod-examples.t
CommitLineData
1fa27116 1use strict;
2use warnings;
3
4use Test::More;
5
1fa27116 6{
7 package Test::MooseX::Types::Parameterizable::Synopsis;
8
9 use Moose;
10 use MooseX::Types::Parameterizable qw(Parameterizable);
2d8c1bf6 11 use MooseX::Types::Moose qw(Str Int ArrayRef);
1fa27116 12 use MooseX::Types -declare=>[qw(Varchar)];
13
3b297820 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.
17
1fa27116 18 subtype Varchar,
19 as Parameterizable[Str,Int],
20 where {
21 my($string, $int) = @_;
22 $int >= length($string) ? 1:0;
23 },
24 message { "'$_' is too long" };
25
2d8c1bf6 26 coerce Varchar,
27 from ArrayRef,
28 via {
29 my ($arrayref, $int) = @_;
30 join('', @$arrayref);
31 };
32
1fa27116 33 my $varchar_five = Varchar[5];
34
35 Test::More::ok $varchar_five->check('four');
36 Test::More::ok ! $varchar_five->check('verylongstrong');
37
38 my $varchar_ten = Varchar[10];
39
40 Test::More::ok $varchar_ten->check( 'X' x 9 );
41 Test::More::ok ! $varchar_ten->check( 'X' x 12 );
42
2d8c1bf6 43 has varchar_five => (isa=>$varchar_five, is=>'ro', coerce=>1);
1fa27116 44 has varchar_ten => (isa=>Varchar[10], is=>'ro');
45
46 my $object1 = __PACKAGE__->new(
47 varchar_five => '1234',
48 varchar_ten => '123456789',
49 );
50
51 eval {
52 my $object2 = __PACKAGE__->new(
53 varchar_five => '12345678',
54 varchar_ten => '123456789',
55 );
56 };
57
58 Test::More::ok $@, 'There was an error';
59 Test::More::like $@, qr('12345678' is too long), 'Correct custom error';
60}
61
62done_testing;
63
64
65__END__
66
67use MooseX::Types -declare=>[qw(Set UniqueInt PositiveSet PositiveUniqueInt )];
68
69subtype Set,
70 as class_type("Set::Scalar");
71
72subtype UniqueInt,
73 as Parameterizable[Int, Set],
74 where {
75 my ($int, $set) = @_;
76 !$set->has($int);
77 };
78
79subtype PositiveSet,
80 as Set,
81 where {
82 my ($set) = @_;
83 ! grep { $_ < 0 } $set->members;
84 };
85
86subtype PositiveUniqueInt,
87 as UniqueInt[PositiveSet];
88
89my $set = Set::Scalar->new(-1,-2,1,2,3);
90my $positive_set = Set::Scalar->new(1,2,3);
91my $negative_set = Set::Scalar->new(-1,-2,-3);
92
93ok Set->check($set),
94 'Is a Set';
95
96ok Set->check($positive_set),
97 'Is a Set';
98
99ok Set->check($negative_set),
100 'Is a Set';
101
102ok !PositiveSet->check($set),
103 'Is Not a Positive Set';
104
105ok PositiveSet->check($positive_set),
106 'Is a Positive Set';
107
108ok !PositiveSet->check($negative_set),
109 'Is Not a Positive Set';
110
111ok UniqueInt([$set])->check(100),
112 '100 not in Set';
113
114ok UniqueInt([$positive_set])->check(100),
115 '100 not in Set';
116
117ok UniqueInt([$negative_set])->check(100),
118 '100 not in Set';
119
120ok UniqueInt([$set])->check(-99),
121 '-99 not in Set';
122
123ok UniqueInt([$positive_set])->check(-99),
124 '-99 not in Set';
125
126ok UniqueInt([$negative_set])->check(-99),
127 '-99 not in Set';
128
129ok !UniqueInt([$set])->check(2),
130 '2 in Set';
131
132ok !UniqueInt([$positive_set])->check(2),
133 '2 in Set';
134
135ok UniqueInt([$negative_set])->check(2),
136 '2 not in Set';
137
138
139__END__
140
141ok UniqueInt([$set])->check(100); ## Okay, 100 isn't in (1,2,3)
142ok UniqueInt([$set])->check(-99); ## Okay, -99 isn't in (1,2,3)
143ok !UniqueInt([$set])->check(2); ## Not OK, 2 is in (1,2,3)
144
145ok PositiveUniqueInt([$set])->check(100); ## Okay, 100 isn't in (1,2,3)
146ok !PositiveUniqueInt([$set])->check(-99); ## Not OK, -99 not Positive Int
147ok !PositiveUniqueInt([$set])->check(2); ## Not OK, 2 is in (1,2,3)
148
149my $negative_set = Set::Scalar->new(-1,-2,-3);
150
151ok UniqueInt([$negative_set])->check(100); ## Throws exception
152