Redid conversion to Test::Fatal
[gitmo/Moose.git] / t / 300_immutable / 004_inlined_constructors_n_types.t
CommitLineData
238b424d 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
b10dde3a 7use Test::Fatal;
238b424d 8
238b424d 9=pod
10
11This tests to make sure that the inlined constructor
d03bd989 12has all the type constraints in order, even in the
13cases when there is no type constraint available, such
238b424d 14as with a Class::MOP::Attribute object.
15
16=cut
17
18{
19 package Foo;
20 use Moose;
c7a43a7a 21 use Moose::Util::TypeConstraints;
d03bd989 22
c7a43a7a 23 coerce 'Int' => from 'Str' => via { length $_ ? $_ : 69 };
238b424d 24
d03bd989 25 has 'foo' => (is => 'rw', isa => 'Int');
238b424d 26 has 'baz' => (is => 'rw', isa => 'Int');
84981146 27 has 'zot' => (is => 'rw', isa => 'Int', init_arg => undef);
c7a43a7a 28 has 'moo' => (is => 'rw', isa => 'Int', coerce => 1, default => '', required => 1);
29 has 'boo' => (is => 'rw', isa => 'Int', coerce => 1, builder => '_build_boo', required => 1);
30
31 sub _build_boo { '' }
32
238b424d 33 Foo->meta->add_attribute(
34 Class::MOP::Attribute->new(
35 'bar' => (
36 accessor => 'bar',
37 )
38 )
39 );
238b424d 40}
41
c7a43a7a 42for (1..2) {
43 my $is_immutable = Foo->meta->is_immutable;
44 my $mutable_string = $is_immutable ? 'immutable' : 'mutable';
b10dde3a 45 is( exception {
c7a43a7a 46 my $f = Foo->new(foo => 10, bar => "Hello World", baz => 10, zot => 4);
dd654de5 47 is($f->moo, 69, "Type coercion works as expected on default ($mutable_string)");
48 is($f->boo, 69, "Type coercion works as expected on builder ($mutable_string)");
b10dde3a 49 }, undef, "... this passes the constuctor correctly ($mutable_string)" );
238b424d 50
b10dde3a 51 is( exception {
c7a43a7a 52 Foo->new(foo => 10, bar => "Hello World", baz => 10, zot => "not an int");
b10dde3a 53 }, undef, "... the constructor doesn't care about 'zot' ($mutable_string)" );
84981146 54
b10dde3a 55 isnt( exception {
c7a43a7a 56 Foo->new(foo => "Hello World", bar => 100, baz => "Hello World");
b10dde3a 57 }, undef, "... this fails the constuctor correctly ($mutable_string)" );
238b424d 58
c7a43a7a 59 Foo->meta->make_immutable(debug => 0) unless $is_immutable;
60}
238b424d 61
a28e50e4 62done_testing;