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