Mouse::Util::does_role() respects $thing->does() method
[gitmo/Mouse.git] / t / 300_immutable / 004_inlined_constructors_n_types.t
CommitLineData
1ca8d984 1#!/usr/bin/perl
16504b15 2# This is automatically generated by author/import-moose-test.pl.
3# DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4use t::lib::MooseCompat;
1ca8d984 5
6use strict;
7use warnings;
8
16504b15 9use Test::More;
1ca8d984 10use Test::Exception;
11
12=pod
13
14This tests to make sure that the inlined constructor
15has all the type constraints in order, even in the
16cases when there is no type constraint available, such
16504b15 17as with a Mouse::Meta::Attribute object.
1ca8d984 18
19=cut
20
21{
22 package Foo;
23 use Mouse;
24 use Mouse::Util::TypeConstraints;
25
26 coerce 'Int' => from 'Str' => via { length $_ ? $_ : 69 };
27
28 has 'foo' => (is => 'rw', isa => 'Int');
29 has 'baz' => (is => 'rw', isa => 'Int');
30 has 'zot' => (is => 'rw', isa => 'Int', init_arg => undef);
31 has 'moo' => (is => 'rw', isa => 'Int', coerce => 1, default => '', required => 1);
32 has 'boo' => (is => 'rw', isa => 'Int', coerce => 1, builder => '_build_boo', required => 1);
33
34 sub _build_boo { '' }
35
36 Foo->meta->add_attribute(
37 Mouse::Meta::Attribute->new(
38 'bar' => (
39 accessor => 'bar',
40 )
41 )
42 );
43}
44
45for (1..2) {
46 my $is_immutable = Foo->meta->is_immutable;
47 my $mutable_string = $is_immutable ? 'immutable' : 'mutable';
48 lives_ok {
49 my $f = Foo->new(foo => 10, bar => "Hello World", baz => 10, zot => 4);
50 is($f->moo, 69, "Type coercion works as expected on default ($mutable_string)");
51 is($f->boo, 69, "Type coercion works as expected on builder ($mutable_string)");
52 } "... this passes the constuctor correctly ($mutable_string)";
53
54 lives_ok {
55 Foo->new(foo => 10, bar => "Hello World", baz => 10, zot => "not an int");
56 } "... the constructor doesn't care about 'zot' ($mutable_string)";
57
58 dies_ok {
59 Foo->new(foo => "Hello World", bar => 100, baz => "Hello World");
60 } "... this fails the constuctor correctly ($mutable_string)";
61
62 Foo->meta->make_immutable(debug => 0) unless $is_immutable;
63}
64
16504b15 65done_testing;