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