error_tests
[gitmo/Moose.git] / t / 006_basic.t
CommitLineData
b841b2a3 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
ef333f17 6use Test::More tests => 21;
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
ef333f17 106ok($no_more_than_10->does('Constraint'), '... Constraint::NoMoreThan does Constraint');
107
a7d0cd00 108ok(!defined($no_more_than_10->validate(1)), '... validated correctly');
109is($no_more_than_10->validate(11), 'must be no more than 10', '... validation failed correctly');
110
111my $at_least_10 = Constraint::AtLeast->new(value => 10);
112isa_ok($at_least_10, 'Constraint::AtLeast');
113
ef333f17 114ok($at_least_10->does('Constraint'), '... Constraint::AtLeast does Constraint');
115
a7d0cd00 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
ef333f17 125ok($no_more_than_10_chars->does('Constraint'), '... Constraint::LengthNoMoreThan does Constraint');
126ok($no_more_than_10_chars->does('Constraint::OnLength'), '... Constraint::LengthNoMoreThan does Constraint::OnLength');
127
a7d0cd00 128ok(!defined($no_more_than_10_chars->validate('foo')), '... validated correctly');
129is($no_more_than_10_chars->validate('foooooooooo'),
130 'must be no more than 10 chars',
131 '... validation failed correctly');
132
133my $at_least_10_chars = Constraint::LengthAtLeast->new(value => 10, units => 'chars');
134isa_ok($at_least_10_chars, 'Constraint::LengthAtLeast');
135isa_ok($at_least_10_chars, 'Constraint::AtLeast');
136
ef333f17 137ok($at_least_10_chars->does('Constraint'), '... Constraint::LengthAtLeast does Constraint');
138ok($at_least_10_chars->does('Constraint::OnLength'), '... Constraint::LengthAtLeast does Constraint::OnLength');
139
a7d0cd00 140ok(!defined($at_least_10_chars->validate('barrrrrrrrr')), '... validated correctly');
141is($at_least_10_chars->validate('bar'), 'must be at least 10 chars', '... validation failed correctly');
142