Commit | Line | Data |
b841b2a3 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
9e93dd19 |
6 | use Test::More tests => 62; |
b841b2a3 |
7 | use Test::Exception; |
8 | |
9 | BEGIN { |
10 | use_ok('Moose'); |
11 | } |
e9bb8a31 |
12 | |
a7d0cd00 |
13 | ## Roles |
14 | |
78cd1d3b |
15 | { |
446e850f |
16 | package Eq; |
3824830b |
17 | use strict; |
18 | use warnings; |
a7d0cd00 |
19 | use Moose::Role; |
a7d0cd00 |
20 | |
446e850f |
21 | requires 'equal_to'; |
a7d0cd00 |
22 | |
446e850f |
23 | sub not_equal_to { |
24 | my ($self, $other) = @_; |
9e93dd19 |
25 | not $self->equal_to($other); |
446e850f |
26 | } |
a7d0cd00 |
27 | |
9e93dd19 |
28 | package Comparable; |
a7d0cd00 |
29 | use strict; |
30 | use warnings; |
31 | use Moose::Role; |
446e850f |
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); |
9e93dd19 |
60 | } |
61 | |
62 | package Printable; |
63 | use strict; |
64 | use warnings; |
65 | use Moose::Role; |
66 | |
67 | requires 'to_string'; |
a7d0cd00 |
68 | } |
69 | |
446e850f |
70 | ## Classes |
3824830b |
71 | |
a7d0cd00 |
72 | { |
446e850f |
73 | package US::Currency; |
3824830b |
74 | use strict; |
75 | use warnings; |
76 | use Moose; |
446e850f |
77 | |
9e93dd19 |
78 | with 'Comparable', 'Printable'; |
446e850f |
79 | |
81dc201f |
80 | has 'amount' => (is => 'rw', isa => 'Num', default => 0); |
446e850f |
81 | |
82 | sub compare { |
83 | my ($self, $other) = @_; |
84 | $self->amount <=> $other->amount; |
85 | } |
9e93dd19 |
86 | |
87 | sub to_string { |
88 | my $self = shift; |
89 | sprintf '$%0.2f USD' => $self->amount |
90 | } |
446e850f |
91 | } |
3824830b |
92 | |
9e93dd19 |
93 | ok(US::Currency->does('Comparable'), '... US::Currency does Comparable'); |
446e850f |
94 | ok(US::Currency->does('Eq'), '... US::Currency does Eq'); |
9e93dd19 |
95 | ok(US::Currency->does('Printable'), '... US::Currency does Printable'); |
3824830b |
96 | |
446e850f |
97 | my $hundred = US::Currency->new(amount => 100.00); |
98 | isa_ok($hundred, 'US::Currency'); |
3824830b |
99 | |
446e850f |
100 | can_ok($hundred, 'amount'); |
101 | is($hundred->amount, 100, '... got the right amount'); |
3824830b |
102 | |
9e93dd19 |
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'); |
446e850f |
107 | ok($hundred->does('Eq'), '... US::Currency does Eq'); |
9e93dd19 |
108 | ok($hundred->does('Printable'), '... US::Currency does Printable'); |
3824830b |
109 | |
446e850f |
110 | my $fifty = US::Currency->new(amount => 50.00); |
111 | isa_ok($fifty, 'US::Currency'); |
3824830b |
112 | |
446e850f |
113 | can_ok($fifty, 'amount'); |
114 | is($fifty->amount, 50, '... got the right amount'); |
3824830b |
115 | |
9e93dd19 |
116 | can_ok($fifty, 'to_string'); |
117 | is($fifty->to_string, '$50.00 USD', '... got the right stringified value'); |
118 | |
446e850f |
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'); |
3824830b |
125 | |
446e850f |
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'); |
3824830b |
132 | |
446e850f |
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'); |
a7d0cd00 |
139 | |
446e850f |
140 | ## ... check some meta-stuff |
a7d0cd00 |
141 | |
446e850f |
142 | # Eq |
ef333f17 |
143 | |
446e850f |
144 | my $eq_meta = Eq->meta; |
145 | isa_ok($eq_meta, 'Moose::Meta::Role'); |
a7d0cd00 |
146 | |
446e850f |
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'); |
a7d0cd00 |
149 | |
9e93dd19 |
150 | # Comparable |
ef333f17 |
151 | |
9e93dd19 |
152 | my $comparable_meta = Comparable->meta; |
153 | isa_ok($comparable_meta, 'Moose::Meta::Role'); |
a7d0cd00 |
154 | |
9e93dd19 |
155 | ok($comparable_meta->does_role('Eq'), '... Comparable does Eq'); |
a7d0cd00 |
156 | |
446e850f |
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 | )) { |
9e93dd19 |
162 | ok($comparable_meta->has_method($method_name), '... Comparable has_method ' . $method_name); |
446e850f |
163 | } |
a7d0cd00 |
164 | |
9e93dd19 |
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'); |
ef333f17 |
173 | |
446e850f |
174 | # US::Currency |
a7d0cd00 |
175 | |
446e850f |
176 | my $currency_meta = US::Currency->meta; |
177 | isa_ok($currency_meta, 'Moose::Meta::Class'); |
a7d0cd00 |
178 | |
9e93dd19 |
179 | ok($currency_meta->does_role('Comparable'), '... US::Currency does Comparable'); |
446e850f |
180 | ok($currency_meta->does_role('Eq'), '... US::Currency does Eq'); |
9e93dd19 |
181 | ok($currency_meta->does_role('Printable'), '... US::Currency does Printable'); |
ef333f17 |
182 | |
446e850f |
183 | foreach my $method_name (qw( |
184 | amount |
185 | equal_to not_equal_to |
186 | compare |
187 | greater_than greater_than_or_equal_to |
9e93dd19 |
188 | less_than less_than_or_equal_to |
189 | to_string |
446e850f |
190 | )) { |
191 | ok($currency_meta->has_method($method_name), '... US::Currency has_method ' . $method_name); |
192 | } |
a7d0cd00 |
193 | |