lots of more refactored goodness in the TC system
[gitmo/Moose.git] / t / 040_type_constraints / 008_union_types.t
CommitLineData
451c8248 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 27;
7use Test::Exception;
8
9BEGIN {
10 use_ok('Moose::Util::TypeConstraints');
11}
12
13my $Str = find_type_constraint('Str');
14isa_ok($Str, 'Moose::Meta::TypeConstraint');
15
16my $Undef = find_type_constraint('Undef');
17isa_ok($Undef, 'Moose::Meta::TypeConstraint');
18
19ok(!$Str->check(undef), '... Str cannot accept an Undef value');
20ok($Str->check('String'), '... Str can accept an String value');
21ok(!$Undef->check('String'), '... Undef cannot accept an Str value');
22ok($Undef->check(undef), '... Undef can accept an Undef value');
23
3726f905 24my $Str_or_Undef = Moose::Meta::TypeConstraint::Union->new(type_constraints => [$Str, $Undef]);
451c8248 25isa_ok($Str_or_Undef, 'Moose::Meta::TypeConstraint::Union');
26
27ok($Str_or_Undef->check(undef), '... (Str | Undef) can accept an Undef value');
28ok($Str_or_Undef->check('String'), '... (Str | Undef) can accept a String value');
29
30# another ....
31
32my $ArrayRef = find_type_constraint('ArrayRef');
33isa_ok($ArrayRef, 'Moose::Meta::TypeConstraint');
34
35my $HashRef = find_type_constraint('HashRef');
36isa_ok($HashRef, 'Moose::Meta::TypeConstraint');
37
38ok($ArrayRef->check([]), '... ArrayRef can accept an [] value');
39ok(!$ArrayRef->check({}), '... ArrayRef cannot accept an {} value');
40ok($HashRef->check({}), '... HashRef can accept an {} value');
41ok(!$HashRef->check([]), '... HashRef cannot accept an [] value');
42
3726f905 43my $HashOrArray = Moose::Meta::TypeConstraint::Union->new(type_constraints => [$ArrayRef, $HashRef]);
451c8248 44isa_ok($HashOrArray, 'Moose::Meta::TypeConstraint::Union');
45
46ok($HashOrArray->check([]), '... (ArrayRef | HashRef) can accept []');
47ok($HashOrArray->check({}), '... (ArrayRef | HashRef) can accept {}');
48
49ok(!$HashOrArray->check(\(my $var1)), '... (ArrayRef | HashRef) cannot accept scalar refs');
50ok(!$HashOrArray->check(sub {}), '... (ArrayRef | HashRef) cannot accept code refs');
51ok(!$HashOrArray->check(50), '... (ArrayRef | HashRef) cannot accept Numbers');
52
53diag $HashOrArray->validate([]);
54
55ok(!defined($HashOrArray->validate([])), '... (ArrayRef | HashRef) can accept []');
56ok(!defined($HashOrArray->validate({})), '... (ArrayRef | HashRef) can accept {}');
57
58is($HashOrArray->validate(\(my $var2)), 'Validation failed for \'ArrayRef\' failed and Validation failed for \'HashRef\' failed in (ArrayRef | HashRef)', '... (ArrayRef | HashRef) cannot accept scalar refs');
59is($HashOrArray->validate(sub {}), 'Validation failed for \'ArrayRef\' failed and Validation failed for \'HashRef\' failed in (ArrayRef | HashRef)', '... (ArrayRef | HashRef) cannot accept code refs');
60is($HashOrArray->validate(50), 'Validation failed for \'ArrayRef\' failed and Validation failed for \'HashRef\' failed in (ArrayRef | HashRef)', '... (ArrayRef | HashRef) cannot accept Numbers');
61