Add smartmatch support
[gitmo/MooseX-Types.git] / t / 22_smartmatch.t
CommitLineData
f161acbe 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More;
7
8BEGIN {
9 plan skip_all => "5.10.0 required" unless eval { require 5.010 };
10 plan tests => 8;
11}
12
13use MooseX::Types::Moose qw(Str HashRef ArrayRef);
14
15use 5.010;
16
17ok( "foo" ~~ Str, "string" );
18ok( Str() ~~ "foo", "string" );
19ok( !("foo" ~~ HashRef), "hashref" );
20ok( !(HashRef() ~~ "foo"), "hashref" );
21
22sub foo {
23 given ($_[0]) {
24 # unfortunately we can't actually have when(HashRef) etc, because that
25 # resolves as a boolean (checks whether the return value of HashRef etc
26 # is true)
27 when ($_ ~~ HashRef) { return "hash" }
28 when ($_ ~~ ArrayRef) { return "array" }
29 when ($_ ~~ Str) { return "string" }
30 default { return "something else" }
31 }
32}
33
34is( foo({}), "hash" );
35is( foo("foo"), "string" );
36is( foo([]), "array" );
37is( foo(sub { }), "something else" );