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
|
use strict;
use warnings;
use Test::More;
use Test::Fatal;
use Moose::Util::TypeConstraints;
use Moose();
# tests for type coercions
{
subtype 'HexNum' => as 'Int', where { /[a-f0-9]/i };
my $type_object = find_type_constraint 'HexNum';
my $exception = exception {
$type_object->coerce;
};
like(
$exception,
qr/Cannot coerce without a type coercion/,
"You cannot coerce a type unless coercion is supported by that type");
is(
$exception->type_name,
'HexNum',
"You cannot coerce a type unless coercion is supported by that type");
isa_ok(
$exception,
"Moose::Exception::CoercingWithoutCoercions",
"You cannot coerce a type unless coercion is supported by that type");
}
{
my $exception = exception {
Moose::Meta::TypeConstraint->new( message => "foo");
};
like(
$exception,
qr/The 'message' parameter must be a coderef/,
"'foo' is not a CODE ref");
isa_ok(
$exception,
"Moose::Exception::MessageParameterMustBeCodeRef",
"'foo' is not a CODE ref");
}
{
subtype 'NotInlinable',
as 'Str',
where { $_ !~ /Q/ };
my $not_inlinable = find_type_constraint('NotInlinable');
my $exception = exception {
$not_inlinable->_inline_check('$foo');
};
like(
$exception,
qr/Cannot inline a type constraint check for NotInlinable/,
"cannot inline NotInlinable");
isa_ok(
$exception,
"Moose::Exception::CannotInlineTypeConstraintCheck",
"cannot inline NotInlinable");
is(
$exception->type_name,
"NotInlinable",
"cannot inline NotInlinable");
is(
find_type_constraint( $exception->type_name ),
$not_inlinable,
"cannot inline NotInlinable");
}
{
my $exception = exception {
Moose::Meta::TypeConstraint->new(name => "FooTypeConstraint", constraint => undef)
};
like(
$exception,
qr/Could not compile type constraint 'FooTypeConstraint' because no constraint check/,
"constraint is set to undef");
isa_ok(
$exception,
"Moose::Exception::NoConstraintCheckForTypeConstraint",
"constraint is set to undef");
is(
$exception->type_name,
"FooTypeConstraint",
"constraint is set to undef");
}
{
subtype 'OnlyPositiveInts',
as 'Int',
where { $_ > 1 };
my $onlyposint = find_type_constraint('OnlyPositiveInts');
my $exception = exception {
$onlyposint->assert_valid( -123 );
};
like(
$exception,
qr/Validation failed for 'OnlyPositiveInts' with value -123/,
"-123 is not valid for OnlyPositiveInts");
isa_ok(
$exception,
"Moose::Exception::ValidationFailedForTypeConstraint",
"-123 is not valid for OnlyPositiveInts");
is(
$exception->type->name,
"OnlyPositiveInts",
"-123 is not valid for OnlyPositiveInts");
is(
$exception->type,
$onlyposint,
"-123 is not valid for OnlyPositiveInts");
is(
$exception->value,
-123,
"-123 is not valid for OnlyPositiveInts");
}
done_testing;
|