Version 0.06
[gitmo/MooseX-MetaDescription.git] / t / 002_custom_description.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More 'no_plan';
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('MooseX::MetaDescription');
11 }
12
13 {
14     package Foo::Description;
15     use Moose;
16
17     extends 'MooseX::MetaDescription::Description';
18
19     has 'bar'   => (is => 'ro', isa => 'Str');
20     has 'baz'   => (is => 'ro', isa => 'Str');
21     has 'gorch' => (is => 'ro', isa => 'Str');
22
23     package Foo::MetaDescription::Trait;
24     use Moose::Role;
25
26     with 'MooseX::MetaDescription::Meta::Trait';
27
28     has 'metadescription_classname' => (
29         default => 'Foo::Description',
30         is => 'ro',
31     );
32
33     package Foo::MetaDescription::Attribute;
34     use Moose;
35
36     extends 'MooseX::MetaDescription::Meta::Attribute';
37
38     has 'metadescription_classname' => (
39         default => 'Foo::Description',
40         is => 'ro',
41     );
42 }
43
44 {
45     package Foo;
46     use Moose;
47
48     has 'bar' => (
49         metaclass   => 'Foo::MetaDescription::Attribute',
50         is          => 'ro',
51         isa         => 'Str',
52         default     => sub { 'Foo::bar' },
53         description => {
54             baz   => 'Foo::bar::baz',
55             gorch => 'Foo::bar::gorch',
56         }
57     );
58
59     has 'baz' => (
60         traits      => [ 'Foo::MetaDescription::Trait' ],
61         is          => 'ro',
62         isa         => 'Str',
63         default     => sub { 'Foo::baz' },
64         description => {
65             bar   => 'Foo::baz::bar',
66             gorch => 'Foo::baz::gorch',
67         }
68     );
69 }
70
71 # check the meta-desc
72
73 my $bar_attr = Foo->meta->get_attribute('bar');
74 isa_ok($bar_attr->metadescription, 'MooseX::MetaDescription::Description');
75 isa_ok($bar_attr->metadescription, 'Foo::Description');
76 is($bar_attr->metadescription->descriptor, $bar_attr, '... got the circular ref');
77
78 my $baz_attr = Foo->meta->get_attribute('baz');
79 isa_ok($baz_attr->metadescription, 'MooseX::MetaDescription::Description');
80 isa_ok($baz_attr->metadescription, 'Foo::Description');
81 is($baz_attr->metadescription->descriptor, $baz_attr, '... got the circular ref');
82
83 # check the actual descs
84
85 foreach my $foo ('Foo', Foo->new) {
86
87     is_deeply(
88         $foo->meta->get_attribute('bar')->description,
89         {
90             baz   => 'Foo::bar::baz',
91             gorch => 'Foo::bar::gorch',
92         },
93         '... got the right class description'
94     );
95
96     my $bar_meta_desc = $foo->meta->get_attribute('bar')->metadescription;
97     is($bar_meta_desc->baz,   'Foo::bar::baz',   '... we have methods');
98     is($bar_meta_desc->gorch, 'Foo::bar::gorch', '... we have methods');
99
100     is_deeply(
101         $foo->meta->get_attribute('baz')->description,
102         {
103             bar   => 'Foo::baz::bar',
104             gorch => 'Foo::baz::gorch',
105         },
106         '... got the right class description'
107     );
108
109     my $baz_meta_desc = $foo->meta->get_attribute('baz')->metadescription;
110     is($baz_meta_desc->bar,   'Foo::baz::bar',   '... we have methods');
111     is($baz_meta_desc->gorch, 'Foo::baz::gorch', '... we have methods');
112 }
113