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