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