Add smartmatch support
[gitmo/MooseX-Types.git] / t / 22_smartmatch.t
diff --git a/t/22_smartmatch.t b/t/22_smartmatch.t
new file mode 100644 (file)
index 0000000..ef54192
--- /dev/null
@@ -0,0 +1,37 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More;
+
+BEGIN {
+    plan skip_all => "5.10.0 required" unless eval { require 5.010 };
+    plan tests => 8;
+}
+
+use MooseX::Types::Moose qw(Str HashRef ArrayRef);
+
+use 5.010;
+
+ok( "foo" ~~ Str, "string" );
+ok( Str() ~~ "foo", "string" );
+ok( !("foo" ~~ HashRef), "hashref" );
+ok( !(HashRef() ~~ "foo"), "hashref" );
+
+sub foo {
+    given ($_[0]) {
+        # unfortunately we can't actually have when(HashRef) etc, because that
+        # resolves as a boolean (checks whether the return value of HashRef etc
+        # is true)
+        when ($_ ~~ HashRef)  { return "hash" }
+        when ($_ ~~ ArrayRef) { return "array" }
+        when ($_ ~~ Str)      { return "string" }
+        default               { return "something else" }
+    }
+}
+
+is( foo({}), "hash" );
+is( foo("foo"), "string" );
+is( foo([]), "array" );
+is( foo(sub { }), "something else" );