Merge branch 'blead'
[gitmo/Mouse.git] / t / 000-recipes / moose_cookbook_meta_recipe3.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Test::More 'no_plan';
5 use Test::Exception;
6 $| = 1;
7
8
9
10 # =begin testing SETUP
11 {
12
13   package MyApp::Meta::Attribute::Trait::Labeled;
14   use Mouse::Role;
15
16   has label => (
17       is        => 'rw',
18       isa       => 'Str',
19       predicate => 'has_label',
20   );
21
22   package Mouse::Meta::Attribute::Custom::Trait::Labeled;
23   sub register_implementation {'MyApp::Meta::Attribute::Trait::Labeled'}
24
25   package MyApp::Website;
26   use Mouse;
27
28   has url => (
29       traits => [qw/Labeled/],
30       is     => 'rw',
31       isa    => 'Str',
32       label  => "The site's URL",
33   );
34
35   has name => (
36       is  => 'rw',
37       isa => 'Str',
38   );
39
40   sub dump {
41       my $self = shift;
42
43       my $dump = '';
44
45       my %attributes = %{ $self->meta->get_attribute_map };
46       for my $name ( sort keys %attributes ) {
47           my $attribute = $attributes{$name};
48
49           if (   $attribute->does('MyApp::Meta::Attribute::Trait::Labeled')
50               && $attribute->has_label ) {
51               $dump .= $attribute->label;
52           }
53           else {
54               $dump .= $name;
55           }
56
57           my $reader = $attribute->get_read_method;
58           $dump .= ": " . $self->$reader . "\n";
59       }
60
61       return $dump;
62   }
63
64   package main;
65
66   my $app = MyApp::Website->new( url => "http://google.com", name => "Google" );
67 }
68
69
70
71 # =begin testing
72 {
73 my $app2
74     = MyApp::Website->new( url => "http://google.com", name => "Google" );
75 is(
76     $app2->dump, q{name: Google
77 The site's URL: http://google.com
78 }, '... got the expected dump value'
79 );
80 }
81
82
83
84
85 1;