Created
May 24, 2013 14:25
-
-
Save j1n3l0/5643884 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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