docs-n-attr-refactor
[gitmo/Moose.git] / t / 006_recipe.t
CommitLineData
b841b2a3 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
9e93dd19 6use Test::More tests => 62;
b841b2a3 7use Test::Exception;
8
9BEGIN {
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 93ok(US::Currency->does('Comparable'), '... US::Currency does Comparable');
446e850f 94ok(US::Currency->does('Eq'), '... US::Currency does Eq');
9e93dd19 95ok(US::Currency->does('Printable'), '... US::Currency does Printable');
3824830b 96
446e850f 97my $hundred = US::Currency->new(amount => 100.00);
98isa_ok($hundred, 'US::Currency');
3824830b 99
446e850f 100can_ok($hundred, 'amount');
101is($hundred->amount, 100, '... got the right amount');
3824830b 102
9e93dd19 103can_ok($hundred, 'to_string');
104is($hundred->to_string, '$100.00 USD', '... got the right stringified value');
105
106ok($hundred->does('Comparable'), '... US::Currency does Comparable');
446e850f 107ok($hundred->does('Eq'), '... US::Currency does Eq');
9e93dd19 108ok($hundred->does('Printable'), '... US::Currency does Printable');
3824830b 109
446e850f 110my $fifty = US::Currency->new(amount => 50.00);
111isa_ok($fifty, 'US::Currency');
3824830b 112
446e850f 113can_ok($fifty, 'amount');
114is($fifty->amount, 50, '... got the right amount');
3824830b 115
9e93dd19 116can_ok($fifty, 'to_string');
117is($fifty->to_string, '$50.00 USD', '... got the right stringified value');
118
446e850f 119ok($hundred->greater_than($fifty), '... 100 gt 50');
120ok($hundred->greater_than_or_equal_to($fifty), '... 100 ge 50');
121ok(!$hundred->less_than($fifty), '... !100 lt 50');
122ok(!$hundred->less_than_or_equal_to($fifty), '... !100 le 50');
123ok(!$hundred->equal_to($fifty), '... !100 eq 50');
124ok($hundred->not_equal_to($fifty), '... 100 ne 50');
3824830b 125
446e850f 126ok(!$fifty->greater_than($hundred), '... !50 gt 100');
127ok(!$fifty->greater_than_or_equal_to($hundred), '... !50 ge 100');
128ok($fifty->less_than($hundred), '... 50 lt 100');
129ok($fifty->less_than_or_equal_to($hundred), '... 50 le 100');
130ok(!$fifty->equal_to($hundred), '... !50 eq 100');
131ok($fifty->not_equal_to($hundred), '... 50 ne 100');
3824830b 132
446e850f 133ok(!$fifty->greater_than($fifty), '... !50 gt 50');
134ok($fifty->greater_than_or_equal_to($fifty), '... !50 ge 50');
135ok(!$fifty->less_than($fifty), '... 50 lt 50');
136ok($fifty->less_than_or_equal_to($fifty), '... 50 le 50');
137ok($fifty->equal_to($fifty), '... 50 eq 50');
138ok(!$fifty->not_equal_to($fifty), '... !50 ne 50');
a7d0cd00 139
446e850f 140## ... check some meta-stuff
a7d0cd00 141
446e850f 142# Eq
ef333f17 143
446e850f 144my $eq_meta = Eq->meta;
145isa_ok($eq_meta, 'Moose::Meta::Role');
a7d0cd00 146
446e850f 147ok($eq_meta->has_method('not_equal_to'), '... Eq has_method not_equal_to');
148ok($eq_meta->requires_method('equal_to'), '... Eq requires_method not_equal_to');
a7d0cd00 149
9e93dd19 150# Comparable
ef333f17 151
9e93dd19 152my $comparable_meta = Comparable->meta;
153isa_ok($comparable_meta, 'Moose::Meta::Role');
a7d0cd00 154
9e93dd19 155ok($comparable_meta->does_role('Eq'), '... Comparable does Eq');
a7d0cd00 156
446e850f 157foreach 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 165ok($comparable_meta->requires_method('compare'), '... Comparable requires_method compare');
166
167# Printable
168
169my $printable_meta = Printable->meta;
170isa_ok($printable_meta, 'Moose::Meta::Role');
171
172ok($printable_meta->requires_method('to_string'), '... Printable requires_method to_string');
ef333f17 173
446e850f 174# US::Currency
a7d0cd00 175
446e850f 176my $currency_meta = US::Currency->meta;
177isa_ok($currency_meta, 'Moose::Meta::Class');
a7d0cd00 178
9e93dd19 179ok($currency_meta->does_role('Comparable'), '... US::Currency does Comparable');
446e850f 180ok($currency_meta->does_role('Eq'), '... US::Currency does Eq');
9e93dd19 181ok($currency_meta->does_role('Printable'), '... US::Currency does Printable');
ef333f17 182
446e850f 183foreach 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