From: Shawn M Moore Date: Sat, 24 May 2008 19:13:11 +0000 (+0000) Subject: Basic test for recipe 22 X-Git-Tag: 0_55~149 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e2a5f2c688a94171e5eff67d940381059e1b7d3c;p=gitmo%2FMoose.git Basic test for recipe 22 --- diff --git a/MANIFEST b/MANIFEST index 9054e9c..008a175 100644 --- a/MANIFEST +++ b/MANIFEST @@ -78,6 +78,7 @@ t/000_recipes/005_recipe.t t/000_recipes/006_recipe.t t/000_recipes/007_recipe.t t/000_recipes/011_recipe.t +t/000_recipes/022_recipe.t t/010_basics/001_basic_class_setup.t t/010_basics/002_require_superclasses.t t/010_basics/003_super_and_override.t diff --git a/t/000_recipes/022_recipe.t b/t/000_recipes/022_recipe.t new file mode 100644 index 0000000..5986ca0 --- /dev/null +++ b/t/000_recipes/022_recipe.t @@ -0,0 +1,80 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 2; +use Test::Exception; + +BEGIN { + use_ok('Moose'); +} + +## attribute trait example +{ + + package MyApp::Meta::Attribute::Trait::Labeled; + use Moose::Role; + + has label => ( + is => 'rw', + isa => 'Str', + predicate => 'has_label', + ); + + package Moose::Meta::Attribute::Custom::Trait::Labeled; + sub register_implementation { 'MyApp::Meta::Attribute::Trait::Labeled' } + + package MyApp::Website; + use Moose; + + has url => ( + traits => [qw/Labeled/], + isa => 'Str', + is => 'rw', + label => "The site's URL", + ); + + has name => ( + is => 'rw', + isa => 'Str', + ); + + sub dump { + my $self = shift; + + my $dump_value = ''; + + # iterate over all the attributes in $self + my %attributes = %{ $self->meta->get_attribute_map }; + foreach my $name (sort keys %attributes) { + + my $attribute = $attributes{$name}; + + # print the label if available + if ($attribute->does('MyApp::Meta::Attribute::Trait::Labeled') + && $attribute->has_label) { + $dump_value .= $attribute->label; + } + # otherwise print the name + else { + $dump_value .= $name; + } + + # print the attribute's value + my $reader = $attribute->get_read_method; + $dump_value .= ": " . $self->$reader . "\n"; + } + + return $dump_value; + } + +} + +my $app = MyApp::Website->new(url => "http://google.com", name => "Google"); +is($app->dump, q{name: Google +The site's URL: http://google.com +}, '... got the expected dump value'); + + +