6 use Test::More tests => 34;
10 use_ok('Moose::Util::TypeConstraints');
13 my $Str = find_type_constraint('Str');
14 isa_ok($Str, 'Moose::Meta::TypeConstraint');
16 my $Undef = find_type_constraint('Undef');
17 isa_ok($Undef, 'Moose::Meta::TypeConstraint');
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');
24 my $Str_or_Undef = Moose::Meta::TypeConstraint::Union->new(type_constraints => [$Str, $Undef]);
25 isa_ok($Str_or_Undef, 'Moose::Meta::TypeConstraint::Union');
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');
32 my $ArrayRef = find_type_constraint('ArrayRef');
33 isa_ok($ArrayRef, 'Moose::Meta::TypeConstraint');
35 my $HashRef = find_type_constraint('HashRef');
36 isa_ok($HashRef, 'Moose::Meta::TypeConstraint');
38 ok($ArrayRef->check([]), '... ArrayRef can accept an [] value');
39 ok(!$ArrayRef->check({}), '... ArrayRef cannot accept an {} value');
40 ok($HashRef->check({}), '... HashRef can accept an {} value');
41 ok(!$HashRef->check([]), '... HashRef cannot accept an [] value');
43 my $HashOrArray = Moose::Meta::TypeConstraint::Union->new(type_constraints => [$ArrayRef, $HashRef]);
44 isa_ok($HashOrArray, 'Moose::Meta::TypeConstraint::Union');
46 ok($HashOrArray->check([]), '... (ArrayRef | HashRef) can accept []');
47 ok($HashOrArray->check({}), '... (ArrayRef | HashRef) can accept {}');
49 ok(!$HashOrArray->check(\(my $var1)), '... (ArrayRef | HashRef) cannot accept scalar refs');
50 ok(!$HashOrArray->check(sub {}), '... (ArrayRef | HashRef) cannot accept code refs');
51 ok(!$HashOrArray->check(50), '... (ArrayRef | HashRef) cannot accept Numbers');
53 diag $HashOrArray->validate([]);
55 ok(!defined($HashOrArray->validate([])), '... (ArrayRef | HashRef) can accept []');
56 ok(!defined($HashOrArray->validate({})), '... (ArrayRef | HashRef) can accept {}');
58 like($HashOrArray->validate(\(my $var2)),
59 qr/Validation failed for \'ArrayRef\' failed with value SCALAR\(0x.+?\) and Validation failed for \'HashRef\' failed with value SCALAR\(0x.+?\) in \(ArrayRef \| HashRef\)/,
60 '... (ArrayRef | HashRef) cannot accept scalar refs');
62 like($HashOrArray->validate(sub {}),
63 qr/Validation failed for \'ArrayRef\' failed with value CODE\(0x.+?\) and Validation failed for \'HashRef\' failed with value CODE\(0x.+?\) in \(ArrayRef \| HashRef\)/,
64 '... (ArrayRef | HashRef) cannot accept code refs');
66 is($HashOrArray->validate(50),
67 'Validation failed for \'ArrayRef\' failed with value 50 and Validation failed for \'HashRef\' failed with value 50 in (ArrayRef | HashRef)',
68 '... (ArrayRef | HashRef) cannot accept Numbers');
70 my $StrOrArray = Moose::Meta::TypeConstraint::Union->new(type_constraints => [$Str, $ArrayRef]);
71 isa_ok($StrOrArray, 'Moose::Meta::TypeConstraint');
73 ok(!defined($StrOrArray->validate("hi")));
74 ok(!defined($StrOrArray->validate([])));
75 ok(!defined($StrOrArray->validate([1])));
77 like($StrOrArray->validate({}),
78 qr/Validation failed for \'Str\' failed with value HASH\(0x.+?\) and Validation failed for \'ArrayRef\' failed with value HASH\(0x.+?\) in \(Str \| ArrayRef\)/,
79 '... (Str | ArrayRef) cannot accept HashRefs');
81 ok($Str->is_subtype_of($StrOrArray), "Str is a subtype of (Str | ArrayRef)");
82 ok($ArrayRef->is_subtype_of($StrOrArray), "ArrayRef is a subtype of (Str | ArrayRef)" );