Commit | Line | Data |
b841b2a3 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
471c4f09 |
6 | use Test::More tests => 1; |
b841b2a3 |
7 | use Test::Exception; |
8 | |
9 | BEGIN { |
10 | use_ok('Moose'); |
11 | } |
e9bb8a31 |
12 | |
13 | =pod |
14 | |
9deed647 |
15 | This test will eventually be for the code shown below. |
16 | Moose::Role is on the TODO list for 0.04. |
17 | |
3824830b |
18 | package Constraint; |
19 | use strict; |
20 | use warnings; |
21 | use Moose; |
22 | |
23 | sub validate { confess "Abstract method!" } |
24 | sub error_message { confess "Abstract method!" } |
25 | |
26 | sub validation_value { |
27 | my ($self, $field) = @_; |
28 | return $field->value; |
29 | } |
30 | |
31 | package Constraint::AtLeast; |
32 | use strict; |
33 | use warnings; |
34 | use Moose; |
35 | |
36 | extends 'Constraint'; |
37 | |
38 | has 'value' => (isa => 'Num', is => 'ro'); |
39 | |
40 | sub validate { |
41 | my ($self, $field) = @_; |
42 | if ($self->validation_value($field) >= $self->value) { |
43 | return undef; |
44 | } |
45 | else { |
46 | return $self->error_message; |
47 | } |
48 | } |
49 | |
50 | sub error_message { 'must be at least ' . (shift)->value; } |
51 | |
52 | package Constraint::NoMoreThan; |
53 | use strict; |
54 | use warnings; |
55 | use Moose; |
56 | |
57 | extends 'Constraint'; |
58 | |
59 | has 'value' => (isa => 'Num', is => 'ro'); |
60 | |
61 | sub validate { |
62 | my ($self, $field) = @_; |
63 | if ($self->validation_value($field) <= $self->value) { |
64 | return undef; |
65 | } else { |
66 | return $self->error_message; |
67 | } |
68 | } |
69 | |
70 | sub error_message { 'must be no more than ' . (shift)->value; } |
71 | |
72 | package Constraint::OnLength; |
73 | use strict; |
74 | use warnings; |
75 | use Moose::Role; |
76 | |
77 | has 'units' => (isa => 'Str', is => 'ro'); |
78 | |
79 | override 'value' => sub { |
80 | return length(super()); |
81 | }; |
82 | |
83 | override 'error_message' => sub { |
84 | my $self = shift; |
85 | return super() . ' ' . $self->units; |
86 | }; |
87 | |
88 | package Constraint::LengthNoMoreThan; |
89 | use strict; |
90 | use warnings; |
91 | use Moose; |
92 | |
93 | extends 'Constraint::NoMoreThan'; |
94 | with 'Constraint::OnLength'; |
95 | |
96 | package Constraint::LengthAtLeast; |
97 | use strict; |
98 | use warnings; |
99 | use Moose; |
100 | |
101 | extends 'Constraint::AtLeast'; |
102 | with 'Constraint::OnLength'; |
e9bb8a31 |
103 | |
104 | =cut |