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