Add a tool to import tests
[gitmo/Mouse.git] / t-failing / 040_type_constraints / 008_union_types.t
1 #!/usr/bin/perl
2 # This is automatically generated by author/import-moose-test.pl.
3 # DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4 use t::lib::MooseCompat;
5
6 use strict;
7 use warnings;
8
9 use Test::More;
10 $TODO = q{Mouse is not yet completed};
11 use Test::Exception;
12
13 BEGIN {
14     use_ok('Mouse::Util::TypeConstraints');
15 }
16
17 my $Str = find_type_constraint('Str');
18 isa_ok($Str, 'Mouse::Meta::TypeConstraint');
19
20 my $Undef = find_type_constraint('Undef');
21 isa_ok($Undef, 'Mouse::Meta::TypeConstraint');
22
23 ok(!$Str->check(undef), '... Str cannot accept an Undef value');
24 ok($Str->check('String'), '... Str can accept an String value');
25 ok(!$Undef->check('String'), '... Undef cannot accept an Str value');
26 ok($Undef->check(undef), '... Undef can accept an Undef value');
27
28 my $Str_or_Undef = Mouse::Meta::TypeConstraint->new(type_constraints => [$Str, $Undef]);
29 isa_ok($Str_or_Undef, 'Mouse::Meta::TypeConstraint');
30
31 ok($Str_or_Undef->check(undef), '... (Str | Undef) can accept an Undef value');
32 ok($Str_or_Undef->check('String'), '... (Str | Undef) can accept a String value');
33
34 ok($Str_or_Undef->is_a_type_of($Str), "subtype of Str");
35 ok($Str_or_Undef->is_a_type_of($Undef), "subtype of Undef");
36
37 cmp_ok($Str_or_Undef->find_type_for('String'), 'eq', 'Str', 'find_type_for Str');
38 cmp_ok($Str_or_Undef->find_type_for(undef), 'eq', 'Undef', 'find_type_for Undef');
39 ok(!defined($Str_or_Undef->find_type_for(sub { })), 'no find_type_for CodeRef');
40
41 ok( !$Str_or_Undef->equals($Str), "not equal to Str" );
42 ok( $Str_or_Undef->equals($Str_or_Undef), "equal to self" );
43 ok( $Str_or_Undef->equals(Mouse::Meta::TypeConstraint->new(type_constraints => [ $Str, $Undef ])), "equal to clone" );
44 ok( $Str_or_Undef->equals(Mouse::Meta::TypeConstraint->new(type_constraints => [ $Undef, $Str ])), "equal to reversed clone" );
45
46 ok( !$Str_or_Undef->is_a_type_of("ThisTypeDoesNotExist"), "not type of non existant type" );
47 ok( !$Str_or_Undef->is_subtype_of("ThisTypeDoesNotExist"), "not subtype of non existant type" );
48
49 # another ....
50
51 my $ArrayRef = find_type_constraint('ArrayRef');
52 isa_ok($ArrayRef, 'Mouse::Meta::TypeConstraint');
53
54 my $HashRef = find_type_constraint('HashRef');
55 isa_ok($HashRef, 'Mouse::Meta::TypeConstraint');
56
57 ok($ArrayRef->check([]), '... ArrayRef can accept an [] value');
58 ok(!$ArrayRef->check({}), '... ArrayRef cannot accept an {} value');
59 ok($HashRef->check({}), '... HashRef can accept an {} value');
60 ok(!$HashRef->check([]), '... HashRef cannot accept an [] value');
61
62 my $HashOrArray = Mouse::Meta::TypeConstraint->new(type_constraints => [$ArrayRef, $HashRef]);
63 isa_ok($HashOrArray, 'Mouse::Meta::TypeConstraint');
64
65 ok($HashOrArray->check([]), '... (ArrayRef | HashRef) can accept []');
66 ok($HashOrArray->check({}), '... (ArrayRef | HashRef) can accept {}');
67
68 ok(!$HashOrArray->check(\(my $var1)), '... (ArrayRef | HashRef) cannot accept scalar refs');
69 ok(!$HashOrArray->check(sub {}), '... (ArrayRef | HashRef) cannot accept code refs');
70 ok(!$HashOrArray->check(50), '... (ArrayRef | HashRef) cannot accept Numbers');
71
72 diag $HashOrArray->validate([]);
73
74 ok(!defined($HashOrArray->validate([])), '... (ArrayRef | HashRef) can accept []');
75 ok(!defined($HashOrArray->validate({})), '... (ArrayRef | HashRef) can accept {}');
76
77 like($HashOrArray->validate(\(my $var2)),
78 qr/Validation failed for \'ArrayRef\' with value SCALAR\(0x.+?\) and Validation failed for \'HashRef\' with value SCALAR\(0x.+?\) in \(ArrayRef\|HashRef\)/,
79 '... (ArrayRef | HashRef) cannot accept scalar refs');
80
81 like($HashOrArray->validate(sub {}),
82 qr/Validation failed for \'ArrayRef\' with value CODE\(0x.+?\) and Validation failed for \'HashRef\' with value CODE\(0x.+?\) in \(ArrayRef\|HashRef\)/,
83 '... (ArrayRef | HashRef) cannot accept code refs');
84
85 is($HashOrArray->validate(50),
86 'Validation failed for \'ArrayRef\' with value 50 and Validation failed for \'HashRef\' with value 50 in (ArrayRef|HashRef)',
87 '... (ArrayRef | HashRef) cannot accept Numbers');
88
89 done_testing;