For class_types, explicitly state that the value is not an instance of
[gitmo/Moose.git] / t / 200_examples / 001_example.t
CommitLineData
bdabd620 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
d443cad0 6use Test::More tests => 22;
bdabd620 7use Test::Exception;
d443cad0 8use 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 94my $no_more_than_10 = Constraint::NoMoreThan->new(value => 10);
95isa_ok($no_more_than_10, 'Constraint::NoMoreThan');
bdabd620 96
446e850f 97ok($no_more_than_10->does('Constraint'), '... Constraint::NoMoreThan does Constraint');
bdabd620 98
446e850f 99ok(!defined($no_more_than_10->validate(1)), '... validated correctly');
100is($no_more_than_10->validate(11), 'must be no more than 10', '... validation failed correctly');
bdabd620 101
446e850f 102my $at_least_10 = Constraint::AtLeast->new(value => 10);
103isa_ok($at_least_10, 'Constraint::AtLeast');
bdabd620 104
446e850f 105ok($at_least_10->does('Constraint'), '... Constraint::AtLeast does Constraint');
bdabd620 106
446e850f 107ok(!defined($at_least_10->validate(11)), '... validated correctly');
108is($at_least_10->validate(1), 'must be at least 10', '... validation failed correctly');
bdabd620 109
446e850f 110# onlength
bdabd620 111
446e850f 112my $no_more_than_10_chars = Constraint::LengthNoMoreThan->new(value => 10, units => 'chars');
113isa_ok($no_more_than_10_chars, 'Constraint::LengthNoMoreThan');
114isa_ok($no_more_than_10_chars, 'Constraint::NoMoreThan');
bdabd620 115
446e850f 116ok($no_more_than_10_chars->does('Constraint'), '... Constraint::LengthNoMoreThan does Constraint');
117ok($no_more_than_10_chars->does('Constraint::OnLength'), '... Constraint::LengthNoMoreThan does Constraint::OnLength');
bdabd620 118
446e850f 119ok(!defined($no_more_than_10_chars->validate('foo')), '... validated correctly');
120is($no_more_than_10_chars->validate('foooooooooo'),
121 'must be no more than 10 chars',
122 '... validation failed correctly');
1331430a 123
446e850f 124my $at_least_10_chars = Constraint::LengthAtLeast->new(value => 10, units => 'chars');
125isa_ok($at_least_10_chars, 'Constraint::LengthAtLeast');
126isa_ok($at_least_10_chars, 'Constraint::AtLeast');
bdabd620 127
446e850f 128ok($at_least_10_chars->does('Constraint'), '... Constraint::LengthAtLeast does Constraint');
129ok($at_least_10_chars->does('Constraint::OnLength'), '... Constraint::LengthAtLeast does Constraint::OnLength');
bdabd620 130
446e850f 131ok(!defined($at_least_10_chars->validate('barrrrrrrrr')), '... validated correctly');
132is($at_least_10_chars->validate('bar'), 'must be at least 10 chars', '... validation failed correctly');
bdabd620 133