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