merge trunk to pluggable errors
[gitmo/Moose.git] / t / 300_immutable / 004_inlined_constructors_n_types.t
CommitLineData
238b424d 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
e606ae5f 6use Test::More tests => 10;
238b424d 7use Test::Exception;
8
238b424d 9=pod
10
11This tests to make sure that the inlined constructor
12has all the type constraints in order, even in the
13cases when there is no type constraint available, such
14as with a Class::MOP::Attribute object.
15
16=cut
17
18{
19 package Foo;
20 use Moose;
e606ae5f 21 use Moose::Util::TypeConstraints;
22
23 coerce 'Int' => from 'Str' => via { length $_ ? $_ : 69 };
238b424d 24
25 has 'foo' => (is => 'rw', isa => 'Int');
26 has 'baz' => (is => 'rw', isa => 'Int');
84981146 27 has 'zot' => (is => 'rw', isa => 'Int', init_arg => undef);
e606ae5f 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
e606ae5f 42for (1..2) {
43 my $is_immutable = Foo->meta->is_immutable;
44 my $mutable_string = $is_immutable ? 'immutable' : 'mutable';
45 lives_ok {
46 my $f = Foo->new(foo => 10, bar => "Hello World", baz => 10, zot => 4);
47 is($f->moo, 69, "Type coersion works as expected on default ($mutable_string)");
48 is($f->boo, 69, "Type coersion works as expected on builder ($mutable_string)");
49 } "... this passes the constuctor correctly ($mutable_string)";
238b424d 50
e606ae5f 51 lives_ok {
52 Foo->new(foo => 10, bar => "Hello World", baz => 10, zot => "not an int");
53 } "... the constructor doesn't care about 'zot' ($mutable_string)";
84981146 54
e606ae5f 55 dies_ok {
56 Foo->new(foo => "Hello World", bar => 100, baz => "Hello World");
57 } "... this fails the constuctor correctly ($mutable_string)";
238b424d 58
e606ae5f 59 Foo->meta->make_immutable(debug => 0) unless $is_immutable;
60}
238b424d 61
62
63