Mouse::Util::does_role() respects $thing->does() method
[gitmo/Mouse.git] / t / 040_type_constraints / 010_misc_type_tests.t
CommitLineData
b2b106d7 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
184f8f53 6use Test::More;
b2b106d7 7use Test::Exception;
184f8f53 8use Scalar::Util qw(refaddr);
b2b106d7 9
10BEGIN {
11 use_ok('Mouse::Util::TypeConstraints');
12}
13
14# subtype 'aliasing' ...
15
16lives_ok {
17 subtype 'Numb3rs' => as 'Num';
18} '... create bare subtype fine';
19
20my $numb3rs = find_type_constraint('Numb3rs');
21isa_ok($numb3rs, 'Mouse::Meta::TypeConstraint');
22
23# subtype with unions
24
25{
9864f0e4 26 package Test::Mouse::Meta::TypeConstraint::Union;
b2b106d7 27
28 use overload '""' => sub {'Broken|Test'}, fallback => 1;
29 use Mouse;
30
31 extends 'Mouse::Meta::TypeConstraint';
32}
33
9864f0e4 34my $dummy_instance = Test::Mouse::Meta::TypeConstraint::Union->new;
b2b106d7 35
36ok $dummy_instance => "Created Instance";
37
38isa_ok $dummy_instance,
9864f0e4 39 'Test::Mouse::Meta::TypeConstraint::Union' => 'isa correct type';
b2b106d7 40
41is "$dummy_instance", "Broken|Test" =>
42 'Got expected stringification result';
43
44my $subtype1 = subtype 'New1' => as $dummy_instance;
45
46ok $subtype1 => 'made a subtype from our type object';
47
48my $subtype2 = subtype 'New2' => as $subtype1;
49
50ok $subtype2 => 'made a subtype of our subtype';
51
52# assert_valid
53
54{
55 my $type = find_type_constraint('Num');
56
57 my $ok_1 = eval { $type->assert_valid(1); };
58 ok($ok_1, "we can assert_valid that 1 is of type $type");
59
60 my $ok_2 = eval { $type->assert_valid('foo'); };
61 my $error = $@;
62 ok(! $ok_2, "'foo' is not of type $type");
63 like(
64 $error,
65 qr{validation failed for .\Q$type\E.}i,
66 "correct error thrown"
67 );
68}
184f8f53 69
70{
71 for my $t (qw(Bar Foo)) {
72 my $tc = Mouse::Meta::TypeConstraint->new({
73 name => $t,
74 });
75
76 Mouse::Util::TypeConstraints::register_type_constraint($tc);
77 }
78
79 my $foo = Mouse::Util::TypeConstraints::find_type_constraint('Foo');
80 my $bar = Mouse::Util::TypeConstraints::find_type_constraint('Bar');
81
9864f0e4 82 ok(!$foo->is_a_type_of($bar), "Foo type is not equal to Bar type");
83 ok( $foo->is_a_type_of($foo), "Foo equals Foo");
184f8f53 84 ok( 0+$foo == refaddr($foo), "overloading works");
85}
86
87ok $subtype1, "type constraint boolean overload works";
88
89done_testing;