Remove references to jules git repo
[gitmo/Moose.git] / t / 300_immutable / 004_inlined_constructors_n_types.t
CommitLineData
238b424d 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
c7a43a7a 6use Test::More tests => 10;
238b424d 7use Test::Exception;
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';
45 lives_ok {
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)");
c7a43a7a 49 } "... this passes the constuctor correctly ($mutable_string)";
238b424d 50
c7a43a7a 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
c7a43a7a 55 dies_ok {
56 Foo->new(foo => "Hello World", bar => 100, baz => "Hello World");
57 } "... this fails the constuctor correctly ($mutable_string)";
238b424d 58
c7a43a7a 59 Foo->meta->make_immutable(debug => 0) unless $is_immutable;
60}
238b424d 61
62
63