Add smartmatch support
[gitmo/MooseX-Types.git] / t / 22_smartmatch.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 BEGIN {
9     plan skip_all => "5.10.0 required" unless eval { require 5.010 };
10     plan tests => 8;
11 }
12
13 use MooseX::Types::Moose qw(Str HashRef ArrayRef);
14
15 use 5.010;
16
17 ok( "foo" ~~ Str, "string" );
18 ok( Str() ~~ "foo", "string" );
19 ok( !("foo" ~~ HashRef), "hashref" );
20 ok( !(HashRef() ~~ "foo"), "hashref" );
21
22 sub 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
34 is( foo({}), "hash" );
35 is( foo("foo"), "string" );
36 is( foo([]), "array" );
37 is( foo(sub { }), "something else" );