6 use Test::More tests => 21;
19 has 'value' => (isa => 'Num', is => 'ro');
21 around 'validate' => sub {
23 my ($self, $field) = @_;
24 return undef if $c->($self, $self->validation_value($field));
25 return $self->error_message;
28 sub validation_value {
29 my ($self, $field) = @_;
33 sub error_message { confess "Abstract method!" }
35 package Constraint::OnLength;
38 has 'units' => (isa => 'Str', is => 'ro');
40 override 'validation_value' => sub {
41 return length(super());
44 override 'error_message' => sub {
46 return super() . ' ' . $self->units;
54 package Constraint::AtLeast;
60 my ($self, $field) = @_;
61 ($field >= $self->value);
64 sub error_message { 'must be at least ' . (shift)->value; }
66 package Constraint::NoMoreThan;
72 my ($self, $field) = @_;
73 ($field <= $self->value);
76 sub error_message { 'must be no more than ' . (shift)->value; }
78 package Constraint::LengthNoMoreThan;
81 extends 'Constraint::NoMoreThan';
82 with 'Constraint::OnLength';
84 package Constraint::LengthAtLeast;
87 extends 'Constraint::AtLeast';
88 with 'Constraint::OnLength';
91 my $no_more_than_10 = Constraint::NoMoreThan->new(value => 10);
92 isa_ok($no_more_than_10, 'Constraint::NoMoreThan');
94 ok($no_more_than_10->does('Constraint'), '... Constraint::NoMoreThan does Constraint');
96 ok(!defined($no_more_than_10->validate(1)), '... validated correctly');
97 is($no_more_than_10->validate(11), 'must be no more than 10', '... validation failed correctly');
99 my $at_least_10 = Constraint::AtLeast->new(value => 10);
100 isa_ok($at_least_10, 'Constraint::AtLeast');
102 ok($at_least_10->does('Constraint'), '... Constraint::AtLeast does Constraint');
104 ok(!defined($at_least_10->validate(11)), '... validated correctly');
105 is($at_least_10->validate(1), 'must be at least 10', '... validation failed correctly');
109 my $no_more_than_10_chars = Constraint::LengthNoMoreThan->new(value => 10, units => 'chars');
110 isa_ok($no_more_than_10_chars, 'Constraint::LengthNoMoreThan');
111 isa_ok($no_more_than_10_chars, 'Constraint::NoMoreThan');
113 ok($no_more_than_10_chars->does('Constraint'), '... Constraint::LengthNoMoreThan does Constraint');
114 ok($no_more_than_10_chars->does('Constraint::OnLength'), '... Constraint::LengthNoMoreThan does Constraint::OnLength');
116 ok(!defined($no_more_than_10_chars->validate('foo')), '... validated correctly');
117 is($no_more_than_10_chars->validate('foooooooooo'),
118 'must be no more than 10 chars',
119 '... validation failed correctly');
121 my $at_least_10_chars = Constraint::LengthAtLeast->new(value => 10, units => 'chars');
122 isa_ok($at_least_10_chars, 'Constraint::LengthAtLeast');
123 isa_ok($at_least_10_chars, 'Constraint::AtLeast');
125 ok($at_least_10_chars->does('Constraint'), '... Constraint::LengthAtLeast does Constraint');
126 ok($at_least_10_chars->does('Constraint::OnLength'), '... Constraint::LengthAtLeast does Constraint::OnLength');
128 ok(!defined($at_least_10_chars->validate('barrrrrrrrr')), '... validated correctly');
129 is($at_least_10_chars->validate('bar'), 'must be at least 10 chars', '... validation failed correctly');