Commit | Line | Data |
bdabd620 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a28e50e4 |
6 | use Test::More; |
bdabd620 |
7 | use Test::Exception; |
8 | |
7ff56534 |
9 | |
bdabd620 |
10 | ## Roles |
11 | |
12 | { |
446e850f |
13 | package Constraint; |
bdabd620 |
14 | use Moose::Role; |
446e850f |
15 | |
81dc201f |
16 | has 'value' => (isa => 'Num', is => 'ro'); |
446e850f |
17 | |
18 | around 'validate' => sub { |
19 | my $c = shift; |
20 | my ($self, $field) = @_; |
21 | return undef if $c->($self, $self->validation_value($field)); |
d03bd989 |
22 | return $self->error_message; |
446e850f |
23 | }; |
d03bd989 |
24 | |
446e850f |
25 | sub validation_value { |
26 | my ($self, $field) = @_; |
27 | return $field; |
bdabd620 |
28 | } |
d03bd989 |
29 | |
446e850f |
30 | sub error_message { confess "Abstract method!" } |
d03bd989 |
31 | |
446e850f |
32 | package Constraint::OnLength; |
bdabd620 |
33 | use Moose::Role; |
446e850f |
34 | |
35 | has 'units' => (isa => 'Str', is => 'ro'); |
36 | |
37 | override 'validation_value' => sub { |
38 | return length(super()); |
39 | }; |
40 | |
41 | override 'error_message' => sub { |
42 | my $self = shift; |
43 | return super() . ' ' . $self->units; |
d03bd989 |
44 | }; |
446e850f |
45 | |
bdabd620 |
46 | } |
47 | |
d03bd989 |
48 | ## Classes |
bdabd620 |
49 | |
50 | { |
446e850f |
51 | package Constraint::AtLeast; |
bdabd620 |
52 | use Moose; |
bdabd620 |
53 | |
f6bee6fe |
54 | with 'Constraint'; |
bdabd620 |
55 | |
446e850f |
56 | sub validate { |
57 | my ($self, $field) = @_; |
58 | ($field >= $self->value); |
59 | } |
bdabd620 |
60 | |
446e850f |
61 | sub error_message { 'must be at least ' . (shift)->value; } |
bdabd620 |
62 | |
446e850f |
63 | package Constraint::NoMoreThan; |
446e850f |
64 | use Moose; |
65 | |
f6bee6fe |
66 | with 'Constraint'; |
bdabd620 |
67 | |
446e850f |
68 | sub validate { |
69 | my ($self, $field) = @_; |
70 | ($field <= $self->value); |
71 | } |
bdabd620 |
72 | |
446e850f |
73 | sub error_message { 'must be no more than ' . (shift)->value; } |
bdabd620 |
74 | |
446e850f |
75 | package Constraint::LengthNoMoreThan; |
446e850f |
76 | use Moose; |
bdabd620 |
77 | |
446e850f |
78 | extends 'Constraint::NoMoreThan'; |
79 | with 'Constraint::OnLength'; |
d03bd989 |
80 | |
446e850f |
81 | package Constraint::LengthAtLeast; |
446e850f |
82 | use Moose; |
d03bd989 |
83 | |
446e850f |
84 | extends 'Constraint::AtLeast'; |
d03bd989 |
85 | with 'Constraint::OnLength'; |
446e850f |
86 | } |
bdabd620 |
87 | |
446e850f |
88 | my $no_more_than_10 = Constraint::NoMoreThan->new(value => 10); |
89 | isa_ok($no_more_than_10, 'Constraint::NoMoreThan'); |
bdabd620 |
90 | |
446e850f |
91 | ok($no_more_than_10->does('Constraint'), '... Constraint::NoMoreThan does Constraint'); |
bdabd620 |
92 | |
446e850f |
93 | ok(!defined($no_more_than_10->validate(1)), '... validated correctly'); |
94 | is($no_more_than_10->validate(11), 'must be no more than 10', '... validation failed correctly'); |
bdabd620 |
95 | |
446e850f |
96 | my $at_least_10 = Constraint::AtLeast->new(value => 10); |
97 | isa_ok($at_least_10, 'Constraint::AtLeast'); |
bdabd620 |
98 | |
446e850f |
99 | ok($at_least_10->does('Constraint'), '... Constraint::AtLeast does Constraint'); |
bdabd620 |
100 | |
446e850f |
101 | ok(!defined($at_least_10->validate(11)), '... validated correctly'); |
102 | is($at_least_10->validate(1), 'must be at least 10', '... validation failed correctly'); |
bdabd620 |
103 | |
446e850f |
104 | # onlength |
bdabd620 |
105 | |
446e850f |
106 | my $no_more_than_10_chars = Constraint::LengthNoMoreThan->new(value => 10, units => 'chars'); |
107 | isa_ok($no_more_than_10_chars, 'Constraint::LengthNoMoreThan'); |
108 | isa_ok($no_more_than_10_chars, 'Constraint::NoMoreThan'); |
bdabd620 |
109 | |
446e850f |
110 | ok($no_more_than_10_chars->does('Constraint'), '... Constraint::LengthNoMoreThan does Constraint'); |
111 | ok($no_more_than_10_chars->does('Constraint::OnLength'), '... Constraint::LengthNoMoreThan does Constraint::OnLength'); |
bdabd620 |
112 | |
446e850f |
113 | ok(!defined($no_more_than_10_chars->validate('foo')), '... validated correctly'); |
d03bd989 |
114 | is($no_more_than_10_chars->validate('foooooooooo'), |
115 | 'must be no more than 10 chars', |
446e850f |
116 | '... validation failed correctly'); |
1331430a |
117 | |
446e850f |
118 | my $at_least_10_chars = Constraint::LengthAtLeast->new(value => 10, units => 'chars'); |
119 | isa_ok($at_least_10_chars, 'Constraint::LengthAtLeast'); |
120 | isa_ok($at_least_10_chars, 'Constraint::AtLeast'); |
bdabd620 |
121 | |
446e850f |
122 | ok($at_least_10_chars->does('Constraint'), '... Constraint::LengthAtLeast does Constraint'); |
123 | ok($at_least_10_chars->does('Constraint::OnLength'), '... Constraint::LengthAtLeast does Constraint::OnLength'); |
bdabd620 |
124 | |
446e850f |
125 | ok(!defined($at_least_10_chars->validate('barrrrrrrrr')), '... validated correctly'); |
126 | is($at_least_10_chars->validate('bar'), 'must be at least 10 chars', '... validation failed correctly'); |
bdabd620 |
127 | |
a28e50e4 |
128 | done_testing; |