e6a4a2d3da85f4dd8aa49a6117d82cbdea95b5e5
[gitmo/Mouse.git] / t / 200_examples / 001_example.t
1 #!/usr/bin/perl
2 # This is automatically generated by author/import-moose-test.pl.
3 # DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4 use t::lib::MooseCompat;
5
6 use strict;
7 use warnings;
8
9 use Test::More;
10 use Test::Exception;
11
12
13 ## Roles
14
15 {
16     package Constraint;
17     use Mouse::Role;
18
19     has 'value' => (isa => 'Num', is => 'ro');
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     };
27
28     sub validation_value {
29         my ($self, $field) = @_;
30         return $field;
31     }
32
33     sub error_message { confess "Abstract method!" }
34
35     package Constraint::OnLength;
36     use Mouse::Role;
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
49 }
50
51 ## Classes
52
53 {
54     package Constraint::AtLeast;
55     use Mouse;
56
57     with 'Constraint';
58
59     sub validate {
60         my ($self, $field) = @_;
61         ($field >= $self->value);
62     }
63
64     sub error_message { 'must be at least ' . (shift)->value; }
65
66     package Constraint::NoMoreThan;
67     use Mouse;
68
69     with 'Constraint';
70
71     sub validate {
72         my ($self, $field) = @_;
73         ($field <= $self->value);
74     }
75
76     sub error_message { 'must be no more than ' . (shift)->value; }
77
78     package Constraint::LengthNoMoreThan;
79     use Mouse;
80
81     extends 'Constraint::NoMoreThan';
82        with 'Constraint::OnLength';
83
84     package Constraint::LengthAtLeast;
85     use Mouse;
86
87     extends 'Constraint::AtLeast';
88        with 'Constraint::OnLength';
89 }
90
91 my $no_more_than_10 = Constraint::NoMoreThan->new(value => 10);
92 isa_ok($no_more_than_10, 'Constraint::NoMoreThan');
93
94 ok($no_more_than_10->does('Constraint'), '... Constraint::NoMoreThan does Constraint');
95
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');
98
99 my $at_least_10 = Constraint::AtLeast->new(value => 10);
100 isa_ok($at_least_10, 'Constraint::AtLeast');
101
102 ok($at_least_10->does('Constraint'), '... Constraint::AtLeast does Constraint');
103
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');
106
107 # onlength
108
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');
112
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');
115
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');
120
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');
124
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');
127
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');
130
131 done_testing;