Permalink
Browse files

convert to EasyNew, support struct fail

  • Loading branch information...
rjbs committed Jul 1, 2012
1 parent 92a2495 commit c3ad3f04ff620a025a877310c50612f507cb9a95
Showing with 35 additions and 15 deletions.
  1. +13 −8 lib/Data/Rx/Type/MooseTC.pm
  2. +22 −7 t/basic.t
@@ -2,6 +2,7 @@ use strict;
use warnings;
package Data::Rx::Type::MooseTC;
# ABSTRACT: experimental / proof of concept Rx types from Moose types
use parent 'Data::Rx::CommonType::EasyNew';
use Carp ();
use Moose::Util::TypeConstraints ();
@@ -37,8 +38,8 @@ Moose type constraints may change their interface in the future.
sub type_uri { 'tag:rjbs.manxome.org,2008-10-04:rx/moose/tc' }
sub new_checker {
my ($class, $arg, $rx) = @_;
sub guts_from_arg {
my ($class, $arg) = @_;
Carp::croak("no type supplied for $class") unless my $mt = $arg->{moose_type};
@@ -55,16 +56,20 @@ sub new_checker {
Carp::croak("could not make Moose type constraint from $mt")
unless $tc->isa('Moose::Meta::TypeConstraint');
my $self = { tc => $tc };
bless $self => $class;
return $self;
return { tc => $tc };
}
sub check {
sub assert_valid {
my ($self, $value) = @_;
return unless $self->{tc}->check($value);
unless ($self->{tc}->check($value)) {
$self->fail({
error => [ qw(type) ],
message => "found value does not pass type constraint",
value => $value,
});
}
return 1;
}
View
@@ -3,7 +3,7 @@ use warnings;
use Data::Rx;
use Data::Rx::Type::MooseTC;
use Test::More tests => 2;
use Test::More tests => 4;
my $rx = Data::Rx->new({
prefix => {
@@ -12,10 +12,25 @@ my $rx = Data::Rx->new({
type_plugins => [ 'Data::Rx::Type::MooseTC' ]
});
my $array_of_int = $rx->make_schema({
type => '/moose/tc',
moose_type => 'ArrayRef[Int]',
});
{
my $array_of_int = $rx->make_schema({
type => '/moose/tc',
moose_type => 'ArrayRef[Int]',
});
ok($array_of_int->check([1]), "[1] is an ArrayRef[Int]");
ok(! $array_of_int->check( 1 ), "1 is not an ArrayRef[Int]");
}
{
my $array_of_int = $rx->make_schema({
type => '//arr',
contents => {
type => '/moose/tc',
moose_type => 'Int',
},
});
ok($array_of_int->check([1]), "[1] is an ArrayRef[Int]");
ok(! $array_of_int->check( 1 ), "1 is not an ArrayRef[Int]");
ok($array_of_int->check([1]), "[1] is an ArrayRef[Int]");
ok(! $array_of_int->check( 1 ), "1 is not an ArrayRef[Int]");
}

0 comments on commit c3ad3f0

Please sign in to comment.