6 use Test::More tests => 20;
15 has 'value' => (isa => 'Num', is => 'ro');
17 around 'validate' => sub {
19 my ($self, $field) = @_;
20 return undef if $c->($self, $self->validation_value($field));
21 return $self->error_message;
24 sub validation_value {
25 my ($self, $field) = @_;
29 sub error_message { confess "Abstract method!" }
31 package Constraint::OnLength;
34 has 'units' => (isa => 'Str', is => 'ro');
36 override 'validation_value' => sub {
37 return length(super());
40 override 'error_message' => sub {
42 return super() . ' ' . $self->units;
50 package Constraint::AtLeast;
56 my ($self, $field) = @_;
57 ($field >= $self->value);
60 sub error_message { 'must be at least ' . (shift)->value; }
62 package Constraint::NoMoreThan;
68 my ($self, $field) = @_;
69 ($field <= $self->value);
72 sub error_message { 'must be no more than ' . (shift)->value; }
74 package Constraint::LengthNoMoreThan;
77 extends 'Constraint::NoMoreThan';
78 with 'Constraint::OnLength';
80 package Constraint::LengthAtLeast;
83 extends 'Constraint::AtLeast';
84 with 'Constraint::OnLength';
87 my $no_more_than_10 = Constraint::NoMoreThan->new(value => 10);
88 isa_ok($no_more_than_10, 'Constraint::NoMoreThan');
90 ok($no_more_than_10->does('Constraint'), '... Constraint::NoMoreThan does Constraint');
92 ok(!defined($no_more_than_10->validate(1)), '... validated correctly');
93 is($no_more_than_10->validate(11), 'must be no more than 10', '... validation failed correctly');
95 my $at_least_10 = Constraint::AtLeast->new(value => 10);
96 isa_ok($at_least_10, 'Constraint::AtLeast');
98 ok($at_least_10->does('Constraint'), '... Constraint::AtLeast does Constraint');
100 ok(!defined($at_least_10->validate(11)), '... validated correctly');
101 is($at_least_10->validate(1), 'must be at least 10', '... validation failed correctly');
105 my $no_more_than_10_chars = Constraint::LengthNoMoreThan->new(value => 10, units => 'chars');
106 isa_ok($no_more_than_10_chars, 'Constraint::LengthNoMoreThan');
107 isa_ok($no_more_than_10_chars, 'Constraint::NoMoreThan');
109 ok($no_more_than_10_chars->does('Constraint'), '... Constraint::LengthNoMoreThan does Constraint');
110 ok($no_more_than_10_chars->does('Constraint::OnLength'), '... Constraint::LengthNoMoreThan does Constraint::OnLength');
112 ok(!defined($no_more_than_10_chars->validate('foo')), '... validated correctly');
113 is($no_more_than_10_chars->validate('foooooooooo'),
114 'must be no more than 10 chars',
115 '... validation failed correctly');
117 my $at_least_10_chars = Constraint::LengthAtLeast->new(value => 10, units => 'chars');
118 isa_ok($at_least_10_chars, 'Constraint::LengthAtLeast');
119 isa_ok($at_least_10_chars, 'Constraint::AtLeast');
121 ok($at_least_10_chars->does('Constraint'), '... Constraint::LengthAtLeast does Constraint');
122 ok($at_least_10_chars->does('Constraint::OnLength'), '... Constraint::LengthAtLeast does Constraint::OnLength');
124 ok(!defined($at_least_10_chars->validate('barrrrrrrrr')), '... validated correctly');
125 is($at_least_10_chars->validate('bar'), 'must be at least 10 chars', '... validation failed correctly');