Merge 'Moose-moosex_compile_support' into 'trunk'
[gitmo/Moose.git] / t / 000_recipes / 011_recipe.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More no_plan => 1;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('Moose');           
11 }
12
13 ## meta-attribute example
14 {
15
16     package MyApp::Meta::Attribute::Labeled;
17     use Moose;
18     extends 'Moose::Meta::Attribute';
19
20     has label => (
21         is  => 'rw',
22         isa => 'Str',
23         predicate => 'has_label',
24     );
25
26     package Moose::Meta::Attribute::Custom::Labeled;
27     sub register_implementation { 'MyApp::Meta::Attribute::Labeled' }
28
29     package MyApp::Website;
30     use Moose;
31
32     has url => (
33         metaclass => 'Labeled',
34         isa => 'Str',
35         is => 'rw',
36         label => "The site's URL",
37     );
38
39     has name => (
40         is => 'rw',
41         isa => 'Str',
42     );
43
44     sub dump {
45         my $self = shift;
46
47         my $dump_value = '';
48         
49         # iterate over all the attributes in $self
50         my %attributes = %{ $self->meta->get_attribute_map };
51         while (my ($name, $attribute) = each %attributes) {
52             
53             # print the label if available
54             if ($attribute->isa('MyApp::Meta::Attribute::Labeled')
55                 && $attribute->has_label) {
56                     $dump_value .= $attribute->label;
57             }
58             # otherwise print the name
59             else {
60                 $dump_value .= $name;
61             }
62
63             # print the attribute's value
64             my $reader = $attribute->get_read_method;
65             $dump_value .= ": " . $self->$reader . "\n";
66         }
67         
68         return $dump_value;
69     }
70
71 }
72
73 my $app = MyApp::Website->new(url => "http://google.com", name => "Google");
74 is($app->dump, q{The site's URL: http://google.com
75 name: Google
76 }, '... got the expected dump value');
77
78