1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
use strict;
use warnings;
{
package TestClass;
use Moose;
use namespace::autoclean;
use MooseX::AttributeShortcuts;
has bar => (
is => 'rw',
isa => 'Int',
constraint => sub { $_ > 0 },
);
}
use Test::More;
use Test::Moose::More 0.017;
use Test::Fatal;
# TODO shift the constraint checking out into TMM?
validate_class TestClass => (
attributes => [
bar => {
reader => undef,
writer => undef,
accessor => 'bar',
original_isa => 'Int',
required => undef,
},
],
);
subtest 'value OK' => sub {
my $tc;
my $msg = exception { $tc = TestClass->new(bar => 10) };
is $msg, undef, 'does not die on construction';
is $tc->bar, 10, 'value is correct';
$msg = exception { $tc->bar(20) };
is $msg, undef, 'does not die on setting';
is $tc->bar, 20, 'value is correct';
};
subtest 'value NOT OK' => sub {
my $error = qr/Attribute \(bar\) does not pass the type constraint/;
my $tc;
my $msg = exception { $tc = TestClass->new(bar => -10) };
ok !!$msg, 'dies on bad value';
like $msg, $error, 'dies with expected message';
$msg = exception { $tc = TestClass->new(bar => 10) };
is $msg, undef, 'does not die on construction with OK value';
is $tc->bar, 10, 'value is correct';
$msg = exception { $tc->bar(-10) };
ok !!$msg, 'dies on bad value';
like $msg, $error, 'dies with expected message';
};
done_testing;
|