4b46e8c65fa4eda6f9d6a826af6a44c0045a958b
[gitmo/Moose.git] / t / type_constraints / union_types.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 use Moose::Util::TypeConstraints;
9
10 my $Str = find_type_constraint('Str');
11 isa_ok($Str, 'Moose::Meta::TypeConstraint');
12
13 my $Undef = find_type_constraint('Undef');
14 isa_ok($Undef, 'Moose::Meta::TypeConstraint');
15
16 ok(!$Str->check(undef), '... Str cannot accept an Undef value');
17 ok($Str->check('String'), '... Str can accept an String value');
18 ok(!$Undef->check('String'), '... Undef cannot accept an Str value');
19 ok($Undef->check(undef), '... Undef can accept an Undef value');
20
21 my $Str_or_Undef = Moose::Meta::TypeConstraint::Union->new(type_constraints => [$Str, $Undef]);
22 isa_ok($Str_or_Undef, 'Moose::Meta::TypeConstraint::Union');
23
24 ok($Str_or_Undef->check(undef), '... (Str | Undef) can accept an Undef value');
25 ok($Str_or_Undef->check('String'), '... (Str | Undef) can accept a String value');
26
27 ok($Str_or_Undef->is_a_type_of($Str), "subtype of Str");
28 ok($Str_or_Undef->is_a_type_of($Undef), "subtype of Undef");
29
30 cmp_ok($Str_or_Undef->find_type_for('String'), 'eq', 'Str', 'find_type_for Str');
31 cmp_ok($Str_or_Undef->find_type_for(undef), 'eq', 'Undef', 'find_type_for Undef');
32 ok(!defined($Str_or_Undef->find_type_for(sub { })), 'no find_type_for CodeRef');
33
34 ok( !$Str_or_Undef->equals($Str), "not equal to Str" );
35 ok( $Str_or_Undef->equals($Str_or_Undef), "equal to self" );
36 ok( $Str_or_Undef->equals(Moose::Meta::TypeConstraint::Union->new(type_constraints => [ $Str, $Undef ])), "equal to clone" );
37 ok( $Str_or_Undef->equals(Moose::Meta::TypeConstraint::Union->new(type_constraints => [ $Undef, $Str ])), "equal to reversed clone" );
38
39 ok( !$Str_or_Undef->is_a_type_of("ThisTypeDoesNotExist"), "not type of non existant type" );
40 ok( !$Str_or_Undef->is_subtype_of("ThisTypeDoesNotExist"), "not subtype of non existant type" );
41
42 # another ....
43
44 my $ArrayRef = find_type_constraint('ArrayRef');
45 isa_ok($ArrayRef, 'Moose::Meta::TypeConstraint');
46
47 my $HashRef = find_type_constraint('HashRef');
48 isa_ok($HashRef, 'Moose::Meta::TypeConstraint');
49
50 ok($ArrayRef->check([]), '... ArrayRef can accept an [] value');
51 ok(!$ArrayRef->check({}), '... ArrayRef cannot accept an {} value');
52 ok($HashRef->check({}), '... HashRef can accept an {} value');
53 ok(!$HashRef->check([]), '... HashRef cannot accept an [] value');
54
55 my $HashOrArray = Moose::Meta::TypeConstraint::Union->new(type_constraints => [$ArrayRef, $HashRef]);
56 isa_ok($HashOrArray, 'Moose::Meta::TypeConstraint::Union');
57
58 ok($HashOrArray->check([]), '... (ArrayRef | HashRef) can accept []');
59 ok($HashOrArray->check({}), '... (ArrayRef | HashRef) can accept {}');
60
61 ok(!$HashOrArray->check(\(my $var1)), '... (ArrayRef | HashRef) cannot accept scalar refs');
62 ok(!$HashOrArray->check(sub {}), '... (ArrayRef | HashRef) cannot accept code refs');
63 ok(!$HashOrArray->check(50), '... (ArrayRef | HashRef) cannot accept Numbers');
64
65 diag $HashOrArray->validate([]);
66
67 ok(!defined($HashOrArray->validate([])), '... (ArrayRef | HashRef) can accept []');
68 ok(!defined($HashOrArray->validate({})), '... (ArrayRef | HashRef) can accept {}');
69
70 like($HashOrArray->validate(\(my $var2)),
71 qr/Validation failed for \'ArrayRef\' with value .+ and Validation failed for \'HashRef\' with value .+ in \(ArrayRef\|HashRef\)/,
72 '... (ArrayRef | HashRef) cannot accept scalar refs');
73
74 like($HashOrArray->validate(sub {}),
75 qr/Validation failed for \'ArrayRef\' with value .+ and Validation failed for \'HashRef\' with value .+ in \(ArrayRef\|HashRef\)/,
76 '... (ArrayRef | HashRef) cannot accept code refs');
77
78 is($HashOrArray->validate(50),
79 'Validation failed for \'ArrayRef\' with value 50 and Validation failed for \'HashRef\' with value 50 in (ArrayRef|HashRef)',
80 '... (ArrayRef | HashRef) cannot accept Numbers');
81
82 done_testing;