Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View error_attribute.t
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
use 5.014;
use Data::Dump 'pp';
use Test::Most;
 
package ErrorStack {
use MooseX::Role::Parameterized;
parameter _attributes_ => (
required => 1,
traits => ['Array'],
handles => { attributes => 'elements' },
init_arg => 'attributes'
);
role {
my $parameter = shift;
has _errors_ => (
traits => ['Array'],
default => sub { [] },
handles => { errors => 'elements', add_error => 'push' }
);
for my $attribute ( $parameter->attributes ) {
around $attribute => sub {
my ( $orig, $self, @args ) = @_;
return $self->$orig(@args) || $self->add_error($attribute);
};
}
};
};
 
package Person {
use Moose;
has [qw( name age gender job partner )] => ( is => 'ro' );
with 'ErrorStack' => { 'attributes' => [qw( job partner )] };
};
 
note 'All requested attributes return undef';
{
my @attributes = qw( job partner );
my @errors = @attributes;
my %options = ();
my $person = new_ok Person => [%options];
lives_ok { $person->$_ for @attributes }
'survives calls: ' . pp \@attributes;
is_deeply [ $person->errors ], [@errors], 'errors: ' . pp [@errors];
}
 
note 'No requested attributes return undef';
{
my @attributes = qw( job partner );
my @errors = ();
my %options = ( job => 'plumber', partner => Person->new );
my $person = new_ok Person => [%options];
lives_ok { $person->$_ for @attributes }
'survives calls: ' . pp \@attributes;
is_deeply [ $person->errors ], [@errors], 'errors: ' . pp [@errors];
}
 
note 'Some requested attributes return undef';
{
my @attributes = qw( job partner );
my @errors = qw( partner );
my %options = ( job => 'plumber' );
my $person = new_ok Person => [%options];
lives_ok { $person->$_ for @attributes }
'survives calls: ' . pp \@attributes;
is_deeply [ $person->errors ], [@errors], 'errors: ' . pp [@errors];
}
 
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.