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