no trailing whitespace
[gitmo/MooseX-Types.git] / t / 20_union_with_string_type.t
CommitLineData
442e42ba 1#!/usr/bin/env perl
2use strict;
3use warnings;
4
a344ca96 5use Test::More;
442e42ba 6
7my $exception;
8{
9 package TypeLib;
10
0d07f026 11 use MooseX::Types -declare => [qw( MyUnionType Test1 Test2 Test3 MyStr )];
12 use MooseX::Types::Moose qw(Str Int Item Object);
442e42ba 13
14 subtype MyUnionType, as Str|'Int';
15 subtype MyStr, as Str;
16
17 eval { coerce MyStr, from Item, via {"$_"} };
0d07f026 18 my $exception = $@;
19
20 Test::More::ok !$@, 'types are not mutated by union with a string type';
21
e9dc30af 22 subtype Test1,
0d07f026 23 as Int | 'ArrayRef[Int]';
e9dc30af 24
0d07f026 25 Test::More::ok Test1->check(1), '1 is an Int';
26 Test::More::ok !Test1->check('a'), 'a is not an Int';
27 Test::More::ok Test1->check([1, 2, 3]), 'Passes ArrayRef';
28 Test::More::ok !Test1->check([1, 'a', 3]), 'Fails ArrayRef with a letter';
29 Test::More::ok !Test1->check({a=>1}), 'fails wrong ref type';
30
31 eval {
e9dc30af 32 subtype Test2,
0d07f026 33 as Int | 'IDONTEXIST';
34 };
442e42ba 35
0d07f026 36 my $check = $@;
37
e9dc30af 38 Test::More::ok $@, 'Got an error for bad Type';
0d07f026 39 Test::More::like $check, qr/IDONTEXIST is not a type constraint/, 'correct error';
40
e9dc30af 41 my $obj = subtype Test3,
0d07f026 42 as Int | 'ArrayRef[Int]' | Object;
43
44 Test::More::ok Test3->check(1), '1 is an Int';
45 Test::More::ok !Test3->check('a'), 'a is not an Int';
46 Test::More::ok Test3->check([1, 2, 3]), 'Passes ArrayRef';
47 Test::More::ok !Test3->check([1, 'a', 3]), 'Fails ArrayRef with a letter';
48 Test::More::ok !Test3->check({a=>1}), 'fails wrong ref type';
49 Test::More::ok Test3->check($obj), 'Union allows Object';
50}
a344ca96 51
52done_testing();