}
}
+sub assert_valid {
+ my ($self, $value) = @_;
+
+ my $error = $self->validate($value);
+ return 1 if ! defined $error;
+
+ require Moose;
+ Moose->throw_error($error);
+}
+
sub get_message {
my ($self, $value) = @_;
if (my $msg = $self->message) {
use strict;
use warnings;
-use Test::More tests => 8;
+use Test::More tests => 11;
use Test::Exception;
BEGIN {
my $subtype2 = subtype 'New2' => as $subtype1;
ok $subtype2 => 'made a subtype of our subtype';
+
+# assert_valid
+
+{
+ my $type = find_type_constraint('Num');
+
+ my $ok_1 = eval { $type->assert_valid(1); };
+ ok($ok_1, "we can assert_valid that 1 is of type $type");
+
+ my $ok_2 = eval { $type->assert_valid('foo'); };
+ my $error = $@;
+ ok(! $ok_2, "'foo' is not of type $type");
+ like(
+ $error,
+ qr{validation failed for .\Q$type\E.}i,
+ "correct error thrown"
+ );
+}