updatin
[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) = @_;
20cf91c1 26 return undef if $c->($self, $self->validation_value($field));
27 return $self->error_message;
a7d0cd00 28 };
29
3824830b 30 sub validation_value {
31 my ($self, $field) = @_;
a7d0cd00 32 return $field;
3824830b 33 }
a7d0cd00 34
35 sub error_message { confess "Abstract method!" }
36
37 package Constraint::OnLength;
38 use strict;
39 use warnings;
40 use Moose::Role;
41
42 has 'units' => (isa => 'Str', is => 'ro');
43
44 override 'validation_value' => sub {
45 return length(super());
46 };
47
48 override 'error_message' => sub {
49 my $self = shift;
50 return super() . ' ' . $self->units;
51 };
52
53}
54
55## Classes
3824830b 56
a7d0cd00 57{
3824830b 58 package Constraint::AtLeast;
59 use strict;
60 use warnings;
61 use Moose;
62
a7d0cd00 63 with 'Constraint';
3824830b 64
65 sub validate {
66 my ($self, $field) = @_;
a7d0cd00 67 ($field >= $self->value);
3824830b 68 }
69
70 sub error_message { 'must be at least ' . (shift)->value; }
71
72 package Constraint::NoMoreThan;
73 use strict;
74 use warnings;
75 use Moose;
76
a7d0cd00 77 with 'Constraint';
3824830b 78
79 sub validate {
80 my ($self, $field) = @_;
a7d0cd00 81 ($field <= $self->value);
3824830b 82 }
83
84 sub error_message { 'must be no more than ' . (shift)->value; }
85
3824830b 86 package Constraint::LengthNoMoreThan;
87 use strict;
88 use warnings;
89 use Moose;
90
91 extends 'Constraint::NoMoreThan';
92 with 'Constraint::OnLength';
93
78cd1d3b 94 package Constraint::LengthAtLeast;
95 use strict;
96 use warnings;
97 use Moose;
98
99 extends 'Constraint::AtLeast';
100 with 'Constraint::OnLength';
101}
a7d0cd00 102
103my $no_more_than_10 = Constraint::NoMoreThan->new(value => 10);
104isa_ok($no_more_than_10, 'Constraint::NoMoreThan');
105
106ok(!defined($no_more_than_10->validate(1)), '... validated correctly');
107is($no_more_than_10->validate(11), 'must be no more than 10', '... validation failed correctly');
108
109my $at_least_10 = Constraint::AtLeast->new(value => 10);
110isa_ok($at_least_10, 'Constraint::AtLeast');
111
112ok(!defined($at_least_10->validate(11)), '... validated correctly');
113is($at_least_10->validate(1), 'must be at least 10', '... validation failed correctly');
114
115# onlength
116
117my $no_more_than_10_chars = Constraint::LengthNoMoreThan->new(value => 10, units => 'chars');
118isa_ok($no_more_than_10_chars, 'Constraint::LengthNoMoreThan');
119isa_ok($no_more_than_10_chars, 'Constraint::NoMoreThan');
120
121ok(!defined($no_more_than_10_chars->validate('foo')), '... validated correctly');
122is($no_more_than_10_chars->validate('foooooooooo'),
123 'must be no more than 10 chars',
124 '... validation failed correctly');
125
126my $at_least_10_chars = Constraint::LengthAtLeast->new(value => 10, units => 'chars');
127isa_ok($at_least_10_chars, 'Constraint::LengthAtLeast');
128isa_ok($at_least_10_chars, 'Constraint::AtLeast');
129
130ok(!defined($at_least_10_chars->validate('barrrrrrrrr')), '... validated correctly');
131is($at_least_10_chars->validate('bar'), 'must be at least 10 chars', '... validation failed correctly');
132