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