Regenerate test files
[gitmo/Mouse.git] / t / 200_examples / 001_example.t
CommitLineData
7a50b450 1#!/usr/bin/perl
fde8e43f 2# This is automatically generated by author/import-moose-test.pl.
3# DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4use t::lib::MooseCompat;
7a50b450 5
6use strict;
7use warnings;
8
fde8e43f 9use Test::More;
7a50b450 10use Test::Exception;
11
fde8e43f 12
7a50b450 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
91my $no_more_than_10 = Constraint::NoMoreThan->new(value => 10);
92isa_ok($no_more_than_10, 'Constraint::NoMoreThan');
93
94ok($no_more_than_10->does('Constraint'), '... Constraint::NoMoreThan does Constraint');
95
96ok(!defined($no_more_than_10->validate(1)), '... validated correctly');
97is($no_more_than_10->validate(11), 'must be no more than 10', '... validation failed correctly');
98
99my $at_least_10 = Constraint::AtLeast->new(value => 10);
100isa_ok($at_least_10, 'Constraint::AtLeast');
101
102ok($at_least_10->does('Constraint'), '... Constraint::AtLeast does Constraint');
103
104ok(!defined($at_least_10->validate(11)), '... validated correctly');
105is($at_least_10->validate(1), 'must be at least 10', '... validation failed correctly');
106
107# onlength
108
109my $no_more_than_10_chars = Constraint::LengthNoMoreThan->new(value => 10, units => 'chars');
110isa_ok($no_more_than_10_chars, 'Constraint::LengthNoMoreThan');
111isa_ok($no_more_than_10_chars, 'Constraint::NoMoreThan');
112
113ok($no_more_than_10_chars->does('Constraint'), '... Constraint::LengthNoMoreThan does Constraint');
114ok($no_more_than_10_chars->does('Constraint::OnLength'), '... Constraint::LengthNoMoreThan does Constraint::OnLength');
115
116ok(!defined($no_more_than_10_chars->validate('foo')), '... validated correctly');
117is($no_more_than_10_chars->validate('foooooooooo'),
118 'must be no more than 10 chars',
119 '... validation failed correctly');
120
121my $at_least_10_chars = Constraint::LengthAtLeast->new(value => 10, units => 'chars');
122isa_ok($at_least_10_chars, 'Constraint::LengthAtLeast');
123isa_ok($at_least_10_chars, 'Constraint::AtLeast');
124
125ok($at_least_10_chars->does('Constraint'), '... Constraint::LengthAtLeast does Constraint');
126ok($at_least_10_chars->does('Constraint::OnLength'), '... Constraint::LengthAtLeast does Constraint::OnLength');
127
128ok(!defined($at_least_10_chars->validate('barrrrrrrrr')), '... validated correctly');
129is($at_least_10_chars->validate('bar'), 'must be at least 10 chars', '... validation failed correctly');
130
fde8e43f 131done_testing;