Require Dist::Zilla 4.200016+
[gitmo/Moose.git] / t / type_constraints / union_types.t
index cd26f57..fb1f789 100644 (file)
@@ -81,9 +81,9 @@ is(
     'parent of Str|Undef is Item'
 );
 
-is(
-    $Str_or_Undef->parents,
-    find_type_constraint('Item'),
+is_deeply(
+    [$Str_or_Undef->parents],
+    [find_type_constraint('Item')],
     'parents of Str|Undef is Item'
 );
 
@@ -100,56 +100,98 @@ ok( !$ArrayRef->check( {} ), '... ArrayRef cannot accept an {} value' );
 ok( $HashRef->check(   {} ), '... HashRef can accept an {} value' );
 ok( !$HashRef->check( [] ), '... HashRef cannot accept an [] value' );
 
-my $HashOrArray = Moose::Meta::TypeConstraint::Union->new(
+my $ArrayRef_or_HashRef = Moose::Meta::TypeConstraint::Union->new(
     type_constraints => [ $ArrayRef, $HashRef ] );
-isa_ok( $HashOrArray, 'Moose::Meta::TypeConstraint::Union' );
+isa_ok( $ArrayRef_or_HashRef, 'Moose::Meta::TypeConstraint::Union' );
 
-ok( $HashOrArray->check( [] ), '... (ArrayRef | HashRef) can accept []' );
-ok( $HashOrArray->check( {} ), '... (ArrayRef | HashRef) can accept {}' );
+ok( $ArrayRef_or_HashRef->check( [] ),
+    '... (ArrayRef | HashRef) can accept []' );
+ok( $ArrayRef_or_HashRef->check( {} ),
+    '... (ArrayRef | HashRef) can accept {}' );
 
 ok(
-    !$HashOrArray->check( \( my $var1 ) ),
+    !$ArrayRef_or_HashRef->check( \( my $var1 ) ),
     '... (ArrayRef | HashRef) cannot accept scalar refs'
 );
 ok(
-    !$HashOrArray->check( sub { } ),
+    !$ArrayRef_or_HashRef->check( sub { } ),
     '... (ArrayRef | HashRef) cannot accept code refs'
 );
 ok(
-    !$HashOrArray->check(50),
+    !$ArrayRef_or_HashRef->check(50),
     '... (ArrayRef | HashRef) cannot accept Numbers'
 );
 
-diag $HashOrArray->validate( [] );
+diag $ArrayRef_or_HashRef->validate( [] );
 
 ok(
-    !defined( $HashOrArray->validate( [] ) ),
+    !defined( $ArrayRef_or_HashRef->validate( [] ) ),
     '... (ArrayRef | HashRef) can accept []'
 );
 ok(
-    !defined( $HashOrArray->validate( {} ) ),
+    !defined( $ArrayRef_or_HashRef->validate( {} ) ),
     '... (ArrayRef | HashRef) can accept {}'
 );
 
 like(
-    $HashOrArray->validate( \( my $var2 ) ),
+    $ArrayRef_or_HashRef->validate( \( my $var2 ) ),
     qr/Validation failed for \'ArrayRef\' with value .+ and Validation failed for \'HashRef\' with value .+ in \(ArrayRef\|HashRef\)/,
     '... (ArrayRef | HashRef) cannot accept scalar refs'
 );
 
 like(
-    $HashOrArray->validate( sub { } ),
+    $ArrayRef_or_HashRef->validate( sub { } ),
     qr/Validation failed for \'ArrayRef\' with value .+ and Validation failed for \'HashRef\' with value .+ in \(ArrayRef\|HashRef\)/,
     '... (ArrayRef | HashRef) cannot accept code refs'
 );
 
 is(
-    $HashOrArray->validate(50),
+    $ArrayRef_or_HashRef->validate(50),
     'Validation failed for \'ArrayRef\' with value 50 and Validation failed for \'HashRef\' with value 50 in (ArrayRef|HashRef)',
     '... (ArrayRef | HashRef) cannot accept Numbers'
 );
 
 is(
-   $HasOr
+    $ArrayRef_or_HashRef->parent,
+    find_type_constraint('Ref'),
+    'parent of ArrayRef|HashRef is Ref'
+);
+
+my $double_union = Moose::Meta::TypeConstraint::Union->new(
+    type_constraints => [ $Str_or_Undef, $ArrayRef_or_HashRef ] );
+
+is(
+    $double_union->parent,
+    find_type_constraint('Item'),
+    'parent of (Str|Undef)|(ArrayRef|HashRef) is Item'
+);
+
+ok(
+    $double_union->is_subtype_of('Item'),
+    '(Str|Undef)|(ArrayRef|HashRef) is a subtype of Item'
+);
+
+ok(
+    $double_union->is_a_type_of('Item'),
+    '(Str|Undef)|(ArrayRef|HashRef) is a type of Item'
+);
+
+ok(
+    !$double_union->is_a_type_of('Str'),
+    '(Str|Undef)|(ArrayRef|HashRef) is not a type of Str'
+);
+
+type 'SomeType', where { 1 };
+type 'OtherType', where { 1 };
+
+my $parentless_union = Moose::Meta::TypeConstraint::Union->new(
+    type_constraints => [
+        find_type_constraint('SomeType'),
+        find_type_constraint('OtherType'),
+    ],
+);
+
+is($parentless_union->parent, undef, "no common ancestor gives undef parent");
+
 
 done_testing;