Rename so each trait is in alpha order and we have room for multiple files per trait
[gitmo/Moose.git] / t / 040_type_constraints / 036_match_type_operator.t
index 6656651..d969b6a 100644 (file)
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 22;
+use Test::More;
 use Test::Exception;
 
 use Moose::Util::TypeConstraints;
@@ -174,7 +174,53 @@ is(
     '... got the right pretty printed values'
 );
 
+# simple JSON serializer
 
+sub to_json {
+    my $x = shift;
+    match_on_type $x =>
+        HashRef   => sub {
+            my $hash = shift;
+            '{ ' . (join ", " => map {
+                        '"' . $_ . '" : ' . to_json( $hash->{ $_ } )
+                    } sort keys %$hash ) . ' }'                         },
+        ArrayRef  => sub {
+            my $array = shift;
+            '[ ' . (join ", " => map { to_json( $_ ) } @$array ) . ' ]' },
+        Num       => sub { $_                                           },
+        Str       => sub { '"'. $_ . '"'                                },
+        Undef     => sub { 'null'                                       },
+                  => sub { die "$_ is not acceptable json type"         };
+}
 
+is(
+    to_json( { one => 1, two => 2 } ),
+    '{ "one" : 1, "two" : 2 }',
+    '... got our valid JSON'
+);
+
+is(
+    to_json( {
+        one   => [ 1, 2, 3, 4 ],
+        two   => undef,
+        three => "Hello World"
+    } ),
+    '{ "one" : [ 1, 2, 3, 4 ], "three" : "Hello World", "two" : null }',
+    '... got our valid JSON'
+);
+
+
+# some error cases
+
+sub not_enough_matches {
+    my $x = shift;
+    match_on_type $x =>
+        Undef => sub { 'hello undef world'          },
+      CodeRef => sub { $_->('Hello code ref world') };
+}
 
+throws_ok {
+    not_enough_matches( [] )
+} qr/No cases matched for /, '... not enough matches';
 
+done_testing;