8 if(eval{ require Class::Method::Modifiers::Fast } || eval{ require Class::Method::Modifiers }){
12 plan skip_all => 'This test requires Class::Method::Modifiers(::Fast)?';
23 has 'value' => (isa => 'Num', is => 'ro');
25 around 'validate' => sub {
27 my ($self, $field) = @_;
28 return undef if $c->($self, $self->validation_value($field));
29 return $self->error_message;
32 sub validation_value {
33 my ($self, $field) = @_;
37 sub error_message { confess "Abstract method!" }
39 package Constraint::OnLength;
42 has 'units' => (isa => 'Str', is => 'ro');
44 override 'validation_value' => sub {
45 return length(super());
48 override 'error_message' => sub {
50 return super() . ' ' . $self->units;
58 package Constraint::AtLeast;
64 my ($self, $field) = @_;
65 ($field >= $self->value);
68 sub error_message { 'must be at least ' . (shift)->value; }
70 package Constraint::NoMoreThan;
76 my ($self, $field) = @_;
77 ($field <= $self->value);
80 sub error_message { 'must be no more than ' . (shift)->value; }
82 package Constraint::LengthNoMoreThan;
85 extends 'Constraint::NoMoreThan';
86 with 'Constraint::OnLength';
88 package Constraint::LengthAtLeast;
91 extends 'Constraint::AtLeast';
92 with 'Constraint::OnLength';
95 my $no_more_than_10 = Constraint::NoMoreThan->new(value => 10);
96 isa_ok($no_more_than_10, 'Constraint::NoMoreThan');
98 ok($no_more_than_10->does('Constraint'), '... Constraint::NoMoreThan does Constraint');
100 ok(!defined($no_more_than_10->validate(1)), '... validated correctly');
101 is($no_more_than_10->validate(11), 'must be no more than 10', '... validation failed correctly');
103 my $at_least_10 = Constraint::AtLeast->new(value => 10);
104 isa_ok($at_least_10, 'Constraint::AtLeast');
106 ok($at_least_10->does('Constraint'), '... Constraint::AtLeast does Constraint');
108 ok(!defined($at_least_10->validate(11)), '... validated correctly');
109 is($at_least_10->validate(1), 'must be at least 10', '... validation failed correctly');
113 my $no_more_than_10_chars = Constraint::LengthNoMoreThan->new(value => 10, units => 'chars');
114 isa_ok($no_more_than_10_chars, 'Constraint::LengthNoMoreThan');
115 isa_ok($no_more_than_10_chars, 'Constraint::NoMoreThan');
117 ok($no_more_than_10_chars->does('Constraint'), '... Constraint::LengthNoMoreThan does Constraint');
118 ok($no_more_than_10_chars->does('Constraint::OnLength'), '... Constraint::LengthNoMoreThan does Constraint::OnLength');
120 ok(!defined($no_more_than_10_chars->validate('foo')), '... validated correctly');
121 is($no_more_than_10_chars->validate('foooooooooo'),
122 'must be no more than 10 chars',
123 '... validation failed correctly');
125 my $at_least_10_chars = Constraint::LengthAtLeast->new(value => 10, units => 'chars');
126 isa_ok($at_least_10_chars, 'Constraint::LengthAtLeast');
127 isa_ok($at_least_10_chars, 'Constraint::AtLeast');
129 ok($at_least_10_chars->does('Constraint'), '... Constraint::LengthAtLeast does Constraint');
130 ok($at_least_10_chars->does('Constraint::OnLength'), '... Constraint::LengthAtLeast does Constraint::OnLength');
132 ok(!defined($at_least_10_chars->validate('barrrrrrrrr')), '... validated correctly');
133 is($at_least_10_chars->validate('bar'), 'must be at least 10 chars', '... validation failed correctly');