Remove cruft
[gitmo/Moose.git] / t / 040_type_constraints / 008_union_types.t
CommitLineData
451c8248 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
451c8248 7use Test::Exception;
8
9BEGIN {
d03bd989 10 use_ok('Moose::Util::TypeConstraints');
451c8248 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
dabed765 30ok($Str_or_Undef->is_a_type_of($Str), "subtype of Str");
31ok($Str_or_Undef->is_a_type_of($Undef), "subtype of Undef");
32
1aae641c 33cmp_ok($Str_or_Undef->find_type_for('String'), 'eq', 'Str', 'find_type_for Str');
34cmp_ok($Str_or_Undef->find_type_for(undef), 'eq', 'Undef', 'find_type_for Undef');
35ok(!defined($Str_or_Undef->find_type_for(sub { })), 'no find_type_for CodeRef');
36
dabed765 37ok( !$Str_or_Undef->equals($Str), "not equal to Str" );
38ok( $Str_or_Undef->equals($Str_or_Undef), "equal to self" );
39ok( $Str_or_Undef->equals(Moose::Meta::TypeConstraint::Union->new(type_constraints => [ $Str, $Undef ])), "equal to clone" );
40ok( $Str_or_Undef->equals(Moose::Meta::TypeConstraint::Union->new(type_constraints => [ $Undef, $Str ])), "equal to reversed clone" );
41
4c015454 42ok( !$Str_or_Undef->is_a_type_of("ThisTypeDoesNotExist"), "not type of non existant type" );
43ok( !$Str_or_Undef->is_subtype_of("ThisTypeDoesNotExist"), "not subtype of non existant type" );
44
451c8248 45# another ....
46
47my $ArrayRef = find_type_constraint('ArrayRef');
48isa_ok($ArrayRef, 'Moose::Meta::TypeConstraint');
49
50my $HashRef = find_type_constraint('HashRef');
51isa_ok($HashRef, 'Moose::Meta::TypeConstraint');
52
53ok($ArrayRef->check([]), '... ArrayRef can accept an [] value');
54ok(!$ArrayRef->check({}), '... ArrayRef cannot accept an {} value');
55ok($HashRef->check({}), '... HashRef can accept an {} value');
56ok(!$HashRef->check([]), '... HashRef cannot accept an [] value');
57
3726f905 58my $HashOrArray = Moose::Meta::TypeConstraint::Union->new(type_constraints => [$ArrayRef, $HashRef]);
451c8248 59isa_ok($HashOrArray, 'Moose::Meta::TypeConstraint::Union');
60
61ok($HashOrArray->check([]), '... (ArrayRef | HashRef) can accept []');
62ok($HashOrArray->check({}), '... (ArrayRef | HashRef) can accept {}');
63
64ok(!$HashOrArray->check(\(my $var1)), '... (ArrayRef | HashRef) cannot accept scalar refs');
65ok(!$HashOrArray->check(sub {}), '... (ArrayRef | HashRef) cannot accept code refs');
66ok(!$HashOrArray->check(50), '... (ArrayRef | HashRef) cannot accept Numbers');
67
68diag $HashOrArray->validate([]);
69
70ok(!defined($HashOrArray->validate([])), '... (ArrayRef | HashRef) can accept []');
71ok(!defined($HashOrArray->validate({})), '... (ArrayRef | HashRef) can accept {}');
72
d03bd989 73like($HashOrArray->validate(\(my $var2)),
8c063f8e 74qr/Validation failed for \'ArrayRef\' with value SCALAR\(0x.+?\) and Validation failed for \'HashRef\' with value SCALAR\(0x.+?\) in \(ArrayRef\|HashRef\)/,
688fcdda 75'... (ArrayRef | HashRef) cannot accept scalar refs');
76
d03bd989 77like($HashOrArray->validate(sub {}),
8c063f8e 78qr/Validation failed for \'ArrayRef\' with value CODE\(0x.+?\) and Validation failed for \'HashRef\' with value CODE\(0x.+?\) in \(ArrayRef\|HashRef\)/,
688fcdda 79'... (ArrayRef | HashRef) cannot accept code refs');
80
81is($HashOrArray->validate(50),
8c063f8e 82'Validation failed for \'ArrayRef\' with value 50 and Validation failed for \'HashRef\' with value 50 in (ArrayRef|HashRef)',
688fcdda 83'... (ArrayRef | HashRef) cannot accept Numbers');
451c8248 84
a28e50e4 85done_testing;