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