6 use Test::More tests => 20;
17 has 'value' => (isa => 'Num', is => 'ro');
19 around 'validate' => sub {
21 my ($self, $field) = @_;
22 return undef if $c->($self, $self->validation_value($field));
23 return $self->error_message;
26 sub validation_value {
27 my ($self, $field) = @_;
31 sub error_message { confess "Abstract method!" }
33 package Constraint::OnLength;
36 has 'units' => (isa => 'Str', is => 'ro');
38 override 'validation_value' => sub {
39 return length(super());
42 override 'error_message' => sub {
44 return super() . ' ' . $self->units;
52 package Constraint::AtLeast;
58 my ($self, $field) = @_;
59 ($field >= $self->value);
62 sub error_message { 'must be at least ' . (shift)->value; }
64 package Constraint::NoMoreThan;
70 my ($self, $field) = @_;
71 ($field <= $self->value);
74 sub error_message { 'must be no more than ' . (shift)->value; }
76 package Constraint::LengthNoMoreThan;
79 extends 'Constraint::NoMoreThan';
80 with 'Constraint::OnLength';
82 package Constraint::LengthAtLeast;
85 extends 'Constraint::AtLeast';
86 with 'Constraint::OnLength';
89 my $no_more_than_10 = Constraint::NoMoreThan->new(value => 10);
90 isa_ok($no_more_than_10, 'Constraint::NoMoreThan');
92 ok($no_more_than_10->does('Constraint'), '... Constraint::NoMoreThan does Constraint');
94 ok(!defined($no_more_than_10->validate(1)), '... validated correctly');
95 is($no_more_than_10->validate(11), 'must be no more than 10', '... validation failed correctly');
97 my $at_least_10 = Constraint::AtLeast->new(value => 10);
98 isa_ok($at_least_10, 'Constraint::AtLeast');
100 ok($at_least_10->does('Constraint'), '... Constraint::AtLeast does Constraint');
102 ok(!defined($at_least_10->validate(11)), '... validated correctly');
103 is($at_least_10->validate(1), 'must be at least 10', '... validation failed correctly');
107 my $no_more_than_10_chars = Constraint::LengthNoMoreThan->new(value => 10, units => 'chars');
108 isa_ok($no_more_than_10_chars, 'Constraint::LengthNoMoreThan');
109 isa_ok($no_more_than_10_chars, 'Constraint::NoMoreThan');
111 ok($no_more_than_10_chars->does('Constraint'), '... Constraint::LengthNoMoreThan does Constraint');
112 ok($no_more_than_10_chars->does('Constraint::OnLength'), '... Constraint::LengthNoMoreThan does Constraint::OnLength');
114 ok(!defined($no_more_than_10_chars->validate('foo')), '... validated correctly');
115 is($no_more_than_10_chars->validate('foooooooooo'),
116 'must be no more than 10 chars',
117 '... validation failed correctly');
119 my $at_least_10_chars = Constraint::LengthAtLeast->new(value => 10, units => 'chars');
120 isa_ok($at_least_10_chars, 'Constraint::LengthAtLeast');
121 isa_ok($at_least_10_chars, 'Constraint::AtLeast');
123 ok($at_least_10_chars->does('Constraint'), '... Constraint::LengthAtLeast does Constraint');
124 ok($at_least_10_chars->does('Constraint::OnLength'), '... Constraint::LengthAtLeast does Constraint::OnLength');
126 ok(!defined($at_least_10_chars->validate('barrrrrrrrr')), '... validated correctly');
127 is($at_least_10_chars->validate('bar'), 'must be at least 10 chars', '... validation failed correctly');