Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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.