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