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