1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
use strict;
use warnings;
use Test::More;
use Test::Fatal;
use Moose::Util::TypeConstraints;
my $x = "123";
{
my $default = [1, 2, 3];
my $exception = exception {
match_on_type $x => ( 'Int' =>
sub { "Action for Int"; } =>
$default
);
};
like(
$exception,
qr/\QDefault case must be a CODE ref, not $default/,
"an ArrayRef is passed as a default");
#Default case must be a CODE ref, not ARRAY(0x14f6fc8)
isa_ok(
$exception,
'Moose::Exception::DefaultToMatchOnTypeMustBeCodeRef',
"an ArrayRef is passed as a default");
is(
$exception->default_action,
$default,
"an ArrayRef is passed as a default");
is(
$exception->to_match,
$x,
"an ArrayRef is passed as a default");
}
{
my $exception = exception {
match_on_type $x => ( 'doesNotExist' => sub { "Action for Int"; } );
};
like(
$exception,
qr/\QCannot find or parse the type 'doesNotExist'/,
"doesNotExist is not a valid type");
isa_ok(
$exception,
'Moose::Exception::CannotFindTypeGivenToMatchOnType',
"doesNotExist is not a valid type");
is(
$exception->type,
"doesNotExist",
"doesNotExist is not a valid type");
is(
$exception->to_match,
$x,
"doesNotExist is not a valid type");
}
{
my $action = [1, 2, 3];
my $exception = exception {
match_on_type $x => ( Int => $action );
};
like(
$exception,
qr/\QMatch action must be a CODE ref, not $action/,
"an ArrayRef is given as action");
#Match action must be a CODE ref, not ARRAY(0x27a0748)
isa_ok(
$exception,
'Moose::Exception::MatchActionMustBeACodeRef',
"an ArrayRef is given as action");
is(
$exception->type_name,
"Int",
"an ArrayRef is given as action");
is(
$exception->to_match,
$x,
"an ArrayRef is given as action");
is(
$exception->action,
$action,
"an ArrayRef is given as action");
}
{
my $exception = exception {
match_on_type $x => ( 'ArrayRef' => sub { "Action for Int"; } );
};
like(
$exception,
qr/\QNo cases matched for $x/,
"$x is not an ArrayRef");
#No cases matched for 123
isa_ok(
$exception,
'Moose::Exception::NoCasesMatched',
"$x is not an ArrayRef");
is(
$exception->to_match,
$x,
"$x is not an ArrayRef");
}
{
{
package TestType;
use Moose;
extends 'Moose::Meta::TypeConstraint';
sub name {
undef;
}
}
my $tt = TestType->new;
my $exception = exception {
register_type_constraint( $tt );
};
like(
$exception,
qr/can't register an unnamed type constraint/,
"name has been set to undef for TestType");
isa_ok(
$exception,
'Moose::Exception::CannotRegisterUnnamedTypeConstraint',
"name has been set to undef for TestType");
}
{
my $exception = exception {
union 'StrUndef', 'Str | Undef |';
};
like(
$exception,
qr/\Q'Str | Undef |' didn't parse (parse-pos=11 and str-length=13)/,
"cannot parse 'Str| Undef |'");
isa_ok(
$exception,
'Moose::Exception::CouldNotParseType',
"cannot parse 'Str| Undef |'");
is(
$exception->type,
'Str | Undef |',
"cannot parse 'Str| Undef |'");
}
done_testing;
|