Fix the string check on the test
[gitmo/Moose.git] / t / type_constraints / intersection_types.t
CommitLineData
8aab053a 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 34;
7use Test::Exception;
8
9BEGIN {
10 use_ok('Moose::Util::TypeConstraints');
11}
12
13my $Str = find_type_constraint('Str');
14isa_ok($Str, 'Moose::Meta::TypeConstraint');
15
16my $Defined = find_type_constraint('Defined');
17isa_ok($Defined, 'Moose::Meta::TypeConstraint');
18
19ok(!$Str->check(undef), '... Str cannot accept an Undef value');
20ok($Str->check('String'), '... Str can accept an String value');
21ok($Defined->check('String'), '... Defined can accept an Str value');
22ok(!$Defined->check(undef), '... Defined cannot accept an undef value');
23
24my $Str_and_Defined = Moose::Meta::TypeConstraint::Intersection->new(type_constraints => [$Str, $Defined]);
25isa_ok($Str_and_Defined, 'Moose::Meta::TypeConstraint::Intersection');
26
27ok($Str_and_Defined->check(''), '... (Str & Defined) can accept a Defined value');
28ok($Str_and_Defined->check('String'), '... (Str & Defined) can accept a String value');
29ok(!$Str_and_Defined->check([]), '... (Str & Defined) cannot accept an array reference');
30
31ok($Str_and_Defined->is_a_type_of($Str), "subtype of Str");
32ok($Str_and_Defined->is_a_type_of($Defined), "subtype of Defined");
33
34ok( !$Str_and_Defined->equals($Str), "not equal to Str" );
35ok( $Str_and_Defined->equals($Str_and_Defined), "equal to self" );
36ok( $Str_and_Defined->equals(Moose::Meta::TypeConstraint::Intersection->new(type_constraints => [ $Str, $Defined ])), "equal to clone" );
37ok( $Str_and_Defined->equals(Moose::Meta::TypeConstraint::Intersection->new(type_constraints => [ $Defined, $Str ])), "equal to reversed clone" );
38
39ok( !$Str_and_Defined->is_a_type_of("ThisTypeDoesNotExist"), "not type of non existant type" );
40ok( !$Str_and_Defined->is_subtype_of("ThisTypeDoesNotExist"), "not subtype of non existant type" );
41
42# another ....
43
44my $ArrayRef = find_type_constraint('ArrayRef');
45isa_ok($ArrayRef, 'Moose::Meta::TypeConstraint');
46
47my $Ref = find_type_constraint('Ref');
48isa_ok($Ref, 'Moose::Meta::TypeConstraint');
49
50ok($ArrayRef->check([]), '... ArrayRef can accept an [] value');
51ok(!$ArrayRef->check({}), '... ArrayRef cannot accept an {} value');
52ok($Ref->check({}), '... Ref can accept an {} value');
53ok(!$Ref->check(5), '... Ref cannot accept a 5 value');
54
55my $RefAndArray = Moose::Meta::TypeConstraint::Intersection->new(type_constraints => [$ArrayRef, $Ref]);
56isa_ok($RefAndArray, 'Moose::Meta::TypeConstraint::Intersection');
57
58ok($RefAndArray->check([]), '... (ArrayRef & Ref) can accept []');
59ok(!$RefAndArray->check({}), '... (ArrayRef & Ref) cannot accept {}');
60
61ok(!$RefAndArray->check(\(my $var1)), '... (ArrayRef & Ref) cannot accept scalar refs');
62ok(!$RefAndArray->check(sub {}), '... (ArrayRef & Ref) cannot accept code refs');
63ok(!$RefAndArray->check(50), '... (ArrayRef & Ref) cannot accept Numbers');
64
65diag $RefAndArray->validate([]);
66
67ok(!defined($RefAndArray->validate([])), '... (ArrayRef & Ref) can accept []');
68ok(defined($RefAndArray->validate(undef)), '... (ArrayRef & Ref) cannot accept undef');
69
70like($RefAndArray->validate(undef),
cb6d4b32 71qr/Validation failed for \'ArrayRef\' with value undef and Validation failed for \'Ref\' with value undef in \(ArrayRef&Ref\)/,
8aab053a 72'... (ArrayRef & Ref) cannot accept undef');
73