9 use Moose::Util::TypeConstraints;
11 # some simple type dispatching ...
15 => where { scalar @{$_} == 0 };
19 Null => sub { die "Cannot get the head of Null" },
20 ArrayRef => sub { $_->[0] };
25 Null => sub { die "Cannot get the tail of Null" },
26 ArrayRef => sub { [ @{ $_ }[ 1 .. $#{ $_ } ] ] };
32 ArrayRef => sub { len( tail( $_ ) ) + 1 };
38 ArrayRef => sub { [ @{ rev( tail( $_ ) ) }, head( $_ ) ] };
41 is( len( [] ), 0, '... got the right length');
42 is( len( [ 1 ] ), 1, '... got the right length');
43 is( len( [ 1 .. 5 ] ), 5, '... got the right length');
44 is( len( [ 1 .. 50 ] ), 50, '... got the right length');
49 '... got the right reversed value'
52 # break down a Maybe Type ...
58 'Undef' => sub { 'undef' },
65 is( break_it_down( 'FOO' ), 'FOO', '... got the right value');
66 is( break_it_down( [] ), 'default', '... got the right value');
67 is( break_it_down( undef ), 'undef', '... got the right value');
68 is( break_it_down(), 'undef', '... got the right value');
70 # checking against enum types
72 enum RGB => qw[ red green blue ];
73 enum CMYK => qw[ cyan magenta yellow black ];
75 sub is_acceptable_color {
77 'RGB' => sub { 'RGB' },
78 'CMYK' => sub { 'CMYK' },
79 sub { die "bad color $_" };
82 is( is_acceptable_color( 'blue' ), 'RGB', '... got the right value');
83 is( is_acceptable_color( 'green' ), 'RGB', '... got the right value');
84 is( is_acceptable_color( 'red' ), 'RGB', '... got the right value');
85 is( is_acceptable_color( 'cyan' ), 'CMYK', '... got the right value');
86 is( is_acceptable_color( 'magenta' ), 'CMYK', '... got the right value');
87 is( is_acceptable_color( 'yellow' ), 'CMYK', '... got the right value');
88 is( is_acceptable_color( 'black' ), 'CMYK', '... got the right value');
91 is_acceptable_color( 'orange' )
92 }, undef, '... got the exception' );
94 ## using it in an OO context
99 use Moose::Util::TypeConstraints;
105 default => sub { __PACKAGE__->new },
106 predicate => 'has_next'
111 match_on_type $list =>
114 where { ! $_->has_next }
116 'LinkedList' => sub { '[' . $_->next->pprint . ']' };
120 my $l = LinkedList->new;
121 is($l->pprint, '[]', '... got the right pprint');
123 is($l->pprint, '[[]]', '... got the right pprint');
125 is($l->pprint, '[[[]]]', '... got the right pprint');
126 $l->next->next->next;
127 is($l->pprint, '[[[[]]]]', '... got the right pprint');
135 sub to_string { 'Foo()' }
145 '{ ' . (join ", " => map {
146 $_ . ' => ' . ppprint( $hash->{ $_ } )
147 } sort keys %$hash ) . ' }' },
150 '[ ' . (join ", " => map { ppprint( $_ ) } @$array ) . ' ]' },
151 CodeRef => sub { 'sub { ... }' },
152 RegexpRef => sub { 'qr/' . $_ . '/' },
153 GlobRef => sub { '*' . B::svref_2object($_)->NAME },
154 Object => sub { $_->can('to_string') ? $_->to_string : $_ },
155 ScalarRef => sub { '\\' . ppprint( ${$_} ) },
157 Str => sub { '"'. $_ . '"' },
158 Undef => sub { 'undef' },
159 => sub { die "I don't know what $_ is" };
162 # The stringification of qr// has changed in 5.13.5+
163 my $re_prefix = qr/x/ =~ /\(\?\^/ ? '(?^:' :'(?-xism:';
168 one => [ 1, 2, "three", 4, "five", \(my $x = "six") ],
170 three => sub { "OH HAI" },
176 qq~{ five => *ppprint, four => qr/$re_prefix.*?)/, one => [ 1, 2, "three", 4, "five", \\"six" ], six => Foo(), three => sub { ... }, two => undef }~,
177 '... got the right pretty printed values'
180 # simple JSON serializer
187 '{ ' . (join ", " => map {
188 '"' . $_ . '" : ' . to_json( $hash->{ $_ } )
189 } sort keys %$hash ) . ' }' },
192 '[ ' . (join ", " => map { to_json( $_ ) } @$array ) . ' ]' },
194 Str => sub { '"'. $_ . '"' },
195 Undef => sub { 'null' },
196 => sub { die "$_ is not acceptable json type" };
200 to_json( { one => 1, two => 2 } ),
201 '{ "one" : 1, "two" : 2 }',
202 '... got our valid JSON'
207 one => [ 1, 2, 3, 4 ],
209 three => "Hello World"
211 '{ "one" : [ 1, 2, 3, 4 ], "three" : "Hello World", "two" : null }',
212 '... got our valid JSON'
218 sub not_enough_matches {
221 Undef => sub { 'hello undef world' },
222 CodeRef => sub { $_->('Hello code ref world') };
226 not_enough_matches( [] )
227 }, qr/No cases matched for /, '... not enough matches' );