docs-n-attr-refactor
[gitmo/Moose.git] / t / 006_recipe.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 62;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('Moose');           
11 }
12
13 ## Roles
14
15 {
16     package Eq;
17     use strict;
18     use warnings;
19     use Moose::Role;
20     
21     requires 'equal_to';
22     
23     sub not_equal_to { 
24         my ($self, $other) = @_;
25         not $self->equal_to($other);
26     }
27     
28     package Comparable;
29     use strict;
30     use warnings;
31     use Moose::Role;
32     
33     with 'Eq';
34     
35     requires 'compare';
36     
37     sub equal_to {
38         my ($self, $other) = @_;
39         $self->compare($other) == 0;
40     }    
41     
42     sub greater_than {
43         my ($self, $other) = @_;
44         $self->compare($other) == 1;
45     }    
46     
47     sub less_than {
48         my ($self, $other) = @_;
49         $self->compare($other) == -1;
50     }
51     
52     sub greater_than_or_equal_to {
53         my ($self, $other) = @_;
54         $self->greater_than($other) || $self->equal_to($other);
55     }        
56
57     sub less_than_or_equal_to {
58         my ($self, $other) = @_;
59         $self->less_than($other) || $self->equal_to($other);
60     }  
61     
62     package Printable;
63     use strict;
64     use warnings;
65     use Moose::Role;
66     
67     requires 'to_string';    
68 }
69
70 ## Classes
71
72 {
73     package US::Currency;
74     use strict;
75     use warnings;
76     use Moose;
77     
78     with 'Comparable', 'Printable';
79     
80     has 'amount' => (is => 'rw', isa => 'Num', default => 0);
81     
82     sub compare {
83         my ($self, $other) = @_;
84         $self->amount <=> $other->amount;
85     }
86     
87     sub to_string {
88         my $self = shift;
89         sprintf '$%0.2f USD' => $self->amount
90     }
91 }
92
93 ok(US::Currency->does('Comparable'), '... US::Currency does Comparable');
94 ok(US::Currency->does('Eq'), '... US::Currency does Eq');
95 ok(US::Currency->does('Printable'), '... US::Currency does Printable');
96
97 my $hundred = US::Currency->new(amount => 100.00);
98 isa_ok($hundred, 'US::Currency');
99
100 can_ok($hundred, 'amount');
101 is($hundred->amount, 100, '... got the right amount');
102
103 can_ok($hundred, 'to_string');
104 is($hundred->to_string, '$100.00 USD', '... got the right stringified value');
105
106 ok($hundred->does('Comparable'), '... US::Currency does Comparable');
107 ok($hundred->does('Eq'), '... US::Currency does Eq');
108 ok($hundred->does('Printable'), '... US::Currency does Printable');
109
110 my $fifty = US::Currency->new(amount => 50.00);
111 isa_ok($fifty, 'US::Currency');
112
113 can_ok($fifty, 'amount');
114 is($fifty->amount, 50, '... got the right amount');
115
116 can_ok($fifty, 'to_string');
117 is($fifty->to_string, '$50.00 USD', '... got the right stringified value');
118
119 ok($hundred->greater_than($fifty),             '... 100 gt 50');
120 ok($hundred->greater_than_or_equal_to($fifty), '... 100 ge 50');
121 ok(!$hundred->less_than($fifty),               '... !100 lt 50');
122 ok(!$hundred->less_than_or_equal_to($fifty),   '... !100 le 50');
123 ok(!$hundred->equal_to($fifty),                '... !100 eq 50');
124 ok($hundred->not_equal_to($fifty),             '... 100 ne 50');
125
126 ok(!$fifty->greater_than($hundred),             '... !50 gt 100');
127 ok(!$fifty->greater_than_or_equal_to($hundred), '... !50 ge 100');
128 ok($fifty->less_than($hundred),                 '... 50 lt 100');
129 ok($fifty->less_than_or_equal_to($hundred),     '... 50 le 100');
130 ok(!$fifty->equal_to($hundred),                 '... !50 eq 100');
131 ok($fifty->not_equal_to($hundred),              '... 50 ne 100');
132
133 ok(!$fifty->greater_than($fifty),            '... !50 gt 50');
134 ok($fifty->greater_than_or_equal_to($fifty), '... !50 ge 50');
135 ok(!$fifty->less_than($fifty),               '... 50 lt 50');
136 ok($fifty->less_than_or_equal_to($fifty),    '... 50 le 50');
137 ok($fifty->equal_to($fifty),                 '... 50 eq 50');
138 ok(!$fifty->not_equal_to($fifty),            '... !50 ne 50');
139
140 ## ... check some meta-stuff
141
142 # Eq
143
144 my $eq_meta = Eq->meta;
145 isa_ok($eq_meta, 'Moose::Meta::Role');
146
147 ok($eq_meta->has_method('not_equal_to'), '... Eq has_method not_equal_to');
148 ok($eq_meta->requires_method('equal_to'), '... Eq requires_method not_equal_to');
149
150 # Comparable
151
152 my $comparable_meta = Comparable->meta;
153 isa_ok($comparable_meta, 'Moose::Meta::Role');
154
155 ok($comparable_meta->does_role('Eq'), '... Comparable does Eq');
156
157 foreach my $method_name (qw(
158                         equal_to not_equal_to
159                         greater_than greater_than_or_equal_to
160                         less_than less_than_or_equal_to                            
161                         )) {
162     ok($comparable_meta->has_method($method_name), '... Comparable has_method ' . $method_name);
163 }
164
165 ok($comparable_meta->requires_method('compare'), '... Comparable requires_method compare');
166
167 # Printable
168
169 my $printable_meta = Printable->meta;
170 isa_ok($printable_meta, 'Moose::Meta::Role');
171
172 ok($printable_meta->requires_method('to_string'), '... Printable requires_method to_string');
173
174 # US::Currency
175
176 my $currency_meta = US::Currency->meta;
177 isa_ok($currency_meta, 'Moose::Meta::Class');
178
179 ok($currency_meta->does_role('Comparable'), '... US::Currency does Comparable');
180 ok($currency_meta->does_role('Eq'), '... US::Currency does Eq');
181 ok($currency_meta->does_role('Printable'), '... US::Currency does Printable');
182
183 foreach my $method_name (qw(
184                         amount
185                         equal_to not_equal_to
186                         compare
187                         greater_than greater_than_or_equal_to
188                         less_than less_than_or_equal_to     
189                         to_string                       
190                         )) {
191     ok($currency_meta->has_method($method_name), '... US::Currency has_method ' . $method_name);
192 }
193