Last active
September 3, 2018 14:22
-
-
Save j1n3l0/87422666c4e2a890a07126093d235e89 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.026; | |
use Test::Most; | |
{ | |
package TemplateGen; | |
use Moose; | |
use Types::Standard qw( ArrayRef Str ); | |
use experimental qw( signatures ); | |
has descriptors => ( is => 'ro', isa => ArrayRef[Str], lazy => 1, builder => '_build_descriptors' ); | |
has force_parse => ( is => 'ro', isa => Str, lazy => 1, builder => '_build_force_parse' ); | |
has name => ( is => 'ro', isa => Str, required => 1 ); | |
around descriptors => sub ($orig, $self) { [ 'api_key', $self->$orig->@* ] }; | |
sub _build_descriptors ($self) { [] } | |
sub _build_force_parse ($self) { | |
join( | |
' ', | |
'force_parse:', | |
map join('', '%', join('_', $self->name, $_), '%'), $self->descriptors->@* | |
); | |
} | |
} | |
{ | |
my $generator = new_ok TemplateGen => [ | |
descriptors => [ qw( sector ) ], | |
name => 'client', | |
]; | |
can_ok $generator, qw( name descriptors force_parse ); | |
cmp_methods( | |
$generator, | |
[ | |
descriptors => bag( qw( api_key sector ) ), | |
force_parse => 'force_parse: %client_api_key% %client_sector%', | |
name => 'client', | |
], | |
'attributes are as expceted', | |
); | |
} | |
done_testing; |
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.024; | |
use Test::Most; | |
{ | |
package Meta::Attribute::Trait::Section; | |
use Moose::Role; | |
Moose::Util::meta_attribute_alias('Section'); | |
after install_accessors => sub { | |
my $self = shift; | |
my $class = $self->associated_class; | |
$class->add_attribute( | |
sections => { is => 'ro', lazy => 1, builder => '_build_sections' }, | |
); | |
$class->add_method( | |
_build_sections => sub { | |
my $self = shift; | |
my $meta = $self->meta; | |
[ | |
map $_->name, | |
grep $_->does('Meta::Attribute::Trait::Section'), | |
map $meta->get_attribute($_), | |
sort $meta->get_attribute_list | |
]; | |
}, | |
); | |
}; | |
} | |
{ | |
package Foo; | |
use Moose; | |
has foo => ( is => 'ro', traits => ['Section'] ); | |
} | |
{ | |
package Bar; | |
use Moose; | |
has ['bar', 'baz'] => ( is => 'ro', traits => ['Section'] ); | |
} | |
{ | |
my $object = new_ok Foo => []; | |
can_ok $object, 'sections'; | |
cmp_bag $object->sections, ['foo'], 'have the expected sections'; | |
} | |
{ | |
my $object = new_ok Bar => []; | |
can_ok $object, 'sections'; | |
cmp_bag $object->sections, ['bar', 'baz'], 'have the expected sections'; | |
} | |
done_testing; |
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.024; | |
use Test::Most; | |
{ | |
package Meta::Attribute::Trait::Tag; | |
use Moose::Role; | |
use Types::Standard qw(Enum); | |
Moose::Util::meta_attribute_alias('Tag'); | |
has tag => ( | |
is => 'ro', | |
required => 1, | |
isa => Enum['header', 'meta_data'], | |
); | |
after install_accessors => sub { | |
my $self = shift; | |
my $class = $self->associated_class; | |
$class->add_method( | |
attributes_tagged_as => sub { | |
my $self = shift; | |
my $tag = shift; | |
my $meta = $self->meta; | |
map $_->name, | |
grep $_->tag eq $tag, | |
grep $_->does('Meta::Attribute::Trait::Tag'), | |
map $meta->get_attribute($_), | |
sort $meta->get_attribute_list; | |
}, | |
); | |
}; | |
} | |
{ | |
package TemplateGen; | |
use Moose; | |
has ['url'] => ( | |
is => 'ro', | |
tag => 'meta_data', | |
traits => ['Tag'], | |
); | |
has ['tech_name', 'ticket_number'] => ( | |
is => 'ro', | |
tag => 'header', | |
traits => ['Tag'], | |
); | |
} | |
{ | |
my $template_gen = new_ok( TemplateGen => [] ); | |
cmp_bag( | |
[ $template_gen->attributes_tagged_as('header') ], | |
['tech_name', 'ticket_number'], | |
'found the attributes tagged as "header"', | |
); | |
cmp_bag( | |
[ $template_gen->attributes_tagged_as('meta_data') ], | |
['url'], | |
'found the attributes tagged as "meta_data"', | |
); | |
} | |
done_testing; |
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.024; | |
use Test::Most; | |
package App::Meta::Attribute::Trait::Templatize { | |
use Moose::Role; | |
Moose::Util::meta_attribute_alias('Templatize'); | |
has label => ( is => 'ro', lazy => 1, default => sub { (shift)->name } ); | |
}; | |
package App::TempGen { | |
use Moo; | |
has html_support => ( is => 'ro', traits => ['Templatize'], default => 1, coerce => sub { $_[0] ? 'yes' : 'no' } ); | |
has nice_name => ( is => 'ro', traits => ['Templatize'], label => 'name' ); | |
has template => ( is => 'ro', lazy => 1, builder => '_build_template' ); | |
sub _build_template { | |
my $self = shift; | |
my $meta = $self->meta; | |
join( | |
"\n", | |
map join( ': ', $_->label, $_->get_value($self) ), | |
grep $_->does('App::Meta::Attribute::Trait::Templatize'), | |
map $meta->get_attribute($_), | |
sort $meta->get_attribute_list, | |
); | |
} | |
}; | |
subtest 'traits => ["Templatize"]' => sub { | |
cmp_deeply( | |
new_ok( 'App::TempGen', [ nice_name => 'Some Client' ] ), | |
methods( | |
html_support => 'yes', | |
template => "html_support: yes\nname: Some Client", | |
), | |
'template striingifies as expected', | |
); | |
}; | |
done_testing; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment