BUGS
[gitmo/Moose.git] / t / 006_basic.t
CommitLineData
b841b2a3 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
446e850f 6use Test::More tests => 52;
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) = @_;
25 !$self->equal_to($other);
26 }
a7d0cd00 27
446e850f 28 package Ord;
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);
60 }
a7d0cd00 61}
62
446e850f 63## Classes
3824830b 64
a7d0cd00 65{
446e850f 66 package US::Currency;
3824830b 67 use strict;
68 use warnings;
69 use Moose;
446e850f 70
71 with 'Ord';
72
73 has 'amount' => (is => 'rw', isa => 'Int', default => 0);
74
75 sub compare {
76 my ($self, $other) = @_;
77 $self->amount <=> $other->amount;
78 }
79}
3824830b 80
446e850f 81ok(US::Currency->does('Ord'), '... US::Currency does Ord');
82ok(US::Currency->does('Eq'), '... US::Currency does Eq');
3824830b 83
446e850f 84my $hundred = US::Currency->new(amount => 100.00);
85isa_ok($hundred, 'US::Currency');
3824830b 86
446e850f 87can_ok($hundred, 'amount');
88is($hundred->amount, 100, '... got the right amount');
3824830b 89
446e850f 90ok($hundred->does('Ord'), '... US::Currency does Ord');
91ok($hundred->does('Eq'), '... US::Currency does Eq');
3824830b 92
446e850f 93my $fifty = US::Currency->new(amount => 50.00);
94isa_ok($fifty, 'US::Currency');
3824830b 95
446e850f 96can_ok($fifty, 'amount');
97is($fifty->amount, 50, '... got the right amount');
3824830b 98
446e850f 99ok($hundred->greater_than($fifty), '... 100 gt 50');
100ok($hundred->greater_than_or_equal_to($fifty), '... 100 ge 50');
101ok(!$hundred->less_than($fifty), '... !100 lt 50');
102ok(!$hundred->less_than_or_equal_to($fifty), '... !100 le 50');
103ok(!$hundred->equal_to($fifty), '... !100 eq 50');
104ok($hundred->not_equal_to($fifty), '... 100 ne 50');
3824830b 105
446e850f 106ok(!$fifty->greater_than($hundred), '... !50 gt 100');
107ok(!$fifty->greater_than_or_equal_to($hundred), '... !50 ge 100');
108ok($fifty->less_than($hundred), '... 50 lt 100');
109ok($fifty->less_than_or_equal_to($hundred), '... 50 le 100');
110ok(!$fifty->equal_to($hundred), '... !50 eq 100');
111ok($fifty->not_equal_to($hundred), '... 50 ne 100');
3824830b 112
446e850f 113ok(!$fifty->greater_than($fifty), '... !50 gt 50');
114ok($fifty->greater_than_or_equal_to($fifty), '... !50 ge 50');
115ok(!$fifty->less_than($fifty), '... 50 lt 50');
116ok($fifty->less_than_or_equal_to($fifty), '... 50 le 50');
117ok($fifty->equal_to($fifty), '... 50 eq 50');
118ok(!$fifty->not_equal_to($fifty), '... !50 ne 50');
a7d0cd00 119
446e850f 120## ... check some meta-stuff
a7d0cd00 121
446e850f 122# Eq
ef333f17 123
446e850f 124my $eq_meta = Eq->meta;
125isa_ok($eq_meta, 'Moose::Meta::Role');
a7d0cd00 126
446e850f 127ok($eq_meta->has_method('not_equal_to'), '... Eq has_method not_equal_to');
128ok($eq_meta->requires_method('equal_to'), '... Eq requires_method not_equal_to');
a7d0cd00 129
446e850f 130# Ord
ef333f17 131
446e850f 132my $ord_meta = Ord->meta;
133isa_ok($ord_meta, 'Moose::Meta::Role');
a7d0cd00 134
446e850f 135ok($ord_meta->does_role('Eq'), '... Ord does Eq');
a7d0cd00 136
446e850f 137foreach my $method_name (qw(
138 equal_to not_equal_to
139 greater_than greater_than_or_equal_to
140 less_than less_than_or_equal_to
141 )) {
142 ok($ord_meta->has_method($method_name), '... Ord has_method ' . $method_name);
143}
a7d0cd00 144
446e850f 145ok($ord_meta->requires_method('compare'), '... Ord requires_method compare');
ef333f17 146
446e850f 147# US::Currency
a7d0cd00 148
446e850f 149my $currency_meta = US::Currency->meta;
150isa_ok($currency_meta, 'Moose::Meta::Class');
a7d0cd00 151
446e850f 152ok($currency_meta->does_role('Ord'), '... US::Currency does Ord');
153ok($currency_meta->does_role('Eq'), '... US::Currency does Eq');
ef333f17 154
446e850f 155foreach my $method_name (qw(
156 amount
157 equal_to not_equal_to
158 compare
159 greater_than greater_than_or_equal_to
160 less_than less_than_or_equal_to
161 )) {
162 ok($currency_meta->has_method($method_name), '... US::Currency has_method ' . $method_name);
163}
a7d0cd00 164