ROLES
[gitmo/Moose.git] / t / 006_basic.t
CommitLineData
b841b2a3 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a7d0cd00 6use Test::More tests => 15;
b841b2a3 7use Test::Exception;
8
9BEGIN {
10 use_ok('Moose');
11}
e9bb8a31 12
a7d0cd00 13## Roles
14
78cd1d3b 15{
3824830b 16 package Constraint;
17 use strict;
18 use warnings;
a7d0cd00 19 use Moose::Role;
3824830b 20
a7d0cd00 21 has 'value' => (isa => 'Int', is => 'ro');
3824830b 22
a7d0cd00 23 around 'validate' => sub {
24 my $c = shift;
25 my ($self, $field) = @_;
26 if ($c->($self, $self->validation_value($field))) {
27 return undef;
28 }
29 else {
30 return $self->error_message;
31 }
32 };
33
3824830b 34 sub validation_value {
35 my ($self, $field) = @_;
a7d0cd00 36 return $field;
3824830b 37 }
a7d0cd00 38
39 sub error_message { confess "Abstract method!" }
40
41 package Constraint::OnLength;
42 use strict;
43 use warnings;
44 use Moose::Role;
45
46 has 'units' => (isa => 'Str', is => 'ro');
47
48 override 'validation_value' => sub {
49 return length(super());
50 };
51
52 override 'error_message' => sub {
53 my $self = shift;
54 return super() . ' ' . $self->units;
55 };
56
57}
58
59## Classes
3824830b 60
a7d0cd00 61{
3824830b 62 package Constraint::AtLeast;
63 use strict;
64 use warnings;
65 use Moose;
66
a7d0cd00 67 with 'Constraint';
3824830b 68
69 sub validate {
70 my ($self, $field) = @_;
a7d0cd00 71 ($field >= $self->value);
3824830b 72 }
73
74 sub error_message { 'must be at least ' . (shift)->value; }
75
76 package Constraint::NoMoreThan;
77 use strict;
78 use warnings;
79 use Moose;
80
a7d0cd00 81 with 'Constraint';
3824830b 82
83 sub validate {
84 my ($self, $field) = @_;
a7d0cd00 85 ($field <= $self->value);
3824830b 86 }
87
88 sub error_message { 'must be no more than ' . (shift)->value; }
89
3824830b 90 package Constraint::LengthNoMoreThan;
91 use strict;
92 use warnings;
93 use Moose;
94
95 extends 'Constraint::NoMoreThan';
96 with 'Constraint::OnLength';
97
78cd1d3b 98 package Constraint::LengthAtLeast;
99 use strict;
100 use warnings;
101 use Moose;
102
103 extends 'Constraint::AtLeast';
104 with 'Constraint::OnLength';
105}
a7d0cd00 106
107my $no_more_than_10 = Constraint::NoMoreThan->new(value => 10);
108isa_ok($no_more_than_10, 'Constraint::NoMoreThan');
109
110ok(!defined($no_more_than_10->validate(1)), '... validated correctly');
111is($no_more_than_10->validate(11), 'must be no more than 10', '... validation failed correctly');
112
113my $at_least_10 = Constraint::AtLeast->new(value => 10);
114isa_ok($at_least_10, 'Constraint::AtLeast');
115
116ok(!defined($at_least_10->validate(11)), '... validated correctly');
117is($at_least_10->validate(1), 'must be at least 10', '... validation failed correctly');
118
119# onlength
120
121my $no_more_than_10_chars = Constraint::LengthNoMoreThan->new(value => 10, units => 'chars');
122isa_ok($no_more_than_10_chars, 'Constraint::LengthNoMoreThan');
123isa_ok($no_more_than_10_chars, 'Constraint::NoMoreThan');
124
125ok(!defined($no_more_than_10_chars->validate('foo')), '... validated correctly');
126is($no_more_than_10_chars->validate('foooooooooo'),
127 'must be no more than 10 chars',
128 '... validation failed correctly');
129
130my $at_least_10_chars = Constraint::LengthAtLeast->new(value => 10, units => 'chars');
131isa_ok($at_least_10_chars, 'Constraint::LengthAtLeast');
132isa_ok($at_least_10_chars, 'Constraint::AtLeast');
133
134ok(!defined($at_least_10_chars->validate('barrrrrrrrr')), '... validated correctly');
135is($at_least_10_chars->validate('bar'), 'must be at least 10 chars', '... validation failed correctly');
136